Skip to content

Commit

Permalink
feat: use insertDataId to inherit verified state from parent
Browse files Browse the repository at this point in the history
  • Loading branch information
karlprieb authored and djwhitt committed Oct 15, 2024
1 parent c7422ff commit 5184d8d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
38 changes: 17 additions & 21 deletions src/database/sql/data/content-attributes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,35 +35,20 @@ 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
FROM contiguous_data_ids
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,
Expand Down
27 changes: 11 additions & 16 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ export class StandaloneSqliteDatabaseWorker {

saveDataContentAttributes({
id,
parentId,
dataRoot,
hash,
dataSize,
Expand All @@ -1143,6 +1144,7 @@ export class StandaloneSqliteDatabaseWorker {
verified,
}: {
id: string;
parentId?: string;
dataRoot?: string;
hash: string;
dataSize: number;
Expand All @@ -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) {
Expand All @@ -1169,7 +1171,7 @@ export class StandaloneSqliteDatabaseWorker {
contiguous_data_hash: hashBuffer,
indexed_at: currentTimestamp,
verified: isVerified,
verified_at: verifiedAt,
verified_at: currentTimestamp,
});
}

Expand Down Expand Up @@ -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(),
});
}

Expand Down Expand Up @@ -2843,13 +2835,15 @@ export class StandaloneSqliteDatabase

saveDataContentAttributes({
id,
parentId,
dataRoot,
hash,
dataSize,
contentType,
verified,
}: {
id: string;
parentId?: string;
dataRoot?: string;
hash: string;
dataSize: number;
Expand All @@ -2868,6 +2862,7 @@ export class StandaloneSqliteDatabase
return this.queueWrite('data', 'saveDataContentAttributes', [
{
id,
parentId,
dataRoot,
hash,
dataSize,
Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ export interface ContiguousDataIndex {
getDataParent(id: string): Promise<ContiguousDataParent | undefined>;
saveDataContentAttributes({
id,
parentId,
dataRoot,
hash,
dataSize,
Expand All @@ -548,6 +549,7 @@ export interface ContiguousDataIndex {
verified,
}: {
id: string;
parentId?: string;
dataRoot?: string;
hash: string;
dataSize: number;
Expand Down
1 change: 1 addition & 0 deletions src/workers/ans104-data-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5184d8d

Please sign in to comment.