Skip to content

Commit

Permalink
Sketch of vacuum.
Browse files Browse the repository at this point in the history
See #20.
  • Loading branch information
flatheadmill committed Mar 30, 2020
1 parent 5a07001 commit 6ec83c9
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 5 deletions.
52 changes: 52 additions & 0 deletions commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Commit {
this._commit = path.join(journalist.directory, 'commit')
}

// TODO Should be a hash of specific files to filter, not a regex.
async write (commit) {
const dir = await this._readdir()
const unemplaced = dir.filter(file => ! /\d+\.\d+-\d+\.\d+\.[0-9a-f]/)
Expand Down Expand Up @@ -126,6 +127,7 @@ class Commit {
const buffer = Buffer.from(JSON.stringify(entry.value.items))
const hash = fnv(buffer)
entry.heft = buffer.length
// TODO `this._path()`
await fs.writeFile(path.join(this._commit, `${entry.value.id}-${hash}`), buffer)
const from = path.join('commit', `${entry.value.id}-${hash}`)
const to = path.join('pages', entry.value.id, hash)
Expand All @@ -136,6 +138,51 @@ class Commit {
}
}

async _vacuum (id, first, second, items) {
await fs.mkdir(this._commit, { recursive: true })
const filename = this._path(`${id}-${first}`)
const recorder = this._journalist._recorder
// Write out a new page slowly, a record at a time.
for (let index = 0, I = items.length; index < I; index++) {
const { key, body } = items[index]
await fs.appendFile(filename, recorder({ method: 'insert', index, key }, body))
}
await fs.appendFile(filename, recorder({
method: 'dependent', id: id, append: second
}))
return {
method: 'rename',
from: path.join('commit', `${id}-${first}`),
to: path.join('pages', id, first),
hash: hash
}
}

async vacuum (id, first, second, items, right) {
await fs.mkdir(this._commit, { recursive: true })
const filename = this._path(`${id}-${first}`)
const recorder = this._journalist._recorder
const buffers = []
buffers.push(recorder({ method: 'right', right }))
// Write out a new page slowly, a record at a time.
for (let index = 0, I = items.length; index < I; index++) {
const { key, value } = items[index]
buffers.push(recorder({ method: 'insert', index, key }, value))
}
buffers.push(recorder({
method: 'dependent', id: id, append: second
}))
const buffer = Buffer.concat(buffers)
const hash = fnv(buffer)
await fs.writeFile(filename, buffer)
return {
method: 'rename',
from: path.join('commit', `${id}-${first}`),
to: path.join('pages', id, first),
hash: hash
}
}

// Okay. Now I see. I wanted the commit to be light and easy and minimal, so
// that it could be written quickly and loaded quickly, but that is only
// necessary for the leaf. We really want a `Prepare` that will write files
Expand Down Expand Up @@ -186,6 +233,11 @@ class Commit {
await this._prepare([ 'rename', from, to, hash ])
}
break
case 'rename': {
const { from, to, hash } = operation
await this._prepare([ 'rename', from, to, hash ])
}
break
case 'emplace': {
const { page, hash } = operation
const from = path.join('commit', `${page.id}-${hash}`)
Expand Down
Loading

0 comments on commit 6ec83c9

Please sign in to comment.