Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up truncate #369

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,16 @@ module.exports = class Hyperdrive extends ReadyResource {
}

async truncate (version, { blobs = -1 } = {}) {
if (!this.opened) await this.ready()

if (version > this.core.length) {
throw BAD_ARGUMENT('Bad truncation length')
}

const blobsVersion = blobs === -1 ? await this.getBlobsLength(version) : blobs
const bl = await this.getBlobs()

if (version > this.core.length || blobsVersion > bl.core.length) {
if (blobsVersion > bl.core.length) {
throw BAD_ARGUMENT('Bad truncation length')
}

Expand All @@ -107,7 +113,8 @@ module.exports = class Hyperdrive extends ReadyResource {
}

async getBlobsLength (checkout) {
await this.ready()
if (!this.opened) await this.ready()

if (!checkout) checkout = this.version

const c = this.db.checkout(checkout)
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,18 @@ test('truncate happy path', async t => {
t.is(drive.blobs.core.fork, 1, 'sanity check on blobs fork')
})

test('truncate throws when truncating future version)', async t => {
const corestore = new Corestore(RAM.reusable())
const drive = new Hyperdrive(corestore)

await drive.put('./file', 'here')
await t.exception(
() => drive.truncate(10),
/Bad truncation length/,
'throws when truncating the future'
)
})

async function testenv (teardown) {
const corestore = new Corestore(RAM)
await corestore.ready()
Expand Down
Loading