Skip to content

Commit

Permalink
Only close file if all open handles are closed
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Aug 24, 2021
1 parent 35b9c86 commit 640af75
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ It implements a backend for [sql.js](https://github.com/sql-js/sql.js/) (sqlite3

It basically stores a whole database into another database. Which is absurd.

[See the demo](https://priceless-keller-d097e5.netlify.app/). You can also view an entire app using this [here](https://app-next.actualbudget.com/).
[See the demo](https://priceless-keller-d097e5.netlify.app/). You can also view an entire app using this [here](https://app-next.actualbudget.com/?wtf_source=absurd).

You should also read [this blog post](https://jlongster.com/future-sql-web) which explains the project in great detail.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "absurd-sql",
"version": "0.0.52",
"version": "0.0.53",
"main": "./dist/index.js",
"scripts": {
"build": "rm -rf dist && rollup -c rollup.config.js",
Expand Down
36 changes: 24 additions & 12 deletions src/sqlite-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class File {
this.meta = meta;
this._metaDirty = false;
this.writeLock = false;
this.openHandles = 0;
}

bufferChunks(chunks) {
Expand All @@ -120,27 +121,38 @@ export class File {
}

open() {
this.ops.open();
let meta = this.ops.readMeta();
this.openHandles++;

// It's possible that `setattr` has already been called if opening
// the file in a mode that truncates it to 0
if (this.meta == null) {
if (meta == null) {
// New file
// Don't open the file again if it's already open
if (this.openHandles === 1) {
this.ops.open();
let meta = this.ops.readMeta();

meta = { size: 0 };
}
// It's possible that `setattr` has already been called if opening
// the file in a mode that truncates it to 0
if (this.meta == null) {
if (meta == null) {
// New file

this.meta = meta;
meta = { size: 0 };
}

this.meta = meta;
}
}

return meta;
return this.meta;
}

close() {
this.fsync();
this.ops.close();

this.openHandles = Math.max(this.openHandles - 1, 0);

// Only close it if there are no existing open handles
if (this.openHandles === 0) {
this.ops.close();
}
}

delete() {
Expand Down

0 comments on commit 640af75

Please sign in to comment.