aboutsummaryrefslogtreecommitdiff
path: root/state/pebble_state.go
diff options
context:
space:
mode:
authorsangtran <tranquangsang12.7@gmail.com>2026-09-05 17:06:30 +0700
committersangtran <tranquangsang12.7@gmail.com>2026-09-05 17:06:30 +0700
commite626cee8f7de0dd3ffca4c7297e59a87f790cc02 (patch)
tree1b57f5e3134459872890e3df7484be5cf3cb3ba6 /state/pebble_state.go
parent67be8b5e2b5188d4291ed0a3c3a91b5e40bfbebe (diff)
signatureVerified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust
downloadflux-e626cee8f7de0dd3ffca4c7297e59a87f790cc02.tar.gz
flux-e626cee8f7de0dd3ffca4c7297e59a87f790cc02.zip
Verified feat: add on pebble state backend and using sync Pool to reuse buffer []byte
Diffstat (limited to 'state/pebble_state.go')
-rw-r--r--state/pebble_state.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/state/pebble_state.go b/state/pebble_state.go
new file mode 100644
index 0000000..3eeaa05
--- /dev/null
+++ b/state/pebble_state.go
@@ -0,0 +1,54 @@
1package state
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "flux/core"
8
9 "github.com/cockroachdb/pebble/v2"
10)
11
12type pebbleStateValue struct {
13 backend *PebbleStateBackend
14 stateID uint8
15}
16
17func (p *pebbleStateValue) Update(ctx context.Context, newVal any) (any, error) {
18 key := p.backend.deserialize(p.stateID)
19 // convert newVal to []byte raw
20 v, err := json.Marshal(newVal)
21 if err != nil {
22 return nil, err
23 }
24
25 err = p.backend.db.Set(key, v, pebble.NoSync)
26 if err != nil {
27 return nil, err
28 }
29
30 return newVal, nil
31}
32
33func (p *pebbleStateValue) Value(ctx context.Context) (any, error) {
34 key := p.backend.deserialize(p.stateID)
35 v, closer, err := p.backend.db.Get(key)
36 if err != nil {
37 if errors.Is(err, pebble.ErrNotFound) {
38 return nil, nil
39 }
40 return nil, err
41 }
42
43 defer closer.Close()
44
45 res := core.GetBuffer(len(v))
46 copy(res, v)
47 return v, nil
48}
49
50func (p *pebbleStateValue) Clear(ctx context.Context) error {
51 key := p.backend.deserialize(p.stateID)
52
53 return p.backend.db.Delete(key, pebble.NoSync)
54}