From 9e85ebef011aa52ddcbb707ad912e61a68421838 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 17 Aug 2023 14:36:28 -0700 Subject: [PATCH 1/5] refactor: memiterator to use cursor --- tm2/pkg/store/cache/memiterator.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tm2/pkg/store/cache/memiterator.go b/tm2/pkg/store/cache/memiterator.go index 6a349cc34b8..4f545a4e0b6 100644 --- a/tm2/pkg/store/cache/memiterator.go +++ b/tm2/pkg/store/cache/memiterator.go @@ -14,6 +14,7 @@ type memIterator struct { start, end []byte items []*std.KVPair ascending bool + pos int } func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { @@ -31,11 +32,17 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte entered = true } + pos := 0 + if !ascending { + pos = len(itemsInDomain) - 1 + } + return &memIterator{ start: start, end: end, items: itemsInDomain, ascending: ascending, + pos: pos, } } @@ -44,7 +51,10 @@ func (mi *memIterator) Domain() ([]byte, []byte) { } func (mi *memIterator) Valid() bool { - return len(mi.items) > 0 + if mi.pos < 0 || len(mi.items) <= mi.pos { + return false + } + return true } func (mi *memIterator) assertValid() { @@ -56,26 +66,20 @@ func (mi *memIterator) assertValid() { func (mi *memIterator) Next() { mi.assertValid() if mi.ascending { - mi.items = mi.items[1:] + mi.pos++ } else { - mi.items = mi.items[:len(mi.items)-1] + mi.pos-- } } func (mi *memIterator) Key() []byte { mi.assertValid() - if mi.ascending { - return mi.items[0].Key - } - return mi.items[len(mi.items)-1].Key + return mi.items[mi.pos].Key } func (mi *memIterator) Value() []byte { mi.assertValid() - if mi.ascending { - return mi.items[0].Value - } - return mi.items[len(mi.items)-1].Value + return mi.items[mi.pos].Value } func (mi *memIterator) Close() { From 3b4a2c74da729acd964fc0451b6e1fc5e32410de Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 17 Aug 2023 14:36:55 -0700 Subject: [PATCH 2/5] test: add reverse iterator tests --- tm2/pkg/store/cache/store_test.go | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tm2/pkg/store/cache/store_test.go b/tm2/pkg/store/cache/store_test.go index 51731f82800..5daf83fcb61 100644 --- a/tm2/pkg/store/cache/store_test.go +++ b/tm2/pkg/store/cache/store_test.go @@ -150,6 +150,50 @@ func TestCacheKVIteratorBounds(t *testing.T) { require.Equal(t, 4, i) } +func TestCacheKVReverseIteratorBounds(t *testing.T) { + st := newCacheStore() + + // set some items + nItems := 5 + for i := 0; i < nItems; i++ { + st.Set(keyFmt(i), valFmt(i)) + } + + // iterate over all of them in reverse + i := nItems - 1 + for itr := st.ReverseIterator(nil, nil); itr.Valid(); itr.Next() { + require.Equal(t, keyFmt(i), itr.Key()) + require.Equal(t, valFmt(i), itr.Value()) + i-- + } + require.Equal(t, -1, i) + + // iterate over none + i = 0 + for itr := st.ReverseIterator(bz("money"), nil); itr.Valid(); itr.Next() { + i++ + } + require.Equal(t, 0, i) + + // iterate over lower + i = 2 + for itr := st.ReverseIterator(keyFmt(0), keyFmt(3)); itr.Valid(); itr.Next() { + require.Equal(t, keyFmt(i), itr.Key()) + require.Equal(t, valFmt(i), itr.Value()) + i-- + } + require.Equal(t, -1, i) + + // iterate over upper + i = 3 + for itr := st.ReverseIterator(keyFmt(2), keyFmt(4)); itr.Valid(); itr.Next() { + require.Equal(t, keyFmt(i), itr.Key()) + require.Equal(t, valFmt(i), itr.Value()) + i-- + } + require.Equal(t, 1, i) +} + func TestCacheKVMergeIteratorBasics(t *testing.T) { st := newCacheStore() From f9a8718ba3a48d1f942f6cfcdee7f29d8f9671f3 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Fri, 18 Aug 2023 13:04:41 -0700 Subject: [PATCH 3/5] style: name change from pos to cur --- tm2/pkg/store/cache/memiterator.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tm2/pkg/store/cache/memiterator.go b/tm2/pkg/store/cache/memiterator.go index 4f545a4e0b6..4989d89747d 100644 --- a/tm2/pkg/store/cache/memiterator.go +++ b/tm2/pkg/store/cache/memiterator.go @@ -14,7 +14,7 @@ type memIterator struct { start, end []byte items []*std.KVPair ascending bool - pos int + cur int } func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { @@ -32,9 +32,9 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte entered = true } - pos := 0 + cur := 0 if !ascending { - pos = len(itemsInDomain) - 1 + cur = len(itemsInDomain) - 1 } return &memIterator{ @@ -42,7 +42,7 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte end: end, items: itemsInDomain, ascending: ascending, - pos: pos, + cur: cur, } } @@ -51,7 +51,7 @@ func (mi *memIterator) Domain() ([]byte, []byte) { } func (mi *memIterator) Valid() bool { - if mi.pos < 0 || len(mi.items) <= mi.pos { + if mi.cur < 0 || len(mi.items) <= mi.cur { return false } return true @@ -66,20 +66,20 @@ func (mi *memIterator) assertValid() { func (mi *memIterator) Next() { mi.assertValid() if mi.ascending { - mi.pos++ + mi.cur++ } else { - mi.pos-- + mi.cur-- } } func (mi *memIterator) Key() []byte { mi.assertValid() - return mi.items[mi.pos].Key + return mi.items[mi.cur].Key } func (mi *memIterator) Value() []byte { mi.assertValid() - return mi.items[mi.pos].Value + return mi.items[mi.cur].Value } func (mi *memIterator) Close() { From 0b69c5c2a3370ab8218e407ae0968ce66233034d Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 24 Aug 2023 17:20:09 -0700 Subject: [PATCH 4/5] Revert "style: name change from pos to cur" This reverts commit f9a8718ba3a48d1f942f6cfcdee7f29d8f9671f3. --- tm2/pkg/store/cache/memiterator.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tm2/pkg/store/cache/memiterator.go b/tm2/pkg/store/cache/memiterator.go index 4989d89747d..4f545a4e0b6 100644 --- a/tm2/pkg/store/cache/memiterator.go +++ b/tm2/pkg/store/cache/memiterator.go @@ -14,7 +14,7 @@ type memIterator struct { start, end []byte items []*std.KVPair ascending bool - cur int + pos int } func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { @@ -32,9 +32,9 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte entered = true } - cur := 0 + pos := 0 if !ascending { - cur = len(itemsInDomain) - 1 + pos = len(itemsInDomain) - 1 } return &memIterator{ @@ -42,7 +42,7 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte end: end, items: itemsInDomain, ascending: ascending, - cur: cur, + pos: pos, } } @@ -51,7 +51,7 @@ func (mi *memIterator) Domain() ([]byte, []byte) { } func (mi *memIterator) Valid() bool { - if mi.cur < 0 || len(mi.items) <= mi.cur { + if mi.pos < 0 || len(mi.items) <= mi.pos { return false } return true @@ -66,20 +66,20 @@ func (mi *memIterator) assertValid() { func (mi *memIterator) Next() { mi.assertValid() if mi.ascending { - mi.cur++ + mi.pos++ } else { - mi.cur-- + mi.pos-- } } func (mi *memIterator) Key() []byte { mi.assertValid() - return mi.items[mi.cur].Key + return mi.items[mi.pos].Key } func (mi *memIterator) Value() []byte { mi.assertValid() - return mi.items[mi.cur].Value + return mi.items[mi.pos].Value } func (mi *memIterator) Close() { From a7412c6c5cea8b8582ea599c1b7868e59e8ea0d5 Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Thu, 24 Aug 2023 17:20:33 -0700 Subject: [PATCH 5/5] Revert "refactor: memiterator to use cursor" This reverts commit 9e85ebef011aa52ddcbb707ad912e61a68421838. --- tm2/pkg/store/cache/memiterator.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/tm2/pkg/store/cache/memiterator.go b/tm2/pkg/store/cache/memiterator.go index 4f545a4e0b6..6a349cc34b8 100644 --- a/tm2/pkg/store/cache/memiterator.go +++ b/tm2/pkg/store/cache/memiterator.go @@ -14,7 +14,6 @@ type memIterator struct { start, end []byte items []*std.KVPair ascending bool - pos int } func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator { @@ -32,17 +31,11 @@ func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIte entered = true } - pos := 0 - if !ascending { - pos = len(itemsInDomain) - 1 - } - return &memIterator{ start: start, end: end, items: itemsInDomain, ascending: ascending, - pos: pos, } } @@ -51,10 +44,7 @@ func (mi *memIterator) Domain() ([]byte, []byte) { } func (mi *memIterator) Valid() bool { - if mi.pos < 0 || len(mi.items) <= mi.pos { - return false - } - return true + return len(mi.items) > 0 } func (mi *memIterator) assertValid() { @@ -66,20 +56,26 @@ func (mi *memIterator) assertValid() { func (mi *memIterator) Next() { mi.assertValid() if mi.ascending { - mi.pos++ + mi.items = mi.items[1:] } else { - mi.pos-- + mi.items = mi.items[:len(mi.items)-1] } } func (mi *memIterator) Key() []byte { mi.assertValid() - return mi.items[mi.pos].Key + if mi.ascending { + return mi.items[0].Key + } + return mi.items[len(mi.items)-1].Key } func (mi *memIterator) Value() []byte { mi.assertValid() - return mi.items[mi.pos].Value + if mi.ascending { + return mi.items[0].Value + } + return mi.items[len(mi.items)-1].Value } func (mi *memIterator) Close() {