forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathleveldb.go
More file actions
211 lines (165 loc) · 5.45 KB
/
Copy pathleveldb.go
File metadata and controls
211 lines (165 loc) · 5.45 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package XDCxDAO
import (
"bytes"
"encoding/hex"
"errors"
"sync"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log"
)
type BatchItem struct {
Value interface{}
}
type BatchDatabase struct {
db ethdb.Database
emptyKey []byte
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, "", false)
if err != nil {
log.Error("Can't create new DB", "error", err)
return nil
}
itemCacheLimit := defaultCacheLimit
if cacheLimit > 0 {
itemCacheLimit = cacheLimit
}
batchDB := &BatchDatabase{
db: db,
emptyKey: EmptyKey(), // pre alloc for comparison
cacheLimit: itemCacheLimit,
}
return batchDB
}
func (db *BatchDatabase) IsEmptyKey(key []byte) bool {
return 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
}
// Tail returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) Tail() (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
}
// TruncateHead returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) TruncateHead(items uint64) error {
return errNotSupported
}
// TruncateTail returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) TruncateTail(items uint64) error {
return errNotSupported
}
func (db *BatchDatabase) AncientRange(kind string, start, max, maxByteSize uint64) ([][]byte, error) {
return nil, errNotSupported
}
func (db *BatchDatabase) ReadAncients(fn func(ethdb.AncientReaderOp) error) (err error) {
return fn(db)
}
// MigrateTable processes the entries in a given table in sequence
// converting them to a new format if they're of an old format.
func (db *BatchDatabase) MigrateTable(kind string, convert func([]byte) ([]byte, error)) error {
return errNotSupported
}
// AncientDatadir returns an error as we don't have a backing chain freezer.
func (db *BatchDatabase) AncientDatadir() (string, error) {
return "", errNotSupported
}
func (db *BatchDatabase) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (writeSize int64, err error) {
return 0, 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 {
panic("NewIterator from XDCxDAO leveldb is not supported")
}
func (db *BatchDatabase) Stat(property string) (string, error) {
return "", errNotSupported
}
func (db *BatchDatabase) Compact(start []byte, limit []byte) error {
return errNotSupported
}