@native-sdk/cli 0.8.4 → 0.9.0
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/build/app.zig +859 -62
- package/build.zig +543 -13
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +2 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +365 -2
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_typecheck.mjs +116 -15
- package/packages/core/scripts/gen_service_surface.mjs +268 -0
- package/packages/core/scripts/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
- package/packages/core/scripts/stage_external_core.mjs +9 -1
- package/packages/core/scripts/stage_external_services.mjs +172 -0
- package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
- package/packages/core/scripts/vendor_service_packages.mjs +189 -0
- package/packages/core/sdk/core.d.ts +149 -0
- package/packages/core/sdk/core.ts +529 -12
- package/packages/core/sdk/events.d.ts +36 -0
- package/packages/core/sdk/events.ts +57 -0
- package/packages/core/src/checker.ts +468 -15
- package/packages/core/src/cli.ts +143 -3
- package/packages/core/src/contract.ts +26 -5
- package/packages/core/src/devhost.ts +1847 -16
- package/packages/core/src/devhost_journal.mjs +181 -0
- package/packages/core/src/devhost_resolver.mjs +17 -1
- package/packages/core/src/devhost_service_worker.mjs +90 -0
- package/packages/core/src/diagnostics.ts +82 -16
- package/packages/core/src/frontend.ts +204 -34
- package/packages/core/src/modules.ts +129 -80
- package/packages/core/src/service_contract.ts +597 -0
- package/packages/core/src/sqlite_cli.ts +93 -0
- package/packages/core/src/sqlite_codegen.ts +806 -0
- package/packages/core/src/sqlite_runtime_policy.ts +364 -0
- package/packages/core/src/typed_ast.ts +36 -8
- package/skill-data/core/references/project-anatomy.md +1 -1
- package/skill-data/native-ui/SKILL.md +47 -0
- package/skill-data/ts-core/SKILL.md +80 -31
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +603 -0
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +1 -1
- package/src/app_runner/no_migrations.zig +3 -0
- package/src/app_runner/no_services.zig +20 -0
- package/src/app_runner/root.zig +104 -4
- package/src/app_runner/ts_core_main.zig +249 -8
- package/src/automation/snapshot.zig +23 -5
- package/src/embed/app_exports.zig +11 -1
- package/src/embed/c_api.zig +48 -2
- package/src/embed/conversions.zig +4 -5
- package/src/embed/host.zig +109 -1
- package/src/embed/root.zig +7 -0
- package/src/embed/tests.zig +213 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +119 -5
- package/src/platform/android/NativeSdkActivity.java +131 -0
- package/src/platform/android/android_host.c +182 -1
- package/src/platform/android/native_sdk_app.h +17 -0
- package/src/platform/ios/native_sdk_app.h +17 -0
- package/src/platform/ios/uikit_host.m +115 -0
- package/src/platform/linux/cef_host.cpp +1 -1
- package/src/platform/linux/gtk_host.c +89 -17
- package/src/platform/linux/gtk_host.h +3 -0
- package/src/platform/linux/root.zig +40 -18
- package/src/platform/macos/appkit_host.h +16 -3
- package/src/platform/macos/appkit_host.m +536 -36
- package/src/platform/macos/cef_host.mm +248 -36
- package/src/platform/macos/root.zig +244 -34
- package/src/platform/null_platform.zig +223 -19
- package/src/platform/null_platform_tests.zig +74 -0
- package/src/platform/root.zig +9 -0
- package/src/platform/types.zig +179 -15
- package/src/platform/windows/root.zig +187 -21
- package/src/platform/windows/webview2_host.cpp +293 -46
- package/src/primitives/app_manifest/comptime_scene.zig +2 -0
- package/src/primitives/app_manifest/root.zig +4 -0
- package/src/primitives/app_manifest/tests.zig +3 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/app_manifest/validation.zig +19 -1
- package/src/root.zig +46 -0
- package/src/runtime/api.zig +11 -0
- package/src/runtime/automation_snapshot.zig +4 -0
- package/src/runtime/bridge_permission_tests.zig +23 -2
- package/src/runtime/bridge_responses.zig +3 -1
- package/src/runtime/canvas_frame.zig +18 -11
- package/src/runtime/canvas_frame_animation_tests.zig +4 -0
- package/src/runtime/clock.zig +13 -1
- package/src/runtime/core.zig +133 -1
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +2295 -113
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/flow.zig +14 -1
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +42 -0
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +49 -0
- package/src/runtime/runtime_event_tests.zig +70 -0
- package/src/runtime/service_host.zig +711 -0
- package/src/runtime/service_pool.zig +1314 -0
- package/src/runtime/session_blobs.zig +1 -1
- package/src/runtime/session_journal.zig +102 -0
- package/src/runtime/session_record.zig +177 -0
- package/src/runtime/session_replay.zig +109 -4
- package/src/runtime/shell_layout.zig +1 -0
- package/src/runtime/shell_layout_tests.zig +120 -0
- package/src/runtime/sqlite_engine.zig +474 -0
- package/src/runtime/state.zig +6 -0
- package/src/runtime/system_services.zig +49 -10
- package/src/runtime/tests.zig +4 -0
- package/src/runtime/ts_core_host.zig +788 -86
- package/src/runtime/ts_core_host_tests.zig +231 -11
- package/src/runtime/ts_ui_app.zig +499 -1
- package/src/runtime/ui_app.zig +142 -16
- package/src/runtime/ui_app_tests.zig +155 -5
- package/src/runtime/validation.zig +22 -0
- package/src/runtime/view.zig +9 -0
- package/src/runtime/window_storage.zig +1 -0
- package/src/runtime/window_view_runtime.zig +2 -3
- package/src/runtime/window_views.zig +19 -0
- package/src/tooling/android.zig +21 -16
- package/src/tooling/db.zig +160 -0
- package/src/tooling/ios.zig +7 -1
- package/src/tooling/manifest.zig +214 -2
- package/src/tooling/package.zig +116 -0
- package/src/tooling/raw_manifest.zig +30 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +161 -4
- package/src/tooling/ts_core.zig +283 -17
- package/src/tooling/verbs.zig +7 -0
- package/third_party/sqlite/README.md +13 -0
- package/third_party/sqlite/sqlite3.c +269649 -0
- package/third_party/sqlite/sqlite3.h +14349 -0
- package/tools/corewire/core_abi.zig +14 -2
- package/tools/corewire/emit.zig +98 -29
- package/tools/corewire/emit_facade.zig +378 -23
- package/tools/corewire/emit_profile.zig +6 -2
- package/tools/corewire/emit_service.zig +924 -0
- package/tools/corewire/main.zig +122 -0
- package/tools/corewire/service_contract.zig +250 -0
- package/tools/corewire/sidecar.zig +26 -15
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
//! SQLite-backed, engine-owned record store. The public effect layer passes
|
|
2
|
+
//! bounded wire payloads through `execute`; this module owns schema, SQL,
|
|
3
|
+
//! transactions, byte ordering, and page framing.
|
|
4
|
+
|
|
5
|
+
const std = @import("std");
|
|
6
|
+
const sqlite = @import("sqlite_engine.zig");
|
|
7
|
+
|
|
8
|
+
pub const max_key_bytes: usize = 512;
|
|
9
|
+
pub const max_value_bytes: usize = 1024 * 1024;
|
|
10
|
+
pub const max_batch_entries: usize = 64;
|
|
11
|
+
pub const max_batch_bytes: usize = 8 * 1024 * 1024;
|
|
12
|
+
pub const default_scan_limit: u32 = 100;
|
|
13
|
+
pub const max_scan_limit: u32 = 256;
|
|
14
|
+
/// Enough for one maximum-sized record plus its key and page framing. Larger
|
|
15
|
+
/// scans end at a record boundary and return that record's predecessor as the
|
|
16
|
+
/// next cursor; no key or value is ever cut.
|
|
17
|
+
pub const max_result_bytes: usize = max_value_bytes + (2 * max_key_bytes) + 32;
|
|
18
|
+
pub const schema_version: u32 = 1;
|
|
19
|
+
|
|
20
|
+
pub const Outcome = enum(u8) {
|
|
21
|
+
ok,
|
|
22
|
+
miss,
|
|
23
|
+
io_failed,
|
|
24
|
+
over_bound,
|
|
25
|
+
bad_key,
|
|
26
|
+
rejected,
|
|
27
|
+
busy,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
pub const Operation = enum(u8) { set, get, delete, scan, set_many };
|
|
31
|
+
|
|
32
|
+
pub const Execution = struct {
|
|
33
|
+
outcome: Outcome,
|
|
34
|
+
len: usize = 0,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
pub const Binding = struct {
|
|
38
|
+
context: *anyopaque,
|
|
39
|
+
execute_fn: *const fn (context: *anyopaque, op: Operation, payload: []const u8, output: []u8) Execution,
|
|
40
|
+
reserve_write_fn: *const fn (context: *anyopaque) u64,
|
|
41
|
+
execute_write_fn: *const fn (context: *anyopaque, sequence: u64, op: Operation, payload: []const u8, output: []u8) Execution,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const schema =
|
|
45
|
+
"CREATE TABLE IF NOT EXISTS kv (" ++
|
|
46
|
+
"scope INTEGER NOT NULL DEFAULT 0," ++
|
|
47
|
+
"k BLOB NOT NULL," ++
|
|
48
|
+
"v BLOB NOT NULL," ++
|
|
49
|
+
"PRIMARY KEY(scope,k)) WITHOUT ROWID;";
|
|
50
|
+
|
|
51
|
+
pub const Store = struct {
|
|
52
|
+
allocator: std.mem.Allocator,
|
|
53
|
+
write_db: sqlite.Connection,
|
|
54
|
+
/// Durable stores keep reads on their own query-only connection so a
|
|
55
|
+
/// cached get/scan never borrows the writer. In-memory tests intentionally
|
|
56
|
+
/// leave this null: SQLite's `:memory:` database belongs to one connection.
|
|
57
|
+
read_db: ?sqlite.Connection = null,
|
|
58
|
+
path: [:0]u8,
|
|
59
|
+
write_mutex: std.atomic.Mutex = .unlocked,
|
|
60
|
+
read_mutex: std.atomic.Mutex = .unlocked,
|
|
61
|
+
next_write_sequence: std.atomic.Value(u64) = std.atomic.Value(u64).init(0),
|
|
62
|
+
reserved_write_sequence: std.atomic.Value(u64) = std.atomic.Value(u64).init(0),
|
|
63
|
+
|
|
64
|
+
pub fn open(allocator: std.mem.Allocator, data_dir: []const u8) !Store {
|
|
65
|
+
const path_plain = try std.fs.path.join(allocator, &.{ data_dir, "store.db" });
|
|
66
|
+
defer allocator.free(path_plain);
|
|
67
|
+
const path = try allocator.dupeZ(u8, path_plain);
|
|
68
|
+
errdefer allocator.free(path);
|
|
69
|
+
var write_db = try sqlite.Connection.open(path);
|
|
70
|
+
errdefer write_db.close();
|
|
71
|
+
try initialize(&write_db, true);
|
|
72
|
+
var read_db = try sqlite.Connection.open(path);
|
|
73
|
+
errdefer read_db.close();
|
|
74
|
+
try read_db.exec("PRAGMA busy_timeout=250;");
|
|
75
|
+
try read_db.exec("PRAGMA query_only=ON;");
|
|
76
|
+
return .{ .allocator = allocator, .write_db = write_db, .read_db = read_db, .path = path };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pub fn openMemory(allocator: std.mem.Allocator) !Store {
|
|
80
|
+
const path = try allocator.dupeZ(u8, ":memory:");
|
|
81
|
+
errdefer allocator.free(path);
|
|
82
|
+
var write_db = try sqlite.Connection.open(path);
|
|
83
|
+
errdefer write_db.close();
|
|
84
|
+
try initialize(&write_db, false);
|
|
85
|
+
return .{ .allocator = allocator, .write_db = write_db, .path = path };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pub fn deinit(self: *Store) void {
|
|
89
|
+
self.lockWrite();
|
|
90
|
+
if (self.read_db) |*db| {
|
|
91
|
+
self.lockRead();
|
|
92
|
+
db.close();
|
|
93
|
+
self.unlockRead();
|
|
94
|
+
}
|
|
95
|
+
self.write_db.close();
|
|
96
|
+
self.unlockWrite();
|
|
97
|
+
self.allocator.free(self.path);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
pub fn binding(self: *Store) Binding {
|
|
101
|
+
return .{
|
|
102
|
+
.context = self,
|
|
103
|
+
.execute_fn = executeBound,
|
|
104
|
+
.reserve_write_fn = reserveWriteBound,
|
|
105
|
+
.execute_write_fn = executeWriteBound,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
pub fn execute(self: *Store, op: Operation, payload: []const u8, output: []u8) Execution {
|
|
110
|
+
// Reads stay on the issuing thread, but they may not pass a write
|
|
111
|
+
// already reserved by an earlier command in the same walk. No later
|
|
112
|
+
// write can be reserved while that walk is waiting here, so this
|
|
113
|
+
// snapshot is the exact command-stream barrier the read owes.
|
|
114
|
+
const write_barrier = self.reserved_write_sequence.load(.acquire);
|
|
115
|
+
while (self.next_write_sequence.load(.acquire) < write_barrier) std.Thread.yield() catch {};
|
|
116
|
+
return self.executeRaw(op, payload, output);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn executeRaw(self: *Store, op: Operation, payload: []const u8, output: []u8) Execution {
|
|
120
|
+
return switch (op) {
|
|
121
|
+
.get => blk: {
|
|
122
|
+
self.lockRead();
|
|
123
|
+
defer self.unlockRead();
|
|
124
|
+
break :blk self.executeGet(payload, output);
|
|
125
|
+
},
|
|
126
|
+
.scan => blk: {
|
|
127
|
+
self.lockRead();
|
|
128
|
+
defer self.unlockRead();
|
|
129
|
+
break :blk self.executeScan(payload, output);
|
|
130
|
+
},
|
|
131
|
+
.set => blk: {
|
|
132
|
+
self.lockWrite();
|
|
133
|
+
defer self.unlockWrite();
|
|
134
|
+
break :blk self.executeSet(payload);
|
|
135
|
+
},
|
|
136
|
+
.delete => blk: {
|
|
137
|
+
self.lockWrite();
|
|
138
|
+
defer self.unlockWrite();
|
|
139
|
+
break :blk self.executeDelete(payload);
|
|
140
|
+
},
|
|
141
|
+
.set_many => blk: {
|
|
142
|
+
self.lockWrite();
|
|
143
|
+
defer self.unlockWrite();
|
|
144
|
+
break :blk self.executeSetMany(payload);
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
fn executeBound(context: *anyopaque, op: Operation, payload: []const u8, output: []u8) Execution {
|
|
150
|
+
const self: *Store = @ptrCast(@alignCast(context));
|
|
151
|
+
return self.execute(op, payload, output);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/// Reserve a writer position on the loop thread. Workers may start in
|
|
155
|
+
/// any OS scheduling order, but `executeWrite` admits them to SQLite in
|
|
156
|
+
/// the exact order their effects were issued.
|
|
157
|
+
pub fn reserveWrite(self: *Store) u64 {
|
|
158
|
+
return self.reserved_write_sequence.fetchAdd(1, .monotonic);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pub fn executeWrite(self: *Store, sequence: u64, op: Operation, payload: []const u8, output: []u8) Execution {
|
|
162
|
+
while (sequence != self.next_write_sequence.load(.acquire)) std.Thread.yield() catch {};
|
|
163
|
+
defer _ = self.next_write_sequence.fetchAdd(1, .release);
|
|
164
|
+
return self.executeRaw(op, payload, output);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
fn reserveWriteBound(context: *anyopaque) u64 {
|
|
168
|
+
const self: *Store = @ptrCast(@alignCast(context));
|
|
169
|
+
return self.reserveWrite();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
fn executeWriteBound(context: *anyopaque, sequence: u64, op: Operation, payload: []const u8, output: []u8) Execution {
|
|
173
|
+
const self: *Store = @ptrCast(@alignCast(context));
|
|
174
|
+
return self.executeWrite(sequence, op, payload, output);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
fn executeSet(self: *Store, payload: []const u8) Execution {
|
|
178
|
+
var cursor: Cursor = .{ .bytes = payload };
|
|
179
|
+
const scope = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
180
|
+
const key = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
181
|
+
const value = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
182
|
+
if (!cursor.done()) return .{ .outcome = .rejected };
|
|
183
|
+
if (!validKey(key)) return .{ .outcome = .bad_key };
|
|
184
|
+
if (value.len > max_value_bytes) return .{ .outcome = .over_bound };
|
|
185
|
+
|
|
186
|
+
var statement = self.write_db.prepare("INSERT INTO kv(scope,k,v) VALUES(?1,?2,?3) ON CONFLICT(scope,k) DO UPDATE SET v=excluded.v;") catch |err| return failure(err);
|
|
187
|
+
defer statement.finalize();
|
|
188
|
+
statement.bindInt(1, scope) catch |err| return failure(err);
|
|
189
|
+
statement.bindBlob(2, key) catch |err| return failure(err);
|
|
190
|
+
statement.bindBlob(3, value) catch |err| return failure(err);
|
|
191
|
+
_ = statement.step() catch |err| return failure(err);
|
|
192
|
+
return .{ .outcome = .ok };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn executeGet(self: *Store, payload: []const u8, output: []u8) Execution {
|
|
196
|
+
var cursor: Cursor = .{ .bytes = payload };
|
|
197
|
+
const scope = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
198
|
+
const key = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
199
|
+
if (!cursor.done()) return .{ .outcome = .rejected };
|
|
200
|
+
if (!validKey(key)) return .{ .outcome = .bad_key };
|
|
201
|
+
|
|
202
|
+
var statement = self.readConnection().prepare("SELECT v FROM kv WHERE scope=?1 AND k=?2;") catch |err| return failure(err);
|
|
203
|
+
defer statement.finalize();
|
|
204
|
+
statement.bindInt(1, scope) catch |err| return failure(err);
|
|
205
|
+
statement.bindBlob(2, key) catch |err| return failure(err);
|
|
206
|
+
switch (statement.step() catch |err| return failure(err)) {
|
|
207
|
+
.done => {
|
|
208
|
+
if (output.len < 1) return .{ .outcome = .over_bound };
|
|
209
|
+
output[0] = 0;
|
|
210
|
+
return .{ .outcome = .miss, .len = 1 };
|
|
211
|
+
},
|
|
212
|
+
.row => {
|
|
213
|
+
const value = statement.columnBlob(0);
|
|
214
|
+
if (value.len > max_value_bytes or output.len < value.len + 1) return .{ .outcome = .over_bound };
|
|
215
|
+
output[0] = 1;
|
|
216
|
+
@memcpy(output[1 .. value.len + 1], value);
|
|
217
|
+
return .{ .outcome = .ok, .len = value.len + 1 };
|
|
218
|
+
},
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
fn executeDelete(self: *Store, payload: []const u8) Execution {
|
|
223
|
+
var cursor: Cursor = .{ .bytes = payload };
|
|
224
|
+
const scope = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
225
|
+
const key = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
226
|
+
if (!cursor.done()) return .{ .outcome = .rejected };
|
|
227
|
+
if (!validKey(key)) return .{ .outcome = .bad_key };
|
|
228
|
+
var statement = self.write_db.prepare("DELETE FROM kv WHERE scope=?1 AND k=?2;") catch |err| return failure(err);
|
|
229
|
+
defer statement.finalize();
|
|
230
|
+
statement.bindInt(1, scope) catch |err| return failure(err);
|
|
231
|
+
statement.bindBlob(2, key) catch |err| return failure(err);
|
|
232
|
+
_ = statement.step() catch |err| return failure(err);
|
|
233
|
+
return .{ .outcome = .ok };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
fn executeScan(self: *Store, payload: []const u8, output: []u8) Execution {
|
|
237
|
+
var cursor: Cursor = .{ .bytes = payload };
|
|
238
|
+
const scope = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
239
|
+
const prefix = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
240
|
+
const requested_limit = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
241
|
+
const after = cursor.bytesField() orelse return .{ .outcome = .rejected };
|
|
242
|
+
if (!cursor.done()) return .{ .outcome = .rejected };
|
|
243
|
+
if (!validPrefix(prefix) or (after.len > 0 and !validKey(after))) return .{ .outcome = .bad_key };
|
|
244
|
+
const limit = if (requested_limit == 0) default_scan_limit else requested_limit;
|
|
245
|
+
if (limit > max_scan_limit) return .{ .outcome = .over_bound };
|
|
246
|
+
if (output.len < 8) return .{ .outcome = .over_bound };
|
|
247
|
+
|
|
248
|
+
var statement = self.readConnection().prepare(
|
|
249
|
+
"SELECT k,v FROM kv WHERE scope=?1 " ++
|
|
250
|
+
"AND (?2=X'' OR substr(k,1,length(?2))=?2) " ++
|
|
251
|
+
"AND (?3=X'' OR k>?3) ORDER BY k LIMIT ?4;",
|
|
252
|
+
) catch |err| return failure(err);
|
|
253
|
+
defer statement.finalize();
|
|
254
|
+
statement.bindInt(1, scope) catch |err| return failure(err);
|
|
255
|
+
statement.bindBlob(2, prefix) catch |err| return failure(err);
|
|
256
|
+
statement.bindBlob(3, after) catch |err| return failure(err);
|
|
257
|
+
statement.bindInt(4, @as(i64, limit) + 1) catch |err| return failure(err);
|
|
258
|
+
|
|
259
|
+
var writer = PageWriter.init(output);
|
|
260
|
+
writer.writeInt(u32, 0) catch return .{ .outcome = .over_bound };
|
|
261
|
+
var count: u32 = 0;
|
|
262
|
+
var last_key_buffer: [max_key_bytes]u8 = undefined;
|
|
263
|
+
var last_key_len: usize = 0;
|
|
264
|
+
var has_more = false;
|
|
265
|
+
while (true) {
|
|
266
|
+
switch (statement.step() catch |err| return failure(err)) {
|
|
267
|
+
.done => break,
|
|
268
|
+
.row => {
|
|
269
|
+
const key = statement.columnBlob(0);
|
|
270
|
+
const value = statement.columnBlob(1);
|
|
271
|
+
if (count >= limit) {
|
|
272
|
+
has_more = true;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
const needed = 4 + key.len + 4 + value.len + 4 + key.len;
|
|
276
|
+
if (writer.remaining() < needed) {
|
|
277
|
+
// The row is left whole for the next page. A first row
|
|
278
|
+
// always fits by max_result_bytes' definition.
|
|
279
|
+
if (count == 0) return .{ .outcome = .over_bound };
|
|
280
|
+
has_more = true;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
writer.writeBytes(key) catch unreachable;
|
|
284
|
+
writer.writeBytes(value) catch unreachable;
|
|
285
|
+
count += 1;
|
|
286
|
+
@memcpy(last_key_buffer[0..key.len], key);
|
|
287
|
+
last_key_len = key.len;
|
|
288
|
+
},
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
std.mem.writeInt(u32, output[0..4], count, .little);
|
|
292
|
+
writer.writeBytes(if (has_more) last_key_buffer[0..last_key_len] else "") catch return .{ .outcome = .over_bound };
|
|
293
|
+
return .{ .outcome = .ok, .len = writer.at };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
fn executeSetMany(self: *Store, payload: []const u8) Execution {
|
|
297
|
+
if (payload.len > max_batch_bytes) return .{ .outcome = .over_bound };
|
|
298
|
+
var cursor: Cursor = .{ .bytes = payload };
|
|
299
|
+
const scope = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
300
|
+
const count = cursor.int(u32) orelse return .{ .outcome = .rejected };
|
|
301
|
+
if (count == 0 or count > max_batch_entries) return .{ .outcome = .over_bound };
|
|
302
|
+
|
|
303
|
+
// Validate the complete envelope before opening a transaction. This
|
|
304
|
+
// keeps malformed batches out of SQLite altogether.
|
|
305
|
+
var validation = cursor;
|
|
306
|
+
for (0..count) |_| {
|
|
307
|
+
const key = validation.bytesField() orelse return .{ .outcome = .rejected };
|
|
308
|
+
const value = validation.bytesField() orelse return .{ .outcome = .rejected };
|
|
309
|
+
if (!validKey(key)) return .{ .outcome = .bad_key };
|
|
310
|
+
if (value.len > max_value_bytes) return .{ .outcome = .over_bound };
|
|
311
|
+
}
|
|
312
|
+
if (!validation.done()) return .{ .outcome = .rejected };
|
|
313
|
+
|
|
314
|
+
self.write_db.exec("BEGIN IMMEDIATE;") catch |err| return failure(err);
|
|
315
|
+
var committed = false;
|
|
316
|
+
defer if (!committed) self.write_db.exec("ROLLBACK;") catch {};
|
|
317
|
+
var statement = self.write_db.prepare("INSERT INTO kv(scope,k,v) VALUES(?1,?2,?3) ON CONFLICT(scope,k) DO UPDATE SET v=excluded.v;") catch |err| return failure(err);
|
|
318
|
+
defer statement.finalize();
|
|
319
|
+
for (0..count) |_| {
|
|
320
|
+
const key = cursor.bytesField().?;
|
|
321
|
+
const value = cursor.bytesField().?;
|
|
322
|
+
statement.bindInt(1, scope) catch |err| return failure(err);
|
|
323
|
+
statement.bindBlob(2, key) catch |err| return failure(err);
|
|
324
|
+
statement.bindBlob(3, value) catch |err| return failure(err);
|
|
325
|
+
_ = statement.step() catch |err| return failure(err);
|
|
326
|
+
statement.reset() catch |err| return failure(err);
|
|
327
|
+
}
|
|
328
|
+
std.debug.assert(cursor.done());
|
|
329
|
+
self.write_db.exec("COMMIT;") catch |err| return failure(err);
|
|
330
|
+
committed = true;
|
|
331
|
+
return .{ .outcome = .ok };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
fn readConnection(self: *Store) *sqlite.Connection {
|
|
335
|
+
if (self.read_db) |*db| return db;
|
|
336
|
+
return &self.write_db;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
fn lockWrite(self: *Store) void {
|
|
340
|
+
while (!self.write_mutex.tryLock()) std.atomic.spinLoopHint();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
fn unlockWrite(self: *Store) void {
|
|
344
|
+
self.write_mutex.unlock();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
fn lockRead(self: *Store) void {
|
|
348
|
+
// An in-memory store has no second connection, so its reads share the
|
|
349
|
+
// writer mutex as well as the writer handle.
|
|
350
|
+
const mutex = if (self.read_db != null) &self.read_mutex else &self.write_mutex;
|
|
351
|
+
while (!mutex.tryLock()) std.atomic.spinLoopHint();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
fn unlockRead(self: *Store) void {
|
|
355
|
+
const mutex = if (self.read_db != null) &self.read_mutex else &self.write_mutex;
|
|
356
|
+
mutex.unlock();
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
fn initialize(db: *sqlite.Connection, durable: bool) !void {
|
|
361
|
+
const version = blk: {
|
|
362
|
+
var version_query = try db.prepare("PRAGMA user_version;");
|
|
363
|
+
defer version_query.finalize();
|
|
364
|
+
if (try version_query.step() != .row) return error.InvalidSchemaVersion;
|
|
365
|
+
break :blk version_query.columnInt(0);
|
|
366
|
+
};
|
|
367
|
+
if (version < 0 or version > schema_version) return error.VersionUnknown;
|
|
368
|
+
if (durable) {
|
|
369
|
+
try db.exec("PRAGMA journal_mode=WAL;");
|
|
370
|
+
try db.exec("PRAGMA synchronous=NORMAL;");
|
|
371
|
+
try db.exec("PRAGMA busy_timeout=250;");
|
|
372
|
+
}
|
|
373
|
+
try db.exec(schema);
|
|
374
|
+
if (version == 0) try db.exec("PRAGMA user_version=1;");
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
pub fn outcomeName(outcome: Outcome) []const u8 {
|
|
378
|
+
return @tagName(outcome);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
fn failure(err: sqlite.Error) Execution {
|
|
382
|
+
return .{ .outcome = if (err == error.Busy) .busy else .io_failed };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
pub fn validKey(key: []const u8) bool {
|
|
386
|
+
return key.len > 0 and key.len <= max_key_bytes and std.unicode.utf8ValidateSlice(key);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
pub fn validPrefix(prefix: []const u8) bool {
|
|
390
|
+
return prefix.len <= max_key_bytes and std.unicode.utf8ValidateSlice(prefix);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const Cursor = struct {
|
|
394
|
+
bytes: []const u8,
|
|
395
|
+
at: usize = 0,
|
|
396
|
+
|
|
397
|
+
fn int(self: *Cursor, comptime T: type) ?T {
|
|
398
|
+
if (self.at > self.bytes.len or self.bytes.len - self.at < @sizeOf(T)) return null;
|
|
399
|
+
const value = std.mem.readInt(T, self.bytes[self.at..][0..@sizeOf(T)], .little);
|
|
400
|
+
self.at += @sizeOf(T);
|
|
401
|
+
return value;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
fn bytesField(self: *Cursor) ?[]const u8 {
|
|
405
|
+
const len: usize = self.int(u32) orelse return null;
|
|
406
|
+
if (len > self.bytes.len - self.at) return null;
|
|
407
|
+
const value = self.bytes[self.at..][0..len];
|
|
408
|
+
self.at += len;
|
|
409
|
+
return value;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
fn done(self: *const Cursor) bool {
|
|
413
|
+
return self.at == self.bytes.len;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const PageWriter = struct {
|
|
418
|
+
buffer: []u8,
|
|
419
|
+
at: usize = 0,
|
|
420
|
+
|
|
421
|
+
fn init(buffer: []u8) PageWriter {
|
|
422
|
+
return .{ .buffer = buffer };
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
fn remaining(self: *const PageWriter) usize {
|
|
426
|
+
return self.buffer.len - self.at;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
fn writeInt(self: *PageWriter, comptime T: type, value: T) error{NoSpace}!void {
|
|
430
|
+
if (self.remaining() < @sizeOf(T)) return error.NoSpace;
|
|
431
|
+
std.mem.writeInt(T, self.buffer[self.at..][0..@sizeOf(T)], value, .little);
|
|
432
|
+
self.at += @sizeOf(T);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
fn writeBytes(self: *PageWriter, bytes: []const u8) error{NoSpace}!void {
|
|
436
|
+
if (bytes.len > std.math.maxInt(u32) or self.remaining() < 4 + bytes.len) return error.NoSpace;
|
|
437
|
+
try self.writeInt(u32, @intCast(bytes.len));
|
|
438
|
+
@memcpy(self.buffer[self.at..][0..bytes.len], bytes);
|
|
439
|
+
self.at += bytes.len;
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
fn request(allocator: std.mem.Allocator, scope: u32, fields: []const []const u8) ![]u8 {
|
|
444
|
+
var len: usize = 4;
|
|
445
|
+
for (fields) |field| len += 4 + field.len;
|
|
446
|
+
const bytes = try allocator.alloc(u8, len);
|
|
447
|
+
std.mem.writeInt(u32, bytes[0..4], scope, .little);
|
|
448
|
+
var writer = PageWriter{ .buffer = bytes, .at = 4 };
|
|
449
|
+
for (fields) |field| try writer.writeBytes(field);
|
|
450
|
+
return bytes;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
fn scanRequest(allocator: std.mem.Allocator, scope: u32, prefix: []const u8, limit: u32, after: []const u8) ![]u8 {
|
|
454
|
+
const bytes = try allocator.alloc(u8, 16 + prefix.len + after.len);
|
|
455
|
+
var writer = PageWriter.init(bytes);
|
|
456
|
+
try writer.writeInt(u32, scope);
|
|
457
|
+
try writer.writeBytes(prefix);
|
|
458
|
+
try writer.writeInt(u32, limit);
|
|
459
|
+
try writer.writeBytes(after);
|
|
460
|
+
return bytes;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const TestEntry = struct { key: []const u8, value: []const u8 };
|
|
464
|
+
|
|
465
|
+
fn setManyRequest(allocator: std.mem.Allocator, scope: u32, entries: []const TestEntry) ![]u8 {
|
|
466
|
+
var len: usize = 8;
|
|
467
|
+
for (entries) |entry| len += 8 + entry.key.len + entry.value.len;
|
|
468
|
+
const bytes = try allocator.alloc(u8, len);
|
|
469
|
+
var writer = PageWriter.init(bytes);
|
|
470
|
+
try writer.writeInt(u32, scope);
|
|
471
|
+
try writer.writeInt(u32, @intCast(entries.len));
|
|
472
|
+
for (entries) |entry| {
|
|
473
|
+
try writer.writeBytes(entry.key);
|
|
474
|
+
try writer.writeBytes(entry.value);
|
|
475
|
+
}
|
|
476
|
+
return bytes;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
test "durable stores read through a dedicated WAL connection" {
|
|
480
|
+
const allocator = std.testing.allocator;
|
|
481
|
+
var tmp = std.testing.tmpDir(.{});
|
|
482
|
+
defer tmp.cleanup();
|
|
483
|
+
var path_buffer: [256]u8 = undefined;
|
|
484
|
+
const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/record-store", .{tmp.sub_path[0..]});
|
|
485
|
+
try std.Io.Dir.cwd().createDirPath(std.testing.io, path);
|
|
486
|
+
var store = try Store.open(allocator, path);
|
|
487
|
+
defer store.deinit();
|
|
488
|
+
try std.testing.expect(store.read_db != null);
|
|
489
|
+
|
|
490
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
491
|
+
const set_payload = try request(allocator, 0, &.{ "durable/key", "value" });
|
|
492
|
+
defer allocator.free(set_payload);
|
|
493
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.set, set_payload, &output).outcome);
|
|
494
|
+
const get_payload = try request(allocator, 0, &.{"durable/key"});
|
|
495
|
+
defer allocator.free(get_payload);
|
|
496
|
+
const found = store.execute(.get, get_payload, &output);
|
|
497
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
498
|
+
try std.testing.expectEqualSlices(u8, "value", output[1..found.len]);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
test "record store CRUD distinguishes empty values from misses" {
|
|
502
|
+
const allocator = std.testing.allocator;
|
|
503
|
+
var store = try Store.openMemory(allocator);
|
|
504
|
+
defer store.deinit();
|
|
505
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
506
|
+
|
|
507
|
+
const set_payload = try request(allocator, 0, &.{ "doc/a", "" });
|
|
508
|
+
defer allocator.free(set_payload);
|
|
509
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.set, set_payload, &output).outcome);
|
|
510
|
+
|
|
511
|
+
const get_payload = try request(allocator, 0, &.{"doc/a"});
|
|
512
|
+
defer allocator.free(get_payload);
|
|
513
|
+
const found = store.execute(.get, get_payload, &output);
|
|
514
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
515
|
+
try std.testing.expectEqualSlices(u8, &.{1}, output[0..found.len]);
|
|
516
|
+
|
|
517
|
+
const missing_payload = try request(allocator, 0, &.{"doc/missing"});
|
|
518
|
+
defer allocator.free(missing_payload);
|
|
519
|
+
const missing = store.execute(.get, missing_payload, &output);
|
|
520
|
+
try std.testing.expectEqual(Outcome.miss, missing.outcome);
|
|
521
|
+
try std.testing.expectEqualSlices(u8, &.{0}, output[0..missing.len]);
|
|
522
|
+
|
|
523
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.delete, get_payload, &output).outcome);
|
|
524
|
+
try std.testing.expectEqual(Outcome.miss, store.execute(.get, get_payload, &output).outcome);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
test "record store scan is byte ordered and paginated" {
|
|
528
|
+
const allocator = std.testing.allocator;
|
|
529
|
+
var store = try Store.openMemory(allocator);
|
|
530
|
+
defer store.deinit();
|
|
531
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
532
|
+
const keys = [_][]const u8{ "chat/1", "chat/2", "chat/3", "other/1" };
|
|
533
|
+
for (&keys) |key| {
|
|
534
|
+
const payload = try request(allocator, 0, &.{ key, key });
|
|
535
|
+
defer allocator.free(payload);
|
|
536
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.set, payload, &output).outcome);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const first_request = try scanRequest(allocator, 0, "chat/", 2, "");
|
|
540
|
+
defer allocator.free(first_request);
|
|
541
|
+
const first_page = store.execute(.scan, first_request, &output);
|
|
542
|
+
try std.testing.expectEqual(Outcome.ok, first_page.outcome);
|
|
543
|
+
var first = Cursor{ .bytes = output[0..first_page.len] };
|
|
544
|
+
try std.testing.expectEqual(@as(u32, 2), first.int(u32).?);
|
|
545
|
+
try std.testing.expectEqualStrings("chat/1", first.bytesField().?);
|
|
546
|
+
try std.testing.expectEqualStrings("chat/1", first.bytesField().?);
|
|
547
|
+
try std.testing.expectEqualStrings("chat/2", first.bytesField().?);
|
|
548
|
+
try std.testing.expectEqualStrings("chat/2", first.bytesField().?);
|
|
549
|
+
const next = first.bytesField().?;
|
|
550
|
+
var next_copy: [max_key_bytes]u8 = undefined;
|
|
551
|
+
@memcpy(next_copy[0..next.len], next);
|
|
552
|
+
try std.testing.expectEqualStrings("chat/2", next);
|
|
553
|
+
try std.testing.expect(first.done());
|
|
554
|
+
|
|
555
|
+
const second_request = try scanRequest(allocator, 0, "chat/", 2, next_copy[0..next.len]);
|
|
556
|
+
defer allocator.free(second_request);
|
|
557
|
+
const second_page = store.execute(.scan, second_request, &output);
|
|
558
|
+
try std.testing.expectEqual(Outcome.ok, second_page.outcome);
|
|
559
|
+
var second = Cursor{ .bytes = output[0..second_page.len] };
|
|
560
|
+
try std.testing.expectEqual(@as(u32, 1), second.int(u32).?);
|
|
561
|
+
try std.testing.expectEqualStrings("chat/3", second.bytesField().?);
|
|
562
|
+
try std.testing.expectEqualStrings("chat/3", second.bytesField().?);
|
|
563
|
+
try std.testing.expectEqual(@as(usize, 0), second.bytesField().?.len);
|
|
564
|
+
try std.testing.expect(second.done());
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
test "record store setMany rolls back the whole batch on a bad key" {
|
|
568
|
+
const allocator = std.testing.allocator;
|
|
569
|
+
var store = try Store.openMemory(allocator);
|
|
570
|
+
defer store.deinit();
|
|
571
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
572
|
+
var bytes: [4 + 4 + 4 + 2 + 4 + 1 + 4 + 0 + 4 + 1]u8 = undefined;
|
|
573
|
+
var writer = PageWriter.init(&bytes);
|
|
574
|
+
try writer.writeInt(u32, 0);
|
|
575
|
+
try writer.writeInt(u32, 2);
|
|
576
|
+
try writer.writeBytes("ok");
|
|
577
|
+
try writer.writeBytes("1");
|
|
578
|
+
try writer.writeBytes("");
|
|
579
|
+
try writer.writeBytes("2");
|
|
580
|
+
try std.testing.expectEqual(Outcome.bad_key, store.execute(.set_many, &bytes, &output).outcome);
|
|
581
|
+
const get_payload = try request(allocator, 0, &.{"ok"});
|
|
582
|
+
defer allocator.free(get_payload);
|
|
583
|
+
try std.testing.expectEqual(Outcome.miss, store.execute(.get, get_payload, &output).outcome);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
test "record store setMany rolls back writes when SQLite rejects a later row" {
|
|
587
|
+
const allocator = std.testing.allocator;
|
|
588
|
+
var store = try Store.openMemory(allocator);
|
|
589
|
+
defer store.deinit();
|
|
590
|
+
try store.write_db.exec(
|
|
591
|
+
"CREATE TRIGGER reject_second BEFORE INSERT ON kv " ++
|
|
592
|
+
"WHEN new.k=X'626164' BEGIN SELECT RAISE(ABORT,'fixture'); END;",
|
|
593
|
+
);
|
|
594
|
+
const entries = [_]TestEntry{
|
|
595
|
+
.{ .key = "kept-only-on-commit", .value = "one" },
|
|
596
|
+
.{ .key = "bad", .value = "two" },
|
|
597
|
+
};
|
|
598
|
+
const payload = try setManyRequest(allocator, 0, &entries);
|
|
599
|
+
defer allocator.free(payload);
|
|
600
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
601
|
+
try std.testing.expectEqual(Outcome.io_failed, store.execute(.set_many, payload, &output).outcome);
|
|
602
|
+
|
|
603
|
+
const get_payload = try request(allocator, 0, &.{"kept-only-on-commit"});
|
|
604
|
+
defer allocator.free(get_payload);
|
|
605
|
+
try std.testing.expectEqual(Outcome.miss, store.execute(.get, get_payload, &output).outcome);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
test "record store enforces exact key value scan and batch bounds" {
|
|
609
|
+
const allocator = std.testing.allocator;
|
|
610
|
+
var store = try Store.openMemory(allocator);
|
|
611
|
+
defer store.deinit();
|
|
612
|
+
const output = try allocator.alloc(u8, max_result_bytes);
|
|
613
|
+
defer allocator.free(output);
|
|
614
|
+
|
|
615
|
+
const max_key = try allocator.alloc(u8, max_key_bytes);
|
|
616
|
+
defer allocator.free(max_key);
|
|
617
|
+
@memset(max_key, 'k');
|
|
618
|
+
const max_value = try allocator.alloc(u8, max_value_bytes);
|
|
619
|
+
defer allocator.free(max_value);
|
|
620
|
+
@memset(max_value, 0xa5);
|
|
621
|
+
const exact = try request(allocator, 0, &.{ max_key, max_value });
|
|
622
|
+
defer allocator.free(exact);
|
|
623
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.set, exact, output).outcome);
|
|
624
|
+
|
|
625
|
+
const exact_get = try request(allocator, 0, &.{max_key});
|
|
626
|
+
defer allocator.free(exact_get);
|
|
627
|
+
const found = store.execute(.get, exact_get, output);
|
|
628
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
629
|
+
try std.testing.expectEqual(max_value_bytes + 1, found.len);
|
|
630
|
+
try std.testing.expectEqualSlices(u8, max_value, output[1..found.len]);
|
|
631
|
+
|
|
632
|
+
const long_key = try allocator.alloc(u8, max_key_bytes + 1);
|
|
633
|
+
defer allocator.free(long_key);
|
|
634
|
+
@memset(long_key, 'x');
|
|
635
|
+
const long_key_request = try request(allocator, 0, &.{long_key});
|
|
636
|
+
defer allocator.free(long_key_request);
|
|
637
|
+
try std.testing.expectEqual(Outcome.bad_key, store.execute(.get, long_key_request, output).outcome);
|
|
638
|
+
const empty_key_request = try request(allocator, 0, &.{""});
|
|
639
|
+
defer allocator.free(empty_key_request);
|
|
640
|
+
try std.testing.expectEqual(Outcome.bad_key, store.execute(.get, empty_key_request, output).outcome);
|
|
641
|
+
const invalid_utf8_request = try request(allocator, 0, &.{&.{0xff}});
|
|
642
|
+
defer allocator.free(invalid_utf8_request);
|
|
643
|
+
try std.testing.expectEqual(Outcome.bad_key, store.execute(.get, invalid_utf8_request, output).outcome);
|
|
644
|
+
|
|
645
|
+
const long_value = try allocator.alloc(u8, max_value_bytes + 1);
|
|
646
|
+
defer allocator.free(long_value);
|
|
647
|
+
@memset(long_value, 0x5a);
|
|
648
|
+
const long_value_request = try request(allocator, 0, &.{ "value/too-large", long_value });
|
|
649
|
+
defer allocator.free(long_value_request);
|
|
650
|
+
try std.testing.expectEqual(Outcome.over_bound, store.execute(.set, long_value_request, output).outcome);
|
|
651
|
+
|
|
652
|
+
const long_prefix_request = try scanRequest(allocator, 0, long_key, 1, "");
|
|
653
|
+
defer allocator.free(long_prefix_request);
|
|
654
|
+
try std.testing.expectEqual(Outcome.bad_key, store.execute(.scan, long_prefix_request, output).outcome);
|
|
655
|
+
const long_limit_request = try scanRequest(allocator, 0, "", max_scan_limit + 1, "");
|
|
656
|
+
defer allocator.free(long_limit_request);
|
|
657
|
+
try std.testing.expectEqual(Outcome.over_bound, store.execute(.scan, long_limit_request, output).outcome);
|
|
658
|
+
|
|
659
|
+
var too_many_header: [8]u8 = undefined;
|
|
660
|
+
std.mem.writeInt(u32, too_many_header[0..4], 0, .little);
|
|
661
|
+
std.mem.writeInt(u32, too_many_header[4..8], max_batch_entries + 1, .little);
|
|
662
|
+
try std.testing.expectEqual(Outcome.over_bound, store.execute(.set_many, &too_many_header, output).outcome);
|
|
663
|
+
const oversized_batch = try allocator.alloc(u8, max_batch_bytes + 1);
|
|
664
|
+
defer allocator.free(oversized_batch);
|
|
665
|
+
@memset(oversized_batch, 0);
|
|
666
|
+
try std.testing.expectEqual(Outcome.over_bound, store.execute(.set_many, oversized_batch, output).outcome);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
test "record store scopes remain isolated on the extensible wire" {
|
|
670
|
+
const allocator = std.testing.allocator;
|
|
671
|
+
var store = try Store.openMemory(allocator);
|
|
672
|
+
defer store.deinit();
|
|
673
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
674
|
+
const scoped_set = try request(allocator, 7, &.{ "shared/key", "scoped" });
|
|
675
|
+
defer allocator.free(scoped_set);
|
|
676
|
+
try std.testing.expectEqual(Outcome.ok, store.execute(.set, scoped_set, &output).outcome);
|
|
677
|
+
const default_get = try request(allocator, 0, &.{"shared/key"});
|
|
678
|
+
defer allocator.free(default_get);
|
|
679
|
+
try std.testing.expectEqual(Outcome.miss, store.execute(.get, default_get, &output).outcome);
|
|
680
|
+
const scoped_get = try request(allocator, 7, &.{"shared/key"});
|
|
681
|
+
defer allocator.free(scoped_get);
|
|
682
|
+
const found = store.execute(.get, scoped_get, &output);
|
|
683
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
684
|
+
try std.testing.expectEqualStrings("scoped", output[1..found.len]);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
test "record store reports busy and io failures through the closed outcome enum" {
|
|
688
|
+
const allocator = std.testing.allocator;
|
|
689
|
+
var tmp = std.testing.tmpDir(.{});
|
|
690
|
+
defer tmp.cleanup();
|
|
691
|
+
var path_buffer: [256]u8 = undefined;
|
|
692
|
+
const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/record-store-failures", .{tmp.sub_path[0..]});
|
|
693
|
+
try std.Io.Dir.cwd().createDirPath(std.testing.io, path);
|
|
694
|
+
var locking = try Store.open(allocator, path);
|
|
695
|
+
defer locking.deinit();
|
|
696
|
+
var blocked = try Store.open(allocator, path);
|
|
697
|
+
defer blocked.deinit();
|
|
698
|
+
try locking.write_db.exec("BEGIN IMMEDIATE;");
|
|
699
|
+
defer locking.write_db.exec("ROLLBACK;") catch {};
|
|
700
|
+
const set_payload = try request(allocator, 0, &.{ "locked", "value" });
|
|
701
|
+
defer allocator.free(set_payload);
|
|
702
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
703
|
+
try std.testing.expectEqual(Outcome.busy, blocked.execute(.set, set_payload, &output).outcome);
|
|
704
|
+
|
|
705
|
+
var broken = try Store.openMemory(allocator);
|
|
706
|
+
defer broken.deinit();
|
|
707
|
+
try broken.write_db.exec("DROP TABLE kv;");
|
|
708
|
+
const get_payload = try request(allocator, 0, &.{"missing-table"});
|
|
709
|
+
defer allocator.free(get_payload);
|
|
710
|
+
try std.testing.expectEqual(Outcome.io_failed, broken.execute(.get, get_payload, &output).outcome);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
test "record store refuses schema versions newer than the engine" {
|
|
714
|
+
const allocator = std.testing.allocator;
|
|
715
|
+
var tmp = std.testing.tmpDir(.{});
|
|
716
|
+
defer tmp.cleanup();
|
|
717
|
+
var dir_buffer: [256]u8 = undefined;
|
|
718
|
+
const dir = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/record-store-version", .{tmp.sub_path[0..]});
|
|
719
|
+
try std.Io.Dir.cwd().createDirPath(std.testing.io, dir);
|
|
720
|
+
const path_plain = try std.fs.path.join(allocator, &.{ dir, "store.db" });
|
|
721
|
+
defer allocator.free(path_plain);
|
|
722
|
+
const path = try allocator.dupeZ(u8, path_plain);
|
|
723
|
+
defer allocator.free(path);
|
|
724
|
+
var db = try sqlite.Connection.open(path);
|
|
725
|
+
try db.exec("PRAGMA user_version=2;");
|
|
726
|
+
db.close();
|
|
727
|
+
try std.testing.expectError(error.VersionUnknown, Store.open(allocator, dir));
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
test "record store workers commit in reserved issue order" {
|
|
731
|
+
const allocator = std.testing.allocator;
|
|
732
|
+
var store = try Store.openMemory(allocator);
|
|
733
|
+
defer store.deinit();
|
|
734
|
+
const first = try request(allocator, 0, &.{ "ordered/key", "first" });
|
|
735
|
+
defer allocator.free(first);
|
|
736
|
+
const second = try request(allocator, 0, &.{ "ordered/key", "second" });
|
|
737
|
+
defer allocator.free(second);
|
|
738
|
+
|
|
739
|
+
const first_sequence = store.reserveWrite();
|
|
740
|
+
const second_sequence = store.reserveWrite();
|
|
741
|
+
var second_output: [32]u8 = undefined;
|
|
742
|
+
const Worker = struct {
|
|
743
|
+
fn run(target: *Store, sequence: u64, payload: []const u8, output: []u8) void {
|
|
744
|
+
const result = target.executeWrite(sequence, .set, payload, output);
|
|
745
|
+
std.debug.assert(result.outcome == .ok);
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
const second_thread = try std.Thread.spawn(.{}, Worker.run, .{ &store, second_sequence, second, &second_output });
|
|
749
|
+
var first_output: [32]u8 = undefined;
|
|
750
|
+
try std.testing.expectEqual(Outcome.ok, store.executeWrite(first_sequence, .set, first, &first_output).outcome);
|
|
751
|
+
second_thread.join();
|
|
752
|
+
|
|
753
|
+
const get_payload = try request(allocator, 0, &.{"ordered/key"});
|
|
754
|
+
defer allocator.free(get_payload);
|
|
755
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
756
|
+
const found = store.execute(.get, get_payload, &output);
|
|
757
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
758
|
+
try std.testing.expectEqualSlices(u8, "second", output[1..found.len]);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
test "synchronous reads wait for earlier reserved writes" {
|
|
762
|
+
const allocator = std.testing.allocator;
|
|
763
|
+
var store = try Store.openMemory(allocator);
|
|
764
|
+
defer store.deinit();
|
|
765
|
+
const set_payload = try request(allocator, 0, &.{ "ordered/read", "visible" });
|
|
766
|
+
defer allocator.free(set_payload);
|
|
767
|
+
const get_payload = try request(allocator, 0, &.{"ordered/read"});
|
|
768
|
+
defer allocator.free(get_payload);
|
|
769
|
+
|
|
770
|
+
const sequence = store.reserveWrite();
|
|
771
|
+
var write_output: [32]u8 = undefined;
|
|
772
|
+
const Worker = struct {
|
|
773
|
+
fn run(target: *Store, write_sequence: u64, payload: []const u8, output: []u8) void {
|
|
774
|
+
// Make it plausible for the loop-thread read to reach its barrier
|
|
775
|
+
// before this worker enters SQLite.
|
|
776
|
+
std.Thread.yield() catch {};
|
|
777
|
+
const result = target.executeWrite(write_sequence, .set, payload, output);
|
|
778
|
+
std.debug.assert(result.outcome == .ok);
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
const thread = try std.Thread.spawn(.{}, Worker.run, .{ &store, sequence, set_payload, &write_output });
|
|
782
|
+
|
|
783
|
+
var output: [max_result_bytes]u8 = undefined;
|
|
784
|
+
const found = store.execute(.get, get_payload, &output);
|
|
785
|
+
thread.join();
|
|
786
|
+
try std.testing.expectEqual(Outcome.ok, found.outcome);
|
|
787
|
+
try std.testing.expectEqualSlices(u8, "visible", output[1..found.len]);
|
|
788
|
+
}
|