/
batch.go
71 lines (66 loc) · 2.74 KB
/
batch.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
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package batch
import "github.com/pkg/errors"
var (
// ErrAlreadyDeleted indicates the key has been deleted
ErrAlreadyDeleted = errors.New("already deleted from DB")
// ErrAlreadyExist indicates certain item already exists in Blockchain database
ErrAlreadyExist = errors.New("already exist in DB")
// ErrNotExist indicates certain item does not exist in Blockchain database
ErrNotExist = errors.New("not exist in DB")
// ErrOutOfBound indicates an out of bound error
ErrOutOfBound = errors.New("out of bound")
)
type (
// KVStoreBatch defines a batch buffer interface that stages Put/Delete entries in sequential order
// To use it, first start a new batch
// b := NewBatch()
// and keep batching Put/Delete operation into it
// b.Put(bucket, k, v)
// b.Delete(bucket, k, v)
// once it's done, call KVStore interface's WriteBatch() to persist to underlying DB
// KVStore.WriteBatch(b)
// if commit succeeds, the batch is cleared
// otherwise the batch is kept intact (so batch user can figure out what’s wrong and attempt re-commit later)
KVStoreBatch interface {
// Lock locks the batch
Lock()
// Unlock unlocks the batch
Unlock()
// ClearAndUnlock clears the write queue and unlocks the batch
ClearAndUnlock()
// Put insert or update a record identified by (namespace, key)
Put(string, []byte, []byte, string, ...interface{})
// Delete deletes a record by (namespace, key)
Delete(string, []byte, string, ...interface{})
// Size returns the size of batch
Size() int
// Entry returns the entry at the index
Entry(int) (*WriteInfo, error)
// SerializeQueue serialize the writes in queue
SerializeQueue(WriteInfoSerialize, WriteInfoFilter) []byte
// Clear clears entries staged in batch
Clear()
// Translate clones the batch
Translate(WriteInfoTranslate) KVStoreBatch
// CheckFillPercent
CheckFillPercent(string) (float64, bool)
// AddFillPercent
AddFillPercent(string, float64)
}
// CachedBatch derives from Batch interface
// A local cache is added to provide fast retrieval of pending Put/Delete entries
CachedBatch interface {
KVStoreBatch
// Get gets a record by (namespace, key)
Get(string, []byte) ([]byte, error)
// Snapshot takes a snapshot of current cached batch
Snapshot() int
// Revert sets the cached batch to the state at the given snapshot
Revert(int) error
}
)