-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbatch.go
134 lines (107 loc) · 3.16 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
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
package pebbledb
import (
"encoding/binary"
"fmt"
"github.com/cockroachdb/pebble"
"github.com/sei-protocol/sei-db/common/errors"
)
type Batch struct {
storage *pebble.DB
batch *pebble.Batch
version int64
}
func NewBatch(storage *pebble.DB, version int64) (*Batch, error) {
var versionBz [VersionSize]byte
binary.LittleEndian.PutUint64(versionBz[:], uint64(version))
batch := storage.NewBatch()
if err := batch.Set([]byte(latestVersionKey), versionBz[:], nil); err != nil {
return nil, fmt.Errorf("failed to write PebbleDB batch: %w", err)
}
return &Batch{
storage: storage,
batch: batch,
version: version,
}, nil
}
func (b *Batch) Size() int {
return b.batch.Len()
}
func (b *Batch) Reset() {
b.batch.Reset()
}
func (b *Batch) set(storeKey string, tombstone int64, key, value []byte) error {
prefixedKey := MVCCEncode(prependStoreKey(storeKey, key), b.version)
prefixedVal := MVCCEncode(value, tombstone)
if err := b.batch.Set(prefixedKey, prefixedVal, nil); err != nil {
return fmt.Errorf("failed to write PebbleDB batch: %w", err)
}
return nil
}
func (b *Batch) Set(storeKey string, key, value []byte) error {
return b.set(storeKey, 0, key, value)
}
func (b *Batch) Delete(storeKey string, key []byte) error {
return b.set(storeKey, b.version, key, []byte(tombstoneVal))
}
func (b *Batch) Write() (err error) {
defer func() {
err = errors.Join(err, b.batch.Close())
}()
return b.batch.Commit(defaultWriteOpts)
}
// RawBatch handles writing key-value pairs where versions and storeKeys can vary.
// It maintains separate batches for each storage (pebble.DB instance).
type RawBatch struct {
batches map[*pebble.DB]*pebble.Batch // Map from storage to batch
}
func NewRawBatch() *RawBatch {
return &RawBatch{
batches: make(map[*pebble.DB]*pebble.Batch),
}
}
func (rb *RawBatch) Size() int {
size := 0
for _, batch := range rb.batches {
size += batch.Len()
}
return size
}
func (rb *RawBatch) Reset() {
for _, batch := range rb.batches {
batch.Reset()
}
}
func (rb *RawBatch) set(db *pebble.DB, storeKey string, tombstone int64, key, value []byte, version int64) error {
batch, ok := rb.batches[db]
if !ok {
batch = db.NewBatch()
rb.batches[db] = batch
}
prefixedKey := MVCCEncode(prependStoreKey(storeKey, key), version)
prefixedVal := MVCCEncode(value, tombstone)
if err := batch.Set(prefixedKey, prefixedVal, nil); err != nil {
return fmt.Errorf("failed to write PebbleDB batch: %w", err)
}
return nil
}
func (rb *RawBatch) Set(db *pebble.DB, storeKey string, key, value []byte, version int64) error {
return rb.set(db, storeKey, 0, key, value, version)
}
func (rb *RawBatch) Delete(db *pebble.DB, storeKey string, key []byte, version int64) error {
return rb.set(db, storeKey, version, key, []byte(tombstoneVal), version)
}
func (rb *RawBatch) Write() (err error) {
for _, batch := range rb.batches {
if batch.Count() > 0 {
if commitErr := batch.Commit(defaultWriteOpts); commitErr != nil {
err = errors.Join(err, commitErr)
}
}
if closeErr := batch.Close(); closeErr != nil {
err = errors.Join(err, closeErr)
}
}
// Reset the batches after writing
rb.batches = make(map[*pebble.DB]*pebble.Batch)
return err
}