mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
* Add CacheDB & SimpleMap * Generic memBatch; Fix cLevelDB tests * CacheWrap() for CacheDB and MemDB * Change Iterator to match LeviGo Iterator * Fixes from review * cacheWrapWriteMutex and some race fixes * Use tmlibs/common * NewCWWMutex is exposed. DB can be CacheWrap'd * Remove GetOK, not needed * Fsdb (#72) * Add FSDB * Review fixes from Anton * Review changes * Fixes from review
51 lines
852 B
Go
51 lines
852 B
Go
package db
|
|
|
|
import "sync"
|
|
|
|
type atomicSetDeleter interface {
|
|
Mutex() *sync.Mutex
|
|
SetNoLock(key, value []byte)
|
|
DeleteNoLock(key []byte)
|
|
}
|
|
|
|
type memBatch struct {
|
|
db atomicSetDeleter
|
|
ops []operation
|
|
}
|
|
|
|
type opType int
|
|
|
|
const (
|
|
opTypeSet opType = 1
|
|
opTypeDelete opType = 2
|
|
)
|
|
|
|
type operation struct {
|
|
opType
|
|
key []byte
|
|
value []byte
|
|
}
|
|
|
|
func (mBatch *memBatch) Set(key, value []byte) {
|
|
mBatch.ops = append(mBatch.ops, operation{opTypeSet, key, value})
|
|
}
|
|
|
|
func (mBatch *memBatch) Delete(key []byte) {
|
|
mBatch.ops = append(mBatch.ops, operation{opTypeDelete, key, nil})
|
|
}
|
|
|
|
func (mBatch *memBatch) Write() {
|
|
mtx := mBatch.db.Mutex()
|
|
mtx.Lock()
|
|
defer mtx.Unlock()
|
|
|
|
for _, op := range mBatch.ops {
|
|
switch op.opType {
|
|
case opTypeSet:
|
|
mBatch.db.SetNoLock(op.key, op.value)
|
|
case opTypeDelete:
|
|
mBatch.db.DeleteNoLock(op.key)
|
|
}
|
|
}
|
|
}
|