diff options
Diffstat (limited to 'state')
| -rw-r--r-- | state/memory_backend.go | 57 | ||||
| -rw-r--r-- | state/memory_state.go | 44 |
2 files changed, 101 insertions, 0 deletions
diff --git a/state/memory_backend.go b/state/memory_backend.go new file mode 100644 index 0000000..6594657 --- /dev/null +++ b/state/memory_backend.go | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | package state | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "flux/core" | ||
| 6 | ) | ||
| 7 | |||
| 8 | // TODO: currently using Golang Map for go through the DAG pipline | ||
| 9 | // This should be use index map index map[uint64]uint32 Hash(KeyGroup + StateName + Key) -> Offset | ||
| 10 | // with 1GB of []byte allocation | ||
| 11 | // This should be optimize with ZERO GC Scanning, Memory Alignment | ||
| 12 | |||
| 13 | type MemoryStateBackend struct { | ||
| 14 | maxParallelism int | ||
| 15 | currentKey string | ||
| 16 | currentGroupKey int | ||
| 17 | currentNamespace any | ||
| 18 | // 4D structure StateName -> KeyGroup -> Key -> Namespace -> Value | ||
| 19 | states map[string]map[int]map[string]map[any]any | ||
| 20 | } | ||
| 21 | |||
| 22 | func NewMemoryStateBackend(maxParallelism int) *MemoryStateBackend { | ||
| 23 | return &MemoryStateBackend{ | ||
| 24 | maxParallelism: maxParallelism, | ||
| 25 | states: make(map[string]map[int]map[string]map[any]any), | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | func (ms *MemoryStateBackend) GetCurrentKey() string { | ||
| 30 | return ms.currentKey | ||
| 31 | } | ||
| 32 | |||
| 33 | func (ms *MemoryStateBackend) SetCurrentKey(key string) error { | ||
| 34 | ms.currentKey = key | ||
| 35 | ms.currentGroupKey = core.AssignToKeyGroup(key, ms.maxParallelism) | ||
| 36 | return nil | ||
| 37 | } | ||
| 38 | |||
| 39 | func (ms *MemoryStateBackend) GetValueState(ctx context.Context, key string) (core.ValueState[any], error) { | ||
| 40 | // Assign if state not existed | ||
| 41 | |||
| 42 | if _, ok := ms.states[key]; !ok { | ||
| 43 | ms.states[key] = make(map[int]map[string]map[any]any) | ||
| 44 | } | ||
| 45 | |||
| 46 | return &memoryStateValue{backend: ms, stateName: key}, nil | ||
| 47 | } | ||
| 48 | |||
| 49 | func (ms *MemoryStateBackend) GetCurrentNamespace() any{ | ||
| 50 | |||
| 51 | return ms.currentNamespace | ||
| 52 | } | ||
| 53 | |||
| 54 | func (ms *MemoryStateBackend) SetCurrentNamespace(namespace any) error { | ||
| 55 | ms.currentNamespace = namespace | ||
| 56 | return nil | ||
| 57 | } | ||
diff --git a/state/memory_state.go b/state/memory_state.go new file mode 100644 index 0000000..95c7318 --- /dev/null +++ b/state/memory_state.go | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | package state | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | ) | ||
| 6 | |||
| 7 | type memoryStateValue struct { | ||
| 8 | backend *MemoryStateBackend | ||
| 9 | stateName string | ||
| 10 | } | ||
| 11 | |||
| 12 | func (m *memoryStateValue) Value(ctx context.Context) (any, error) { | ||
| 13 | kg := m.backend.currentGroupKey | ||
| 14 | key := m.backend.currentKey | ||
| 15 | ns := m.backend.currentNamespace | ||
| 16 | if kgMap, exist := m.backend.states[m.stateName][kg][key]; exist { | ||
| 17 | if v, hasKey := kgMap[ns]; hasKey { | ||
| 18 | return v, nil | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | return nil, nil | ||
| 23 | } | ||
| 24 | |||
| 25 | func (m *memoryStateValue) Update(ctx context.Context, newVal any) (any, error) { | ||
| 26 | kg := m.backend.currentGroupKey | ||
| 27 | key := m.backend.currentKey | ||
| 28 | ns := m.backend.currentNamespace | ||
| 29 | if _, exists := m.backend.states[m.stateName][kg][key]; !exists { | ||
| 30 | m.backend.states[m.stateName][kg] = make(map[string]map[any]any) | ||
| 31 | } | ||
| 32 | |||
| 33 | m.backend.states[m.stateName][kg][key][ns] = newVal | ||
| 34 | return newVal, nil | ||
| 35 | } | ||
| 36 | |||
| 37 | func (m *memoryStateValue) Clear(ctx context.Context) error { | ||
| 38 | kg := m.backend.currentGroupKey | ||
| 39 | key := m.backend.currentKey | ||
| 40 | if kgMap, exists := m.backend.states[m.stateName][kg]; exists { | ||
| 41 | delete(kgMap, key) | ||
| 42 | } | ||
| 43 | return nil | ||
| 44 | } | ||
