-
Notifications
You must be signed in to change notification settings - Fork 6
/
leveldb.go
184 lines (143 loc) · 4.51 KB
/
leveldb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package XDCxDAO
import (
"bytes"
"encoding/hex"
"errors"
"sync"
"github.com/XinFinOrg/XDC-Subnet/common"
"github.com/XinFinOrg/XDC-Subnet/core/rawdb"
"github.com/XinFinOrg/XDC-Subnet/ethdb"
"github.com/XinFinOrg/XDC-Subnet/log"
lru "github.com/hashicorp/golang-lru"
)
type BatchItem struct {
Value interface{}
}
type BatchDatabase struct {
db ethdb.Database
emptyKey []byte
cacheItems *lru.Cache // Cache for reading
lock sync.RWMutex
cacheLimit int
Debug bool
}
// NewBatchDatabase use rlp as encoding
func NewBatchDatabase(datadir string, cacheLimit int) *BatchDatabase {
return NewBatchDatabaseWithEncode(datadir, cacheLimit)
}
// batchdatabase is a fast cache db to retrieve in-mem object
func NewBatchDatabaseWithEncode(datadir string, cacheLimit int) *BatchDatabase {
db, err := rawdb.NewLevelDBDatabase(datadir, 128, 1024, "")
if err != nil {
log.Error("Can't create new DB", "error", err)
return nil
}
itemCacheLimit := defaultCacheLimit
if cacheLimit > 0 {
itemCacheLimit = cacheLimit
}
cacheItems, _ := lru.New(itemCacheLimit)
batchDB := &BatchDatabase{
db: db,
cacheItems: cacheItems,
emptyKey: EmptyKey(), // pre alloc for comparison
cacheLimit: itemCacheLimit,
}
return batchDB
}
func (db *BatchDatabase) IsEmptyKey(key []byte) bool {
return key == nil || len(key) == 0 || bytes.Equal(key, db.emptyKey)
}
func (db *BatchDatabase) getCacheKey(key []byte) string {
return hex.EncodeToString(key)
}
func (db *BatchDatabase) HasObject(hash common.Hash, val interface{}) (bool, error) {
// for mongodb only
return false, nil
}
func (db *BatchDatabase) GetObject(hash common.Hash, val interface{}) (interface{}, error) {
// for mongodb only
return nil, nil
}
func (db *BatchDatabase) PutObject(hash common.Hash, val interface{}) error {
// for mongodb only
return nil
}
func (db *BatchDatabase) DeleteObject(hash common.Hash, val interface{}) error {
// for mongodb only
return nil
}
func (db *BatchDatabase) Put(key []byte, val []byte) error {
return db.db.Put(key, val)
}
func (db *BatchDatabase) Delete(key []byte) error {
return db.db.Delete(key)
}
func (db *BatchDatabase) Has(key []byte) (bool, error) {
return db.db.Has(key)
}
func (db *BatchDatabase) Get(key []byte) ([]byte, error) {
return db.db.Get(key)
}
func (db *BatchDatabase) Close() error {
return db.db.Close()
}
func (db *BatchDatabase) NewBatch() ethdb.Batch {
return db.db.NewBatch()
}
func (db *BatchDatabase) DeleteItemByTxHash(txhash common.Hash, val interface{}) {
}
func (db *BatchDatabase) GetListItemByTxHash(txhash common.Hash, val interface{}) interface{} {
return []interface{}{}
}
func (db *BatchDatabase) GetListItemByHashes(hashes []string, val interface{}) interface{} {
return []interface{}{}
}
func (db *BatchDatabase) InitBulk() {
}
func (db *BatchDatabase) CommitBulk() error {
return nil
}
func (db *BatchDatabase) InitLendingBulk() {
}
func (db *BatchDatabase) CommitLendingBulk() error {
return nil
}
var errNotSupported = errors.New("this operation is not supported")
// HasAncient returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) HasAncient(kind string, number uint64) (bool, error) {
return false, errNotSupported
}
// Ancient returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) Ancient(kind string, number uint64) ([]byte, error) {
return nil, errNotSupported
}
// Ancients returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) Ancients() (uint64, error) {
return 0, errNotSupported
}
// AncientSize returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) AncientSize(kind string) (uint64, error) {
return 0, errNotSupported
}
// AppendAncient returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) AppendAncient(number uint64, hash, header, body, receipts, td []byte) error {
return errNotSupported
}
// TruncateAncients returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) TruncateAncients(items uint64) error {
return errNotSupported
}
// Sync returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) Sync() error {
return errNotSupported
}
func (db *BatchDatabase) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
return db.NewIterator(prefix, start)
}
func (db *BatchDatabase) Stat(property string) (string, error) {
return db.Stat(property)
}
func (db *BatchDatabase) Compact(start []byte, limit []byte) error {
return db.Compact(start, limit)
}