diff options
| author | sangtran <tranquangsang12.7@gmail.com> | 2026-09-05 17:06:30 +0700 |
|---|---|---|
| committer | sangtran <tranquangsang12.7@gmail.com> | 2026-09-05 17:06:30 +0700 |
| commit | e626cee8f7de0dd3ffca4c7297e59a87f790cc02 (patch) | |
| tree | 1b57f5e3134459872890e3df7484be5cf3cb3ba6 /state/pebble_backend.go | |
| parent | 67be8b5e2b5188d4291ed0a3c3a91b5e40bfbebe (diff) | |
| signature | Verified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust | |
| download | flux-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_backend.go')
| -rw-r--r-- | state/pebble_backend.go | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/state/pebble_backend.go b/state/pebble_backend.go index e428988..fbb8d68 100644 --- a/state/pebble_backend.go +++ b/state/pebble_backend.go | |||
| @@ -33,10 +33,11 @@ func NewPebbleStateBackend(dir string, maxParalleism int) (*PebbleStateBackend, | |||
| 33 | return nil, fmt.Errorf("failed to open pebbledb %w", err) | 33 | return nil, fmt.Errorf("failed to open pebbledb %w", err) |
| 34 | } | 34 | } |
| 35 | return &PebbleStateBackend{ | 35 | return &PebbleStateBackend{ |
| 36 | maxParallelism: maxParalleism, | 36 | maxParallelism: maxParalleism, |
| 37 | db: db, | 37 | db: db, |
| 38 | stateMap: make(map[string]uint8), | 38 | stateMap: make(map[string]uint8), |
| 39 | nextStateID: 1, | 39 | nextStateID: 1, |
| 40 | currentNamespace: &core.VoidNamespace{}, | ||
| 40 | }, nil | 41 | }, nil |
| 41 | } | 42 | } |
| 42 | 43 | ||
| @@ -60,23 +61,27 @@ func (p *PebbleStateBackend) GetCurrentNamespace() any { | |||
| 60 | } | 61 | } |
| 61 | 62 | ||
| 62 | // GetValueState(ctx context.Context, key string) (ValueState[any], error) | 63 | // GetValueState(ctx context.Context, key string) (ValueState[any], error) |
| 63 | func (p *PebbleStateBackend) GetValueState(ctx context.Context, statename string) (core.ValueState[any], error) { | 64 | func (p *PebbleStateBackend) GetValueState(ctx context.Context, stateName string) (core.ValueState[any], error) { |
| 64 | stateID := p.getStateID(statename) | 65 | stateID := p.getStateID(stateName) |
| 65 | return nil, nil | 66 | |
| 67 | return &pebbleStateValue{ | ||
| 68 | backend: p, | ||
| 69 | stateID: stateID, | ||
| 70 | }, nil | ||
| 66 | } | 71 | } |
| 67 | 72 | ||
| 68 | func (p *PebbleStateBackend) getStateID(statename string) uint8 { | 73 | func (p *PebbleStateBackend) getStateID(stateName string) uint8 { |
| 69 | p.mu.Lock() | 74 | p.mu.Lock() |
| 70 | defer p.mu.Unlock() | 75 | defer p.mu.Unlock() |
| 71 | 76 | ||
| 72 | if _, ok := p.stateMap[statename]; !ok { | 77 | if _, ok := p.stateMap[stateName]; !ok { |
| 73 | 78 | ||
| 74 | id := p.nextStateID | 79 | id := p.nextStateID |
| 75 | p.stateMap[statename] = id | 80 | p.stateMap[stateName] = id |
| 76 | p.nextStateID++ | 81 | p.nextStateID++ |
| 77 | } | 82 | } |
| 78 | 83 | ||
| 79 | return p.stateMap[statename] | 84 | return p.stateMap[stateName] |
| 80 | } | 85 | } |
| 81 | 86 | ||
| 82 | func (p *PebbleStateBackend) calculateNamespaceSize() int { | 87 | func (p *PebbleStateBackend) calculateNamespaceSize() int { |
| @@ -94,7 +99,7 @@ func (p *PebbleStateBackend) calculateNamespaceSize() int { | |||
| 94 | return nsLen | 99 | return nsLen |
| 95 | } | 100 | } |
| 96 | 101 | ||
| 97 | func (p *PebbleStateBackend) deserialize(statename string) []byte { | 102 | func (p *PebbleStateBackend) deserialize(stateID uint8) []byte { |
| 98 | // this will store in SSTable, order is important, keygroup MUST be first for scaling | 103 | // this will store in SSTable, order is important, keygroup MUST be first for scaling |
| 99 | // 2B(KeyGroup) + 1B(StateID) + 2B(KeyLen) + len(Key) + 16B(Window) | 104 | // 2B(KeyGroup) + 1B(StateID) + 2B(KeyLen) + len(Key) + 16B(Window) |
| 100 | 105 | ||
| @@ -105,7 +110,6 @@ func (p *PebbleStateBackend) deserialize(statename string) []byte { | |||
| 105 | b = binary.BigEndian.AppendUint16(b, uint16(p.currentGroupKey)) | 110 | b = binary.BigEndian.AppendUint16(b, uint16(p.currentGroupKey)) |
| 106 | 111 | ||
| 107 | // add state id | 112 | // add state id |
| 108 | stateID := p.getStateID(statename) | ||
| 109 | b = append(b, stateID) | 113 | b = append(b, stateID) |
| 110 | 114 | ||
| 111 | // add key | 115 | // add key |
