Skip to content

Commit

Permalink
perf(FileAdapter): Remove need for atomically. With RWLock we have al…
Browse files Browse the repository at this point in the history
…l we need.

One less dependency for the library.
  • Loading branch information
Belphemur committed Jan 24, 2023
1 parent d70bd56 commit 4b52670
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 45 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
]
},
"dependencies": {
"atomically": "^1.7.0",
"rwlock": "^5.0.0"
},
"devDependencies": {
Expand Down
32 changes: 0 additions & 32 deletions src/adapter/file/AtomicFileAdapter.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/adapter/file/FileAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {IFileAdapter} from "../IAdapter";
import {readFile, open, FileHandle, mkdir} from "fs/promises";
import * as path from "path";

export class FileAdapter implements IFileAdapter<string> {
public readonly filename: string;
private readonly fsync: boolean;

constructor(filename: string, fsync: boolean) {
this.filename = filename;
this.fsync = fsync;
}

async readAsync(): Promise<string | null> {
try {
return await readFile(this.filename, {
encoding: 'utf-8'
})
} catch (e) {
if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
return null;
}
throw e
}
}

async writeAsync(data: string): Promise<void> {
let fd: FileHandle | null = null;
try {
fd = await open(this.filename, 'w')
} catch (e) {
if ((e as NodeJS.ErrnoException).code !== 'ENOENT') {
throw e;
}
const basepath = path.dirname(this.filename);
await mkdir(basepath, {recursive: true});
fd = await open(this.filename, 'w');
}
try {
await fd.writeFile(data, {
encoding: 'utf-8'
})
if (this.fsync) {
await fd.sync()
}
} finally {
await fd.close();
}
}
}
4 changes: 2 additions & 2 deletions src/lib/JsonDBConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import {IAdapter} from "../adapter/IAdapter";
import {JsonAdapter} from "../adapter/data/JsonAdapter";
import {AtomicFileAdapter} from "../adapter/file/AtomicFileAdapter";
import {FileAdapter} from "../adapter/file/FileAdapter";

export interface JsonDBConfig {
readonly adapter: IAdapter<any>,
Expand All @@ -25,7 +25,7 @@ export class Config implements JsonDBConfig {

this.saveOnPush = saveOnPush
this.separator = separator
this.adapter = new JsonAdapter(new AtomicFileAdapter(this.filename, syncOnSave), humanReadable);
this.adapter = new JsonAdapter(new FileAdapter(this.filename, syncOnSave), humanReadable);
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/adapter/adapters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AtomicFileAdapter} from "../../src/adapter/file/AtomicFileAdapter";
import {FileAdapter} from "../../src/adapter/file/FileAdapter";
import * as fs from "fs";
import {JsonAdapter} from "../../src/adapter/data/JsonAdapter";
import {IAdapter} from "../../src/adapter/IAdapter";
Expand All @@ -25,12 +25,12 @@ class MemoryAdapter implements IAdapter<any> {
}

describe('Adapter', () => {
describe('Atomic', () => {
describe('File', () => {
test('should be able to write then read to a file', async () => {
const filename = "data/test.file";
const data = "Hello World";

const adapter = new AtomicFileAdapter(filename, false);
const adapter = new FileAdapter(filename, false);
await adapter.writeAsync(data);
const exists = await checkFileExists(filename);
expect(exists).toBeTruthy();
Expand All @@ -40,7 +40,7 @@ describe('Adapter', () => {
test('should return null data when file doesn\'t exists', async () => {
const filename = "data/test2.file";

const adapter = new AtomicFileAdapter(filename, false);
const adapter = new FileAdapter(filename, false);
const data = await adapter.readAsync();
expect(data).toBeNull();

Expand All @@ -50,7 +50,7 @@ describe('Adapter', () => {
const filename = "data/test.json";
const data = {Hello: "World", Foo: "Bar"};

const adapter = new JsonAdapter(new AtomicFileAdapter(filename, false), false);
const adapter = new JsonAdapter(new FileAdapter(filename, false), false);
await adapter.writeAsync(data);
const exists = await checkFileExists(filename);
expect(exists).toBeTruthy();
Expand All @@ -60,7 +60,7 @@ describe('Adapter', () => {
})
test('should create file when loading if it doesn\'t exists', async () => {
const filename = "data/test.json";
const adapter = new JsonAdapter(new AtomicFileAdapter(filename, false), false);
const adapter = new JsonAdapter(new FileAdapter(filename, false), false);
await adapter.readAsync();

const fileExists = await checkFileExists(filename);
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2064,10 +2064,6 @@ asap@^2.0.0:
version "2.0.6"
resolved "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz"

atomically@^1.7.0:
version "1.7.0"
resolved "https://registry.npmjs.org/atomically/-/atomically-1.7.0.tgz"

babel-jest@^28.1.3:
version "28.1.3"
resolved "https://registry.npmjs.org/babel-jest/-/babel-jest-28.1.3.tgz"
Expand Down

0 comments on commit 4b52670

Please sign in to comment.