Store the [ptr, len] reference in the Lazy(Array|Object) instead of dirty flag (when...
authorNick Downing <nick@ndcode.org>
Thu, 6 Jan 2022 23:17:16 +0000 (10:17 +1100)
committerNick Downing <nick@ndcode.org>
Thu, 6 Jan 2022 23:17:16 +0000 (10:17 +1100)
logjson.mjs

index 6efe4d2..522e27d 100644 (file)
@@ -199,8 +199,8 @@ class Transaction {
         if (typeof new_value === 'object' && new_value !== null)
           new_value =
             new_value instanceof Array ?
-              new LazyArray(this, new_value) :
-              new LazyObject(this, new_value)
+              new LazyArray(this, [ptr, len], new_value) :
+              new LazyObject(this, [ptr, len], new_value)
         value[2] = new_value
       }
       value = new_value
@@ -209,11 +209,6 @@ class Transaction {
   }
 
   set(value) {
-    //assert(
-    //  typeof value !== 'object' ||
-    //  value === null ||
-    //  (value instanceof Lazy && value.transaction === this)
-    //)
     this.dirty = true
     if (typeof value === 'object' && value !== null) {
       assert(value instanceof Lazy && value.transaction === this)
@@ -254,11 +249,11 @@ class Transaction {
   }
 
   LazyArray(...args) {
-    return new LazyArray(this, ...args)
+    return new LazyArray(this, null, ...args)
   }
 
   LazyObject(...args) {
-    return new LazyObject(this, ...args)
+    return new LazyObject(this, null, ...args)
   }
 
   json_to_logjson(value) {
@@ -282,10 +277,10 @@ class Transaction {
 
 // logjson array or object
 class Lazy {
-  constructor(transaction) {
+  constructor(transaction, ptr_len) {
     assert(transaction instanceof Transaction)
     this.transaction = transaction
-    this.dirty = false
+    this.ptr_len = ptr_len || null
   }
 
   has(key) {
@@ -314,8 +309,8 @@ class Lazy {
 }
 
 class LazyArray extends Lazy {
-  constructor(transaction, array) {
-    super(transaction)
+  constructor(transaction, ptr_len, array) {
+    super(transaction, ptr_len)
     this.array = array || []
     this.length = this.array.length
   }
@@ -341,8 +336,8 @@ class LazyArray extends Lazy {
         if (typeof new_value === 'object' && new_value !== null)
           new_value =
             new_value instanceof Array ?
-              new LazyArray(this.transaction, new_value) :
-              new LazyObject(this.transaction, new_value)
+              new LazyArray(this.transaction, [ptr, len], new_value) :
+              new LazyObject(this.transaction, [ptr, len], new_value)
         value[2] = new_value
       }
       value = new_value
@@ -352,12 +347,7 @@ class LazyArray extends Lazy {
 
   set(key, value) {
     assert(typeof key === 'number')
-    //assert(
-    //  typeof value !== 'object' ||
-    //  value === null ||
-    //  (value instanceof Lazy && value.transaction === this.transaction)
-    //)
-    this.dirty = true
+    this.ptr_len = null // mark dirty
     if (typeof value === 'object' && value !== null) {
       assert(value instanceof Lazy && value.transaction === this.transaction)
       value = [-1, 0, value]
@@ -395,16 +385,14 @@ class LazyArray extends Lazy {
 
           item[0] = ptr
           item[1] = len
-          this.dirty = true
+          this.ptr_len = null // mark dirty
         }
       }
     }
-    return this.dirty
+    return this.ptr_len === null
   }
 
   pack() {
-    this.dirty = false
-
     let new_array = []
     for (let i = 0; i < this.length; ++i) {
       let item = this.array[i]
@@ -417,8 +405,8 @@ class LazyArray extends Lazy {
 }
 
 class LazyObject extends Lazy {
-  constructor(transaction, object) {
-    super(transaction)
+  constructor(transaction, ptr_len, object) {
+    super(transaction, ptr_len)
     this.object = object || {}
   }
 
@@ -443,8 +431,8 @@ class LazyObject extends Lazy {
         if (typeof new_value === 'object' && new_value !== null)
           new_value =
             new_value instanceof Array ?
-              new LazyArray(this.transaction, new_value) :
-              new LazyObject(this.transaction, new_value)
+              new LazyArray(this.transaction, [ptr, len], new_value) :
+              new LazyObject(this.transaction, [ptr, len], new_value)
         value[2] = new_value
       }
       value = new_value
@@ -454,12 +442,7 @@ class LazyObject extends Lazy {
 
   set(key, value) {
     assert(typeof key === 'string')
-    //assert(
-    //  typeof value !== 'object' ||
-    //  value === null ||
-    //  (value instanceof Lazy && value.transaction === this.transaction)
-    //)
-    this.dirty = true
+    this.ptr_len = null // mark dirty
     if (typeof value === 'object' && value !== null) {
       assert(value instanceof Lazy && value.transaction === this.transaction)
       value = [-1, 0, value]
@@ -490,16 +473,14 @@ class LazyObject extends Lazy {
 
           item[0] = ptr
           item[1] = len
-          this.dirty = true
+          this.ptr_len = null // mark dirty
         }
       }
     }
-    return this.dirty
+    return this.ptr_len === null
   }
 
   pack() {
-    this.dirty = false
-
     let new_object = {}
     for (let i in this.object) {
       let item = this.object[i]