Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Dec 10, 2024
1 parent 3cd6794 commit 36d90b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
27 changes: 17 additions & 10 deletions examples/gno.land/p/demo/avl/index/index.gno
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,26 @@ func (it *IndexedTree) removeFromIndexes(primaryKey string, value interface{}) {
}
}

// Add these methods to IndexedTree to satisfy avl.TreeInterface
func (it *IndexedTree) Get(key string) (interface{}, bool) {
return it.primary.Get(key)
}

func (it *IndexedTree) Iterate(start, end string, cb func(key string, value interface{}) bool) bool {
return it.primary.Iterate(start, end, cb)
}

// Add this method to get access to an index as an avl.TreeInterface
func (it *IndexedTree) GetIndexTree(name string) avl.TreeInterface {
if idx, exists := it.indexes[name]; exists {
return idx.tree
}
return nil
}

func (it *IndexedTree) GetPrimary() avl.TreeInterface {
return it.primary
}

func (it *IndexedTree) Update(key string, oldValue interface{}, newValue interface{}) bool {
// Remove old value from indexes
it.removeFromIndexes(key, oldValue)

// Update primary tree
updated := it.primary.Set(key, newValue)

// Add new value to indexes
it.addToIndexes(key, newValue)

return updated
}
16 changes: 8 additions & 8 deletions examples/gno.land/p/demo/avl/index/index_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ func TestIndexedTreeComprehensive(t *testing.T) {
tree := NewIndexedTree()
p1 := &Person{ID: "1", Name: "Alice", Age: 30}

// Test Set and Get
// Test Set and Get using primary tree directly
tree.Set("1", p1)
val, exists := tree.Get("1")
val, exists := tree.GetPrimary().Get("1")
if !exists || val.(*Person).Name != "Alice" {
t.Error("Basic Get failed without indexes")
}

// Test direct tree iteration
count := 0
tree.Iterate("", "", func(key string, value interface{}) bool {
tree.GetPrimary().Iterate("", "", func(key string, value interface{}) bool {
count++
return false
})
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestIndexedTreeComprehensive(t *testing.T) {
p1 := &Person{ID: "1", Name: "Alice", Age: 30}

// Use direct tree methods
tree.primary.Set("1", p1)
tree.GetPrimary().Set("1", p1)

// Index should still work
results := tree.GetByIndex("age", "30")
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestIndexedTreeComprehensive(t *testing.T) {
// Remove and verify both primary and index
tree.Remove("1")

if _, exists := tree.Get("1"); exists {
if _, exists := tree.GetPrimary().Get("1"); exists {
t.Error("Entry still exists in primary after remove")
}

Expand All @@ -165,9 +165,9 @@ func TestIndexedTreeComprehensive(t *testing.T) {
p1 := &Person{ID: "1", Name: "Alice", Age: 30}
tree.Set("1", p1)

// Update age
p1.Age = 31
tree.Set("1", p1)
// Update age using the new Update method
p1New := &Person{ID: "1", Name: "Alice", Age: 31}
tree.Update("1", p1, p1New)

// Check old index is removed
results30 := tree.GetByIndex("age", "30")
Expand Down

0 comments on commit 36d90b0

Please sign in to comment.