From 56f28145dd19105a023fd54d6a7f24c3181fae95 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Fri, 30 Mar 2018 22:36:04 +0200 Subject: [PATCH] working on it --- Gopkg.lock | 2 +- store/iavlstore.go | 19 ++++++++++++++- store/iavlstore_test.go | 52 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 267acf32b..377390297 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -458,6 +458,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "cce90fda84a63ae5b41b40f0edc357eec4020d17fdd61585960ad537418749ea" + inputs-digest = "ed1f3f7f1728cd02945f90ca780e9bdc982573a36a5cc8d7e9f19fb40ba2ca19" solver-name = "gps-cdcl" solver-version = 1 diff --git a/store/iavlstore.go b/store/iavlstore.go index 96110c59c..5f1917e2c 100644 --- a/store/iavlstore.go +++ b/store/iavlstore.go @@ -122,7 +122,24 @@ func (st *iavlStore) Iterator(start, end []byte) Iterator { func (st *iavlStore) Subspace(prefix []byte) Iterator { end := make([]byte, len(prefix)) copy(end, prefix) - end[len(end)-1]++ + finished := false + i := 1 + + for !finished { + fmt.Printf("%v %v \n", len(end), i) + fmt.Printf("%v \n", end) + if end[len(end)-i] != byte(255) { + end[len(end)-i]++ + finished = true + } else { + end[len(end)-i]++ + i++ + if i > len(end) { + end = []byte{} + finished = true + } + } + } return st.Iterator(prefix, end) } diff --git a/store/iavlstore_test.go b/store/iavlstore_test.go index 7adae625e..85e02ba25 100644 --- a/store/iavlstore_test.go +++ b/store/iavlstore_test.go @@ -73,13 +73,26 @@ func TestIAVLIterator(t *testing.T) { iavlStore := newIAVLStore(tree, numHistory) iter := iavlStore.Iterator([]byte("aloha"), []byte("hellz")) expected := []string{"aloha", "hello"} - for i := 0; iter.Valid(); iter.Next() { + i := 0 + for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] key, value := iter.Key(), iter.Value() assert.EqualValues(t, key, expectedKey) assert.EqualValues(t, value, treeData[expectedKey]) i += 1 } + assert.Equal(t, i, len(expected)) + + iter = iavlStore.Iterator([]byte("golang"), []byte("rocks")) + expected = []string{"hello"} + for i = 0; iter.Valid(); iter.Next() { + expectedKey := expected[i] + key, value := iter.Key(), iter.Value() + assert.EqualValues(t, key, expectedKey) + assert.EqualValues(t, value, treeData[expectedKey]) + i += 1 + } + assert.Equal(t, i, len(expected)) } func TestIAVLSubspace(t *testing.T) { @@ -90,16 +103,51 @@ func TestIAVLSubspace(t *testing.T) { iavlStore.Set([]byte("test1"), []byte("test1")) iavlStore.Set([]byte("test2"), []byte("test2")) iavlStore.Set([]byte("test3"), []byte("test3")) + iavlStore.Set([]byte{byte(55), byte(255), byte(255), byte(0)}, []byte("test4")) + iavlStore.Set([]byte{byte(55), byte(255), byte(255), byte(1)}, []byte("test4")) + iavlStore.Set([]byte{byte(55), byte(255), byte(255), byte(255)}, []byte("test4")) + i := 0 iter := iavlStore.Subspace([]byte("test")) expected := []string{"test1", "test2", "test3"} - for i := 0; iter.Valid(); iter.Next() { + for i = 0; iter.Valid(); iter.Next() { expectedKey := expected[i] key, value := iter.Key(), iter.Value() assert.EqualValues(t, key, expectedKey) assert.EqualValues(t, value, expectedKey) i += 1 } + assert.Equal(t, i, len(expected)) + + iter = iavlStore.Subspace([]byte{byte(55), byte(255), byte(255)}) + expected2 := [][]byte{ + []byte{byte(55), byte(255), byte(255), byte(0)}, + []byte{byte(55), byte(255), byte(255), byte(1)}, + []byte{byte(55), byte(255), byte(255), byte(255)}, + } + for i = 0; iter.Valid(); iter.Next() { + expectedKey := expected2[i] + key, value := iter.Key(), iter.Value() + assert.EqualValues(t, key, expectedKey) + assert.EqualValues(t, value, []byte("test4")) + i += 1 + } + assert.Equal(t, i, len(expected)) + + iter = iavlStore.Subspace([]byte{byte(255), byte(255)}) + expected2 = [][]byte{ + []byte{byte(255), byte(255), byte(0)}, + []byte{byte(255), byte(255), byte(1)}, + []byte{byte(255), byte(255), byte(255)}, + } + for i = 0; iter.Valid(); iter.Next() { + expectedKey := expected2[i] + key, value := iter.Key(), iter.Value() + assert.EqualValues(t, key, expectedKey) + assert.EqualValues(t, value, []byte("test4")) + i += 1 + } + assert.Equal(t, i, len(expected)) } func TestIAVLStoreQuery(t *testing.T) {