Skip to content

Commit

Permalink
Write header instead of footer.
Browse files Browse the repository at this point in the history
Closes #443.
  • Loading branch information
flatheadmill committed Feb 4, 2015
1 parent 50b7c7e commit 1822d5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
66 changes: 22 additions & 44 deletions strata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,24 +1224,19 @@ prototype(Sheaf, 'fillRoot', cadence(function (async) {
}))

function Sheaf (options) {
var writeFooter = function (out, position, page, callback) {
this.writeFooter(out, position, page, callback)
}.bind(this)
this.fs = options.fs || require('fs')
this.nextAddress = 0
this.directory = options.directory
this.journal = {
branch: new Journalist({ stage: 'entry' }).createJournal(),
leaf: new Journalist({
stage: 'entry',
closer: writeFooter
stage: 'entry'
}).createJournal()
}
this.journalist = new Journalist({
count: options.fileHandleCount || 64,
stage: options.writeStage || 'entry',
cache: options.jouralistCache || (new Cache),
closer: writeFooter
cache: options.jouralistCache || new Cache()
})
this.cache = options.cache || (new Cache)
this.options = options
Expand Down Expand Up @@ -1276,24 +1271,6 @@ function Sheaf (options) {
this.lengths = {}
}

prototype(Sheaf, 'writeFooter', cadence(function (async, out, position, page) {
var header = [
0, page.right || 0, page.position, page.entries, page.ghosts,
page.items.length - page.ghosts
]
async(function () {
this.writeEntry({
out: out,
page: page,
header: header,
type: 'footer'
}, async())
}, function (position, length) {
page.position = position // todo: can't we use `position`?
return [ position, length ]
})
}))

Sheaf.prototype.readEntry = function (buffer, isKey) {
for (var count = 2, i = 0, I = buffer.length; i < I && count; i++) {
if (buffer[i] == 0x20) count--
Expand Down Expand Up @@ -1425,6 +1402,11 @@ prototype(Sheaf, 'writeDelete', function (out, page, index, callback) {
this.writeEntry({ out: out, page: page, header: header, type: 'delete' }, callback)
})

prototype(Sheaf, 'writeHeader', function (out, page, callback) {
var header = [ 0, ++page.entries, page.right ]
this.writeEntry({ out: out, page: page, header: header, type: 'header' }, callback)
})

prototype(Sheaf, 'io', cadence(function (async, direction, filename) {
async(function () {
this.fs.open(filename, direction[0], async())
Expand Down Expand Up @@ -1499,6 +1481,7 @@ prototype(Sheaf, 'replay', cadence(function (async, fd, stat, read, page, positi
async([function () {
this.fs.close(fd, async())
}], function () {
page.position = 0
var loop = async(function (buffer, position) {
read(buffer, position, async())
}, function (slice, start) {
Expand All @@ -1513,9 +1496,10 @@ prototype(Sheaf, 'replay', cadence(function (async, fd, stat, read, page, positi
}
var position = start + offset
ok(length)
page.position += length
var entry = this.readEntry(slice.slice(offset, offset + length), !leaf)
var header = this.readHeader(entry)
if (header.entry) {
if (entry.header[0]) {
ok(header.entry == ++page.entries, 'entry count is off')
var index = header.index
if (leaf) {
Expand All @@ -1533,26 +1517,18 @@ prototype(Sheaf, 'replay', cadence(function (async, fd, stat, read, page, positi
this.splice(page, -(index + 1), 1)
}
} else {
/* if (index > 0) { */
var address = header.address, key = null, heft = 0
if (index - 1) {
key = entry.body
heft = length
}
this.splice(page, index - 1, 0, {
key: key, address: address, heft: heft
})
/* } else {
var cut = splice('addresses', page, ~index, 1)
if (~index) {
uncacheEntry(page, cut[0])
}
} */
var address = header.address, key = null, heft = 0
if (index - 1) {
key = entry.body
heft = length
}
this.splice(page, index - 1, 0, {
key: key, address: address, heft: heft
})
}
} else {
footer = this.readFooter(entry)
page.position = position
page.right = footer.right
page.right = entry.header[2]
page.entries++
}
i = offset = offset + length
}
Expand Down Expand Up @@ -1590,6 +1566,8 @@ prototype(Sheaf, 'rewriteLeaf', cadence(function (async, page, suffix) {
var items = this.splice(page, 0, page.items.length)

async(function () {
this.writeHeader(out, page, async())
}, function () {
async(function (item) {
async(function () {
this.writeInsert(out, page, index++, item.record, async())
Expand Down
14 changes: 7 additions & 7 deletions t/proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function abstracted (dir, lengths) {
position = 0
dir[file].forEach(function (line, index) {
var json = line.header
console.log(json)
if (json[0]) {
ok(index + 1 == json[0], 'entry record is wrong')
var length = lengths[file][index]
Expand All @@ -166,8 +167,9 @@ function abstracted (dir, lengths) {
}
position += length
} else {
ok(index == dir[file].length - 1, 'footer not last entry')
if (json[1]) record.right = Math.abs(json[1])
ok(index == 0, 'header not first entry')
ok(json[1] == 1, 'header not first entry')
if (json[2]) record.right = Math.abs(json[2])
}
})
} else {
Expand Down Expand Up @@ -287,11 +289,11 @@ function directivize (json) {
}
order.splice(index, 0, entry.value)
positions.splice(index, 0, position)
record = { header: [ count + 1, index + 1 ], body: entry.value }
record = { header: [ count + 2, index + 1 ], body: entry.value }
break
case 'del':
records--
record = { header: [ count + 1, -(entry.index + 1) ] }
record = { header: [ count + 2, -(entry.index + 1) ] }
break
}
var length = JSON.stringify(record.header).length + 1 + checksum + 1
Expand All @@ -308,9 +310,7 @@ function directivize (json) {
position += length
return record
})
directory[address].push({ header: [
0, object.right || 0, position, directory[address].length, ghosts, records
]})
directory[address].unshift({ header: [ 0, 1, object.right || 0 ]})
}
}

Expand Down

0 comments on commit 1822d5a

Please sign in to comment.