Skip to content

Commit

Permalink
fix: concurrent access
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Jul 12, 2023
1 parent ddbee02 commit 7fb4bb1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func (tree *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stoppe
return tree.ImmutableTree.Iterate(fn)
}

tree.mtx.Lock()
defer tree.mtx.Unlock()
itr := NewUnsavedFastIterator(nil, nil, true, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
Expand All @@ -213,6 +215,8 @@ func (tree *MutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterat
}

if isFastCacheEnabled {
tree.mtx.Lock()
defer tree.mtx.Unlock()
return NewUnsavedFastIterator(start, end, ascending, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals), nil
}
}
Expand Down Expand Up @@ -965,13 +969,17 @@ func (tree *MutableTree) getUnsavedFastNodeRemovals() map[string]interface{} {
}

func (tree *MutableTree) addUnsavedAddition(key []byte, node *FastNode) {
tree.mtx.Lock()
defer tree.mtx.Unlock()
skey := unsafeToStr(key)
delete(tree.unsavedFastNodeRemovals, skey)
tree.unsavedFastNodeAdditions[skey] = node
}

func (tree *MutableTree) saveFastNodeAdditions() error {
keysToSort := make([]string, 0, len(tree.unsavedFastNodeAdditions))
tree.mtx.Lock()
defer tree.mtx.Unlock()
for key := range tree.unsavedFastNodeAdditions {
keysToSort = append(keysToSort, key)
}
Expand Down

0 comments on commit 7fb4bb1

Please sign in to comment.