Skip to content
Permalink
Browse files

Remove deletetion batch, because ethereum state db does not support

  • Loading branch information...
tubackkhoa committed Jan 17, 2019
1 parent 8718c78 commit b498438b09b6a4a97fafee443b5b90a3aed55da2
@@ -15,9 +15,10 @@ const (
defaultMaxPending = 1024
)

// BatchItem : currently we do not support deletion batch, so ignore it
type BatchItem struct {
Value interface{}
Deleted bool
Value interface{}
// Deleted bool
}

type BatchDatabase struct {
@@ -82,7 +83,7 @@ func (db *BatchDatabase) Has(key []byte) (bool, error) {
cacheKey := db.getCacheKey(key)

// has in pending and is not deleted
if pendingItem, ok := db.pendingItems[cacheKey]; ok && !pendingItem.Deleted {
if _, ok := db.pendingItems[cacheKey]; ok { // && !pendingItem.Deleted {
return true, nil
}

@@ -103,9 +104,9 @@ func (db *BatchDatabase) Get(key []byte, val interface{}) (interface{}, error) {
cacheKey := db.getCacheKey(key)

if pendingItem, ok := db.pendingItems[cacheKey]; ok {
if pendingItem.Deleted {
return nil, nil
}
// if pendingItem.Deleted {
// return nil, nil
// }
// we get value from the pending item
return pendingItem.Value, nil
}
@@ -147,13 +148,21 @@ func (db *BatchDatabase) Get(key []byte, val interface{}) (interface{}, error) {
func (db *BatchDatabase) Put(key []byte, val interface{}) error {

cacheKey := db.getCacheKey(key)

// // has in pending and is deleted
// if pendingItem, ok := db.pendingItems[cacheKey]; ok && pendingItem.Deleted {
// fmt.Println("Update a delete item")
// return nil
// }

// fmt.Println("PUT", cacheKey, val)
db.pendingItems[cacheKey] = &BatchItem{Value: val}

if len(db.pendingItems) >= db.itemMaxPending {
return db.Commit()
}

// return db.Commit()
return nil
}

@@ -168,8 +177,9 @@ func (db *BatchDatabase) Delete(key []byte, force bool) error {
delete(db.pendingItems, cacheKey)
db.cacheItems.Remove(cacheKey)
} else {
if item, ok := db.pendingItems[cacheKey]; ok {
item.Deleted = true
if _, ok := db.pendingItems[cacheKey]; ok {
// item.Deleted = true
db.db.Delete(key)
// remove cache key as well
db.cacheItems.Remove(cacheKey)
return nil
@@ -188,11 +198,11 @@ func (db *BatchDatabase) Commit() error {

// fmt.Printf("key :%x, cacheKey :%s\n", key, cacheKey)

if item.Deleted {
db.db.Delete(key)
// cache already has this item removed
continue
}
// if item.Deleted {
// db.db.Delete(key)
// // cache already has this item removed
// continue
// }

value, err := db.EncodeToBytes(item.Value)
if err != nil {
@@ -494,6 +494,7 @@ func (tree *Tree) replaceNode(old *Node, new *Node) {
oldParent.RightKey(newKey)
}
// fmt.Printf("Update old parent %v\n", oldParent)
// we can have case like: remove a node, then add it again
tree.Save(oldParent)
// }
// fmt.Println("Replace tree node", old, new, oldParent)
@@ -608,7 +609,10 @@ func (tree *Tree) insertCase5(node *Node) {

func (tree *Tree) Save(node *Node) error {
// value, err := json.Marshal(node.Item)
// tree.assertNotNull(node, hex.EncodeToString(node.Key))

return tree.db.Put(node.Key, node.Item)

// cacheKey := fmt.Sprintf("%x", node.Key)
// if !tree.itemCache.Contains(cacheKey) {
// tree.itemCache.Add(cacheKey, node.Item)
@@ -697,8 +701,9 @@ func (tree *Tree) deleteCase3(node *Node) {
tree.Save(sibling)
tree.deleteCase1(parent)

fmt.Println("delete node", string(node.Key), parent)

if tree.db.Debug {
fmt.Printf("delete node, key: %x, parentKey :%x\n", node.Key, parent.Key)
}
tree.deleteNode(node, false)

} else {
@@ -773,7 +778,7 @@ func (tree *Tree) deleteCase6(node *Node) {
tree.Save(sibling)
tree.Save(parent)

fmt.Println("before-update ", tree, sibling, parent, siblingLeft, siblingRight)
// fmt.Println("before-update ", tree, sibling, parent, siblingLeft, siblingRight)

if tree.Comparator(node.Key, parent.LeftKey()) == 0 && nodeColor(siblingRight) == red {
siblingRight.Item.Color = black
@@ -805,12 +810,11 @@ func (tree *Tree) deleteNode(node *Node, force bool) {
// parent := node.Parent(tree)
// fmt.Println("Update parent", parent, "Delete node", node)
// if tree.Comparator(node.Key, parent.LeftKey()) == 0 {
// parent.LeftKey(emptyKey)
// parent.LeftKey(EmptyKey())
// } else {
// parent.RightKey(emptyKey)
// parent.RightKey(EmptyKey())
// }
// tree.Save(parent)

// if force {
tree.db.Delete(node.Key, force)
// } else {
@@ -55,6 +55,7 @@ func (tree *RedBlackTreeExtended) RemoveMin() (value []byte, deleted bool) {
// fmt.Println("found min", node)
if found {
tree.Remove(node.Key)
// fmt.Printf("%x\n", node.Key)
return node.Value(), found
}
return nil, false
@@ -36,7 +36,7 @@ func getBig(value string) []byte {

func TestManipulateLevelDBTree(t *testing.T) {
tree := NewRedBlackTreeExtended(testDB)

testDB.Debug = true
start := time.Now()
tree.Put(getBig("1"), []byte("a")) // 1->a (in order)
tree.Put(getBig("2"), []byte("b")) // 1->a, 2->b (in order)
@@ -48,7 +48,6 @@ func TestManipulateLevelDBTree(t *testing.T) {

print(tree, t)

testDB.Commit()
// Value for max key: e
// Value for min key: a
// RedBlackTree
@@ -58,22 +57,32 @@ func TestManipulateLevelDBTree(t *testing.T) {
// └── 2
// └── 1

// tree.RemoveMin() // 2->b, 3->c, 4->d, 5->e (in order)
// tree.RemoveMax() // 2->b, 3->c, 4->d (in order)
// tree.RemoveMin() // 3->c, 4->d (in order)
// tree.RemoveMax() // 3->c (in order)
tree.RemoveMin() // 2->b, 3->c, 4->d, 5->e (in order)
tree.RemoveMax() // 2->b, 3->c, 4->d (in order)
tree.RemoveMin() // 3->c, 4->d (in order)

print(tree, t)

// print(tree, t)
// Value for max key: c
// Value for min key: c
// RedBlackTree
// └── 3
// │ ┌── 4
// └── 3

testDB.Commit()
}

func TestRestoreLevelDBTree(t *testing.T) {
tree := NewRedBlackTreeExtended(testDB)

tree.SetRootKey(getBig("2"), 5)
tree.SetRootKey(getBig("3"), 3)

tree.RemoveMax() // 3->c (in order)

print(tree, t)

// Value for max key: c
// Value for min key: c
// RedBlackTree
// └── 3
}

0 comments on commit b498438

Please sign in to comment.
You can’t perform that action at this time.