MCPcopy Create free account
hub / github.com/dgraph-io/dgraph-benchmarks / Seek

Method Seek

bitmap/codec.go:244–302  ·  view source on GitHub ↗

Seek will search for uid in a packed block using the specified whence position. The value of whence must be one of the predefined values SeekStart or SeekCurrent. SeekStart searches uid and includes it as part of the results. SeekCurrent searches uid but only as offset, it won't be included with res

(uid uint64, whence seekPos)

Source from the content-addressed store, hash-verified

242//
243// Returns a slice of all uids whence the position, or an empty slice if none found.
244func (d *Decoder) Seek(uid uint64, whence seekPos) []uint64 {
245 if d.Pack == nil {
246 return []uint64{}
247 }
248 d.blockIdx = 0
249 if uid == 0 {
250 return d.UnpackBlock()
251 }
252
253 pack := d.Pack
254 blocksFunc := func() searchFunc {
255 var f searchFunc
256 switch whence {
257 case SeekStart:
258 f = func(i int) bool { return pack.Blocks[i].Base >= uid }
259 case SeekCurrent:
260 f = func(i int) bool { return pack.Blocks[i].Base > uid }
261 }
262 return f
263 }
264
265 idx := sort.Search(len(pack.Blocks), blocksFunc())
266 // The first block.Base >= uid.
267 if idx == 0 {
268 return d.UnpackBlock()
269 }
270 // The uid is the first entry in the block.
271 if idx < len(pack.Blocks) && pack.Blocks[idx].Base == uid {
272 d.blockIdx = idx
273 return d.UnpackBlock()
274 }
275
276 // Either the idx = len(pack.Blocks) that means it wasn't found in any of the block's base. Or,
277 // we found the first block index whose base is greater than uid. In these cases, go to the
278 // previous block and search there.
279 d.blockIdx = idx - 1 // Move to the previous block. If blockIdx<0, unpack will deal with it.
280 d.UnpackBlock() // And get all their uids.
281
282 uidsFunc := func() searchFunc {
283 var f searchFunc
284 switch whence {
285 case SeekStart:
286 f = func(i int) bool { return d.uids[i] >= uid }
287 case SeekCurrent:
288 f = func(i int) bool { return d.uids[i] > uid }
289 }
290 return f
291 }
292
293 // uidx points to the first uid in the uid list, which is >= uid.
294 uidx := sort.Search(len(d.uids), uidsFunc())
295 if uidx < len(d.uids) { // Found an entry in uids, which >= uid.
296 d.uids = d.uids[uidx:]
297 return d.uids
298 }
299 // Could not find any uid in the block, which is >= uid. The next block might still have valid
300 // entries > uid.
301 return d.Next()

Callers 4

NewDecoderFunction · 0.95
DecodeFunction · 0.95
DecodeToBufferFunction · 0.95
BenchmarkContainsFunction · 0.95

Calls 2

UnpackBlockMethod · 0.95
NextMethod · 0.95

Tested by 1

BenchmarkContainsFunction · 0.76