Rename (F|f)ile to (D|d)atabase
authorNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 03:08:43 +0000 (14:08 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 5 Jan 2022 03:08:43 +0000 (14:08 +1100)
a.mjs
b.mjs
logjson.mjs

diff --git a/a.mjs b/a.mjs
index 2dec243..bcc3cc4 100755 (executable)
--- a/a.mjs
+++ b/a.mjs
@@ -3,12 +3,12 @@
 import logjson from './logjson.mjs'
 import fsPromises from 'fs/promises'
 
-let file = new logjson.File()
-await file.open('a.logjson')
-file.set(
+let database = new logjson.Database()
+await database.open('a.logjson')
+database.set(
   logjson.json_to_logjson(
     JSON.parse(await fsPromises.readFile('random.json', 'utf-8'))
   )
 )
-await file.flush()
-await file.close()
+await database.flush()
+await database.close()
diff --git a/b.mjs b/b.mjs
index 7dfe99f..41883ba 100755 (executable)
--- a/b.mjs
+++ b/b.mjs
@@ -3,17 +3,17 @@
 import logjson from './logjson.mjs'
 import fsPromises from 'fs/promises'
 
-let file = new logjson.File()
-await file.open('a.logjson')
+let database = new logjson.Database()
+await database.open('a.logjson')
 await fsPromises.writeFile(
   'a.json',
   Buffer.from(
     JSON.stringify(
-      await logjson.logjson_to_json(await file.get()),
+      await logjson.logjson_to_json(await database.get()),
       null,
       2
     ) + '\n',
     'utf-8'
   )
 )
-await file.close()
+await database.close()
index 62e7349..620b5ec 100644 (file)
@@ -4,7 +4,7 @@ import Mutex from './Mutex.mjs'
 
 let open_angle = Buffer.from('\n<', 'utf-8')
 let close_angle = Buffer.from('>\n', 'utf-8')
-class File {
+class Database {
   constructor() {
     this.mutex = new Mutex()
     this.log = null
@@ -51,7 +51,7 @@ class File {
       return
     }
 
-    // optional: trim any garbage off the end of the file
+    // optional: trim any garbage off the end of the database file
     let [ptr, len] = this.value
     let eof = ptr + len + 2
     if (eof < this.eof) {
@@ -107,7 +107,7 @@ class File {
           )
         }
 
-        // special case for < at start of file
+        // special case for < at start of database
         if (count && buffer[0] === 0x3c) {
           let start = 1
           return [start, end - start] // ptr, len
@@ -227,8 +227,8 @@ class File {
 
 // logjson array or object
 class Lazy {
-  constructor(file) {
-    this.file = file || null
+  constructor(database) {
+    this.database = database || null
     this.dirty = false
   }
 
@@ -248,7 +248,7 @@ class Lazy {
     throw new Error('not implemented')
   }
 
-  async flush(file) {
+  async flush(database) {
     throw new Error('not implemented')
   }
 
@@ -258,8 +258,8 @@ class Lazy {
 }
 
 class LazyArray extends Lazy {
-  constructor(file, array) {
-    super(file)
+  constructor(database, array) {
+    super(database)
     this.array = array || []
     this.length = this.array.length
   }
@@ -283,14 +283,14 @@ class LazyArray extends Lazy {
       if (new_value === undefined) {
         let buffer = Buffer.alloc(len)
         assert(
-          (await this.file.log.read(buffer, 0, len, ptr)).bytesRead === len
+          (await this.database.log.read(buffer, 0, len, ptr)).bytesRead === len
         )
         new_value = JSON.parse(buffer.toString('utf-8'))
         if (typeof new_value === 'object' && new_value !== null)
           new_value =
             new_value instanceof Array ?
-              new LazyArray(this.file, new_value) :
-              new LazyObject(this.file, new_value)
+              new LazyArray(this.database, new_value) :
+              new LazyObject(this.database, new_value)
         value[2] = new_value
       }
       value = new_value
@@ -329,10 +329,10 @@ class LazyArray extends Lazy {
     return value
   }
 
-  async flush(file) {
-    if (file != this.file) {
-      assert(this.file === null)
-      this.file = file
+  async flush(database) {
+    if (database != this.database) {
+      assert(this.database === null)
+      this.database = database
       this.dirty = true
     }
 
@@ -341,7 +341,7 @@ class LazyArray extends Lazy {
       if (item instanceof Array) {
         let [ptr, len, value] = item
         if (
-          (value instanceof Lazy && await value.flush(file)) ||
+          (value instanceof Lazy && await value.flush(database)) ||
             ptr === -1
         ) {
           if (value instanceof Lazy)
@@ -351,13 +351,13 @@ class LazyArray extends Lazy {
             'utf-8'
           )
 
-          ptr = this.file.eof
+          ptr = this.database.eof
           len = buffer.length
           assert(
-            (await this.file.log.write(buffer, 0, len, ptr)).bytesWritten ===
+            (await this.database.log.write(buffer, 0, len, ptr)).bytesWritten ===
               len
           )
-          this.file.eof += len
+          this.database.eof += len
 
           item[0] = ptr
           item[1] = len - 1
@@ -383,8 +383,8 @@ class LazyArray extends Lazy {
 }
 
 class LazyObject extends Lazy {
-  constructor(file, object) {
-    super(file)
+  constructor(database, object) {
+    super(database)
     this.object = object || {}
   }
 
@@ -407,14 +407,14 @@ class LazyObject extends Lazy {
       if (new_value === undefined) {
         let buffer = Buffer.alloc(len)
         assert(
-          (await this.file.log.read(buffer, 0, len, ptr)).bytesRead === len
+          (await this.database.log.read(buffer, 0, len, ptr)).bytesRead === len
         )
         new_value = JSON.parse(buffer.toString('utf-8'))
         if (typeof new_value === 'object' && new_value !== null)
           new_value =
             new_value instanceof Array ?
-              new LazyArray(this.file, new_value) :
-              new LazyObject(this.file, new_value)
+              new LazyArray(this.database, new_value) :
+              new LazyObject(this.database, new_value)
         value[2] = new_value
       }
       value = new_value
@@ -446,10 +446,10 @@ class LazyObject extends Lazy {
     return keys
   }
 
-  async flush(file) {
-    if (file != this.file) {
-      assert(this.file === null)
-      this.file = file
+  async flush(database) {
+    if (database != this.database) {
+      assert(this.database === null)
+      this.database = database
       this.dirty = true
     }
 
@@ -458,7 +458,7 @@ class LazyObject extends Lazy {
       if (item instanceof Array) {
         let [ptr, len, value] = item
         if (
-          (value instanceof Lazy && await value.flush(file)) ||
+          (value instanceof Lazy && await value.flush(database)) ||
             ptr === -1
         ) {
           if (value instanceof Lazy)
@@ -468,13 +468,13 @@ class LazyObject extends Lazy {
             'utf-8'
           )
 
-          ptr = this.file.eof
+          ptr = this.database.eof
           len = buffer.length
           assert(
-            (await this.file.log.write(buffer, 0, len, ptr)).bytesWritten ===
+            (await this.database.log.write(buffer, 0, len, ptr)).bytesWritten ===
               len
           )
-          this.file.eof += len
+          this.database.eof += len
 
           item[0] = ptr
           item[1] = len
@@ -539,7 +539,7 @@ let json_to_logjson = value => {
 }
 
 export default {
-  File,
+  Database,
   Lazy,
   LazyArray,
   LazyObject,