aboutsummaryrefslogtreecommitdiffstats
path: root/ethdb
diff options
context:
space:
mode:
authorgary rong <garyrong0905@gmail.com>2018-07-02 16:16:30 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-07-02 16:16:30 +0800
commita4a2343cdc1946e38da1aea1476642d1744c1354 (patch)
treec01c563224aaf34a1a391665bcca0b693b9cf0ce /ethdb
parentfdfd6d3c3963b1b3459e4625458495458b11e8a7 (diff)
downloaddexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.gz
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.tar.zst
dexon-a4a2343cdc1946e38da1aea1476642d1744c1354.zip
ethdb, core: implement delete for db batch (#17101)
Diffstat (limited to 'ethdb')
-rw-r--r--ethdb/database.go10
-rw-r--r--ethdb/interface.go8
-rw-r--r--ethdb/memory_database.go9
3 files changed, 26 insertions, 1 deletions
diff --git a/ethdb/database.go b/ethdb/database.go
index f4a5ce2c8..e32c912f9 100644
--- a/ethdb/database.go
+++ b/ethdb/database.go
@@ -388,6 +388,12 @@ func (b *ldbBatch) Put(key, value []byte) error {
return nil
}
+func (b *ldbBatch) Delete(key []byte) error {
+ b.b.Delete(key)
+ b.size += 1
+ return nil
+}
+
func (b *ldbBatch) Write() error {
return b.db.Write(b.b, nil)
}
@@ -453,6 +459,10 @@ func (tb *tableBatch) Put(key, value []byte) error {
return tb.batch.Put(append([]byte(tb.prefix), key...), value)
}
+func (tb *tableBatch) Delete(key []byte) error {
+ return tb.batch.Delete(append([]byte(tb.prefix), key...))
+}
+
func (tb *tableBatch) Write() error {
return tb.batch.Write()
}
diff --git a/ethdb/interface.go b/ethdb/interface.go
index 537312003..af1355779 100644
--- a/ethdb/interface.go
+++ b/ethdb/interface.go
@@ -25,12 +25,17 @@ type Putter interface {
Put(key []byte, value []byte) error
}
+// Deleter wraps the database delete operation supported by both batches and regular databases.
+type Deleter interface {
+ Delete(key []byte) error
+}
+
// Database wraps all database operations. All methods are safe for concurrent use.
type Database interface {
Putter
+ Deleter
Get(key []byte) ([]byte, error)
Has(key []byte) (bool, error)
- Delete(key []byte) error
Close()
NewBatch() Batch
}
@@ -39,6 +44,7 @@ type Database interface {
// when Write is called. Batch cannot be used concurrently.
type Batch interface {
Putter
+ Deleter
ValueSize() int // amount of data in the batch
Write() error
// Reset resets the batch for reuse
diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go
index c57042920..f28ff5481 100644
--- a/ethdb/memory_database.go
+++ b/ethdb/memory_database.go
@@ -110,11 +110,20 @@ func (b *memBatch) Put(key, value []byte) error {
return nil
}
+func (b *memBatch) Delete(key []byte) error {
+ b.writes = append(b.writes, kv{common.CopyBytes(key), nil})
+ return nil
+}
+
func (b *memBatch) Write() error {
b.db.lock.Lock()
defer b.db.lock.Unlock()
for _, kv := range b.writes {
+ if kv.v == nil {
+ delete(b.db.db, string(kv.k))
+ continue
+ }
b.db.db[string(kv.k)] = kv.v
}
return nil