mirror of https://codeberg.org/pzp/pzp-db.git
log: remove some unnecessary methods
This commit is contained in:
parent
b6d17e947f
commit
4302094926
|
@ -579,7 +579,7 @@ function AsyncAppendOnlyLog(filename, opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {extractCodecType<typeof codec> | Array<extractCodecType<typeof codec>>} data
|
* @param {extractCodecType<typeof codec>} data
|
||||||
* @param {CB<number>} cb
|
* @param {CB<number>} cb
|
||||||
*/
|
*/
|
||||||
function append(data, cb) {
|
function append(data, cb) {
|
||||||
|
@ -588,69 +588,13 @@ function AsyncAppendOnlyLog(filename, opts) {
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (Array.isArray(data)) {
|
let offset
|
||||||
let offset = 0
|
try {
|
||||||
for (let i = 0, length = data.length; i < length; ++i)
|
offset = appendSingle(data)
|
||||||
offset = appendSingle(data[i])
|
} catch (err) {
|
||||||
|
return cb(/** @type {any} */ (err))
|
||||||
cb(null, offset)
|
|
||||||
} else cb(null, appendSingle(data))
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Array<any>} dataArray
|
|
||||||
* @param {CB<Array<number>>} cb
|
|
||||||
*/
|
|
||||||
function appendTransaction(dataArray, cb) {
|
|
||||||
if (!Array.isArray(dataArray)) {
|
|
||||||
return cb(appendTransactionWantsArrayErr())
|
|
||||||
}
|
}
|
||||||
// if (compaction) { // FIXME:
|
cb(null, offset)
|
||||||
// waitingCompaction.push(() => appendTransaction(dataArray, cb))
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
let size = 0
|
|
||||||
const encodedDataArray = dataArray.map((data) => {
|
|
||||||
let encodedData = codec.encode(data)
|
|
||||||
if (typeof encodedData === 'string') encodedData = b4a.from(encodedData)
|
|
||||||
size += Record.size(encodedData)
|
|
||||||
return encodedData
|
|
||||||
})
|
|
||||||
|
|
||||||
size += EOB.SIZE
|
|
||||||
|
|
||||||
if (size > blockSize) return cb(appendLargerThanBlockErr())
|
|
||||||
|
|
||||||
assert(typeof nextOffsetInBlock === 'number', 'nextOffsetInBlock not set')
|
|
||||||
assert(typeof latestBlockIndex === 'number', 'latestBlockIndex not set')
|
|
||||||
if (nextOffsetInBlock + size > blockSize) {
|
|
||||||
// doesn't fit
|
|
||||||
const nextBlockBuf = b4a.alloc(blockSize)
|
|
||||||
latestBlockBuf = nextBlockBuf
|
|
||||||
latestBlockIndex += 1
|
|
||||||
nextOffsetInBlock = 0
|
|
||||||
debug("data doesn't fit current block, creating new")
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(latestBlockBuf, 'latestBlockBuf not set')
|
|
||||||
const offsets = /** @type {Array<number>} */ ([])
|
|
||||||
for (const encodedData of encodedDataArray) {
|
|
||||||
Record.write(latestBlockBuf, nextOffsetInBlock, encodedData)
|
|
||||||
cache.set(latestBlockIndex, latestBlockBuf) // update cache
|
|
||||||
const offset = latestBlockIndex * blockSize + nextOffsetInBlock
|
|
||||||
offsets.push(offset)
|
|
||||||
blocksToBeWritten.set(latestBlockIndex, {
|
|
||||||
blockBuf: latestBlockBuf,
|
|
||||||
offset,
|
|
||||||
})
|
|
||||||
nextOffsetInBlock += Record.size(encodedData)
|
|
||||||
debug('data inserted at offset %d', offset)
|
|
||||||
}
|
|
||||||
|
|
||||||
scheduleWrite()
|
|
||||||
|
|
||||||
return cb(null, offsets)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const scheduleWrite = debounce(write, writeTimeout)
|
const scheduleWrite = debounce(write, writeTimeout)
|
||||||
|
@ -873,11 +817,9 @@ function AsyncAppendOnlyLog(filename, opts) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// Public API:
|
// Public API:
|
||||||
get: onLoad(get),
|
scan: onLoad(scan), // TODO
|
||||||
scan: onLoad(scan),
|
|
||||||
del: onLoad(del), // TODO
|
del: onLoad(del), // TODO
|
||||||
append: onLoad(append), // TODO
|
append: onLoad(append), // TODO
|
||||||
appendTransaction: onLoad(appendTransaction),
|
|
||||||
close: onLoad(close), // TODO
|
close: onLoad(close), // TODO
|
||||||
onDrain: onLoad(onDrain), // TODO
|
onDrain: onLoad(onDrain), // TODO
|
||||||
onDeletesFlushed: onLoad(onDeletesFlushed),
|
onDeletesFlushed: onLoad(onDeletesFlushed),
|
||||||
|
@ -892,6 +834,9 @@ function AsyncAppendOnlyLog(filename, opts) {
|
||||||
overwrite,
|
overwrite,
|
||||||
truncate,
|
truncate,
|
||||||
hasNoSpaceFor,
|
hasNoSpaceFor,
|
||||||
|
|
||||||
|
// Useful for tests
|
||||||
|
_get: onLoad(get),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,67 +0,0 @@
|
||||||
const test = require('node:test')
|
|
||||||
const assert = require('node:assert')
|
|
||||||
const fs = require('node:fs')
|
|
||||||
const p = require('node:util').promisify
|
|
||||||
const Log = require('../../lib/log')
|
|
||||||
|
|
||||||
test('Log get() handles bad offset NaN', async function (t) {
|
|
||||||
const file = '/tmp/ppppp-db-log-test-bad-offset.log'
|
|
||||||
try {
|
|
||||||
fs.unlinkSync(file)
|
|
||||||
} catch (_) {}
|
|
||||||
const log = Log(file, { blockSize: 2 * 1024 })
|
|
||||||
|
|
||||||
const msg = Buffer.from('testing')
|
|
||||||
|
|
||||||
const offset1 = await p(log.append)(msg)
|
|
||||||
assert.equal(offset1, 0)
|
|
||||||
|
|
||||||
await assert.rejects(p(log.get)(NaN), (err) => {
|
|
||||||
assert.match(err.message, /Offset NaN is not a number/, err.message)
|
|
||||||
assert.equal(err.code, 'ERR_AAOL_INVALID_OFFSET')
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
await p(log.close)()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Log get() handles bad offset -1', async function (t) {
|
|
||||||
const file = '/tmp/ppppp-db-log-test-bad-offset.log'
|
|
||||||
try {
|
|
||||||
fs.unlinkSync(file)
|
|
||||||
} catch (_) {}
|
|
||||||
const log = Log(file, { blockSize: 2 * 1024 })
|
|
||||||
|
|
||||||
const msg = Buffer.from('testing')
|
|
||||||
|
|
||||||
const offset1 = await p(log.append)(msg)
|
|
||||||
assert.equal(offset1, 0)
|
|
||||||
|
|
||||||
await assert.rejects(p(log.get)(-1), (err) => {
|
|
||||||
assert.match(err.message, /Offset -1 is negative/, err.message)
|
|
||||||
assert.equal(err.code, 'ERR_AAOL_INVALID_OFFSET')
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
await p(log.close)()
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Log get() handles bad offset out of bounds', async function (t) {
|
|
||||||
const file = '/tmp/ppppp-db-log-test-bad-offset.log'
|
|
||||||
try {
|
|
||||||
fs.unlinkSync(file)
|
|
||||||
} catch (_) {}
|
|
||||||
const log = Log(file, { blockSize: 2 * 1024 })
|
|
||||||
|
|
||||||
const msg = Buffer.from('testing')
|
|
||||||
|
|
||||||
const offset1 = await p(log.append)(msg)
|
|
||||||
assert.equal(offset1, 0)
|
|
||||||
|
|
||||||
await assert.rejects(p(log.get)(10240), (err) => {
|
|
||||||
assert.match(err.message, /Offset 10240 is beyond log size/, err.message)
|
|
||||||
assert.equal(err.code, 'ERR_AAOL_OFFSET_OUT_OF_BOUNDS')
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
await p(log.close)()
|
|
||||||
})
|
|
|
@ -20,10 +20,10 @@ test('Log handles basic binary records', async function (t) {
|
||||||
const offset2 = await p(log.append)(msg2)
|
const offset2 = await p(log.append)(msg2)
|
||||||
assert.equal(offset2, msg1.length + 2)
|
assert.equal(offset2, msg1.length + 2)
|
||||||
|
|
||||||
const b1 = await p(log.get)(offset1)
|
const b1 = await p(log._get)(offset1)
|
||||||
assert.equal(b1.toString(), msg1.toString())
|
assert.equal(b1.toString(), msg1.toString())
|
||||||
|
|
||||||
const b2 = await p(log.get)(offset2)
|
const b2 = await p(log._get)(offset2)
|
||||||
assert.equal(b2.toString(), msg2.toString())
|
assert.equal(b2.toString(), msg2.toString())
|
||||||
|
|
||||||
await p(log.close)()
|
await p(log.close)()
|
||||||
|
@ -48,10 +48,10 @@ test('Log handles basic json records', async function (t) {
|
||||||
const offset2 = await p(log.append)(json2)
|
const offset2 = await p(log.append)(json2)
|
||||||
assert.equal(offset2, 20)
|
assert.equal(offset2, 20)
|
||||||
|
|
||||||
const rec1 = await p(log.get)(offset1)
|
const rec1 = await p(log._get)(offset1)
|
||||||
assert.deepEqual(rec1, json1)
|
assert.deepEqual(rec1, json1)
|
||||||
|
|
||||||
const rec2 = await p(log.get)(offset2)
|
const rec2 = await p(log._get)(offset2)
|
||||||
assert.deepEqual(rec2, json2)
|
assert.deepEqual(rec2, json2)
|
||||||
|
|
||||||
await p(log.close)()
|
await p(log.close)()
|
||||||
|
@ -67,10 +67,10 @@ test('Log handles basic json record re-reading', async function (t) {
|
||||||
await p(log.onDrain)()
|
await p(log.onDrain)()
|
||||||
assert.equal(log.since.value, 20)
|
assert.equal(log.since.value, 20)
|
||||||
|
|
||||||
const rec1 = await p(log.get)(0)
|
const rec1 = await p(log._get)(0)
|
||||||
assert.deepEqual(rec1, json1)
|
assert.deepEqual(rec1, json1)
|
||||||
|
|
||||||
const rec2 = await p(log.get)(20)
|
const rec2 = await p(log._get)(20)
|
||||||
assert.deepEqual(rec2, json2)
|
assert.deepEqual(rec2, json2)
|
||||||
|
|
||||||
await p(log.close)()
|
await p(log.close)()
|
||||||
|
|
|
@ -28,16 +28,16 @@ test('Log performing simple delete', async (t) => {
|
||||||
const offset3 = await p(log.append)(msg3)
|
const offset3 = await p(log.append)(msg3)
|
||||||
assert.ok(offset3 > offset2)
|
assert.ok(offset3 > offset2)
|
||||||
|
|
||||||
const buf1 = await p(log.get)(offset1)
|
const buf1 = await p(log._get)(offset1)
|
||||||
assert.equal(buf1.toString(), msg1.toString())
|
assert.equal(buf1.toString(), msg1.toString())
|
||||||
const buf2 = await p(log.get)(offset2)
|
const buf2 = await p(log._get)(offset2)
|
||||||
assert.equal(buf2.toString(), msg2.toString())
|
assert.equal(buf2.toString(), msg2.toString())
|
||||||
const buf3 = await p(log.get)(offset3)
|
const buf3 = await p(log._get)(offset3)
|
||||||
assert.equal(buf3.toString(), msg3.toString())
|
assert.equal(buf3.toString(), msg3.toString())
|
||||||
|
|
||||||
await p(log.del)(offset2)
|
await p(log.del)(offset2)
|
||||||
await p(log.onDeletesFlushed)()
|
await p(log.onDeletesFlushed)()
|
||||||
await assert.rejects(p(log.get)(offset2), (err) => {
|
await assert.rejects(p(log._get)(offset2), (err) => {
|
||||||
assert.ok(err)
|
assert.ok(err)
|
||||||
assert.equal(err.message, 'Record has been deleted')
|
assert.equal(err.message, 'Record has been deleted')
|
||||||
assert.equal(err.code, 'ERR_AAOL_DELETED_RECORD')
|
assert.equal(err.code, 'ERR_AAOL_DELETED_RECORD')
|
||||||
|
@ -99,7 +99,7 @@ test('Log deleted records are not invalid upon re-opening', async (t) => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
await assert.rejects(p(log2.get)(offset2), (err) => {
|
await assert.rejects(p(log2._get)(offset2), (err) => {
|
||||||
assert.ok(err)
|
assert.ok(err)
|
||||||
assert.equal(err.message, 'Record has been deleted')
|
assert.equal(err.message, 'Record has been deleted')
|
||||||
assert.equal(err.code, 'ERR_AAOL_DELETED_RECORD')
|
assert.equal(err.code, 'ERR_AAOL_DELETED_RECORD')
|
||||||
|
|
Loading…
Reference in New Issue