}
set(value) {
- assert(
- typeof value !== 'object' ||
- value === null ||
- value instanceof Lazy
- )
+ //assert(
+ // typeof value !== 'object' ||
+ // value === null ||
+ // (value instanceof Lazy && value.database === this)
+ //)
this.dirty = true
- if (typeof value === 'object' && value !== null)
+ if (typeof value === 'object' && value !== null) {
+ assert(value instanceof Lazy && value.database === this)
value = [-1, 0, value]
+ }
this.value = value
}
if (this.value instanceof Array) {
let [ptr, len, value] = this.value
- if (
- (value instanceof Lazy && await value.flush(this)) ||
- ptr === -1
- ) {
+ if ((value instanceof Lazy && await value.flush()) || ptr === -1) {
if (value instanceof Lazy)
value = value.pack()
let buffer = Buffer.from(
this.dirty = false
this.mutex.release()
}
+
+ LazyArray(...args) {
+ return new LazyArray(this, ...args)
+ }
+
+ LazyObject(...args) {
+ return new LazyObject(this, ...args)
+ }
+
+ json_to_logjson(value) {
+ if (typeof value === 'object' && value !== null) {
+ let new_value
+ if (value instanceof Array) {
+ new_value = new LazyArray(this)
+ for (let i = 0; i < value.length; ++i)
+ new_value.push(this.json_to_logjson(value[i]))
+ }
+ else {
+ new_value = new LazyObject(this)
+ for (let i in value)
+ new_value.set(i, this.json_to_logjson(value[i]))
+ }
+ value = new_value
+ }
+ return value
+ }
}
// logjson array or object
class Lazy {
constructor(database) {
- this.database = database || null
+ this.database = database
this.dirty = false
}
throw new Error('not implemented')
}
- async flush(database) {
+ async flush() {
throw new Error('not implemented')
}
set(key, value) {
assert(typeof key === 'number')
- assert(
- typeof value !== 'object' ||
- value === null ||
- value instanceof Lazy
- )
+ //assert(
+ // typeof value !== 'object' ||
+ // value === null ||
+ // (value instanceof Lazy && value.database === this.database)
+ //)
this.dirty = true
- if (typeof value === 'object' && value !== null)
+ if (typeof value === 'object' && value !== null) {
+ assert(value instanceof Lazy && value.database === this.database)
value = [-1, 0, value]
+ }
this.array[key] = value
this.length = this.array.length
}
return value
}
- async flush(database) {
- if (database != this.database) {
- assert(this.database === null)
- this.database = database
- this.dirty = true
- }
-
+ async flush() {
for (let i = 0; i < this.length; ++i) {
let item = this.array[i]
if (item instanceof Array) {
let [ptr, len, value] = item
- if (
- (value instanceof Lazy && await value.flush(database)) ||
- ptr === -1
- ) {
+ if ((value instanceof Lazy && await value.flush()) || ptr === -1) {
if (value instanceof Lazy)
value = value.pack()
let buffer = Buffer.from(
set(key, value) {
assert(typeof key === 'string')
- assert(
- typeof value !== 'object' ||
- value === null ||
- value instanceof Lazy
- )
+ //assert(
+ // typeof value !== 'object' ||
+ // value === null ||
+ // (value instanceof Lazy && value.database === this.database)
+ //)
this.dirty = true
- if (typeof value === 'object' && value !== null)
+ if (typeof value === 'object' && value !== null) {
+ assert(value instanceof Lazy && value.database === this.database)
value = [-1, 0, value]
+ }
this.object[key] = value
}
return keys
}
- async flush(database) {
- if (database != this.database) {
- assert(this.database === null)
- this.database = database
- this.dirty = true
- }
-
+ async flush() {
for (let i in this.object) {
let item = this.object[i]
if (item instanceof Array) {
let [ptr, len, value] = item
- if (
- (value instanceof Lazy && await value.flush(database)) ||
- ptr === -1
- ) {
+ if ((value instanceof Lazy && await value.flush()) || ptr === -1) {
if (value instanceof Lazy)
value = value.pack()
let buffer = Buffer.from(
return value
}
-let json_to_logjson = value => {
- if (typeof value === 'object' && value !== null) {
- let new_value
- if (value instanceof Array) {
- new_value = new LazyArray()
- for (let i = 0; i < value.length; ++i)
- new_value.push(json_to_logjson(value[i]))
- }
- else {
- new_value = new LazyObject()
- for (let i in value)
- new_value.set(i, json_to_logjson(value[i]))
- }
- value = new_value
- }
- return value
-}
-
export default {
Database,
Lazy,
LazyArray,
LazyObject,
- logjson_to_json,
- json_to_logjson
+ logjson_to_json
}