Skip to content

Commit 0211bc1

Browse files
committed
update
1 parent 7090143 commit 0211bc1

7 files changed

Lines changed: 36 additions & 10 deletions

File tree

docs/Go-SDK/go-sdk-advanced-state-api-zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Keyed API 对应 store 的 **ComplexKey**,有三个维度:
192192

193193
| 工厂 | 方法 | 返回 |
194194
|-----------------------------------|-----------------------------------------------------------------------------------------------------|-----------------------------------------|
195+
| KeyedValueStateFactory[V] | `NewKeyedValue(primaryKey []byte, namespace []byte) (*KeyedValueState[V], error)` | 每个 (primaryKey, namespace) 一个 value 状态。 |
195196
| KeyedListStateFactory[V] | `NewKeyedList(primaryKey []byte, namespace []byte) (*KeyedListState[V], error)` | 每个 (primaryKey, namespace) 一个 list 状态。 |
196197
| KeyedMapStateFactory[MK,MV] | `NewKeyedMap(primaryKey []byte, mapName string) (*KeyedMapState[MK,MV], error)` | 每个 (primaryKey, mapName) 一个 map 状态。 |
197198
| KeyedPriorityQueueStateFactory[V] | `NewKeyedPriorityQueue(primaryKey []byte, namespace []byte) (*KeyedPriorityQueueState[V], error)` | 每个 (primaryKey, namespace) 一个 PQ 状态。 |

docs/Go-SDK/go-sdk-advanced-state-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ The Keyed API maps onto the store’s **ComplexKey** with three dimensions:
192192

193193
| Factory | Method | Returns |
194194
|-----------------------------------|-----------------------------------------------------------------------------------------------------|------------------------------------------------|
195+
| KeyedValueStateFactory[V] | `NewKeyedValue(primaryKey []byte, namespace []byte) (*KeyedValueState[V], error)` | One value state per (primaryKey, namespace). |
195196
| KeyedListStateFactory[V] | `NewKeyedList(primaryKey []byte, namespace []byte) (*KeyedListState[V], error)` | List state per (primaryKey, namespace). |
196197
| KeyedMapStateFactory[MK,MV] | `NewKeyedMap(primaryKey []byte, mapName string) (*KeyedMapState[MK,MV], error)` | Map state per (primaryKey, mapName). |
197198
| KeyedPriorityQueueStateFactory[V] | `NewKeyedPriorityQueue(primaryKey []byte, namespace []byte) (*KeyedPriorityQueueState[V], error)` | PQ state per (primaryKey, namespace). |

docs/Python-SDK/python-sdk-advanced-state-api-zh.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
| 概念 | API 参数 | 含义 |
8080
|---------------|------------------------------------------|---------------------------------------------------------|
8181
| **key_group** | 创建工厂时的 `key_group` | **keyed 组**:标识该状态所属分区/组(如一组 “counters”,另一组 “sessions”)。 |
82-
| **key** | 构造状态时的 `primary_key`(如 `KeyedValueState(factory, primary_key, namespace)`| 当前记录的**流 key 的值**(如用户 ID、分区 key)。不同 key 对应不同状态。 |
82+
| **key** | 工厂方法参数(如 `new_keyed_value(primary_key, namespace)`| 当前记录的**流 key 的值**(如用户 ID、分区 key)。不同 key 对应不同状态。 |
8383
| **namespace** | 创建工厂时的 `namespace`(bytes) | **有窗口时****窗口标识的 bytes****无窗口时****空 bytes**(如 `b""`)。 |
8484

8585
### 4.2 Keyed 工厂构造方法一览
@@ -97,7 +97,7 @@
9797

9898
### 4.3 KeyedValueState
9999

100-
KeyedValueState 与 Go SDK 一致:工厂仅需 `key_group`(无 namespace)。工厂:`KeyedValueStateFactory.from_context(ctx, store_name, key_group, value_codec)``from_context_auto_codec(ctx, store_name, key_group, value_type=None)`构造状态:`KeyedValueState(factory, primary_key, namespace)`,其中 namespace 可为 `state_name.encode("utf-8")`。状态方法:`update(value)``value()`(返回 `(value, found)`)、`clear()`
100+
KeyedValueState 与 Go SDK 一致:工厂仅需 `key_group`(无 namespace)。工厂:`KeyedValueStateFactory.from_context(ctx, store_name, key_group, value_codec)``from_context_auto_codec(ctx, store_name, key_group, value_type=None)`创建状态:`factory.new_keyed_value(primary_key, namespace)`namespace 为 bytes,必填)。状态方法:`update(value)``value()`(返回 `(value, found)`)、`clear()`
101101

102102
### 4.4 KeyedListState
103103

@@ -147,7 +147,7 @@ class CounterProcessor(FSProcessorDriver):
147147

148148
```python
149149
from fs_api import FSProcessorDriver, Context
150-
from fs_api_advanced import KeyedValueState, KeyedValueStateFactory
150+
from fs_api_advanced import KeyedValueStateFactory
151151

152152
class KeyedCounterProcessor(FSProcessorDriver):
153153
def init(self, ctx: Context, config: dict):
@@ -157,7 +157,7 @@ class KeyedCounterProcessor(FSProcessorDriver):
157157

158158
def process(self, ctx: Context, source_id: int, data: bytes):
159159
primary_key = data[:8]
160-
state = KeyedValueState(self._factory, primary_key, "count".encode("utf-8"))
160+
state = self._factory.new_keyed_value(primary_key, b"count")
161161
cur, found = state.value()
162162
if not found:
163163
cur = 0

docs/Python-SDK/python-sdk-advanced-state-api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ All of the above can also be obtained via the corresponding `ctx.getOrCreate*` m
7979
| Term | API parameter | Meaning |
8080
|---------------|-----------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
8181
| **key_group** | `key_group` when creating the factory | The **keyed group**: identifies which keyed partition/group this state belongs to (e.g. one group for "counters", another for "sessions"). |
82-
| **key** | `primary_key` when constructing state (e.g. `KeyedValueState(factory, primary_key, namespace)`) | The **value of the stream key** for the current record (e.g. user ID, partition key). Each distinct key value gets isolated state. |
82+
| **key** | Argument to factory methods (e.g. `new_keyed_value(primary_key, namespace)`) | The **value of the stream key** for the current record (e.g. user ID, partition key). Each distinct key value gets isolated state. |
8383
| **namespace** | `namespace` (bytes) when creating the factory | **If a window function is present**, use the **window identifier as bytes**. **Without windows**, pass **empty bytes** (e.g. `b""`). |
8484

8585
### 4.2 Factory constructor summary (keyed)
@@ -97,7 +97,7 @@ You can also use the corresponding `ctx.getOrCreateKeyed*Factory(...)` methods,
9797

9898
### 4.3 KeyedValueState
9999

100-
KeyedValueState aligns with the Go SDK: the factory takes only `key_group` (no namespace). Factory: `KeyedValueStateFactory.from_context(ctx, store_name, key_group, value_codec)` or `from_context_auto_codec(ctx, store_name, key_group, value_type=None)`. Construct state: `KeyedValueState(factory, primary_key, namespace)` with e.g. `namespace = state_name.encode("utf-8")`. State methods: `update(value)`, `value()` (returns `(value, found)`), `clear()`.
100+
KeyedValueState aligns with the Go SDK: the factory takes only `key_group` (no namespace). Factory: `KeyedValueStateFactory.from_context(ctx, store_name, key_group, value_codec)` or `from_context_auto_codec(ctx, store_name, key_group, value_type=None)`. Create state: `factory.new_keyed_value(primary_key, namespace)` (namespace is bytes, required). State methods: `update(value)`, `value()` (returns `(value, found)`), `clear()`.
101101

102102
### 4.4 KeyedListState
103103

@@ -147,7 +147,7 @@ When the stream is partitioned by key, create the factory in `init` and obtain s
147147

148148
```python
149149
from fs_api import FSProcessorDriver, Context
150-
from fs_api_advanced import KeyedValueState, KeyedValueStateFactory
150+
from fs_api_advanced import KeyedValueStateFactory
151151

152152
class KeyedCounterProcessor(FSProcessorDriver):
153153
def init(self, ctx: Context, config: dict):
@@ -157,7 +157,7 @@ class KeyedCounterProcessor(FSProcessorDriver):
157157

158158
def process(self, ctx: Context, source_id: int, data: bytes):
159159
primary_key = data[:8]
160-
state = KeyedValueState(self._factory, primary_key, "count".encode("utf-8"))
160+
state = self._factory.new_keyed_value(primary_key, b"count")
161161
cur, found = state.value()
162162
if not found:
163163
cur = 0

go-sdk-advanced/keyed/keyed_list_state.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ func (s *KeyedListState[V]) Get() ([]V, error) {
154154

155155
// Update replaces the list with the given values (one Put with batch payload).
156156
func (s *KeyedListState[V]) Update(values []V) error {
157-
s.Clear()
157+
if err := s.Clear(); err != nil {
158+
return err
159+
}
158160
payload, err := s.serializeBatch(values)
159161
if err != nil {
160162
return err

go-sdk-advanced/keyed/keyed_value_state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func newKeyedValueStateFactory[V any](
7373
}, nil
7474
}
7575

76+
// NewKeyedValue creates a KeyedValueState for the given primary key and namespace.
77+
func (f *KeyedValueStateFactory[V]) NewKeyedValue(primaryKey []byte, namespace []byte) (*KeyedValueState[V], error) {
78+
if primaryKey == nil || namespace == nil {
79+
return nil, api.NewError(api.ErrStoreInternal, "primary key and namespace are required")
80+
}
81+
return &KeyedValueState[V]{
82+
factory: f,
83+
primaryKey: common.DupBytes(primaryKey),
84+
namespace: common.DupBytes(namespace),
85+
}, nil
86+
}
87+
7688
type KeyedValueState[V any] struct {
7789
factory *KeyedValueStateFactory[V]
7890
primaryKey []byte

python/functionstream-api-advanced/src/fs_api_advanced/keyed/keyed_value_state.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
class KeyedValueStateFactory(Generic[V]):
22-
"""Factory for keyed value state. Create from context with key_group; construct KeyedValueState(factory, primary_key, namespace) per (primary_key, namespace)."""
22+
"""Factory for keyed value state. Create from context with key_group; obtain state via new_keyed_value(primary_key, namespace)."""
2323

2424
def __init__(
2525
self,
@@ -64,6 +64,16 @@ def from_context_auto_codec(
6464
codec = default_codec_for(value_type)
6565
return cls(store, key_group, codec)
6666

67+
def new_keyed_value(
68+
self, primary_key: bytes, namespace: bytes
69+
) -> "KeyedValueState[V]":
70+
"""Create a KeyedValueState for the given primary key and namespace."""
71+
if primary_key is None:
72+
raise KvError("keyed value state primary_key must not be None")
73+
if namespace is None:
74+
raise KvError("keyed value state namespace is required")
75+
return KeyedValueState(self, primary_key, namespace)
76+
6777

6878
class KeyedValueState(Generic[V]):
6979
"""Value state for one (primary_key, namespace). update(value), value() -> (value, found), clear()."""

0 commit comments

Comments
 (0)