Skip to content

Commit 7f32a08

Browse files
committed
Queued level db writes and batch writes. Closes bnb-chain#647
1 parent 758205b commit 7f32a08

3 files changed

Lines changed: 94 additions & 46 deletions

File tree

‎common/db.go‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package common
44
type Database interface {
55
Put(key []byte, value []byte)
66
Get(key []byte) ([]byte, error)
7-
//GetKeys() []*Key
87
Delete(key []byte) error
98
LastKnownTD() []byte
109
Close()
11-
Print()
1210
}

‎ethdb/database.go‎

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package ethdb
22

33
import (
4-
"fmt"
4+
"sync"
5+
"time"
56

67
"github.com/ethereum/go-ethereum/compression/rle"
7-
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/logger"
9+
"github.com/ethereum/go-ethereum/logger/glog"
810
"github.com/syndtr/goleveldb/leveldb"
911
"github.com/syndtr/goleveldb/leveldb/iterator"
1012
)
1113

1214
type LDBDatabase struct {
13-
db *leveldb.DB
14-
comp bool
15+
fn string
16+
17+
mu sync.Mutex
18+
db *leveldb.DB
19+
20+
queue map[string][]byte
21+
22+
quit chan struct{}
1523
}
1624

1725
func NewLDBDatabase(file string) (*LDBDatabase, error) {
@@ -20,35 +28,61 @@ func NewLDBDatabase(file string) (*LDBDatabase, error) {
2028
if err != nil {
2129
return nil, err
2230
}
23-
database := &LDBDatabase{db: db, comp: true}
31+
database := &LDBDatabase{
32+
fn: file,
33+
db: db,
34+
quit: make(chan struct{}),
35+
}
36+
database.makeQueue()
37+
38+
go database.update()
39+
2440
return database, nil
2541
}
2642

43+
func (self *LDBDatabase) makeQueue() {
44+
self.queue = make(map[string][]byte)
45+
}
46+
2747
func (self *LDBDatabase) Put(key []byte, value []byte) {
28-
if self.comp {
48+
self.mu.Lock()
49+
defer self.mu.Unlock()
50+
51+
self.queue[string(key)] = value
52+
/*
2953
value = rle.Compress(value)
30-
}
3154
32-
err := self.db.Put(key, value, nil)
33-
if err != nil {
34-
fmt.Println("Error put", err)
35-
}
55+
err := self.db.Put(key, value, nil)
56+
if err != nil {
57+
fmt.Println("Error put", err)
58+
}
59+
*/
3660
}
3761

3862
func (self *LDBDatabase) Get(key []byte) ([]byte, error) {
63+
self.mu.Lock()
64+
defer self.mu.Unlock()
65+
66+
// Check queue first
67+
if dat, ok := self.queue[string(key)]; ok {
68+
return dat, nil
69+
}
70+
3971
dat, err := self.db.Get(key, nil)
4072
if err != nil {
4173
return nil, err
4274
}
4375

44-
if self.comp {
45-
return rle.Decompress(dat)
46-
}
47-
48-
return dat, nil
76+
return rle.Decompress(dat)
4977
}
5078

5179
func (self *LDBDatabase) Delete(key []byte) error {
80+
self.mu.Lock()
81+
defer self.mu.Unlock()
82+
83+
// make sure it's not in the queue
84+
delete(self.queue, string(key))
85+
5286
return self.db.Delete(key, nil)
5387
}
5488

@@ -66,23 +100,46 @@ func (self *LDBDatabase) NewIterator() iterator.Iterator {
66100
return self.db.NewIterator(nil, nil)
67101
}
68102

69-
func (self *LDBDatabase) Write(batch *leveldb.Batch) error {
103+
func (self *LDBDatabase) Flush() error {
104+
self.mu.Lock()
105+
defer self.mu.Unlock()
106+
107+
batch := new(leveldb.Batch)
108+
109+
for key, value := range self.queue {
110+
batch.Put([]byte(key), rle.Compress(value))
111+
}
112+
self.makeQueue() // reset the queue
113+
70114
return self.db.Write(batch, nil)
71115
}
72116

73117
func (self *LDBDatabase) Close() {
74-
// Close the leveldb database
75-
self.db.Close()
118+
self.quit <- struct{}{}
119+
<-self.quit
120+
glog.V(logger.Info).Infoln("flushed and closed db:", self.fn)
76121
}
77122

78-
func (self *LDBDatabase) Print() {
79-
iter := self.db.NewIterator(nil, nil)
80-
for iter.Next() {
81-
key := iter.Key()
82-
value := iter.Value()
123+
func (self *LDBDatabase) update() {
124+
ticker := time.NewTicker(1 * time.Minute)
125+
done:
126+
for {
127+
select {
128+
case <-ticker.C:
129+
if err := self.Flush(); err != nil {
130+
glog.V(logger.Error).Infof("error: flush '%s': %v\n", self.fn, err)
131+
}
132+
case <-self.quit:
133+
break done
134+
}
135+
}
83136

84-
fmt.Printf("%x(%d): ", key, len(key))
85-
node := common.NewValueFromBytes(value)
86-
fmt.Printf("%v\n", node)
137+
if err := self.Flush(); err != nil {
138+
glog.V(logger.Error).Infof("error: flush '%s': %v\n", self.fn, err)
87139
}
140+
141+
// Close the leveldb database
142+
self.db.Close()
143+
144+
self.quit <- struct{}{}
88145
}

‎ethdb/database_test.go‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package ethdb
22

3-
/*
43
import (
5-
"bytes"
6-
"testing"
4+
"os"
5+
"path"
6+
7+
"github.com/ethereum/go-ethereum/common"
78
)
89

9-
func TestCompression(t *testing.T) {
10-
db, err := NewLDBDatabase("testdb")
11-
if err != nil {
12-
t.Fatal(err)
10+
func newDb() *LDBDatabase {
11+
file := path.Join("/", "tmp", "ldbtesttmpfile")
12+
if common.FileExist(file) {
13+
os.RemoveAll(file)
1314
}
1415

15-
in := make([]byte, 10)
16-
db.Put([]byte("test1"), in)
17-
out, err := db.Get([]byte("test1"))
18-
if err != nil {
19-
t.Fatal(err)
20-
}
16+
db, _ := NewLDBDatabase(file)
2117

22-
if bytes.Compare(out, in) != 0 {
23-
t.Error("put get", in, out)
24-
}
18+
return db
2519
}
26-
*/

0 commit comments

Comments
 (0)