Skip to content

Commit

Permalink
tidy: move predicate into typed general predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
felixangell committed Oct 10, 2024
1 parent e76743a commit db7883e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
23 changes: 21 additions & 2 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ func (s SegmentPath) Valid() bool {
return true
}

func findLowestSegmentWithNearbyTimestamp(paths []string, target int64, pred func(offs uint64, ts int64) bool) int {
type SegmentLookupPred func(u uint64, ts int64) bool

func SegmentByTimestamp(store ObjectStore, topic string, partition uint32) SegmentLookupPred {
return func(u uint64, ts int64) bool {
key := SegmentName(topic, partition, u).Format(SegmentTimeIndex)
data, err := store.Get(key)
if err != nil {
return false
}

index := TimeIndexFromBytes(data)
if len(index) == 0 {
panic("empty index")
}

return ts >= index[0].Timestamp
}
}

func findLowestSegmentWithNearbyTimestamp(paths []string, target int64, pred SegmentLookupPred) int {
sp := parseSegmentPaths(paths)

l, r := 0, len(sp)-1
best := -1

for l <= r {
mid := l + ((r - l) / 2)
if pred(sp[mid], int64(target)) {
if pred(sp[mid], target) {
l = mid + 1
best = mid
} else {
Expand Down
18 changes: 1 addition & 17 deletions api/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,9 @@ func Test_findOffsetByTimestamp(t *testing.T) {
}))
assert.NoError(t, err)

pred := func(u uint64, ts int64) bool {
key := SegmentName("orders", 0, u).Format(SegmentTimeIndex)
data, err := store.Get(key)
if err != nil {
return false
}

index := TimeIndexFromBytes(data)
if len(index) == 0 {
panic("empty index")
}

return ts >= index[0].Timestamp
}

pred := SegmentByTimestamp(store, "orders", 0)
timestamp := int64(0x10)

best := findLowestSegmentWithNearbyTimestamp(segmentsList, timestamp, pred)

assert.Equal(t, 1, best)
}

Expand Down

0 comments on commit db7883e

Please sign in to comment.