Browse Source

Put->Set

pull/9/head
Jae Kwon 11 years ago
parent
commit
cc4f3c90d3
6 changed files with 26 additions and 26 deletions
  1. +1
    -1
      db/level_db.go
  2. +1
    -1
      db/mem_db.go
  3. +4
    -4
      merkle/iavl_node.go
  4. +16
    -16
      merkle/iavl_test.go
  5. +2
    -2
      merkle/iavl_tree.go
  6. +2
    -2
      merkle/types.go

+ 1
- 1
db/level_db.go View File

@ -20,7 +20,7 @@ func NewLevelDB(name string) (*LevelDB, error) {
return database, nil return database, nil
} }
func (db *LevelDB) Put(key []byte, value []byte) {
func (db *LevelDB) Set(key []byte, value []byte) {
err := db.db.Put(key, value, nil) err := db.db.Put(key, value, nil)
if err != nil { if err != nil {
panic(err) panic(err)


+ 1
- 1
db/mem_db.go View File

@ -13,7 +13,7 @@ func NewMemDB() *MemDB {
return database return database
} }
func (db *MemDB) Put(key []byte, value []byte) {
func (db *MemDB) Set(key []byte, value []byte) {
db.db[string(key)] = value db.db[string(key)] = value
} }


+ 4
- 4
merkle/iavl_node.go View File

@ -133,12 +133,12 @@ func (self *IAVLNode) Save(db Db) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
db.Put([]byte(self.hash), buf.Bytes())
db.Set([]byte(self.hash), buf.Bytes())
self.flags |= IAVLNODE_FLAG_PERSISTED self.flags |= IAVLNODE_FLAG_PERSISTED
} }
func (self *IAVLNode) put(db Db, key Key, value Value) (_ *IAVLNode, updated bool) {
func (self *IAVLNode) set(db Db, key Key, value Value) (_ *IAVLNode, updated bool) {
if self.height == 0 { if self.height == 0 {
if key.Less(self.key) { if key.Less(self.key) {
return &IAVLNode{ return &IAVLNode{
@ -162,9 +162,9 @@ func (self *IAVLNode) put(db Db, key Key, value Value) (_ *IAVLNode, updated boo
} else { } else {
self = self.Copy() self = self.Copy()
if key.Less(self.key) { if key.Less(self.key) {
self.left, updated = self.leftFilled(db).put(db, key, value)
self.left, updated = self.leftFilled(db).set(db, key, value)
} else { } else {
self.right, updated = self.rightFilled(db).put(db, key, value)
self.right, updated = self.rightFilled(db).set(db, key, value)
} }
if updated { if updated {
return self, updated return self, updated


+ 16
- 16
merkle/iavl_test.go View File

@ -75,8 +75,8 @@ func TestUnit(t *testing.T) {
} }
} }
expectPut := func(n *IAVLNode, i int, repr string, hashCount uint64) {
n2, updated := n.put(nil, Int32(i), nil)
expectSet := func(n *IAVLNode, i int, repr string, hashCount uint64) {
n2, updated := n.set(nil, Int32(i), nil)
// ensure node was added & structure is as expected. // ensure node was added & structure is as expected.
if updated == true || P(n2) != repr { if updated == true || P(n2) != repr {
t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v", t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
@ -97,28 +97,28 @@ func TestUnit(t *testing.T) {
expectHash(n2, hashCount) expectHash(n2, hashCount)
} }
//////// Test Put cases:
//////// Test Set cases:
// Case 1: // Case 1:
n1 := N(4, 20) n1 := N(4, 20)
expectPut(n1, 8, "((4 8) 20)", 3)
expectPut(n1, 25, "(4 (20 25))", 3)
expectSet(n1, 8, "((4 8) 20)", 3)
expectSet(n1, 25, "(4 (20 25))", 3)
n2 := N(4, N(20, 25)) n2 := N(4, N(20, 25))
expectPut(n2, 8, "((4 8) (20 25))", 3)
expectPut(n2, 30, "((4 20) (25 30))", 4)
expectSet(n2, 8, "((4 8) (20 25))", 3)
expectSet(n2, 30, "((4 20) (25 30))", 4)
n3 := N(N(1, 2), 6) n3 := N(N(1, 2), 6)
expectPut(n3, 4, "((1 2) (4 6))", 4)
expectPut(n3, 8, "((1 2) (6 8))", 3)
expectSet(n3, 4, "((1 2) (4 6))", 4)
expectSet(n3, 8, "((1 2) (6 8))", 3)
n4 := N(N(1, 2), N(N(5, 6), N(7, 9))) n4 := N(N(1, 2), N(N(5, 6), N(7, 9)))
expectPut(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
expectPut(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
expectSet(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
expectSet(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
//////// Test Remove cases: //////// Test Remove cases:
@ -156,11 +156,11 @@ func TestIntegration(t *testing.T) {
records[i] = r records[i] = r
//t.Log("New record", r) //t.Log("New record", r)
//PrintIAVLNode(tree.root) //PrintIAVLNode(tree.root)
updated = tree.Put(r.key, String(""))
updated = tree.Set(r.key, String(""))
if updated { if updated {
t.Error("should have not been updated") t.Error("should have not been updated")
} }
updated = tree.Put(r.key, r.value)
updated = tree.Set(r.key, r.value)
if !updated { if !updated {
t.Error("should have been updated") t.Error("should have been updated")
} }
@ -216,7 +216,7 @@ func TestPersistence(t *testing.T) {
// Construct some tree and save it // Construct some tree and save it
t1 := NewIAVLTree(db) t1 := NewIAVLTree(db)
for key, value := range records { for key, value := range records {
t1.Put(key, value)
t1.Set(key, value)
} }
t1.Save() t1.Save()
@ -260,7 +260,7 @@ func BenchmarkImmutableAvlTree(b *testing.B) {
t := NewIAVLTree(nil) t := NewIAVLTree(nil)
for i := 0; i < 1000000; i++ { for i := 0; i < 1000000; i++ {
r := randomRecord() r := randomRecord()
t.Put(r.key, r.value)
t.Set(r.key, r.value)
} }
fmt.Println("ok, starting") fmt.Println("ok, starting")
@ -270,7 +270,7 @@ func BenchmarkImmutableAvlTree(b *testing.B) {
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
r := randomRecord() r := randomRecord()
t.Put(r.key, r.value)
t.Set(r.key, r.value)
t.Remove(r.key) t.Remove(r.key)
} }
} }

+ 2
- 2
merkle/iavl_tree.go View File

@ -55,12 +55,12 @@ func (t *IAVLTree) Has(key Key) bool {
return t.root.has(t.db, key) return t.root.has(t.db, key)
} }
func (t *IAVLTree) Put(key Key, value Value) (updated bool) {
func (t *IAVLTree) Set(key Key, value Value) (updated bool) {
if t.root == nil { if t.root == nil {
t.root = NewIAVLNode(key, value) t.root = NewIAVLNode(key, value)
return false return false
} }
t.root, updated = t.root.put(t.db, key, value)
t.root, updated = t.root.set(t.db, key, value)
return updated return updated
} }


+ 2
- 2
merkle/types.go View File

@ -17,7 +17,7 @@ type Key interface {
type Db interface { type Db interface {
Get([]byte) []byte Get([]byte) []byte
Put([]byte, []byte)
Set([]byte, []byte)
} }
type Node interface { type Node interface {
@ -38,7 +38,7 @@ type Tree interface {
Get(key Key) Value Get(key Key) Value
Hash() (ByteSlice, uint64) Hash() (ByteSlice, uint64)
Save() Save()
Put(Key, Value) bool
Set(Key, Value) bool
Remove(Key) (Value, error) Remove(Key) (Value, error)
Copy() Tree Copy() Tree
Traverse(func(Node) bool) Traverse(func(Node) bool)


Loading…
Cancel
Save