-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatchdb.go
132 lines (104 loc) · 2.88 KB
/
batchdb.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
package tomox
import (
"bytes"
"encoding/hex"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/tomox/tomox_state"
"github.com/globalsign/mgo"
"sync"
"github.com/tomochain/tomochain/ethdb"
"github.com/tomochain/tomochain/log"
lru "github.com/hashicorp/golang-lru"
)
const (
defaultCacheLimit = 1024
)
type BatchItem struct {
Value interface{}
}
type BatchDatabase struct {
db *ethdb.LDBDatabase
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 := ethdb.NewLDBDatabase(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: tomox_state.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) (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) 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() {
db.db.Close()
}
func (db *BatchDatabase) NewBatch() ethdb.Batch {
return db.db.NewBatch()
}
func (db *BatchDatabase) DeleteTradeByTxHash(txhash common.Hash) {
}
func (db *BatchDatabase) GetOrderByTxHash(txhash common.Hash) []*tomox_state.OrderItem {
return []*tomox_state.OrderItem{}
}
func (db *BatchDatabase) GetListOrderByHashes(hashes []string) []*tomox_state.OrderItem {
return []*tomox_state.OrderItem{}
}
func (db *BatchDatabase) InitBulk() *mgo.Session {
return nil
}
func (db *BatchDatabase) CommitBulk() error {
return nil
}