Skip to content

Commit

Permalink
Reverse order of blocks from sync.getRepo (#1665)
Browse files Browse the repository at this point in the history
* reverse order of blocks from sync.getRepo

* write to car while fetching next page
  • Loading branch information
dholms authored Sep 25, 2023
1 parent caef7c1 commit 11bf4d3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions packages/pds/src/sql-repo-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,22 @@ export class SqlRepoStorage extends ReadableBlockstore implements RepoStorage {
}
return writeCarStream(root, async (car) => {
let cursor: RevCursor | undefined = undefined
const writeRows = async (
rows: { cid: string; content: Uint8Array }[],
) => {
for (const row of rows) {
await car.put({
cid: CID.parse(row.cid),
bytes: row.content,
})
}
}
// allow us to write to car while fetching the next page
let writePromise: Promise<void> = Promise.resolve()
do {
const res = await this.getBlockRange(since, cursor)
await writePromise
writePromise = writeRows(res)
for (const row of res) {
await car.put({
cid: CID.parse(row.cid),
Expand All @@ -231,6 +245,8 @@ export class SqlRepoStorage extends ReadableBlockstore implements RepoStorage {
cursor = undefined
}
} while (cursor)
// ensure we flush the last page of blocks
await writePromise
})
}

Expand All @@ -240,17 +256,18 @@ export class SqlRepoStorage extends ReadableBlockstore implements RepoStorage {
.selectFrom('ipld_block')
.where('creator', '=', this.did)
.select(['cid', 'repoRev', 'content'])
.orderBy('repoRev', 'asc')
.orderBy('cid', 'asc')
.orderBy('repoRev', 'desc')
.orderBy('cid', 'desc')
.limit(500)
if (cursor) {
// use this syntax to ensure we hit the index
builder = builder.where(
sql`((${ref('repoRev')}, ${ref('cid')}) > (${
sql`((${ref('repoRev')}, ${ref('cid')}) < (${
cursor.rev
}, ${cursor.cid.toString()}))`,
)
} else if (since) {
}
if (since) {
builder = builder.where('repoRev', '>', since)
}
return builder.execute()
Expand Down

0 comments on commit 11bf4d3

Please sign in to comment.