Documentation
¶
Overview ¶
本文件实现 filekv 后端:基于文件系统的键值存储。
本文件实现 memorykv 后端:内存键值存储(测试与临时场景用)。
本文件实现 sqlitekv 后端:基于 SQLite 的键值存储(纯 Go 驱动 modernc.org/sqlite)。
Package storage 提供 Storage Domain KV 抽象(hub → backend → domain 三层)。
对齐上游:packages/core/storage-domain
分层设计:
- Backend:底层键值存储抽象(Get/Put/Delete/List + 版本号),支持 filekv(JSONL 文件)、sqlitekv(SQLite 表)、memorykv(测试用)多实现;
- Domain[T]:面向业务域的类型化读写层,通过编码序列化 T 落到 backend, 并对外暴露 CAS(Compare-And-Swap)语义:写时必须携带期望版本号, 版本冲突返回统一的 ErrCASMismatch,跨后端错误行为一致;
- 供 MessageFeedback / GoalRevisionViews / SessionSidecars / SkillsCatalog / WorkspaceRegistry 等域复用同一套 CAS + 类型化读写。
Index ¶
- func IsCASMismatch(err error) bool
- func IsKeyNotFound(err error) bool
- func Migrate(ctx context.Context, src, dst Backend) (migrated int, skipped []string, err error)
- type Backend
- type Domain
- func (d *Domain[T]) Delete(ctx context.Context, key string) error
- func (d *Domain[T]) Get(ctx context.Context, key string) (T, uint64, error)
- func (d *Domain[T]) List(ctx context.Context) ([]string, error)
- func (d *Domain[T]) Put(ctx context.Context, key string, value T, expectedVersion uint64) (uint64, error)
- type ErrCASMismatch
- type ErrKeyNotFound
- type FileKV
- func (f *FileKV) Delete(ctx context.Context, key string) error
- func (f *FileKV) Get(ctx context.Context, key string) ([]byte, uint64, error)
- func (f *FileKV) List(ctx context.Context, prefix string) ([]string, error)
- func (f *FileKV) Put(ctx context.Context, key string, data []byte, expectedVersion uint64) (uint64, error)
- type MemoryKV
- func (m *MemoryKV) Delete(ctx context.Context, key string) error
- func (m *MemoryKV) Get(ctx context.Context, key string) ([]byte, uint64, error)
- func (m *MemoryKV) List(ctx context.Context, prefix string) ([]string, error)
- func (m *MemoryKV) Put(ctx context.Context, key string, data []byte, expectedVersion uint64) (uint64, error)
- type SQLiteKV
- func (s *SQLiteKV) Close() error
- func (s *SQLiteKV) Delete(ctx context.Context, key string) error
- func (s *SQLiteKV) Get(ctx context.Context, key string) ([]byte, uint64, error)
- func (s *SQLiteKV) List(ctx context.Context, prefix string) ([]string, error)
- func (s *SQLiteKV) Put(ctx context.Context, key string, data []byte, expectedVersion uint64) (uint64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Backend ¶
type Backend interface {
// Get 读取 key 的数据与当前版本;key 不存在返回 ErrKeyNotFound。
Get(ctx context.Context, key string) (data []byte, version uint64, err error)
// Put 按 CAS 语义写入 key;expectedVersion=0 表示期望新建。
Put(ctx context.Context, key string, data []byte, expectedVersion uint64) (version uint64, err error)
// Delete 删除 key(不存在不报错)。
Delete(ctx context.Context, key string) error
// List 返回指定前缀下的所有 key(不含 prefix 前缀)。
List(ctx context.Context, prefix string) (keys []string, err error)
}
Backend 是底层键值存储后端接口。 约定:
- 版本号从 0 开始;首次写入(expectedVersion=0)创建记录,成功后版本变为 1;
- Put 携带的 expectedVersion 必须等于当前版本,否则返回 *ErrCASMismatch。
type Domain ¶
type Domain[T any] struct { // contains filtered or unexported fields }
Domain 是一个业务域的类型化存储视图(key 带统一前缀,避免跨域碰撞)。
type ErrCASMismatch ¶
ErrCASMismatch 表示乐观锁(CAS)版本冲突:写入时携带的期望版本与实际版本不符。
type FileKV ¶
type FileKV struct {
// contains filtered or unexported fields
}
FileKV 是基于文件系统的 Backend 实现。 每个 key 对应 <root>/<key> 文件,内容为 {"version":N,"data":"<base64>"}。
type MemoryKV ¶
type MemoryKV struct {
// contains filtered or unexported fields
}
MemoryKV 是基于内存 map 的 Backend 实现。
type SQLiteKV ¶
type SQLiteKV struct {
// contains filtered or unexported fields
}
SQLiteKV 是基于 SQLite 的 Backend 实现。 表结构:kv(key TEXT PRIMARY KEY, version INTEGER, data BLOB)。
func NewSQLiteKV ¶
NewSQLiteKV 打开(或创建)SQLite 数据库并初始化表。
Click to show internal directories.
Click to hide internal directories.