aboutsummaryrefslogtreecommitdiff
path: root/state/memory_backend.go
blob: de26cabc4713b4463eb404a1c66213a4de720080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package state

import (
	"context"

	"flux/core"
)

// TODO: currently using Golang Map for go through the DAG pipline
// This should be use index map index map[uint64]uint32  Hash(KeyGroup + StateName + Key) -> Offset
// with 1GB of []byte allocation
// This should be optimize with ZERO GC Scanning, Memory Alignment

type MemoryStateBackend struct {
	maxParallelism   int
	currentKey       string
	currentGroupKey  int
	currentNamespace any
	// 4D structure StateName -> KeyGroup -> Key -> Namespace -> Value
	states map[string]map[int]map[string]map[any]any
}

func NewMemoryStateBackend(maxParallelism int) *MemoryStateBackend {
	return &MemoryStateBackend{
		maxParallelism: maxParallelism,
		states:         make(map[string]map[int]map[string]map[any]any),
		// default is void namespace
		currentNamespace: core.VoidNamespace{},
	}
}

func (ms *MemoryStateBackend) GetCurrentKey() string {
	return ms.currentKey
}

func (ms *MemoryStateBackend) SetCurrentKey(key string) error {
	ms.currentKey = key
	ms.currentGroupKey = core.AssignToKeyGroup(key, ms.maxParallelism)
	return nil
}

func (ms *MemoryStateBackend) GetValueState(ctx context.Context, stateName string) (core.ValueState[any], error) {
	// Assign if state not existed
	
	if _, ok := ms.states[stateName]; !ok {
		// create state name map if not existed 
		ms.states[stateName] = make(map[int]map[string]map[any]any)
	}

	return &memoryStateValue{backend: ms, stateName: stateName}, nil
}

func (ms *MemoryStateBackend) GetCurrentNamespace() any {
	return ms.currentNamespace
}

func (ms *MemoryStateBackend) SetCurrentNamespace(namespace any) error {
	ms.currentNamespace = namespace
	return nil
}

func (ms *MemoryStateBackend) Snapshot(checkpointID uint64, targetDir string) error {
	// TODO: temporary return nil to avoid breaking
	return nil
}