From 307d7d393b9ddc03173443da393a24ba49a45365 Mon Sep 17 00:00:00 2001 From: Radu Berinde Date: Thu, 9 May 2024 13:53:55 -0700 Subject: [PATCH] iterator: allow NextPrefix from exhausted position We don't need to support NextPrefix from an exhausted position, but we want to implement predictable semantics for the metamorphic tests. The iterator could be exhausted in either direction; in both cases, returning whatever `Next` returns is correct. Fixes #3597 --- iterator.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/iterator.go b/iterator.go index 19185d4934..98bdae1bb7 100644 --- a/iterator.go +++ b/iterator.go @@ -1724,6 +1724,15 @@ func (i *Iterator) NextPrefix() bool { } func (i *Iterator) nextPrefix() IterValidityState { + // We don't need to support NextPrefix from an exhausted position, but we want + // to implement predictable semantics for the metamorphic tests. + // + // The iterator could be exhausted in either direction; in both cases, + // returning whatever Next returns is correct. + if i.iterValidityState == IterExhausted { + return i.nextWithLimit(nil) + } + if i.rangeKey != nil { // NB: Check Valid() before clearing requiresReposition. i.rangeKey.prevPosHadRangeKey = i.rangeKey.hasRangeKey && i.Valid() @@ -1740,11 +1749,6 @@ func (i *Iterator) nextPrefix() IterValidityState { // key. i.rangeKey.updated = i.rangeKey.hasRangeKey && !i.Valid() && i.opts.rangeKeys() } - // NextPrefix from an exhausted position is undefined. We keep the exhausted - // position, which provides determinism for the metamorphic tests. - if i.iterValidityState == IterExhausted { - return i.iterValidityState - } // Although NextPrefix documents that behavior at IterAtLimit is undefined, // this function handles these cases as a simple prefix-agnostic Next. This