let open_angle = Buffer.from('\n<', 'utf-8')
let close_angle = Buffer.from('>\n', 'utf-8')
-class File {
+class Database {
constructor() {
this.mutex = new Mutex()
this.log = null
return
}
- // optional: trim any garbage off the end of the file
+ // optional: trim any garbage off the end of the database file
let [ptr, len] = this.value
let eof = ptr + len + 2
if (eof < this.eof) {
)
}
- // special case for < at start of file
+ // special case for < at start of database
if (count && buffer[0] === 0x3c) {
let start = 1
return [start, end - start] // ptr, len
// logjson array or object
class Lazy {
- constructor(file) {
- this.file = file || null
+ constructor(database) {
+ this.database = database || null
this.dirty = false
}
throw new Error('not implemented')
}
- async flush(file) {
+ async flush(database) {
throw new Error('not implemented')
}
}
class LazyArray extends Lazy {
- constructor(file, array) {
- super(file)
+ constructor(database, array) {
+ super(database)
this.array = array || []
this.length = this.array.length
}
if (new_value === undefined) {
let buffer = Buffer.alloc(len)
assert(
- (await this.file.log.read(buffer, 0, len, ptr)).bytesRead === len
+ (await this.database.log.read(buffer, 0, len, ptr)).bytesRead === len
)
new_value = JSON.parse(buffer.toString('utf-8'))
if (typeof new_value === 'object' && new_value !== null)
new_value =
new_value instanceof Array ?
- new LazyArray(this.file, new_value) :
- new LazyObject(this.file, new_value)
+ new LazyArray(this.database, new_value) :
+ new LazyObject(this.database, new_value)
value[2] = new_value
}
value = new_value
return value
}
- async flush(file) {
- if (file != this.file) {
- assert(this.file === null)
- this.file = file
+ async flush(database) {
+ if (database != this.database) {
+ assert(this.database === null)
+ this.database = database
this.dirty = true
}
if (item instanceof Array) {
let [ptr, len, value] = item
if (
- (value instanceof Lazy && await value.flush(file)) ||
+ (value instanceof Lazy && await value.flush(database)) ||
ptr === -1
) {
if (value instanceof Lazy)
'utf-8'
)
- ptr = this.file.eof
+ ptr = this.database.eof
len = buffer.length
assert(
- (await this.file.log.write(buffer, 0, len, ptr)).bytesWritten ===
+ (await this.database.log.write(buffer, 0, len, ptr)).bytesWritten ===
len
)
- this.file.eof += len
+ this.database.eof += len
item[0] = ptr
item[1] = len - 1
}
class LazyObject extends Lazy {
- constructor(file, object) {
- super(file)
+ constructor(database, object) {
+ super(database)
this.object = object || {}
}
if (new_value === undefined) {
let buffer = Buffer.alloc(len)
assert(
- (await this.file.log.read(buffer, 0, len, ptr)).bytesRead === len
+ (await this.database.log.read(buffer, 0, len, ptr)).bytesRead === len
)
new_value = JSON.parse(buffer.toString('utf-8'))
if (typeof new_value === 'object' && new_value !== null)
new_value =
new_value instanceof Array ?
- new LazyArray(this.file, new_value) :
- new LazyObject(this.file, new_value)
+ new LazyArray(this.database, new_value) :
+ new LazyObject(this.database, new_value)
value[2] = new_value
}
value = new_value
return keys
}
- async flush(file) {
- if (file != this.file) {
- assert(this.file === null)
- this.file = file
+ async flush(database) {
+ if (database != this.database) {
+ assert(this.database === null)
+ this.database = database
this.dirty = true
}
if (item instanceof Array) {
let [ptr, len, value] = item
if (
- (value instanceof Lazy && await value.flush(file)) ||
+ (value instanceof Lazy && await value.flush(database)) ||
ptr === -1
) {
if (value instanceof Lazy)
'utf-8'
)
- ptr = this.file.eof
+ ptr = this.database.eof
len = buffer.length
assert(
- (await this.file.log.write(buffer, 0, len, ptr)).bytesWritten ===
+ (await this.database.log.write(buffer, 0, len, ptr)).bytesWritten ===
len
)
- this.file.eof += len
+ this.database.eof += len
item[0] = ptr
item[1] = len
}
export default {
- File,
+ Database,
Lazy,
LazyArray,
LazyObject,