diff options
Diffstat (limited to 'state/memory_backend.go')
| -rw-r--r-- | state/memory_backend.go | 57 |
1 files changed, 57 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 | } | ||
