tero 0.0.7 → 0.0.8

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.md CHANGED
@@ -131,6 +131,8 @@ db.cancelScheduledBackup(scheduleId);
131
131
 
132
132
  Tero interacts directly with object storage from each instance, keeping credentials local and isolated.
133
133
 
134
+ 📊 **Cost comparison:** see [BUCKET_COST_DATA.md](./BUCKET_COST_DATA.md) for AWS S3 vs Cloudflare R2 pricing (verified 2026-08-28), how backup operations map to billed requests, and monthly cost scenarios.
135
+
134
136
  ## Cloud recovery
135
137
 
136
138
  ```javascript
@@ -267,6 +269,48 @@ npm run test:legacy # legacy suite
267
269
  node local_tests/v2-test.js # v2: hydrate + bucket backup surface
268
270
  ```
269
271
 
272
+ ### Testing S3 live backup locally (MinIO)
273
+
274
+ `npm run test:s3` round-trips the live backup API against a real S3-compatible
275
+ endpoint: live writes → `backupToBucket()` → `checkpointAndBackupToBucket()` →
276
+ raw bucket verification → hydrate-on-startup into a fresh node. Without
277
+ credentials it skips, so plain `npm test` stays green.
278
+
279
+ The cheapest way to exercise it is MinIO in Docker:
280
+
281
+ ```bash
282
+ # 1. Start a local S3-compatible server
283
+ docker run -d --name tero-minio-test -p 9000:9000 -p 9001:9001 \
284
+ -v /tmp/tero-minio-data:/data \
285
+ minio/minio server /data --console-address ":9001"
286
+
287
+ # 2. Run the round-trip test (bucket is created if missing)
288
+ S3_ENDPOINT=http://127.0.0.1:9000 \
289
+ S3_BUCKET=tero-live-backup-test \
290
+ AWS_ACCESS_KEY_ID=minioadmin \
291
+ AWS_SECRET_ACCESS_KEY=minioadmin \
292
+ npm run test:s3
293
+
294
+ # 3. Inspect what Tero wrote (console at http://localhost:9001)
295
+ # s3://tero-live-backup-test/tero-backups/<dbName>/
296
+ # ├── <key>.json # data snapshots
297
+ # ├── wal/.wal.<ts> # archived WAL segments
298
+ # └── MANIFEST.json # hydration entry point
299
+
300
+ # 4. Clean up
301
+ docker rm -f tero-minio-test && rm -rf /tmp/tero-minio-data
302
+ ```
303
+
304
+ Against real AWS, omit `S3_ENDPOINT` and pass an IAM user with
305
+ `s3:PutObject/GetObject/ListBucket/CreateBucket` on the test bucket:
306
+
307
+ ```bash
308
+ S3_BUCKET=my-test-bucket \
309
+ AWS_ACCESS_KEY_ID=... \
310
+ AWS_SECRET_ACCESS_KEY=... \
311
+ npm run test:s3
312
+ ```
313
+
270
314
  ## Performance characteristics
271
315
 
272
316
  ```
@@ -126,6 +126,22 @@ export declare class LockManager {
126
126
  releaseAllLocks(transactionId: string): void;
127
127
  detectDeadlock(): string[];
128
128
  }
129
+ /**
130
+ * Map a document key to a 2-level hash-prefix partitioned path:
131
+ * ${dbPath}/${hash[0]}/${hash[2]}/${key}.json
132
+ *
133
+ * This spreads files across 256×256 = 65,536 leaf directories, eliminating
134
+ * the POSIX dentry-lock and readdir() array-allocation problems that flat
135
+ * directories hit at >50k entries (ext4/XFS single-dir lookup degrades
136
+ * super-linearly with file count).
137
+ *
138
+ * The partition is deterministic from the key, so reads/writes for a given
139
+ * key always resolve to the same partition without an in-memory index.
140
+ *
141
+ * Exported so DataRecovery can write cloud-restored files into the exact
142
+ * partition the engine reads from (deterministic from the key).
143
+ */
144
+ export declare function partitionedPath(dbPath: string, key: string): string;
129
145
  /**
130
146
  * Walk partitioned directories under dbPath and call `visitor(filePath)` for
131
147
  * every *.json file found. Uses fs.opendir async iteration so each directory
@@ -575,10 +575,10 @@ export class LockManager {
575
575
  * The partition is deterministic from the key, so reads/writes for a given
576
576
  * key always resolve to the same partition without an in-memory index.
577
577
  *
578
- * Crash recovery (redoOperation/undoOperation) naturally walks the whole
579
- * tree via fs.opendir recursion — no readdirSync allocation.
578
+ * Exported so DataRecovery can write cloud-restored files into the exact
579
+ * partition the engine reads from (deterministic from the key).
580
580
  */
581
- function partitionedPath(dbPath, key) {
581
+ export function partitionedPath(dbPath, key) {
582
582
  // Fast 32-bit FNV-1a of the key to pick the partition dirs. We only need
583
583
  // ~16 bits of dispersion for partitioning (65k buckets), so 32-bit hash
584
584
  // is more than sufficient (collision here just means two keys share a
package/dist/recovery.js CHANGED
@@ -2,6 +2,7 @@ import { S3Client, GetObjectCommand, ListObjectsV2Command, HeadObjectCommand } f
2
2
  import { createWriteStream, existsSync, mkdirSync } from "fs";
3
3
  import { join, dirname } from "path";
4
4
  import { pipeline } from "stream/promises";
5
+ import { partitionedPath } from "./acid-engine.js";
5
6
  export class DataRecovery {
6
7
  s3Client;
7
8
  config;
@@ -67,7 +68,10 @@ export class DataRecovery {
67
68
  async recoverSingleFile(key) {
68
69
  try {
69
70
  const cloudKey = this.getCloudKey(`${key}.json`);
70
- const localFilePath = join(this.config.localPath, `${key}.json`);
71
+ // Write into the 2-level hash-partition location the engine reads from —
72
+ // NOT flat at the db root. The partition is deterministic from the key,
73
+ // so restored files are immediately visible to engine reads.
74
+ const localFilePath = partitionedPath(this.config.localPath, key);
71
75
  // Ensure local directory exists
72
76
  const localDir = dirname(localFilePath);
73
77
  if (!existsSync(localDir)) {
@@ -271,7 +275,7 @@ export class DataRecovery {
271
275
  const cloudFiles = await this.listAvailableFiles();
272
276
  const missing = [];
273
277
  for (const key of cloudFiles) {
274
- const localPath = join(this.config.localPath, `${key}.json`);
278
+ const localPath = partitionedPath(this.config.localPath, key);
275
279
  if (!existsSync(localPath)) {
276
280
  missing.push(key);
277
281
  }
@@ -373,9 +377,9 @@ export class DataRecovery {
373
377
  try {
374
378
  const cloudFiles = await this.listAvailableFiles();
375
379
  const localFiles = [];
376
- // Check which files exist locally
380
+ // Check which files exist locally (in their hash-partition location)
377
381
  for (const key of cloudFiles) {
378
- const localPath = join(this.config.localPath, `${key}.json`);
382
+ const localPath = partitionedPath(this.config.localPath, key);
379
383
  if (existsSync(localPath)) {
380
384
  localFiles.push(key);
381
385
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tero",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Tero is a JSON database that provides ACID transactions, schema validation, automated cloud backup, and automatic cloud recovery",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -21,6 +21,7 @@
21
21
  "test:production": "node local_tests/production-test.js",
22
22
  "test:legacy": "node local_tests/test.js",
23
23
  "test:backup": "node local_tests/backup-test.js",
24
+ "test:s3": "node local_tests/s3-live-backup-test.js",
24
25
  "test:schema": "node local_tests/schema-test.js",
25
26
  "test:all": "npm run test",
26
27
  "test:watch": "npm run test",