From dac00faf56168a1cbffd69a33d0b855f253b059d Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Wed, 25 Aug 2021 11:40:50 +0530 Subject: [PATCH] Make iterator seek return the latest ts (#1737) Make iterator seek return the latestTs. Note: It is intended to be used with the forward iterator only. --- iterator.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/iterator.go b/iterator.go index 4e6afef67..c8b489696 100644 --- a/iterator.go +++ b/iterator.go @@ -454,7 +454,8 @@ type Iterator struct { // iterators created by the stream interface ThreadId int - Alloc *z.Allocator + latestTs uint64 + Alloc *z.Allocator } // NewIterator returns a new iterator. Depending upon the options, either only keys, or both @@ -636,6 +637,9 @@ func (it *Iterator) parseItem() bool { // Skip any versions which are beyond the readTs. version := y.ParseTs(key) + if version > it.latestTs { + it.latestTs = version + } // Ignore everything that is above the readTs and below or at the sinceTs. if version > it.readTs || (it.opt.SinceTs > 0 && version <= it.opt.SinceTs) { mi.Next() @@ -748,9 +752,9 @@ func (it *Iterator) prefetch() { // Seek would seek to the provided key if present. If absent, it would seek to the next // smallest key greater than the provided key if iterating in the forward direction. // Behavior would be reversed if iterating backwards. -func (it *Iterator) Seek(key []byte) { +func (it *Iterator) Seek(key []byte) uint64 { if it.iitr == nil { - return + return it.latestTs } if len(key) > 0 { it.txn.addReadKey(key) @@ -767,16 +771,19 @@ func (it *Iterator) Seek(key []byte) { if len(key) == 0 { it.iitr.Rewind() it.prefetch() - return + return it.latestTs } if !it.opt.Reverse { - key = y.KeyWithTs(key, it.txn.readTs) + // Using maxUint64 instead of it.readTs because we want seek to return latestTs of the key. + // All the keys with ts > readTs will be discarded for iteration by the prefetch function. + key = y.KeyWithTs(key, math.MaxUint64) } else { key = y.KeyWithTs(key, 0) } it.iitr.Seek(key) it.prefetch() + return it.latestTs } // Rewind would rewind the iterator cursor all the way to zero-th position, which would be the