From 5184d8df0f5cbd04b9480dd558c5569386b8effd Mon Sep 17 00:00:00 2001 From: Karl Prieb Date: Tue, 15 Oct 2024 18:05:00 -0300 Subject: [PATCH] feat: use insertDataId to inherit verified state from parent --- src/database/sql/data/content-attributes.sql | 38 +++++++++----------- src/database/standalone-sqlite.ts | 27 ++++++-------- src/types.d.ts | 2 ++ src/workers/ans104-data-indexer.ts | 1 + 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/database/sql/data/content-attributes.sql b/src/database/sql/data/content-attributes.sql index 029f035e..301fa456 100644 --- a/src/database/sql/data/content-attributes.sql +++ b/src/database/sql/data/content-attributes.sql @@ -14,6 +14,17 @@ INSERT INTO contiguous_data ( ) ON CONFLICT DO NOTHING -- insertDataId +WITH ParentStatus AS ( + SELECT CASE + WHEN EXISTS ( + SELECT 1 + FROM contiguous_data_ids AS parent + WHERE parent.id = :parent_id + AND parent.verified = 1 + ) THEN 1 + ELSE :verified + END AS verified_status +) INSERT OR REPLACE INTO contiguous_data_ids ( id, contiguous_data_hash, @@ -24,9 +35,13 @@ INSERT OR REPLACE INTO contiguous_data_ids ( SELECT :id, :contiguous_data_hash, - :verified, + verified_status, :indexed_at, - :verified_at + CASE + WHEN verified_status = 1 THEN :verified_at + ELSE NULL + END +FROM ParentStatus WHERE NOT EXISTS ( SELECT 1 @@ -34,25 +49,6 @@ WHERE WHERE id = :id AND verified = 1 ); --- updateVerifiedDataId -UPDATE contiguous_data_ids -SET - verified = CASE - WHEN EXISTS ( - SELECT 1 - FROM contiguous_data_ids AS parent - WHERE parent.id = :parent_id - AND parent.verified = TRUE - ) THEN 1 - ELSE 0 - END, - verified_at = CASE - WHEN verified = 1 THEN :verified_at - ELSE NULL - END -WHERE - id = :id; - -- insertDataRoot INSERT OR REPLACE INTO data_roots ( data_root, diff --git a/src/database/standalone-sqlite.ts b/src/database/standalone-sqlite.ts index b1a614c1..2eae4f1b 100644 --- a/src/database/standalone-sqlite.ts +++ b/src/database/standalone-sqlite.ts @@ -1135,6 +1135,7 @@ export class StandaloneSqliteDatabaseWorker { saveDataContentAttributes({ id, + parentId, dataRoot, hash, dataSize, @@ -1143,6 +1144,7 @@ export class StandaloneSqliteDatabaseWorker { verified, }: { id: string; + parentId?: string; dataRoot?: string; hash: string; dataSize: number; @@ -1153,14 +1155,14 @@ export class StandaloneSqliteDatabaseWorker { const hashBuffer = fromB64Url(hash); const currentTimestamp = currentUnixTimestamp(); const isVerified = verified ? 1 : 0; - const verifiedAt = verified ? currentTimestamp : null; this.stmts.data.insertDataId.run({ id: fromB64Url(id), + parent_id: parentId ? fromB64Url(parentId) : null, contiguous_data_hash: hashBuffer, indexed_at: currentTimestamp, verified: isVerified, - verified_at: verifiedAt, + verified_at: currentTimestamp, }); if (dataRoot !== undefined) { @@ -1169,7 +1171,7 @@ export class StandaloneSqliteDatabaseWorker { contiguous_data_hash: hashBuffer, indexed_at: currentTimestamp, verified: isVerified, - verified_at: verifiedAt, + verified_at: currentTimestamp, }); } @@ -2339,22 +2341,12 @@ export class StandaloneSqliteDatabaseWorker { dataOffset: number; dataSize: number; }) { - const currentTimestamp = currentUnixTimestamp(); - const idBuffer = fromB64Url(id); - const parentIdBuffer = fromB64Url(parentId); - this.stmts.data.insertNestedDataId.run({ - id: idBuffer, - parent_id: parentIdBuffer, + id: fromB64Url(id), + parent_id: fromB64Url(parentId), data_offset: dataOffset, data_size: dataSize, - indexed_at: currentTimestamp, - }); - - this.stmts.data.updateVerifiedDataId.run({ - id: idBuffer, - parent_id: parentIdBuffer, - verified_at: currentTimestamp, + indexed_at: currentUnixTimestamp(), }); } @@ -2843,6 +2835,7 @@ export class StandaloneSqliteDatabase saveDataContentAttributes({ id, + parentId, dataRoot, hash, dataSize, @@ -2850,6 +2843,7 @@ export class StandaloneSqliteDatabase verified, }: { id: string; + parentId?: string; dataRoot?: string; hash: string; dataSize: number; @@ -2868,6 +2862,7 @@ export class StandaloneSqliteDatabase return this.queueWrite('data', 'saveDataContentAttributes', [ { id, + parentId, dataRoot, hash, dataSize, diff --git a/src/types.d.ts b/src/types.d.ts index c14a41b8..787b5145 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -540,6 +540,7 @@ export interface ContiguousDataIndex { getDataParent(id: string): Promise; saveDataContentAttributes({ id, + parentId, dataRoot, hash, dataSize, @@ -548,6 +549,7 @@ export interface ContiguousDataIndex { verified, }: { id: string; + parentId?: string; dataRoot?: string; hash: string; dataSize: number; diff --git a/src/workers/ans104-data-indexer.ts b/src/workers/ans104-data-indexer.ts index c7333f2c..093cc8ae 100644 --- a/src/workers/ans104-data-indexer.ts +++ b/src/workers/ans104-data-indexer.ts @@ -101,6 +101,7 @@ export class Ans104DataIndexer { if (item.data_hash != null) { await this.contiguousDataIndex.saveDataContentAttributes({ id: item.id, + parentId: item.parent_id, hash: item.data_hash, dataSize: item.data_size, contentType: item.content_type,