@deepseek-ai/dsh-storage-json 0.2.1-rc.3 → 0.2.2-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +10 -10
- package/README.md +9 -6
- package/README.zh.md +9 -6
- package/lib/index.js +115 -42
- package/lib/types/atomic.d.ts +6 -0
- package/lib/types/format.d.ts +1 -1
- package/lib/types/per-record-unit.d.ts +34 -13
- package/lib/types/single-unit.d.ts +2 -1
- package/locale/de.json +6 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/package.json +6 -6
package/README.i18n.yaml
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
en: b04256279a328d19
|
|
10
10
|
zh: b0913489b49403b1
|
|
11
11
|
/deepseek-ai-dsh-storage-json/summary:
|
|
12
|
-
en:
|
|
13
|
-
zh:
|
|
12
|
+
en: ba875a866989d7d8
|
|
13
|
+
zh: 92e94db1d3d3577e
|
|
14
14
|
/deepseek-ai-dsh-storage-json/table-of-contents:
|
|
15
15
|
en: d152484eb41ac6b4
|
|
16
16
|
zh: 09388d293f9be9cb
|
|
@@ -18,20 +18,20 @@
|
|
|
18
18
|
en: 87ddc847b7024cac
|
|
19
19
|
zh: 6e97d0f216587d5b
|
|
20
20
|
/deepseek-ai-dsh-storage-json/use-this-package/when-to-choose-it:
|
|
21
|
-
en:
|
|
22
|
-
zh:
|
|
21
|
+
en: 40e60fa139e0d6f6
|
|
22
|
+
zh: 0c4ce58c26ee4bf7
|
|
23
23
|
/deepseek-ai-dsh-storage-json/use-this-package/configuration:
|
|
24
24
|
en: 7144aa3b5af76874
|
|
25
25
|
zh: dfc528d027f86037
|
|
26
26
|
/deepseek-ai-dsh-storage-json/use-this-package/observable-behavior:
|
|
27
|
-
en:
|
|
28
|
-
zh:
|
|
27
|
+
en: 79d83481397cb10b
|
|
28
|
+
zh: e160178363a57ba6
|
|
29
29
|
/deepseek-ai-dsh-storage-json/understand-the-implementation:
|
|
30
30
|
en: b7d222309b3e5438
|
|
31
31
|
zh: 807c7314ee341d94
|
|
32
32
|
/deepseek-ai-dsh-storage-json/understand-the-implementation/design-concept:
|
|
33
|
-
en:
|
|
34
|
-
zh:
|
|
33
|
+
en: 975a4b005d9f8d0d
|
|
34
|
+
zh: 71127701477ae358
|
|
35
35
|
/deepseek-ai-dsh-storage-json/understand-the-implementation/file-formats:
|
|
36
36
|
en: b3de0a697db4f916
|
|
37
37
|
zh: 56b26f7823392bd5
|
|
@@ -57,8 +57,8 @@
|
|
|
57
57
|
en: c13c59d13a5f1651
|
|
58
58
|
zh: 5aab838ae3ff5324
|
|
59
59
|
/deepseek-ai-dsh-storage-json/known-limitations-and-deferred-work:
|
|
60
|
-
en:
|
|
61
|
-
zh:
|
|
60
|
+
en: 073ca8d8c9bb8123
|
|
61
|
+
zh: 088ca8438f11ce6c
|
|
62
62
|
/deepseek-ai-dsh-storage-json/known-limitations-and-deferred-work/dev-note:
|
|
63
63
|
en: dd40dd4cb9109aea
|
|
64
64
|
zh: 3fd79d2242eccadb
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ English | [中文](README.zh.md)
|
|
|
9
9
|
|
|
10
10
|
## Summary
|
|
11
11
|
|
|
12
|
-
`dsh-storage-json` stores domain data as readable JSON under a configured root and registers as backend `json`. Its default `single` layout keeps one complete `<unit>.json` file per unit; its `per-record` layout keeps one version-stamped document per record. Both layouts publish each changed file atomically, while the domain layer orders calls. Choose it when operators need inspectable files and the selected layout fits the write volume; choose SQLite for larger
|
|
12
|
+
`dsh-storage-json` stores domain data as readable JSON under a configured root and registers as backend `json`. Its default `single` layout keeps one complete `<unit>.json` file per unit; its `per-record` layout keeps one version-stamped document per record. Both layouts publish each changed file atomically, while the domain layer orders calls. Choose it when operators need inspectable files and the selected layout fits the write volume; choose SQLite for larger, frequently updated units. The backend is host-side only and contributes no prompt, tool, or schema.
|
|
13
13
|
|
|
14
14
|
## Table of Contents
|
|
15
15
|
|
|
@@ -29,7 +29,7 @@ Use this package when a composition needs readable, editable JSON storage. Route
|
|
|
29
29
|
|
|
30
30
|
### When to choose it
|
|
31
31
|
|
|
32
|
-
Choose the default `single` layout for small units that benefit from one complete, pretty-printed file. Choose `per-record`
|
|
32
|
+
Choose the default `single` layout for small units that benefit from one complete, pretty-printed file. Choose `per-record` for disposable records whose point writes should replace only one document; unsupported or unreadable documents read as absent. Choose SQLite for frequent point updates in large units. The domain API has no multi-record transaction operation on either backend.
|
|
33
33
|
|
|
34
34
|
### Configuration
|
|
35
35
|
|
|
@@ -53,9 +53,11 @@ The generated [configuration catalog](../../../docs/config-catalog.md#deepseek-a
|
|
|
53
53
|
|
|
54
54
|
### Observable behavior
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
Without a legacy bootstrap, a missing `single` file or `per-record` directory opens as an empty unit and materializes on the first write. In `single`, malformed content rejects with `malformed-medium`, and a stored version outside the current and `compatibleVersions` set rejects with `version-mismatch`. An accepted older file stays unchanged on read; the next atomic write stamps the current version and preserves untouched values in declared tables. Deleting missing keys or an empty batch does not restamp. In `per-record`, each malformed or unreadable document, and each document whose version is outside the accepted set, reads as an absent record, so one bad document does not reject the unit. Version refusal does not delete the document. Per-record keys must match `[a-zA-Z0-9_-]+`; an unsafe key rejects before any file operation. Resolved writes are durable; `per-record` deletions sync their directory changes on POSIX before resolving. A batch interrupted before resolution can be partially applied. A batch that cannot restore its readable or durable state rejects with `unknown-state`, after which every later operation except `close()` rejects; calls after close reject with `closed`.
|
|
57
57
|
|
|
58
|
-
An
|
|
58
|
+
An unmaterialized `per-record` tree can initialize its declared tables from a valid `<root>/<unit>.json` whole-unit document only when the source unit name matches and its version is current or declared compatible. This bootstrap writes during loading: it leaves the source file unchanged and copies stored values into current-stamped documents without converting them. A source version outside the accepted set leaves the new tree empty. Any declared table directory, even when empty, or a declared `global.json` suppresses this initialization for the complete unit. Deleted records therefore remain absent while the retained source file stays unchanged.
|
|
59
|
+
|
|
60
|
+
A synchronous serialization failure restores the open `single` unit's previous memory and permits retry on that handle. An I/O failure can occur after file replacement, so a rejected write does not prove that disk data or its stamp stayed unchanged. Stop using and close the backend and its domains, then reopen to reload saved state before deciding whether to retry. See [domain versioning](../../../docs/subsystems/storage.md#domain-versioning) for mixed records and downgrade behavior.
|
|
59
61
|
|
|
60
62
|
-----
|
|
61
63
|
|
|
@@ -69,8 +71,8 @@ The two layouts share atomic publication but assign state ownership differently.
|
|
|
69
71
|
|
|
70
72
|
### Design concept
|
|
71
73
|
|
|
72
|
-
- **`single` keeps memory authoritative.** Each write changes the in-memory unit, serializes its complete state, and atomically replaces `<unit>.json`. A
|
|
73
|
-
- **`per-record` keeps the directory authoritative.** Each put or delete changes one `<unit>/<table>/<key>.json` document, and `loadAll()` rereads the tree. Each document stamps the unit version and carries one record value.
|
|
74
|
+
- **`single` keeps memory authoritative.** Each write changes the in-memory unit, serializes its complete state, and atomically replaces `<unit>.json`. A batch delete publishes once. Serialization failures restore every affected in-memory value; uncertain I/O outcomes require reopening instead of continuing from that snapshot.
|
|
75
|
+
- **`per-record` keeps the directory authoritative.** Each put or delete changes one `<unit>/<table>/<key>.json` document, and `loadAll()` rereads the tree. A batch delete renames the batch's documents out of the readable set, syncs the directory on POSIX, then unlinks the staged copies. A failed staging or sync restores the documents and syncs the restored directory. If that restoration or sync fails, the unit rejects with `unknown-state` and every later operation on it except `close()` rejects. Each document stamps the unit version and carries one record value.
|
|
74
76
|
- **Publication is durable per call.** A write uses a temporary file, fsync, atomic `rename()` replacement, and a parent-directory fsync on POSIX. The domain layer's write chain supplies ordering across calls.
|
|
75
77
|
|
|
76
78
|
### File formats
|
|
@@ -140,6 +142,7 @@ These limits define when this backend is a poor fit or needs special operational
|
|
|
140
142
|
- **`single` rewrites the whole unit** — each write republishes the complete unit file; use `per-record` or route the domain to SQLite when this cost is too high.
|
|
141
143
|
- **No cross-process write locking** — two processes writing the same unit can interleave replacements; writes to the same file use last-completion wins.
|
|
142
144
|
- **Windows rename without explicit write-through** — durability relies on libuv's `rename()` (`MoveFileExW` with replacement); the stricter Win32 write-through publish helper from the session-log backend is planned to move down here when the `log` facet lands.
|
|
145
|
+
- **No crash-atomic `per-record` batch** — a crash before deletion resolves can leave a partial batch and `.deleting` residue. Completed deletions sync the staged directory changes on POSIX; cleanup residue is unreadable.
|
|
143
146
|
|
|
144
147
|
<a id="dev-note"></a>
|
|
145
148
|
### Dev Note
|
package/README.zh.md
CHANGED
|
@@ -9,7 +9,7 @@ kind: "package-reference"
|
|
|
9
9
|
|
|
10
10
|
## 概述
|
|
11
11
|
|
|
12
|
-
`dsh-storage-json` 在配置的根目录下把领域数据存为可读 JSON,并注册为后端 `json`。默认的 `single` 布局为每个单元保存一份完整的 `<unit>.json` 文件;`per-record`
|
|
12
|
+
`dsh-storage-json` 在配置的根目录下把领域数据存为可读 JSON,并注册为后端 `json`。默认的 `single` 布局为每个单元保存一份完整的 `<unit>.json` 文件;`per-record` 布局为每条记录保存一份带版本戳的文档。两种布局都以原子方式发布每个变更文件,领域层负责安排调用顺序。当运维方需要可检查文件且所选布局适合写入量时选择它;对于更大且频繁更新的单元则选择 SQLite。本后端只面向宿主侧,不贡献提示词、工具或 schema。
|
|
13
13
|
|
|
14
14
|
## 目录
|
|
15
15
|
|
|
@@ -29,7 +29,7 @@ kind: "package-reference"
|
|
|
29
29
|
|
|
30
30
|
### 何时选择
|
|
31
31
|
|
|
32
|
-
小型单元需要一份完整、美化打印的文件时,选择默认的 `single`
|
|
32
|
+
小型单元需要一份完整、美化打印的文件时,选择默认的 `single` 布局。可丢弃记录的定点写入只需替换一份文档时,选择 `per-record`;不支持或不可读的文档会读作不存在。大型单元需要频繁定点更新时,选择 SQLite。领域 API 在两种后端上都没有多记录事务操作。
|
|
33
33
|
|
|
34
34
|
### 配置
|
|
35
35
|
|
|
@@ -53,9 +53,11 @@ kind: "package-reference"
|
|
|
53
53
|
|
|
54
54
|
### 可观察行为
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
没有旧文件引导时,缺失的 `single` 文件或 `per-record` 目录会作为空单元打开,并在第一次写入时物化。在 `single` 中,畸形内容以 `malformed-medium` 拒绝,已存版本不在当前版本和 `compatibleVersions` 集合内时以 `version-mismatch` 拒绝。读取接受的旧文件时保持原状;下一次原子写入使用当前版本戳,保留已声明表中未改动记录的原值。删除不存在的键或空批次不会重新盖戳。在 `per-record` 中,每份畸形或不可读的文档,以及版本不在接受集合内的文档,都读作记录不存在,因此单个坏文档不会使单元被拒绝。版本拒绝不会删除文档。逐记录布局的键必须匹配 `[a-zA-Z0-9_-]+`;不安全的键在任何文件操作前被拒绝。写入完成后即已持久化;`per-record` 删除在完成前会在 POSIX 上同步目录变更。在完成前中断的批次可能只应用一部分。无法恢复可读或持久状态的批量删除以 `unknown-state` 拒绝,之后除 `close()` 外的所有操作都会拒绝;关闭后的调用以 `closed` 拒绝。
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
只有当源单元名称匹配,且源版本为当前版本或已声明的兼容版本时,尚未物化的 `per-record` 目录树才可以从有效的 `<root>/<unit>.json` 整单元文档初始化其已声明表。该引导会在加载时写入:它保持源文件不变,并把已存值复制到当前版本戳的文档中,不转换其中的值。接受集合之外的源版本会使新目录树保持为空。只要存在任意已声明表的目录(即使为空),或已声明的 `global.json`,就会对整个单元禁止该初始化。因此删除的记录会保持缺失,同时保留的源文件不变。
|
|
59
|
+
|
|
60
|
+
同步序列化失败会恢复已打开 `single` 单元先前的内存状态,允许在同一句柄上重试。I/O 失败可能发生在文件替换之后,因此写入被拒绝并不能证明磁盘数据或版本戳保持不变。必须停止使用并关闭后端及其领域,然后重新打开以加载已存状态,再决定是否重试。混合记录与降级行为参阅[领域版本](../../../docs/subsystems/storage.zh.md#domain-versioning)。
|
|
59
61
|
|
|
60
62
|
-----
|
|
61
63
|
|
|
@@ -69,8 +71,8 @@ kind: "package-reference"
|
|
|
69
71
|
|
|
70
72
|
### 设计理念
|
|
71
73
|
|
|
72
|
-
- **`single` 以内存为权威状态。** 每次写入都会更改内存单元、序列化其完整状态,并以原子方式替换 `<unit>.json
|
|
73
|
-
- **`per-record` 以目录为权威状态。** 每次 put 或 delete 都会更改一个 `<unit>/<table>/<key>.json` 文档,`loadAll()`
|
|
74
|
+
- **`single` 以内存为权威状态。** 每次写入都会更改内存单元、序列化其完整状态,并以原子方式替换 `<unit>.json`。批量删除只发布一次。序列化失败会恢复所有受影响的内存值;I/O 结果不确定时必须重新打开,不能继续使用该快照。
|
|
75
|
+
- **`per-record` 以目录为权威状态。** 每次 put 或 delete 都会更改一个 `<unit>/<table>/<key>.json` 文档,`loadAll()` 则重新读取目录树。每份文档都带有单元版本戳与一条记录值。批量删除先把整批文档改名移出可读集合,在 POSIX 上同步目录,然后删除暂存副本。暂存或同步失败时会恢复文档,并同步恢复后的目录;恢复或同步失败时该 unit 以 `unknown-state` 拒绝,之后除 `close()` 外对它的所有操作都会拒绝。
|
|
74
76
|
- **每次调用都持久发布。** 写入过程使用临时文件、fsync、原子 `rename()` 替换,并在 POSIX 上 fsync 父目录。领域层写入链负责安排跨调用的顺序。
|
|
75
77
|
|
|
76
78
|
### 文件格式
|
|
@@ -140,6 +142,7 @@ kind: "package-reference"
|
|
|
140
142
|
- **`single` 会重写整个单元**——每次写入都重新发布完整单元文件;当此成本过高时,使用 `per-record` 或把领域路由到 SQLite。
|
|
141
143
|
- **没有跨进程写锁**——两个进程写入同一单元时可能交错执行替换;对同一文件的写入以最后完成者为准。
|
|
142
144
|
- **Windows rename 没有显式 write-through**——持久性依赖 libuv 的 `rename()`(`MoveFileExW` 并启用替换);`log` 分面落地时,计划把会话日志后端更严格的 Win32 write-through 发布辅助函数下移到此处。
|
|
145
|
+
- **`per-record` 批次不保证崩溃原子性**——删除完成前崩溃可能留下部分批次与 `.deleting` 残留。已完成的删除会在 POSIX 上同步暂存后的目录变更;清理残留不可读。
|
|
143
146
|
|
|
144
147
|
<a id="dev-note"></a>
|
|
145
148
|
### 开发备注
|
package/lib/index.js
CHANGED
|
@@ -39,7 +39,11 @@ async function writeAtomic(path, data) {
|
|
|
39
39
|
throw error;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Persist directory-entry changes on POSIX; skip this unsupported operation on Windows.
|
|
44
|
+
* @param path - Absolute directory path.
|
|
45
|
+
* @returns resolution after the directory is synced, or immediately on Windows.
|
|
46
|
+
*/
|
|
43
47
|
/* v8 ignore start -- Windows rejects O_RDONLY directory opens; POSIX coverage exercises this. */
|
|
44
48
|
async function fsyncDirectory(path) {
|
|
45
49
|
if (process.platform === "win32") return;
|
|
@@ -85,7 +89,7 @@ function serialize(name, state) {
|
|
|
85
89
|
/**
|
|
86
90
|
* Parse file content into unit state, validating shape and version.
|
|
87
91
|
* @param text - Raw file content.
|
|
88
|
-
* @param descriptor - Expected identity
|
|
92
|
+
* @param descriptor - Expected identity and accepted current or compatible versions.
|
|
89
93
|
* @returns the parsed state.
|
|
90
94
|
*/
|
|
91
95
|
function parse(text, descriptor) {
|
|
@@ -99,7 +103,7 @@ function parse(text, descriptor) {
|
|
|
99
103
|
const { unit, global: globalValue, tables } = document;
|
|
100
104
|
if (typeof unit !== "object" || unit === null || unit["name"] !== descriptor.name || typeof unit["version"] !== "number") throw new StorageError("malformed-medium", `unit '${descriptor.name}': missing or foreign unit header`);
|
|
101
105
|
const version = unit["version"];
|
|
102
|
-
if (version !== descriptor.version) throw new StorageError("version-mismatch", `unit '${descriptor.name}': stored version ${version}
|
|
106
|
+
if (version !== descriptor.version && !descriptor.compatibleVersions?.includes(version)) throw new StorageError("version-mismatch", `unit '${descriptor.name}': stored version ${version} is not accepted by version ${descriptor.version}`);
|
|
103
107
|
if (typeof tables !== "object" || tables === null || Array.isArray(tables)) throw new StorageError("malformed-medium", `unit '${descriptor.name}': tables is not an object`);
|
|
104
108
|
const state = {
|
|
105
109
|
version,
|
|
@@ -160,7 +164,8 @@ function parseRecord(text, versions) {
|
|
|
160
164
|
/**
|
|
161
165
|
* One opened JSON unit in `single` layout: the whole unit is one document at
|
|
162
166
|
* `<root>/<name>.json`. The in-memory state is authoritative; every write
|
|
163
|
-
* primitive mutates it and republishes the whole file atomically
|
|
167
|
+
* primitive mutates it and republishes the whole file atomically, including a
|
|
168
|
+
* batch delete, which changes several records but publishes once. Writes are
|
|
164
169
|
* NOT queued here — per the backend contract, write ordering belongs to the
|
|
165
170
|
* caller (the domain layer's write chain); this unit only guarantees that
|
|
166
171
|
* each single call publishes a complete, durable file. The `per-record`
|
|
@@ -183,11 +188,13 @@ async function openSingleUnit(descriptor, root, onClose) {
|
|
|
183
188
|
} catch (error) {
|
|
184
189
|
if (error.code !== "ENOENT") throw error;
|
|
185
190
|
}
|
|
186
|
-
|
|
191
|
+
const state = text === void 0 ? {
|
|
187
192
|
version: descriptor.version,
|
|
188
193
|
global: null,
|
|
189
194
|
tables: new Map(descriptor.tables.map((table) => [table, /* @__PURE__ */ new Map()]))
|
|
190
|
-
} : parse(text, descriptor)
|
|
195
|
+
} : parse(text, descriptor);
|
|
196
|
+
state.version = descriptor.version;
|
|
197
|
+
return new SingleJsonUnit(descriptor, path, state, onClose);
|
|
191
198
|
}
|
|
192
199
|
var SingleJsonUnit = class {
|
|
193
200
|
descriptor;
|
|
@@ -224,14 +231,21 @@ var SingleJsonUnit = class {
|
|
|
224
231
|
throw error;
|
|
225
232
|
});
|
|
226
233
|
}
|
|
227
|
-
|
|
234
|
+
deleteRecord(table, key) {
|
|
235
|
+
return this.deleteRecords(table, [key]);
|
|
236
|
+
}
|
|
237
|
+
async deleteRecords(table, keys) {
|
|
228
238
|
this.assertOpen();
|
|
229
239
|
const records = this.records(table);
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
240
|
+
const removed = /* @__PURE__ */ new Map();
|
|
241
|
+
for (const key of new Set(keys)) {
|
|
242
|
+
if (!records.has(key)) continue;
|
|
243
|
+
removed.set(key, records.get(key));
|
|
244
|
+
records.delete(key);
|
|
245
|
+
}
|
|
246
|
+
if (removed.size === 0) return;
|
|
233
247
|
await this.publish().catch((error) => {
|
|
234
|
-
records.set(key,
|
|
248
|
+
for (const [key, value] of removed) records.set(key, value);
|
|
235
249
|
throw error;
|
|
236
250
|
});
|
|
237
251
|
}
|
|
@@ -262,7 +276,7 @@ var SingleJsonUnit = class {
|
|
|
262
276
|
if (!records) throw new Error(`unit '${this.descriptor.name}' does not declare table '${table}'`);
|
|
263
277
|
return records;
|
|
264
278
|
}
|
|
265
|
-
publish() {
|
|
279
|
+
async publish() {
|
|
266
280
|
const write = writeAtomic(this.path, serialize(this.descriptor.name, this.state));
|
|
267
281
|
this.inFlight.add(write);
|
|
268
282
|
write.catch(() => {}).finally(() => this.inFlight.delete(write));
|
|
@@ -275,12 +289,13 @@ var SingleJsonUnit = class {
|
|
|
275
289
|
* One opened JSON unit in `per-record` layout: the unit is a directory at
|
|
276
290
|
* `dir`, holding one document per record under `<dir>/<table>/<key>.json`
|
|
277
291
|
* plus `global.json` for the global slot. The directory is the state — this
|
|
278
|
-
* unit holds NO in-memory state of its own: `loadAll` re-reads the tree
|
|
279
|
-
*
|
|
292
|
+
* unit holds NO in-memory state of its own: `loadAll` re-reads the tree, each
|
|
293
|
+
* put or delete touches one durable document, and a batch delete renames its
|
|
294
|
+
* documents out of the readable set before unlinking them. The domain layer owns the live
|
|
280
295
|
* in-memory tables (seeded by the open-time `loadAll`) and serializes writes
|
|
281
|
-
* through its write chain
|
|
282
|
-
*
|
|
283
|
-
*
|
|
296
|
+
* through its write chain. A rejected write leaves domain memory unchanged;
|
|
297
|
+
* a failed deletion restores its staged documents or quarantines the unit
|
|
298
|
+
* with `unknown-state` when restoration cannot be established.
|
|
284
299
|
*
|
|
285
300
|
* Per-record contract: a record document that is malformed or stamped with a
|
|
286
301
|
* version outside the accepted set (the descriptor's current version plus
|
|
@@ -289,13 +304,13 @@ var SingleJsonUnit = class {
|
|
|
289
304
|
* the record instead of migrating it. Record keys become path segments, so
|
|
290
305
|
* they must be path-safe (`[a-zA-Z0-9_-]+`); an unsafe key rejects at write.
|
|
291
306
|
*
|
|
292
|
-
* Legacy bootstrap: when the new tree has no
|
|
293
|
-
* whole-unit file `<root>/<name>.json`
|
|
307
|
+
* Legacy bootstrap: when the new tree has no declared table directory or
|
|
308
|
+
* global document, a whole-unit file `<root>/<name>.json` seeds
|
|
294
309
|
* per-record documents, provided its stored unit version is in the accepted
|
|
295
310
|
* set — a legacy file stamped with any other version is left alone and reads
|
|
296
|
-
* as the empty unit.
|
|
297
|
-
*
|
|
298
|
-
* legacy file is never changed or deleted.
|
|
311
|
+
* as the empty unit. An existing declared table directory, even when empty,
|
|
312
|
+
* or a declared global document suppresses the bootstrap for the whole unit.
|
|
313
|
+
* The legacy file is never changed or deleted.
|
|
299
314
|
* @module @deepseek-ai/dsh-storage-json/src/per-record-unit
|
|
300
315
|
*/
|
|
301
316
|
/** Keys become path segments in this layout; this set is path-safe on every OS. */
|
|
@@ -338,7 +353,10 @@ async function loadPerRecordState(descriptor, dir) {
|
|
|
338
353
|
if (!(entries === void 0 ? false : (await Promise.all(entries.map(async (entry) => {
|
|
339
354
|
if (entry.isDirectory()) {
|
|
340
355
|
const records = state.tables.get(entry.name);
|
|
341
|
-
if (records !== void 0)
|
|
356
|
+
if (records !== void 0) {
|
|
357
|
+
await loadTableRecords(records, versions, join(dir, entry.name));
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
342
360
|
}
|
|
343
361
|
if (entry.name === "global.json" && descriptor.hasGlobal) {
|
|
344
362
|
const global = await readRecord(join(dir, entry.name), versions);
|
|
@@ -354,7 +372,7 @@ function acceptedStamps(descriptor) {
|
|
|
354
372
|
return [descriptor.version, ...descriptor.compatibleVersions ?? []];
|
|
355
373
|
}
|
|
356
374
|
/**
|
|
357
|
-
* Bootstrap an
|
|
375
|
+
* Bootstrap an unmaterialized per-record tree from a legacy whole-unit file
|
|
358
376
|
* (`<root>/<name>.json`, the pre-per-record layout). Every declared-table
|
|
359
377
|
* record is copied into a current-version document, while the legacy file is
|
|
360
378
|
* retained unchanged. A missing, foreign (another unit's name), malformed,
|
|
@@ -404,12 +422,9 @@ async function bootstrapLegacyUnit(descriptor, dir, state) {
|
|
|
404
422
|
}
|
|
405
423
|
/**
|
|
406
424
|
* Read one declared table's record documents into `records`.
|
|
407
|
-
* @returns whether the directory contains any `.json` document path,
|
|
408
|
-
* independently of key safety, readability, or stored version.
|
|
409
425
|
*/
|
|
410
426
|
async function loadTableRecords(records, versions, dir) {
|
|
411
427
|
const files = await readdir(dir, { withFileTypes: true });
|
|
412
|
-
const hasDocuments = files.some((file) => file.name.endsWith(".json"));
|
|
413
428
|
const loaded = await Promise.all(files.map(async (file) => {
|
|
414
429
|
if (!file.name.endsWith(".json")) return;
|
|
415
430
|
const key = file.name.slice(0, -5);
|
|
@@ -418,7 +433,6 @@ async function loadTableRecords(records, versions, dir) {
|
|
|
418
433
|
if (record !== void 0) return [key, record];
|
|
419
434
|
}));
|
|
420
435
|
for (const record of loaded) if (record !== void 0) records.set(...record);
|
|
421
|
-
return hasDocuments;
|
|
422
436
|
}
|
|
423
437
|
/** Read one record document; a foreign (unreadable or stale) one reads as absent. */
|
|
424
438
|
async function readRecord(path, versions) {
|
|
@@ -430,15 +444,17 @@ async function readRecord(path, versions) {
|
|
|
430
444
|
}
|
|
431
445
|
/**
|
|
432
446
|
* One opened `per-record`-layout unit. Stateless by design: the directory is
|
|
433
|
-
* the medium
|
|
434
|
-
*
|
|
435
|
-
* domain layer's write chain)
|
|
447
|
+
* the medium and the domain layer owns the live memory. Replacements publish
|
|
448
|
+
* one document; deletions stage and sync their documents before cleanup.
|
|
449
|
+
* Write ordering belongs to the caller (the domain layer's write chain).
|
|
436
450
|
*/
|
|
437
451
|
var PerRecordJsonUnit = class {
|
|
438
452
|
descriptor;
|
|
439
453
|
dir;
|
|
440
454
|
onClose;
|
|
441
455
|
closed = false;
|
|
456
|
+
/** Set when a failed batch could not restore its staged documents. */
|
|
457
|
+
unusable;
|
|
442
458
|
/** In-flight durable writes; close() drains them before releasing the unit. */
|
|
443
459
|
inFlight = /* @__PURE__ */ new Set();
|
|
444
460
|
constructor(descriptor, dir, onClose) {
|
|
@@ -464,10 +480,67 @@ var PerRecordJsonUnit = class {
|
|
|
464
480
|
await this.tracked(this.writeDocument(join(this.tableDir(table), `${key}.json`), value));
|
|
465
481
|
}
|
|
466
482
|
/** Durably delete one record. Idempotent: a missing key is a no-op. */
|
|
467
|
-
|
|
483
|
+
deleteRecord(table, key) {
|
|
484
|
+
return this.deleteRecords(table, [key]);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Durably delete many records as one batch. Documents are renamed out of the
|
|
488
|
+
* readable set first; only after the whole batch is staged are the staged
|
|
489
|
+
* copies unlinked. A failure while staging restores the documents it moved,
|
|
490
|
+
* so a rejection normally leaves the batch unapplied. If that restoration
|
|
491
|
+
* itself fails, the readable or durable state is unknown: the unit rejects with
|
|
492
|
+
* `unknown-state` and every later operation on it rejects, while `close`
|
|
493
|
+
* still releases it. Staged directory changes are synced before cleanup on
|
|
494
|
+
* POSIX, so a resolved deletion remains absent after a crash. Cleanup residue
|
|
495
|
+
* is not readable (`open` reads `*.json` documents only).
|
|
496
|
+
*/
|
|
497
|
+
async deleteRecords(table, keys) {
|
|
468
498
|
this.assertOpen();
|
|
469
|
-
|
|
470
|
-
|
|
499
|
+
const dir = this.tableDir(table);
|
|
500
|
+
const unique = [...new Set(keys)];
|
|
501
|
+
for (const key of unique) assertSafeKey(this.descriptor.name, key);
|
|
502
|
+
await this.tracked(this.removeDocuments(dir, unique));
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Remove one batch's documents under a single tracked promise.
|
|
506
|
+
* @param dir - Declared table directory.
|
|
507
|
+
* @param keys - Validated, de-duplicated record keys.
|
|
508
|
+
*/
|
|
509
|
+
async removeDocuments(dir, keys) {
|
|
510
|
+
const staged = [];
|
|
511
|
+
try {
|
|
512
|
+
for (const key of keys) {
|
|
513
|
+
const from = join(dir, `${key}.json`);
|
|
514
|
+
const to = `${from}.deleting`;
|
|
515
|
+
try {
|
|
516
|
+
await rename(from, to);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
if (error.code === "ENOENT") continue;
|
|
519
|
+
throw error;
|
|
520
|
+
}
|
|
521
|
+
staged.push({
|
|
522
|
+
from,
|
|
523
|
+
to
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
if (staged.length > 0) await fsyncDirectory(dir);
|
|
527
|
+
} catch (error) {
|
|
528
|
+
const restoreFailures = [];
|
|
529
|
+
for (const { from, to } of staged) try {
|
|
530
|
+
await rename(to, from);
|
|
531
|
+
} catch (restoreError) {
|
|
532
|
+
restoreFailures.push(restoreError);
|
|
533
|
+
}
|
|
534
|
+
if (staged.length > 0) try {
|
|
535
|
+
await fsyncDirectory(dir);
|
|
536
|
+
} catch (restoreError) {
|
|
537
|
+
restoreFailures.push(restoreError);
|
|
538
|
+
}
|
|
539
|
+
if (restoreFailures.length === 0) throw error;
|
|
540
|
+
this.unusable = new StorageError("unknown-state", `unit '${this.descriptor.name}' could not restore a failed batch delete; its state is unknown`, { cause: new AggregateError([error, ...restoreFailures], "batch delete failed and restoration failed") });
|
|
541
|
+
throw this.unusable;
|
|
542
|
+
}
|
|
543
|
+
await Promise.allSettled(staged.map(({ to }) => rm(to, { force: true })));
|
|
471
544
|
}
|
|
472
545
|
/**
|
|
473
546
|
* Move one record's document aside as `<key>.json.bak.<YYYYMMDDHHmm>`. The
|
|
@@ -502,6 +575,7 @@ var PerRecordJsonUnit = class {
|
|
|
502
575
|
}
|
|
503
576
|
assertOpen() {
|
|
504
577
|
if (this.closed) throw new StorageError("closed", `unit '${this.descriptor.name}' is closed`);
|
|
578
|
+
if (this.unusable !== void 0) throw this.unusable;
|
|
505
579
|
}
|
|
506
580
|
/** Resolve a declared table's directory; an undeclared table is a caller bug and throws. */
|
|
507
581
|
tableDir(table) {
|
|
@@ -509,14 +583,13 @@ var PerRecordJsonUnit = class {
|
|
|
509
583
|
return join(this.dir, table);
|
|
510
584
|
}
|
|
511
585
|
/** Durably replace one document, creating its parent directory. */
|
|
512
|
-
writeDocument(path, value) {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
})();
|
|
586
|
+
async writeDocument(path, value) {
|
|
587
|
+
const text = serializeRecord(this.descriptor.version, value);
|
|
588
|
+
await mkdir(dirname(path), {
|
|
589
|
+
recursive: true,
|
|
590
|
+
mode: 448
|
|
591
|
+
});
|
|
592
|
+
await writeAtomic(path, text);
|
|
520
593
|
}
|
|
521
594
|
/** Track one durable write so close() drains it. */
|
|
522
595
|
tracked(write) {
|
package/lib/types/atomic.d.ts
CHANGED
|
@@ -17,4 +17,10 @@
|
|
|
17
17
|
* @returns resolution after the replacement is crash-durable.
|
|
18
18
|
*/
|
|
19
19
|
export declare function writeAtomic(path: string, data: string): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Persist directory-entry changes on POSIX; skip this unsupported operation on Windows.
|
|
22
|
+
* @param path - Absolute directory path.
|
|
23
|
+
* @returns resolution after the directory is synced, or immediately on Windows.
|
|
24
|
+
*/
|
|
25
|
+
export declare function fsyncDirectory(path: string): Promise<void>;
|
|
20
26
|
//# sourceMappingURL=atomic.d.ts.map
|
package/lib/types/format.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export declare function serialize(name: string, state: UnitState): string;
|
|
|
25
25
|
/**
|
|
26
26
|
* Parse file content into unit state, validating shape and version.
|
|
27
27
|
* @param text - Raw file content.
|
|
28
|
-
* @param descriptor - Expected identity
|
|
28
|
+
* @param descriptor - Expected identity and accepted current or compatible versions.
|
|
29
29
|
* @returns the parsed state.
|
|
30
30
|
*/
|
|
31
31
|
export declare function parse(text: string, descriptor: KvUnitDescriptor): UnitState;
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
* One opened JSON unit in `per-record` layout: the unit is a directory at
|
|
3
3
|
* `dir`, holding one document per record under `<dir>/<table>/<key>.json`
|
|
4
4
|
* plus `global.json` for the global slot. The directory is the state — this
|
|
5
|
-
* unit holds NO in-memory state of its own: `loadAll` re-reads the tree
|
|
6
|
-
*
|
|
5
|
+
* unit holds NO in-memory state of its own: `loadAll` re-reads the tree, each
|
|
6
|
+
* put or delete touches one durable document, and a batch delete renames its
|
|
7
|
+
* documents out of the readable set before unlinking them. The domain layer owns the live
|
|
7
8
|
* in-memory tables (seeded by the open-time `loadAll`) and serializes writes
|
|
8
|
-
* through its write chain
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* through its write chain. A rejected write leaves domain memory unchanged;
|
|
10
|
+
* a failed deletion restores its staged documents or quarantines the unit
|
|
11
|
+
* with `unknown-state` when restoration cannot be established.
|
|
11
12
|
*
|
|
12
13
|
* Per-record contract: a record document that is malformed or stamped with a
|
|
13
14
|
* version outside the accepted set (the descriptor's current version plus
|
|
@@ -16,13 +17,13 @@
|
|
|
16
17
|
* the record instead of migrating it. Record keys become path segments, so
|
|
17
18
|
* they must be path-safe (`[a-zA-Z0-9_-]+`); an unsafe key rejects at write.
|
|
18
19
|
*
|
|
19
|
-
* Legacy bootstrap: when the new tree has no
|
|
20
|
-
* whole-unit file `<root>/<name>.json`
|
|
20
|
+
* Legacy bootstrap: when the new tree has no declared table directory or
|
|
21
|
+
* global document, a whole-unit file `<root>/<name>.json` seeds
|
|
21
22
|
* per-record documents, provided its stored unit version is in the accepted
|
|
22
23
|
* set — a legacy file stamped with any other version is left alone and reads
|
|
23
|
-
* as the empty unit.
|
|
24
|
-
*
|
|
25
|
-
* legacy file is never changed or deleted.
|
|
24
|
+
* as the empty unit. An existing declared table directory, even when empty,
|
|
25
|
+
* or a declared global document suppresses the bootstrap for the whole unit.
|
|
26
|
+
* The legacy file is never changed or deleted.
|
|
26
27
|
* @module @deepseek-ai/dsh-storage-json/src/per-record-unit
|
|
27
28
|
*/
|
|
28
29
|
import type { KvUnit, KvUnitDescriptor } from '@deepseek-ai/dsh-storage';
|
|
@@ -38,15 +39,17 @@ import type { KvUnit, KvUnitDescriptor } from '@deepseek-ai/dsh-storage';
|
|
|
38
39
|
export declare function openPerRecordUnit(descriptor: KvUnitDescriptor, root: string, onClose: () => void): Promise<KvUnit>;
|
|
39
40
|
/**
|
|
40
41
|
* One opened `per-record`-layout unit. Stateless by design: the directory is
|
|
41
|
-
* the medium
|
|
42
|
-
*
|
|
43
|
-
* domain layer's write chain)
|
|
42
|
+
* the medium and the domain layer owns the live memory. Replacements publish
|
|
43
|
+
* one document; deletions stage and sync their documents before cleanup.
|
|
44
|
+
* Write ordering belongs to the caller (the domain layer's write chain).
|
|
44
45
|
*/
|
|
45
46
|
export declare class PerRecordJsonUnit implements KvUnit {
|
|
46
47
|
private readonly descriptor;
|
|
47
48
|
private readonly dir;
|
|
48
49
|
private readonly onClose;
|
|
49
50
|
private closed;
|
|
51
|
+
/** Set when a failed batch could not restore its staged documents. */
|
|
52
|
+
private unusable;
|
|
50
53
|
/** In-flight durable writes; close() drains them before releasing the unit. */
|
|
51
54
|
private readonly inFlight;
|
|
52
55
|
constructor(descriptor: KvUnitDescriptor, dir: string, onClose: () => void);
|
|
@@ -59,6 +62,24 @@ export declare class PerRecordJsonUnit implements KvUnit {
|
|
|
59
62
|
putRecord(table: string, key: string, value: unknown): Promise<void>;
|
|
60
63
|
/** Durably delete one record. Idempotent: a missing key is a no-op. */
|
|
61
64
|
deleteRecord(table: string, key: string): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Durably delete many records as one batch. Documents are renamed out of the
|
|
67
|
+
* readable set first; only after the whole batch is staged are the staged
|
|
68
|
+
* copies unlinked. A failure while staging restores the documents it moved,
|
|
69
|
+
* so a rejection normally leaves the batch unapplied. If that restoration
|
|
70
|
+
* itself fails, the readable or durable state is unknown: the unit rejects with
|
|
71
|
+
* `unknown-state` and every later operation on it rejects, while `close`
|
|
72
|
+
* still releases it. Staged directory changes are synced before cleanup on
|
|
73
|
+
* POSIX, so a resolved deletion remains absent after a crash. Cleanup residue
|
|
74
|
+
* is not readable (`open` reads `*.json` documents only).
|
|
75
|
+
*/
|
|
76
|
+
deleteRecords(table: string, keys: readonly string[]): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Remove one batch's documents under a single tracked promise.
|
|
79
|
+
* @param dir - Declared table directory.
|
|
80
|
+
* @param keys - Validated, de-duplicated record keys.
|
|
81
|
+
*/
|
|
82
|
+
private removeDocuments;
|
|
62
83
|
/**
|
|
63
84
|
* Move one record's document aside as `<key>.json.bak.<YYYYMMDDHHmm>`. The
|
|
64
85
|
* moved file no longer ends in `.json`, so every later read ignores it; the
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* One opened JSON unit in `single` layout: the whole unit is one document at
|
|
3
3
|
* `<root>/<name>.json`. The in-memory state is authoritative; every write
|
|
4
|
-
* primitive mutates it and republishes the whole file atomically
|
|
4
|
+
* primitive mutates it and republishes the whole file atomically, including a
|
|
5
|
+
* batch delete, which changes several records but publishes once. Writes are
|
|
5
6
|
* NOT queued here — per the backend contract, write ordering belongs to the
|
|
6
7
|
* caller (the domain layer's write chain); this unit only guarantees that
|
|
7
8
|
* each single call publishes a complete, durable file. The `per-record`
|
package/locale/de.json
ADDED
package/locale/es.json
ADDED
package/locale/fr.json
ADDED
package/locale/ja.json
ADDED
package/locale/pt.json
ADDED
package/locale/ru.json
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepseek-ai/dsh-storage-json",
|
|
3
3
|
"description": "JSON file KV storage backend for the DeepSeek Harness storage hub",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2-alpha.2",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -29,14 +29,14 @@
|
|
|
29
29
|
],
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"@deepseek-ai/dsh-storage": "0.2.
|
|
33
|
-
"@deepseek-ai/cordis": "~4.0.
|
|
32
|
+
"@deepseek-ai/dsh-storage": "0.2.2-alpha.2",
|
|
33
|
+
"@deepseek-ai/cordis": "~4.0.6-alpha.1"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@deepseek-ai/schemastery": "~3.18.
|
|
36
|
+
"@deepseek-ai/schemastery": "~3.18.6-alpha.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@deepseek-ai/dsh-storage": "0.2.
|
|
40
|
-
"@deepseek-ai/cordis": "~4.0.
|
|
39
|
+
"@deepseek-ai/dsh-storage": "0.2.2-alpha.2",
|
|
40
|
+
"@deepseek-ai/cordis": "~4.0.6-alpha.1"
|
|
41
41
|
}
|
|
42
42
|
}
|