Verified Commit 1fddb765 authored by Aral Balkan's avatar Aral Balkan
Browse files

Use Big Friendly JSON (bfj) to stream writes

parent f4f3825e
......@@ -31,7 +31,7 @@ class WhatDB {
// Load any existing data there might be.
this.loadTables()
} else {
log(` 🗃 ❨WhatDB?❩ No database found at ${basePath}; creating it.`)
log(` 💾 ❨WhatDB?❩ No database found at ${basePath}; creating it.`)
fs.mkdirpSync(basePath)
}
......
......@@ -16,6 +16,7 @@ const fs = require('fs-extra')
const path = require('path')
const { log, needsToBeProxified } = require('./util')
const isProxy = require('util').types.isProxy
const bigFriendlyJSON = require('bfj')
const { performance } = require('perf_hooks')
......@@ -39,7 +40,7 @@ class WhatTable {
// on object definition/change.
const startTime = performance.now()
this.dataProxy = this.deepProxy(data)
console.log(` 🗃 ❨WhatDB?❩ Took ${(performance.now() - startTime).toFixed(3)} ms.`)
console.log(` 💾 ❨WhatDB?❩ ╰─ Proxified in ${(performance.now() - startTime).toFixed(3)} ms.`)
// NB. we are returning the data proxy, not an
// instance of WhatTable. Use accordingly.
......@@ -56,9 +57,15 @@ class WhatTable {
}
load() {
log(` 🗃 ❨WhatDB?❩ Loading table ${this.tableName}.`)
log(` 💾 ❨WhatDB?❩ Loading table ${this.tableName}…`)
const t1 = performance.now()
const jsonSerialisedTable = fs.readFileSync(this.tablePath, 'utf-8')
return JSON.parse(jsonSerialisedTable)
const t2 = performance.now()
console.log(` 💾 ❨WhatDB?❩ ╰─ Read in ${(t2-t1).toFixed(3)} ms.`)
const parsedTable = JSON.parse(jsonSerialisedTable)
const t3 = performance.now()
console.log(` 💾 ❨WhatDB?❩ ╰─ Parsed in ${(t3-t2).toFixed(3)} ms.`)
return parsedTable
}
create (rootObject) {
......@@ -130,10 +137,12 @@ class WhatTable {
this.isSaving = true
console.log('Starting save')
let startTime = performance.now()
const tableContents = JSON.stringify(this.dataProxy)
await fs.writeFile(this.tablePath, tableContents)
// const tableContents = JSON.stringify(this.dataProxy)
// await fs.writeFile(this.tablePath, tableContents)
// Asynchronously write the JSON to file
await bigFriendlyJSON.write(this.tablePath, this.dataProxy)
let endTime = performance.now()
log(` 🗃 ❨WhatDB?❩ Saved table ${this.tableName}. Took ${endTime-startTime} ms.`)
log(` 💾 ❨WhatDB?❩ Saved table ${this.tableName}. Took ${endTime-startTime} ms.`)
// log(tableContents)
this.isSaving = false
}
......
......@@ -322,6 +322,22 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"bfj": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/bfj/-/bfj-7.0.2.tgz",
"integrity": "sha512-+e/UqUzwmzJamNF50tBV6tZPTORow7gQ96iFow+8b562OdMpEK0BcJEq2OSPEDmAbSMBQ7PKZ87ubFkgxpYWgw==",
"requires": {
"bluebird": "^3.5.5",
"check-types": "^11.1.1",
"hoopy": "^0.1.4",
"tryer": "^1.0.1"
}
},
"bluebird": {
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
"integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg=="
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
......@@ -367,6 +383,11 @@
"supports-color": "^5.3.0"
}
},
"check-types": {
"version": "11.1.2",
"resolved": "https://registry.npmjs.org/check-types/-/check-types-11.1.2.tgz",
"integrity": "sha512-tzWzvgePgLORb9/3a0YenggReLKAIb2owL03H2Xdoe5pKcUyWRSEQ8xfCar8t2SIAuEDwtmx2da1YB52YuHQMQ=="
},
"clean-stack": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz",
......@@ -774,6 +795,11 @@
"type-fest": "^0.8.0"
}
},
"hoopy": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz",
"integrity": "sha512-HRcs+2mr52W0K+x8RzcLzuPPmVIKMSv97RGHy0Ea9y/mpcaK+xTrjICA04KAHi4GRzxliNqNJEFYWHghy3rSfQ=="
},
"html-escaper": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz",
......@@ -1893,6 +1919,11 @@
"integrity": "sha1-WFhUf2spB1fulczMZm+1AITEYN0=",
"dev": true
},
"tryer": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz",
"integrity": "sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA=="
},
"type-fest": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
......
......@@ -26,6 +26,7 @@
"author": "Aral Balkan",
"license": "AGPL-3.0-or-later",
"dependencies": {
"bfj": "^7.0.2",
"fs-extra": "^9.0.1"
},
"devDependencies": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment