You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Python-SDK/python-sdk-advanced-state-api.md
+36-7Lines changed: 36 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@
21
21
22
22
# Python SDK — Advanced State API
23
23
24
-
This document describes the **high-level state API** for the Python SDK: typed state abstractions (ValueState, ListState, MapState, etc.) built on top of the low-level KvStore, with serialization via **codecs** and optional **keyed state** per primary key. The design aligns with the [Go SDK Advanced State API](../Go-SDK/go-sdk-guide.md#7-advanced-state-api).
24
+
This document describes the **high-level state API** for the Python SDK: typed state abstractions (ValueState, ListState, MapState, etc.) built on top of the low-level KvStore, with serialization via **codecs** and optional **keyed state** per primary key.
25
25
26
26
**Two separate libraries:** The advanced state API is provided by **functionstream-api-advanced**, which depends on the low-level **functionstream-api**. Install with: `pip install functionstream-api functionstream-api-advanced`. Import Codec, ValueState, ListState, MapState, etc. from `fs_api_advanced`.
27
27
@@ -34,7 +34,7 @@ This document describes the **high-level state API** for the Python SDK: typed s
34
34
35
35
## 1. Overview
36
36
37
-
Use the advanced state API when you need structured state (single value, list, map, priority queue, aggregation, reduction) without manual byte encoding or key layout. You can create state either from the **runtime Context** (e.g. `ctx.getOrCreateValueState(...)` when using functionstream-runtime) or via **type-level constructors** on the state class (recommended for clarity and reuse, same pattern as the Go SDK).
37
+
Use the advanced state API when you need structured state (single value, list, map, priority queue, aggregation, reduction) without manual byte encoding or key layout. You can create state either from the **runtime Context** (e.g. `ctx.getOrCreateValueState(...)` when using functionstream-runtime) or via **type-level constructors** on the state class (recommended for clarity and reuse).
38
38
39
39
---
40
40
@@ -44,7 +44,7 @@ Use the advanced state API when you need structured state (single value, list, m
44
44
45
45
When using **functionstream-api-advanced**, the runtime Context implementation (e.g. WitContext in functionstream-runtime) provides `getOrCreateValueState(store_name, codec)`, `getOrCreateValueStateAutoCodec(store_name)`, and the same pattern for ListState, MapState, PriorityQueueState, AggregatingState, ReducingState, and all Keyed\* factories; these delegate to the type-level `from_context` / `from_context_auto_codec` methods below.
46
46
47
-
### 2.2 From the state type (recommended, same as Go SDK)
47
+
### 2.2 From the state type (recommended)
48
48
49
49
Each state type and keyed factory provides:
50
50
@@ -95,9 +95,15 @@ All of the above can also be obtained via the corresponding `ctx.getOrCreate*` m
95
95
96
96
You can also use the corresponding `ctx.getOrCreateKeyed*Factory(...)` methods, which delegate to these constructors.
97
97
98
+
### 4.3 KeyedValueState
99
+
100
+
KeyedValueState requires only a **value codec**; no ordered codec. Create state from the factory with `factory.new_keyed_value(primary_key, state_name="")`, yielding `KeyedValueState[V]`. State methods: `update(value)`, `value()` (returns `Optional[V]`), `clear()`. The primary key is fixed at creation time via `primary_key` (bytes).
101
+
98
102
---
99
103
100
-
## 5. Example: ValueState with from_context_auto_codec
104
+
## 5. Examples
105
+
106
+
### 5.1 ValueState (from_context_auto_codec)
101
107
102
108
Import ValueState from **fs_api_advanced** (Codec, ListState, MapState, etc. are in the same package):
103
109
@@ -108,8 +114,32 @@ from fs_api_advanced import ValueState
state = ValueState.from_context_auto_codec(ctx, "my-store")
111
-
cur, found = state.value()
112
-
ifnot found or cur isNone:
117
+
cur = state.value()
118
+
if cur isNone:
119
+
cur =0
120
+
state.update(cur +1)
121
+
ctx.emit(str(cur +1).encode(), 0)
122
+
```
123
+
124
+
### 5.2 KeyedValueState (keyed operator)
125
+
126
+
When the stream is partitioned by key, create the factory in `init` and obtain state per record’s `primary_key` in `process`, then use `update(value)` / `value()` / `clear()`:
127
+
128
+
```python
129
+
from fs_api import FSProcessorDriver, Context
130
+
from fs_api_advanced import KeyedValueStateFactory
0 commit comments