blockMetasForSearch returns a list of blocks that are relevant to the search query. start and end are unix timestamps in seconds. rf is the replication factor of the blocks to return.
(allBlocks []*backend.BlockMeta, start, end time.Time, filterFn func(m *backend.BlockMeta) bool)
| 370 | // blockMetasForSearch returns a list of blocks that are relevant to the search query. |
| 371 | // start and end are unix timestamps in seconds. rf is the replication factor of the blocks to return. |
| 372 | func blockMetasForSearch(allBlocks []*backend.BlockMeta, start, end time.Time, filterFn func(m *backend.BlockMeta) bool) []*backend.BlockMeta { |
| 373 | blocks := make([]*backend.BlockMeta, 0, len(allBlocks)/50) // divide by 50 for luck |
| 374 | for _, m := range allBlocks { |
| 375 | // Block overlaps with search range if: |
| 376 | // block start is before or equal to search end AND block end is after or equal to search start |
| 377 | if !m.StartTime.After(end) && // block start <= search end |
| 378 | !m.EndTime.Before(start) && // block end >= search start |
| 379 | filterFn(m) { |
| 380 | blocks = append(blocks, m) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // search backwards in time with deterministic ordering |
| 385 | sort.Slice(blocks, func(i, j int) bool { |
| 386 | if !blocks[i].EndTime.Equal(blocks[j].EndTime) { |
| 387 | return blocks[i].EndTime.After(blocks[j].EndTime) |
| 388 | } |
| 389 | return blocks[i].BlockID.String() < blocks[j].BlockID.String() |
| 390 | }) |
| 391 | |
| 392 | return blocks |
| 393 | } |