Make LazyXXX an inner object of Database, so that LazyXXX.database is set at construc...
authorNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 03:21:08 +0000 (14:21 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 03:21:08 +0000 (14:21 +1100)
a.mjs
logjson.mjs

diff --git a/a.mjs b/a.mjs
index bcc3cc4..8855713 100755 (executable)
--- a/a.mjs
+++ b/a.mjs
@@ -6,7 +6,7 @@ import fsPromises from 'fs/promises'
 let database = new logjson.Database()
 await database.open('a.logjson')
 database.set(
-  logjson.json_to_logjson(
+  database.json_to_logjson(
     JSON.parse(await fsPromises.readFile('random.json', 'utf-8'))
   )
 )
index 620b5ec..f51049e 100644 (file)
@@ -162,14 +162,16 @@ class Database {
   }
 
   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
   }
 
@@ -183,10 +185,7 @@ class Database {
 
     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(
@@ -223,12 +222,38 @@ class Database {
     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
   }
 
@@ -248,7 +273,7 @@ class Lazy {
     throw new Error('not implemented')
   }
 
-  async flush(database) {
+  async flush() {
     throw new Error('not implemented')
   }
 
@@ -300,14 +325,16 @@ class LazyArray extends Lazy {
 
   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
   }
@@ -329,21 +356,12 @@ class LazyArray extends Lazy {
     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(
@@ -424,14 +442,16 @@ class LazyObject extends Lazy {
 
   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
   }
 
@@ -446,21 +466,12 @@ class LazyObject extends Lazy {
     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(
@@ -520,29 +531,10 @@ let logjson_to_json = async value => {
   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
 }