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 c604ec6 commit d73023e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
62 changes: 54 additions & 8 deletions examples/gno.land/p/demo/avl/index/index.gno
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ func (it *IndexedTree) GetByIndex(indexName string, indexKey string) []interface
}

var results []interface{}
index.tree.Iterate(indexKey, indexKey, func(key string, value interface{}) bool {
primaryKey := value.(string) // The index stores primary keys as values
if primaryValue, exists := it.primary.Get(primaryKey); exists {
results = append(results, primaryValue)

// Get all primary keys that match the index key
value, exists := index.tree.Get(indexKey)
if exists {
primaryKeys := value.([]string)
for _, primaryKey := range primaryKeys {
if primaryValue, exists := it.primary.Get(primaryKey); exists {
results = append(results, primaryValue)
}
}
return false // Continue iteration
})
}

return results
}
Expand All @@ -90,13 +94,55 @@ func (it *IndexedTree) GetByIndex(indexName string, indexKey string) []interface
func (it *IndexedTree) addToIndexes(primaryKey string, value interface{}) {
for _, index := range it.indexes {
indexKey := index.extract(value)
index.tree.Set(indexKey, primaryKey)

// Get existing keys or create new slice
var keys []string
if existing, exists := index.tree.Get(indexKey); exists {
keys = existing.([]string)
}
keys = append(keys, primaryKey)

index.tree.Set(indexKey, keys)
}
}

func (it *IndexedTree) removeFromIndexes(primaryKey string, value interface{}) {
for _, index := range it.indexes {
indexKey := index.extract(value)
index.tree.Remove(indexKey)

// Get existing keys
if existing, exists := index.tree.Get(indexKey); exists {
keys := existing.([]string)
// Remove the primary key from the slice
newKeys := make([]string, 0)
for _, k := range keys {
if k != primaryKey {
newKeys = append(newKeys, k)
}
}
// If there are still keys, update the index, otherwise remove it
if len(newKeys) > 0 {
index.tree.Set(indexKey, newKeys)
} else {
index.tree.Remove(indexKey)
}
}
}
}

// 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
}
1 change: 0 additions & 1 deletion examples/gno.land/p/demo/avl/index/index_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestIndexedTree(t *testing.T) {

// Query by name index
aliceResults := it.GetByIndex("name", "Alice")
println(aliceResults)
if len(aliceResults) != 1 {
t.Error("Expected 1 result for Alice")
}
Expand Down

0 comments on commit d73023e

Please sign in to comment.