diff options
Diffstat (limited to 'state')
| -rw-r--r-- | state/memory_backend.go | 21 | ||||
| -rw-r--r-- | state/memory_state.go | 18 |
2 files changed, 24 insertions, 15 deletions
diff --git a/state/memory_backend.go b/state/memory_backend.go index 6594657..f4043a1 100644 --- a/state/memory_backend.go +++ b/state/memory_backend.go | |||
| @@ -2,6 +2,7 @@ package state | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | |||
| 5 | "flux/core" | 6 | "flux/core" |
| 6 | ) | 7 | ) |
| 7 | 8 | ||
| @@ -11,9 +12,9 @@ import ( | |||
| 11 | // This should be optimize with ZERO GC Scanning, Memory Alignment | 12 | // This should be optimize with ZERO GC Scanning, Memory Alignment |
| 12 | 13 | ||
| 13 | type MemoryStateBackend struct { | 14 | type MemoryStateBackend struct { |
| 14 | maxParallelism int | 15 | maxParallelism int |
| 15 | currentKey string | 16 | currentKey string |
| 16 | currentGroupKey int | 17 | currentGroupKey int |
| 17 | currentNamespace any | 18 | currentNamespace any |
| 18 | // 4D structure StateName -> KeyGroup -> Key -> Namespace -> Value | 19 | // 4D structure StateName -> KeyGroup -> Key -> Namespace -> Value |
| 19 | states map[string]map[int]map[string]map[any]any | 20 | states map[string]map[int]map[string]map[any]any |
| @@ -36,18 +37,18 @@ func (ms *MemoryStateBackend) SetCurrentKey(key string) error { | |||
| 36 | return nil | 37 | return nil |
| 37 | } | 38 | } |
| 38 | 39 | ||
| 39 | func (ms *MemoryStateBackend) GetValueState(ctx context.Context, key string) (core.ValueState[any], error) { | 40 | func (ms *MemoryStateBackend) GetValueState(ctx context.Context, statename string) (core.ValueState[any], error) { |
| 40 | // Assign if state not existed | 41 | // Assign if state not existed |
| 41 | 42 | ||
| 42 | if _, ok := ms.states[key]; !ok { | 43 | if _, ok := ms.states[statename]; !ok { |
| 43 | ms.states[key] = make(map[int]map[string]map[any]any) | 44 | // create state name map if not existed |
| 45 | ms.states[statename] = make(map[int]map[string]map[any]any) | ||
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | return &memoryStateValue{backend: ms, stateName: key}, nil | 48 | return &memoryStateValue{backend: ms, stateName: statename}, nil |
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | func (ms *MemoryStateBackend) GetCurrentNamespace() any{ | 51 | func (ms *MemoryStateBackend) GetCurrentNamespace() any { |
| 50 | |||
| 51 | return ms.currentNamespace | 52 | return ms.currentNamespace |
| 52 | } | 53 | } |
| 53 | 54 | ||
diff --git a/state/memory_state.go b/state/memory_state.go index 95c7318..97ecad1 100644 --- a/state/memory_state.go +++ b/state/memory_state.go | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | package state | 1 | package state |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| 5 | ) | 5 | ) |
| 6 | 6 | ||
| 7 | type memoryStateValue struct { | 7 | type memoryStateValue struct { |
| @@ -13,7 +13,7 @@ func (m *memoryStateValue) Value(ctx context.Context) (any, error) { | |||
| 13 | kg := m.backend.currentGroupKey | 13 | kg := m.backend.currentGroupKey |
| 14 | key := m.backend.currentKey | 14 | key := m.backend.currentKey |
| 15 | ns := m.backend.currentNamespace | 15 | ns := m.backend.currentNamespace |
| 16 | if kgMap, exist := m.backend.states[m.stateName][kg][key]; exist { | 16 | if kgMap, ok := m.backend.states[m.stateName][kg][key]; ok { |
| 17 | if v, hasKey := kgMap[ns]; hasKey { | 17 | if v, hasKey := kgMap[ns]; hasKey { |
| 18 | return v, nil | 18 | return v, nil |
| 19 | } | 19 | } |
| @@ -26,10 +26,15 @@ func (m *memoryStateValue) Update(ctx context.Context, newVal any) (any, error) | |||
| 26 | kg := m.backend.currentGroupKey | 26 | kg := m.backend.currentGroupKey |
| 27 | key := m.backend.currentKey | 27 | key := m.backend.currentKey |
| 28 | ns := m.backend.currentNamespace | 28 | ns := m.backend.currentNamespace |
| 29 | if _, exists := m.backend.states[m.stateName][kg][key]; !exists { | 29 | |
| 30 | if _, ok := m.backend.states[m.stateName][kg]; !ok { | ||
| 30 | m.backend.states[m.stateName][kg] = make(map[string]map[any]any) | 31 | m.backend.states[m.stateName][kg] = make(map[string]map[any]any) |
| 31 | } | 32 | } |
| 32 | 33 | ||
| 34 | if _, ok := m.backend.states[m.stateName][kg][key]; !ok { | ||
| 35 | m.backend.states[m.stateName][kg][key] = make(map[any]any) | ||
| 36 | } | ||
| 37 | |||
| 33 | m.backend.states[m.stateName][kg][key][ns] = newVal | 38 | m.backend.states[m.stateName][kg][key][ns] = newVal |
| 34 | return newVal, nil | 39 | return newVal, nil |
| 35 | } | 40 | } |
| @@ -37,8 +42,11 @@ func (m *memoryStateValue) Update(ctx context.Context, newVal any) (any, error) | |||
| 37 | func (m *memoryStateValue) Clear(ctx context.Context) error { | 42 | func (m *memoryStateValue) Clear(ctx context.Context) error { |
| 38 | kg := m.backend.currentGroupKey | 43 | kg := m.backend.currentGroupKey |
| 39 | key := m.backend.currentKey | 44 | key := m.backend.currentKey |
| 40 | if kgMap, exists := m.backend.states[m.stateName][kg]; exists { | 45 | ns := m.backend.currentNamespace |
| 41 | delete(kgMap, key) | 46 | if kgMap, ok := m.backend.states[m.stateName][kg]; ok { |
| 47 | if nsMap, ok := kgMap[key]; ok { | ||
| 48 | delete(nsMap, ns) | ||
| 49 | } | ||
| 42 | } | 50 | } |
| 43 | return nil | 51 | return nil |
| 44 | } | 52 | } |
