Rename LazyXXX.flush() to LazyXXX.commit(), make LazyXXX.commit() write itself to...
authorNick Downing <nick@ndcode.org>
Thu, 6 Jan 2022 23:52:14 +0000 (10:52 +1100)
committerNick Downing <nick@ndcode.org>
Thu, 6 Jan 2022 23:52:14 +0000 (10:52 +1100)
logjson.mjs

index 1dc5a42..08793b5 100644 (file)
@@ -225,21 +225,14 @@ class Transaction {
       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
     }
 
@@ -301,11 +294,7 @@ class Lazy {
     throw new Error('not implemented')
   }
 
-  async flush() {
-    throw new Error('not implemented')
-  }
-
-  pack() {
+  async commit() {
     throw new Error('not implemented')
   }
 }
@@ -361,7 +350,6 @@ class LazyArray extends Lazy {
 
   delete(key) {
     delete this.array[key]
-    this.length = this.array.length
   }
 
   push(value) {
@@ -370,41 +358,31 @@ class LazyArray extends Lazy {
 
   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
   }
 }
 
@@ -466,35 +444,25 @@ class LazyObject extends Lazy {
     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
   }
 }