Add KV Store Support
还没有人认领这个 Issue。
评估
调研方向
从 stubs/wit_world/imports/kv_store.py 开始,将 WIT 接口与所请求的 Python API 进行比较。然后使用 test.toml 和 @on_viceroy 检查 Viceroy 测试配置,以了解内联或基于文件的 KV 数据。完成的标准是已实现 CRUD、流式条目、选项、类似 dict 的访问、列表功能以及相应的测试。
由索引模型根据 Issue 内容生成。
描述
Overview
Add support for Fastly's KV Store, providing distributed key-value storage with read and write operations at the edge.
WIT Interface
interface kv-store {
use types.{error, open-error};
use http-body.{body};
resource store {
open: static func(name: string) -> result<store, open-error>;
lookup: func(key: string) -> result<option<entry>, kv-error>;
insert: func(key: string, body: body, options: insert-options) -> result<_, kv-error>;
delete: func(key: string) -> result<bool, kv-error>;
%list: func(options: list-options) -> result<body, kv-error>;
}
resource entry {
take-body: func() -> option<body>;
metadata: func(max-len: u64) -> result<option<string>, error>;
generation: func() -> u64;
}
resource extra-kv-error;
variant kv-error {
bad-request,
precondition-failed,
payload-too-large,
internal-error,
too-many-requests,
generic-error,
extra(extra-kv-error),
}
enum insert-mode {
overwrite,
add,
append,
prepend,
}
resource extra-insert-options;
record insert-options {
background-fetch: bool,
if-generation-match: option<u64>,
metadata: option<string>,
time-to-live-sec: option<u32>,
mode: insert-mode,
extra: option<borrow<extra-insert-options>>,
}
enum list-mode {
strong,
eventual,
}
resource extra-list-options;
record list-options {
mode: list-mode,
cursor: option<string>,
limit: option<u32>,
prefix: option<string>,
extra: option<borrow<extra-list-options>>,
}
}
WIT bindings: stubs/wit_world/imports/kv_store.py
API Design
- Implement
KVStoreresource wrapper - Implement
KVStoreEntryas a file-like object inheriting fromio.IOBaseorio.RawIOBase:- Implement
read(size=-1)orreadinto(b)for standard file-like interface - Users get
read_all()for free via.read()with no size argument - Standard library functions work:
shutil.copyfileobj(),io.BufferedReader, etc. metadataproperty,generationpropertytext()convenience:entry.read().decode('utf-8')json()convenience:json.loads(entry.read())
- Implement
- Provide dict-like interface on Store:
__getitem__,__setitem__,__delitem__,__contains__ - Support
InsertOptionswith modes (overwrite, add, append, prepend), TTL, metadata, and generation matching - List operation returns iterator over keys with optional prefix filtering
Streaming Support: Entry values can be large (up to 25MB). By implementing standard io.IOBase:
- Users can use familiar file-like API:
entry.read(8192)for chunks,entry.read()for all - Works with stdlib:
shutil.copyfileobj(entry, response_body)for zero-copy proxying - Can wrap in
io.BufferedReaderfor additional buffering if needed - Document that
.read()with no argument loads entire value into memory
Example:
entry = store.lookup("large-file")
# Streaming approach (memory-efficient) - standard file-like API
chunk = entry.read(8192)
while chunk:
process_chunk(chunk)
chunk = entry.read(8192)
# Or use with stdlib utilities
import shutil
shutil.copyfileobj(entry, output_file)
# Eager loading (beware large values!)
data = entry.read() # reads all, standard Python pattern
text = entry.read().decode('utf-8') # or entry.text()
obj = json.loads(entry.read()) # or entry.json()
Note on Future Async Support: Using io.IOBase for sync API is compatible with later adding async support. The WIT layer provides Pollable objects and select() for async operations. If/when async is added:
- Sync API:
entry.read()- returns immediately (blocking) - Async API:
async def read()- returns coroutine, usesawaitwithPollable - These would be separate classes/methods, not the same
io.IOBaseinstance - Similar to how
aiofilesprovides async wrappers over sync file operations
Cross-SDK Comparison:
-
Rust:
StoreHandle::open()with methodslookup(),insert(),delete(). ReturnsLookupResponsewithtake_body()(streaming),take_body_bytes()(eager),metadata(),generation(). Haslist()returning iterator overListPage. Supports async withPendingLookupHandle, etc. Strongly typedInsertModeandListModeenums. -
Go:
Open()returns*StorewithLookup(),Insert(),Delete(). Entry embedsio.Readerfor streaming, plusString()helper for eager loading (with warning about memory).Meta(),Generation()accessors. No built-in async support (blocks). No list method yet. -
JS:
new KVStore(name)with async methodsget(),put(),delete(),list(). Entry hasbody(ReadableStream) for streaming,text(),json(),arrayBuffer()for eager loading,metadata(),metadataText(). Put options includettl,mode,gen. List returns{list: string[], cursor: string | undefined}.
Recommended Python approach:
- Entry should inherit from
io.IOBaseto be a proper file-like object - Implement
read(size=-1)method - standard file API that Python users know - No need for custom
bytes(),read_all()- users just call.read()with no args - Works with stdlib:
shutil.copyfileobj(),io.BufferedReader, etc. - Convenience methods (
text(),json()) are thin wrappers over.read() - Dict-like API:
store[key],store[key] = value,del store[key],key in store list(prefix=None, limit=None, cursor=None)returning iterator/generatorInsertModeenum for put operations (overwrite,add,append,prepend)
Viceroy Testing
Viceroy supports KV Store with inline or file-based test data via test.toml:
[local_server]
# Inline data
kv_stores.my_store = [
{key = "user:123", data = "John Doe"},
{key = "config", file = "path/to/file.txt"},
{key = "metadata_example", data = "value", metadata = "some metadata"}
]
# Or JSON file format
kv_stores.json_store = { file = "data/store.json", format = "json" }
Full CRUD operations (lookup, insert, delete) are supported. List operations work with test data. Tests can use @on_viceroy with inline TOML configuration.
Reference
- 主要语言
- Python
- 星标
- 5
- 派生
- 1
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fastly/compute-sdk-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
fastly/compute-sdk-python#116 ·
-
难度 4/5 3-5 天 新手友好度 35/100
fastly/compute-sdk-python#98 ·
-
难度 5/5 一周以上 新手友好度 35/100
fastly/compute-sdk-python#74 ·
-
难度 5/5 一周以上 新手友好度 35/100
fastly/compute-sdk-python#61 ·
-
fastly/compute-sdk-python#60 · 已指派 1 人 ·
查看 fastly/compute-sdk-python 的全部 Issue
相似的 Issue
-
area: harness bug status: needs-triage
难度 2/5 1-3 小时 新手友好度 75/100
Human-Agent-Society/reef#625 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 1/5 1 小时以内 新手友好度 80/100
learningequality/kolibri#15351 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
Name consistency 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
eellak/triplestore#65 · 1 条评论 ·