@@ -286,59 +286,39 @@ func (d *Decoder) Seek(uid uint64, whence seekPos) []uint64 {
286286 if d .Pack == nil {
287287 return []uint64 {}
288288 }
289-
290- prevBlockIdx := d .blockIdx
291- pack := d .Pack
292- blocksToSearch := pack .Blocks
293- offset := 0
294- if len (d .uids ) > 0 {
295- first := d .uids [0 ]
296- second := d .uids [len (d .uids )- 1 ]
297-
298- if uid >= first && uid <= second {
299- blocksToSearch = nil
300- } else if uid > second {
301- blocksToSearch = blocksToSearch [prevBlockIdx :]
302- offset = prevBlockIdx
303- } else {
304- blocksToSearch = blocksToSearch [:prevBlockIdx ]
305- }
306- }
307-
289+ d .blockIdx = 0
308290 if uid == 0 {
309291 return d .UnpackBlock ()
310292 }
311293
294+ pack := d .Pack
312295 blocksFunc := func () searchFunc {
313296 var f searchFunc
314297 switch whence {
315298 case SeekStart :
316- f = func (i int ) bool { return blocksToSearch [i ].Base >= uid }
299+ f = func (i int ) bool { return pack . Blocks [i ].Base >= uid }
317300 case SeekCurrent :
318- f = func (i int ) bool { return blocksToSearch [i ].Base > uid }
301+ f = func (i int ) bool { return pack . Blocks [i ].Base > uid }
319302 }
320303 return f
321304 }
322305
306+ idx := sort .Search (len (pack .Blocks ), blocksFunc ())
307+ // The first block.Base >= uid.
308+ if idx == 0 {
309+ return d .UnpackBlock ()
310+ }
311+ // The uid is the first entry in the block.
312+ if idx < len (pack .Blocks ) && pack .Blocks [idx ].Base == uid {
313+ d .blockIdx = idx
314+ return d .UnpackBlock ()
315+ }
316+
323317 // Either the idx = len(pack.Blocks) that means it wasn't found in any of the block's base. Or,
324318 // we found the first block index whose base is greater than uid. In these cases, go to the
325319 // previous block and search there.
326- if blocksToSearch != nil {
327- idx := sort .Search (len (blocksToSearch ), blocksFunc ()) + offset
328- // The first block.Base >= uid.
329- if idx == 0 {
330- return d .UnpackBlock ()
331- }
332-
333- // The uid is the first entry in the block.
334- if idx < len (pack .Blocks ) && pack .Blocks [idx ].Base == uid {
335- d .blockIdx = idx
336- return d .UnpackBlock ()
337- }
338-
339- d .blockIdx = idx - 1 // Move to the previous block. If blockIdx<0, unpack will deal with it.
340- d .UnpackBlock () // And get all their uids.
341- }
320+ d .blockIdx = idx - 1 // Move to the previous block. If blockIdx<0, unpack will deal with it.
321+ d .UnpackBlock () // And get all their uids.
342322
343323 uidsFunc := func () searchFunc {
344324 var f searchFunc
0 commit comments