From 4d62f01af159fddd56585b3dce19b8a0f47369c1 Mon Sep 17 00:00:00 2001 From: SangTran-127 Date: Sun, 9 Aug 2026 01:11:31 +0700 Subject: feat: implement memory state backend with key group assignment and value state interface --- core/hash.go | 19 +++++++++++++++++++ core/state.go | 10 ++++++++++ core/state_backend.go | 13 +++++++++++++ state/memory_backend.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 core/hash.go create mode 100644 core/state.go create mode 100644 core/state_backend.go create mode 100644 state/memory_backend.go diff --git a/core/hash.go b/core/hash.go new file mode 100644 index 0000000..af15546 --- /dev/null +++ b/core/hash.go @@ -0,0 +1,19 @@ +package core + +import "hash/fnv" + +const DefaultMaxParallelism = 128 + +func AssignToKeyGroup(key string, maxParallelism int) int { + + if maxParallelism <= 0 { + maxParallelism = DefaultMaxParallelism + } + + // hash + h := fnv.New32a() + h.Write([]byte(key)) + hash := h.Sum32() + + return int(hash % uint32(maxParallelism)) +} diff --git a/core/state.go b/core/state.go new file mode 100644 index 0000000..d6d7847 --- /dev/null +++ b/core/state.go @@ -0,0 +1,10 @@ +package core + +import "context" + +// ValueState is for client side using to update state +type ValueState[T any] interface { + Value(ctx context.Context) (T, error) + Update(ctx context.Context, newVal T) (T, error) + Clear(ctx context.Context) error +} diff --git a/core/state_backend.go b/core/state_backend.go new file mode 100644 index 0000000..219bb63 --- /dev/null +++ b/core/state_backend.go @@ -0,0 +1,13 @@ +package core + +import "context" + +type StateBackend interface { + // SetCurrentKey will perform in Memory + SetCurrentKey(key string) error + // GetCurrentKey will perform in Memory + GetCurrentKey() string + + // GetValueState will perform in disk + GetValueState(ctx context.Context, key string) (ValueState[any], error) +} diff --git a/state/memory_backend.go b/state/memory_backend.go new file mode 100644 index 0000000..58e86e0 --- /dev/null +++ b/state/memory_backend.go @@ -0,0 +1,41 @@ +package state + +import ( + "context" + "goflink/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 + // 3D structure StateName -> KeyGroup -> Key -> Value + states map[string]map[int]map[string]any +} + +func NewMemoryStateBackend(maxParallelism int) *MemoryStateBackend { + return &MemoryStateBackend{ + maxParallelism: maxParallelism, + states: make(map[string]map[int]map[string]any), + } +} + +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, key string) (core.ValueState[any], error) { + //TODO implement me + panic("implement me") +} -- cgit v1.2.3