Skip to content

Commit

Permalink
Make iterator seek return the latest ts (dgraph-io#1737)
Browse files Browse the repository at this point in the history
Make iterator seek return the latestTs.
Note: It is intended to be used with the forward iterator only.
  • Loading branch information
ahsanbarkati authored and fredcarle committed Aug 1, 2023
1 parent e8bf93f commit dac00fa
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions iterator.go
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit dac00fa

Please sign in to comment.