let [ptr, len, value] = this.value
if (value !== undefined) {
assert(value instanceof Lazy)
- if (await value.flush()) {
- let [ptr, len] = await this.database.write(value.pack())
-
- this.value[0] = ptr
- this.value[1] = len
+ if (await value.commit())
this.dirty = true
- }
+ this.value = value.ptr_len
}
}
if (this.dirty) {
- let value = this.value
- if (value instanceof Array)
- value = value.slice(0, 2)
- await this.database.write_root(value)
+ await this.database.write_root(this.value)
this.database.value = value
}
throw new Error('not implemented')
}
- async flush() {
- throw new Error('not implemented')
- }
-
- pack() {
+ async commit() {
throw new Error('not implemented')
}
}
delete(key) {
delete this.array[key]
- this.length = this.array.length
}
push(value) {
async pop() {
assert(this.length)
- let i = this.length - 1
- let value = await this.get(i)
- this.delete(i)
+ this.length -= 1
+ let value = await this.get(this.length)
+ this.delete(this.length)
return value
}
- async flush() {
+ async commit() {
for (let i = 0; i < this.length; ++i) {
let item = this.array[i]
if (item instanceof Array) {
let [ptr, len, value] = item
if (value !== undefined) {
assert(value instanceof Lazy)
- if (await value.flush()) {
- let [ptr, len] = await this.transaction.database.write(value.pack())
-
- item[0] = ptr
- item[1] = len
+ if (await value.commit())
this.ptr_len = null // mark dirty
- }
+ this.array[i] = value.ptr_len
}
}
}
- return this.ptr_len === null
- }
- pack() {
- let new_array = []
- for (let i = 0; i < this.length; ++i) {
- let item = this.array[i]
- if (item instanceof Array)
- item = item.slice(0, 2)
- new_array.push(item)
+ if (this.ptr_len === null) {
+ this.ptr_len = await this.transaction.database.write(value)
+ return true
}
- return new_array
+ return false
}
}
return keys
}
- async flush() {
+ async commit() {
for (let i in this.object) {
let item = this.object[i]
if (item instanceof Array) {
let [ptr, len, value] = item
if (value !== undefined) {
assert(value instanceof Lazy)
- if (await value.flush()) {
- let [ptr, len] = await this.transaction.database.write(value.pack())
-
- item[0] = ptr
- item[1] = len
+ if (await value.commit())
this.ptr_len = null // mark dirty
- }
+ this.object[i] = value.ptr_len
}
}
}
- return this.ptr_len === null
- }
- pack() {
- let new_object = {}
- for (let i in this.object) {
- let item = this.object[i]
- if (item instanceof Array)
- item = item.slice(0, 2)
- new_object[i] = item
+ if (this.ptr_len === null) {
+ this.ptr_len = await this.transaction.database.write(value)
+ return true
}
- return new_object
+ return false
}
}