-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(write): buffered write for speed
- Loading branch information
Showing
12 changed files
with
164 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { SQLiteBackend } from "@zoid-fs/sqlite-backend"; | ||
import { MountOptions } from "@zoid-fs/node-fuse-bindings"; | ||
import { flush } from "./flush"; | ||
|
||
export const fsync: (backend: SQLiteBackend) => MountOptions["fsync"] = ( | ||
backend | ||
) => { | ||
return async (path, fd, datasync, cb) => { | ||
console.log("fsync(%s, %d, %d)", path, fd, datasync); | ||
cb(0); | ||
// @ts-expect-error TODO: implement fsync properly | ||
// We do buffered writes and flush flushes the buffer! | ||
// A program may not call flush but fsync without relenquishing fd (like SQLite) | ||
// In our case currently, the implementation of fsync and flush is same! | ||
flush(backend)(path, fd, cb); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
import { SQLiteBackend } from "@zoid-fs/sqlite-backend"; | ||
import fuse, { MountOptions } from "@zoid-fs/node-fuse-bindings"; | ||
import { match } from "ts-pattern"; | ||
import { MountOptions } from "@zoid-fs/node-fuse-bindings"; | ||
|
||
export const write: (backend: SQLiteBackend) => MountOptions["write"] = ( | ||
backend | ||
) => { | ||
return async (path, fd, buf, len, pos, cb) => { | ||
console.log("write(%s, %d, %d, %d)", path, fd, len, pos); | ||
const chunk = Buffer.from(buf, pos, len); | ||
|
||
const rChunk = await backend.writeFileChunk(path, chunk, pos, len); | ||
match(rChunk) | ||
.with({ status: "ok" }, () => { | ||
cb(chunk.length); | ||
}) | ||
.with({ status: "not_found" }, () => { | ||
cb(fuse.ENOENT); | ||
}) | ||
.exhaustive(); | ||
// TODO: This may throw (because of flush!, what should happen then?) | ||
await backend.write(path, { content: chunk, offset: pos, size: len }); | ||
cb(chunk.length); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export class WriteBuffer<T> { | ||
private buffer: Array<T> = []; | ||
|
||
constructor( | ||
private readonly size: number, | ||
private readonly writer: (bufferSlice: T[]) => Promise<void> | ||
) {} | ||
async write(item: T): Promise<void> { | ||
this.buffer.push(item); | ||
if (this.buffer.length >= this.size) { | ||
await this.flush(); | ||
} | ||
} | ||
|
||
async flush(): Promise<void> { | ||
const bufferSlice = this.buffer.slice(0); | ||
this.buffer = []; | ||
await this.writer(bufferSlice); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { SQLiteBackend } from "./SQLiteBackend"; | ||
export { PrismaClient } from "@prisma/client"; | ||
export { rawCreateMany } from "./prismaRawUtil"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Prisma, PrismaClient } from "@prisma/client"; | ||
import { Value as SqlTagTemplateValue } from "sql-template-tag"; | ||
|
||
type Value = SqlTagTemplateValue | Buffer | Prisma.Sql; | ||
|
||
type ValuesOrNestedSql<T> = { | ||
[K in keyof T]: Value; | ||
}; | ||
|
||
const formatSingleValue = (value: Value): SqlTagTemplateValue | Prisma.Sql => { | ||
if (Buffer.isBuffer(value)) { | ||
return Prisma.raw(`x'${value.toString("hex")}'`); | ||
} | ||
return value; | ||
}; | ||
|
||
const formatRow = <T>( | ||
columns: (keyof T)[], | ||
row: ValuesOrNestedSql<T> | ||
): Prisma.Sql => | ||
Prisma.sql`(${Prisma.join( | ||
columns.map((column) => formatSingleValue(row[column])), | ||
"," | ||
)})`; | ||
|
||
const formatValuesList = <T>( | ||
columns: (keyof T)[], | ||
rows: ValuesOrNestedSql<T>[] | ||
): Prisma.Sql => { | ||
return Prisma.join( | ||
rows.map((row) => formatRow(columns, row)), | ||
",\n" | ||
); | ||
}; | ||
|
||
export const rawCreateMany = async <T>( | ||
db: PrismaClient, | ||
tableName: string, | ||
columns: (keyof T)[], | ||
values: ValuesOrNestedSql<T>[] | ||
) => { | ||
const query = Prisma.sql` | ||
INSERT INTO | ||
${Prisma.raw(tableName)} | ||
(${Prisma.join((columns as string[]).map(Prisma.raw), ", ")}) | ||
VALUES | ||
${formatValuesList(columns, values)}; | ||
`; | ||
return db.$queryRaw(query); | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters