@tinycloud/replica 0.3.0-beta.7 → 0.3.0-beta.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/dist/kv-storage.js +1 -1
- package/dist/kv-storage.worker.f6f98ad5dc547a29274c0b539fb78e25f8a2051ccfaafc9490cb307051a0e0ef.js +19004 -0
- package/dist/kv-storage.worker.js +1 -1
- package/dist/kv-storage.worker.js.map +1 -1
- package/dist/replica.worker.js +1 -1
- package/dist/replica.worker.js.map +1 -1
- package/dist/sqlite.cjs +189 -49
- package/dist/sqlite.cjs.map +1 -1
- package/dist/sqlite.d.cts +25 -1
- package/dist/sqlite.d.ts +25 -1
- package/dist/sqlite.js +189 -49
- package/dist/sqlite.js.map +1 -1
- package/package.json +1 -1
- package/dist/kv-storage.worker.850a5e19b486939f03586f7d1294934f54f44cca0224450b1ba94dfb07912a75.js +0 -19004
package/dist/sqlite.cjs
CHANGED
|
@@ -726,6 +726,76 @@ var isHash = (name) => /^[0-9a-f]{64}$/.test(name);
|
|
|
726
726
|
function errnoOf(error) {
|
|
727
727
|
return typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
728
728
|
}
|
|
729
|
+
var isShard = (name) => /^[0-9a-f]{2}$/.test(name);
|
|
730
|
+
var isTempName = (name) => /^[0-9a-f]{64}\.[0-9a-f]{12}$/.test(name);
|
|
731
|
+
async function writerBlobFiles(root) {
|
|
732
|
+
const out = [];
|
|
733
|
+
let shards;
|
|
734
|
+
try {
|
|
735
|
+
shards = await (0, import_promises2.readdir)(root, { withFileTypes: true });
|
|
736
|
+
} catch (error) {
|
|
737
|
+
if (errnoOf(error) === "ENOENT" || errnoOf(error) === "ENOTDIR") return out;
|
|
738
|
+
throw error;
|
|
739
|
+
}
|
|
740
|
+
for (const shard of shards) {
|
|
741
|
+
const temp = shard.name === ".tmp";
|
|
742
|
+
if (!shard.isDirectory() || !(temp || isShard(shard.name))) continue;
|
|
743
|
+
const shardPath = (0, import_node_path2.join)(root, shard.name);
|
|
744
|
+
let files;
|
|
745
|
+
try {
|
|
746
|
+
files = await (0, import_promises2.readdir)(shardPath, { withFileTypes: true });
|
|
747
|
+
} catch (error) {
|
|
748
|
+
if (errnoOf(error) === "ENOENT" || errnoOf(error) === "ENOTDIR") continue;
|
|
749
|
+
throw error;
|
|
750
|
+
}
|
|
751
|
+
for (const file of files) {
|
|
752
|
+
if (!file.isFile()) continue;
|
|
753
|
+
const ok = temp ? isTempName(file.name) : isHash(file.name) && file.name.startsWith(shard.name);
|
|
754
|
+
if (!ok) continue;
|
|
755
|
+
const path = (0, import_node_path2.join)(shardPath, file.name);
|
|
756
|
+
const info = await (0, import_promises2.lstat)(path).catch(() => void 0);
|
|
757
|
+
if (!info?.isFile()) continue;
|
|
758
|
+
out.push({ path, hash: temp ? null : file.name, size: info.size });
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return out;
|
|
762
|
+
}
|
|
763
|
+
async function unlinkWriterOrphans(root, keep) {
|
|
764
|
+
let removed = 0;
|
|
765
|
+
const touched = /* @__PURE__ */ new Set();
|
|
766
|
+
for (const file of await writerBlobFiles(root)) {
|
|
767
|
+
if (file.hash !== null && keep.has(file.hash)) continue;
|
|
768
|
+
await (0, import_promises2.rm)(file.path, { force: true });
|
|
769
|
+
touched.add((0, import_node_path2.dirname)(file.path));
|
|
770
|
+
removed += 1;
|
|
771
|
+
}
|
|
772
|
+
for (const dir of touched) if (!dir.endsWith(".tmp")) await (0, import_promises2.rmdir)(dir).catch(() => void 0);
|
|
773
|
+
return removed;
|
|
774
|
+
}
|
|
775
|
+
async function unlinkBlobsIn(root, keep) {
|
|
776
|
+
let removed = 0;
|
|
777
|
+
for (const shard of await entriesOf(root)) {
|
|
778
|
+
const shardPath = (0, import_node_path2.join)(root, shard);
|
|
779
|
+
if (shard === ".tmp") {
|
|
780
|
+
for (const name of await entriesOf(shardPath)) {
|
|
781
|
+
await (0, import_promises2.rm)((0, import_node_path2.join)(shardPath, name), { force: true });
|
|
782
|
+
removed += 1;
|
|
783
|
+
}
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
let kept = 0;
|
|
787
|
+
for (const name of await entriesOf(shardPath)) {
|
|
788
|
+
if (isHash(name) && keep.has(name)) {
|
|
789
|
+
kept += 1;
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
await (0, import_promises2.rm)((0, import_node_path2.join)(shardPath, name), { force: true });
|
|
793
|
+
removed += 1;
|
|
794
|
+
}
|
|
795
|
+
if (kept === 0) await (0, import_promises2.rmdir)(shardPath).catch(() => void 0);
|
|
796
|
+
}
|
|
797
|
+
return removed;
|
|
798
|
+
}
|
|
729
799
|
async function entriesOf(path) {
|
|
730
800
|
try {
|
|
731
801
|
return await (0, import_promises2.readdir)(path);
|
|
@@ -789,6 +859,8 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
789
859
|
#guard;
|
|
790
860
|
/** Identity of the database file this store opened: a deleted-and-recreated replica is a different file. */
|
|
791
861
|
#ino;
|
|
862
|
+
#admit;
|
|
863
|
+
#partitionId;
|
|
792
864
|
#dev;
|
|
793
865
|
#namedAncestors;
|
|
794
866
|
constructor(dir, db, identity, options, directoryFd, namedAncestors = [], shared, readonlySnapshot = false) {
|
|
@@ -808,6 +880,8 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
808
880
|
this.#dev = identity.dev;
|
|
809
881
|
this.#now = options.now ?? Date.now;
|
|
810
882
|
this.#faults = options[FAULTS] ?? {};
|
|
883
|
+
this.#admit = options.admit;
|
|
884
|
+
this.#partitionId = options.partitionId ?? "";
|
|
811
885
|
this.#guard = options.guard ?? ((section) => section());
|
|
812
886
|
this.#namedAncestors = namedAncestors;
|
|
813
887
|
}
|
|
@@ -877,6 +951,55 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
877
951
|
}
|
|
878
952
|
}
|
|
879
953
|
}
|
|
954
|
+
/**
|
|
955
|
+
* Blob content bytes on disk in the replica at `dir` (0 when absent): the writer's blob files under `blobs/`,
|
|
956
|
+
* including writer temp files and blobs no entry references any more, so garbage a failed sync
|
|
957
|
+
* left behind still counts. Counts exactly what `collectOrphans` may delete: foreign entries
|
|
958
|
+
* (stray files, symlinks, unknown names) are neither counted nor touched. Only blob content is counted: not the database, its WAL, rows or the
|
|
959
|
+
* pending store. Filesystem-only: it opens no
|
|
960
|
+
* database, so it can sum a profile's replicas under the caller's own mutation hold.
|
|
961
|
+
*/
|
|
962
|
+
static async contentBytes(dir) {
|
|
963
|
+
try {
|
|
964
|
+
let bytes = 0;
|
|
965
|
+
for (const file of await writerBlobFiles((0, import_node_path2.join)(dir, "blobs"))) bytes += file.size;
|
|
966
|
+
return bytes;
|
|
967
|
+
} catch (error) {
|
|
968
|
+
throw storageError(error, "Measuring replica content");
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Remove blobs and temp files in the replica at `dir` that no committed row references: what a
|
|
973
|
+
* crash between writing blobs and committing (or between committing and collecting) leaves.
|
|
974
|
+
* Returns the number of files removed; 0 when the replica is absent or unreadable.
|
|
975
|
+
*
|
|
976
|
+
* The caller MUST hold the profile mutation guard, which every writer of this replica holds
|
|
977
|
+
* across its blob write and commit; that is what makes an unreferenced blob safe to delete
|
|
978
|
+
* (it cannot belong to an in-flight write). Never call it without that hold. The database is
|
|
979
|
+
* read only, so nothing here can take a lease or change committed state.
|
|
980
|
+
*/
|
|
981
|
+
static async collectOrphans(dir) {
|
|
982
|
+
const dbPath = (0, import_node_path2.join)(dir, "replica.db");
|
|
983
|
+
const identity = await (0, import_promises2.lstat)(dbPath).catch(() => void 0);
|
|
984
|
+
if (!identity?.isFile()) return 0;
|
|
985
|
+
const opener = await loadSqlite();
|
|
986
|
+
let keep;
|
|
987
|
+
let db;
|
|
988
|
+
try {
|
|
989
|
+
db = opener(dbPath, { readonly: true });
|
|
990
|
+
db.exec("PRAGMA busy_timeout = 0");
|
|
991
|
+
keep = new Set(db.all("SELECT DISTINCT hash FROM entry WHERE content = 1").map((row) => row.hash));
|
|
992
|
+
} catch {
|
|
993
|
+
return 0;
|
|
994
|
+
} finally {
|
|
995
|
+
db?.close();
|
|
996
|
+
}
|
|
997
|
+
try {
|
|
998
|
+
return await unlinkWriterOrphans((0, import_node_path2.join)(dir, "blobs"), keep);
|
|
999
|
+
} catch (error) {
|
|
1000
|
+
throw storageError(error, "Collecting orphaned content");
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
880
1003
|
/**
|
|
881
1004
|
* Open the replica store in `dir`. With `create: false` a missing replica is
|
|
882
1005
|
* REPLICA_NOT_FOUND and nothing is written to disk.
|
|
@@ -1586,6 +1709,41 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
1586
1709
|
}
|
|
1587
1710
|
reused.set(change.hash, size);
|
|
1588
1711
|
}
|
|
1712
|
+
if (this.#admit) {
|
|
1713
|
+
await this.#collectLocked(t);
|
|
1714
|
+
const changed = new Set(p.changes.map((change) => change.key));
|
|
1715
|
+
const kept = /* @__PURE__ */ new Set();
|
|
1716
|
+
for (const change of p.changes) if (!change.deleted && change.content) kept.add(change.hash);
|
|
1717
|
+
let blobBytes = 0;
|
|
1718
|
+
for (const [hash, bytes] of p.blobs) {
|
|
1719
|
+
const stored = this.#db.get(
|
|
1720
|
+
"SELECT 1 AS hit FROM entry WHERE hash = ? AND content = 1 AND deleted = 0 LIMIT 1",
|
|
1721
|
+
hash
|
|
1722
|
+
);
|
|
1723
|
+
if (stored === void 0) blobBytes += bytes.byteLength;
|
|
1724
|
+
}
|
|
1725
|
+
const released = /* @__PURE__ */ new Map();
|
|
1726
|
+
for (const key of changed) {
|
|
1727
|
+
const old = this.#db.get(
|
|
1728
|
+
"SELECT hash, size FROM entry WHERE key = ? AND deleted = 0 AND content = 1 AND hash IS NOT NULL AND size IS NOT NULL",
|
|
1729
|
+
key
|
|
1730
|
+
);
|
|
1731
|
+
if (old === void 0 || kept.has(old.hash) || released.has(old.hash)) continue;
|
|
1732
|
+
const holders = this.#db.all(
|
|
1733
|
+
"SELECT key FROM entry WHERE hash = ? AND content = 1 AND deleted = 0",
|
|
1734
|
+
old.hash
|
|
1735
|
+
);
|
|
1736
|
+
if (holders.every((holder) => changed.has(holder.key))) released.set(old.hash, old.size);
|
|
1737
|
+
}
|
|
1738
|
+
let freed = 0;
|
|
1739
|
+
for (const size of released.values()) freed += size;
|
|
1740
|
+
await this.#admit({
|
|
1741
|
+
partitionId: this.#partitionId,
|
|
1742
|
+
blobBytes: blobBytes - freed,
|
|
1743
|
+
entries: p.changes.length,
|
|
1744
|
+
freeBytes: null
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1589
1747
|
try {
|
|
1590
1748
|
await this.#writeBlobs(p.blobs);
|
|
1591
1749
|
} catch (error) {
|
|
@@ -1661,36 +1819,16 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
1661
1819
|
});
|
|
1662
1820
|
} catch (error) {
|
|
1663
1821
|
if (isReplicaError(error, ReplicaErrorCode.GRANT_REVOKED)) await this.#purgeContent().catch(() => void 0);
|
|
1822
|
+
else if (this.#admit) await this.#collectLocked(t).catch(() => void 0);
|
|
1664
1823
|
throw error;
|
|
1665
1824
|
}
|
|
1666
1825
|
this.#faults.afterCommit?.();
|
|
1826
|
+
if (this.#admit) await this.#collectLocked(t);
|
|
1667
1827
|
});
|
|
1668
1828
|
}
|
|
1669
1829
|
/** Unlink every blob not in `keep`, and every temp file. Only a missing directory counts as empty. */
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
const root = (0, import_node_path2.join)(this.#pinnedDir, "blobs");
|
|
1673
|
-
for (const shard of await entriesOf(root)) {
|
|
1674
|
-
const shardPath = (0, import_node_path2.join)(root, shard);
|
|
1675
|
-
if (shard === ".tmp") {
|
|
1676
|
-
for (const name of await entriesOf(shardPath)) {
|
|
1677
|
-
await (0, import_promises2.rm)((0, import_node_path2.join)(shardPath, name), { force: true });
|
|
1678
|
-
removed += 1;
|
|
1679
|
-
}
|
|
1680
|
-
continue;
|
|
1681
|
-
}
|
|
1682
|
-
let kept = 0;
|
|
1683
|
-
for (const name of await entriesOf(shardPath)) {
|
|
1684
|
-
if (isHash(name) && keep.has(name)) {
|
|
1685
|
-
kept += 1;
|
|
1686
|
-
continue;
|
|
1687
|
-
}
|
|
1688
|
-
await (0, import_promises2.rm)((0, import_node_path2.join)(shardPath, name), { force: true });
|
|
1689
|
-
removed += 1;
|
|
1690
|
-
}
|
|
1691
|
-
if (kept === 0) await (0, import_promises2.rmdir)(shardPath).catch(() => void 0);
|
|
1692
|
-
}
|
|
1693
|
-
return removed;
|
|
1830
|
+
#unlinkBlobs(keep) {
|
|
1831
|
+
return unlinkBlobsIn((0, import_node_path2.join)(this.#pinnedDir, "blobs"), keep);
|
|
1694
1832
|
}
|
|
1695
1833
|
/** Whether any content file (a blob or a temp file) is on disk. */
|
|
1696
1834
|
async #holdsContentFiles() {
|
|
@@ -1701,33 +1839,35 @@ var SqliteReplicaStore = class _SqliteReplicaStore {
|
|
|
1701
1839
|
return false;
|
|
1702
1840
|
}
|
|
1703
1841
|
async collectGarbage(t) {
|
|
1704
|
-
return this.#mutate(
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1842
|
+
return this.#mutate(() => this.#collectLocked(t));
|
|
1843
|
+
}
|
|
1844
|
+
/** `collectGarbage` for callers already inside the mutation hold (it cannot re-enter `#mutate`). */
|
|
1845
|
+
async #collectLocked(t) {
|
|
1846
|
+
const release = this.#shared ? await acquireWritableMutation(this.#shared) : void 0;
|
|
1847
|
+
try {
|
|
1848
|
+
this.#db.exec("BEGIN IMMEDIATE");
|
|
1849
|
+
} catch (error) {
|
|
1850
|
+
release?.();
|
|
1851
|
+
throw storageError(error, "Collecting unused content");
|
|
1852
|
+
}
|
|
1853
|
+
try {
|
|
1854
|
+
this.#checkLease(t);
|
|
1855
|
+
await this.#faults.afterGarbageCollectionBegin?.();
|
|
1856
|
+
const keep = new Set(
|
|
1857
|
+
this.#db.all("SELECT DISTINCT hash FROM entry WHERE content = 1").map((row) => row.hash)
|
|
1858
|
+
);
|
|
1859
|
+
const removed = await this.#unlinkBlobs(keep);
|
|
1860
|
+
this.#db.exec("COMMIT");
|
|
1861
|
+
return removed;
|
|
1862
|
+
} catch (error) {
|
|
1712
1863
|
try {
|
|
1713
|
-
this.#
|
|
1714
|
-
|
|
1715
|
-
const keep = new Set(
|
|
1716
|
-
this.#db.all("SELECT DISTINCT hash FROM entry WHERE content = 1").map((row) => row.hash)
|
|
1717
|
-
);
|
|
1718
|
-
const removed = await this.#unlinkBlobs(keep);
|
|
1719
|
-
this.#db.exec("COMMIT");
|
|
1720
|
-
return removed;
|
|
1721
|
-
} catch (error) {
|
|
1722
|
-
try {
|
|
1723
|
-
this.#db.exec("ROLLBACK");
|
|
1724
|
-
} catch {
|
|
1725
|
-
}
|
|
1726
|
-
throw storageError(error, "Collecting unused content");
|
|
1727
|
-
} finally {
|
|
1728
|
-
release?.();
|
|
1864
|
+
this.#db.exec("ROLLBACK");
|
|
1865
|
+
} catch {
|
|
1729
1866
|
}
|
|
1730
|
-
|
|
1867
|
+
throw storageError(error, "Collecting unused content");
|
|
1868
|
+
} finally {
|
|
1869
|
+
release?.();
|
|
1870
|
+
}
|
|
1731
1871
|
}
|
|
1732
1872
|
async reset(t, reason, options = {}) {
|
|
1733
1873
|
await this.#mutate(async () => {
|