This commit is contained in:
2023-05-21 07:12:07 +08:00
parent eb416f5fd3
commit 40817604ee
4 changed files with 91 additions and 13 deletions

83
db.ts
View File

@@ -52,7 +52,25 @@ export type GMetaRaw = {
first_gid: number | null;
first_key: string | null;
};
const ALL_TABLES = ["version"];
export type PMeta = {
gid: number;
index: number;
token: string;
name: string;
width: number;
height: number;
is_original: boolean;
};
export type PMetaRaw = {
gid: number;
index: number;
token: string;
name: string;
width: number;
height: number;
is_original: number;
};
const ALL_TABLES = ["version", "task", "gmeta", "pmeta"];
const VERSION_TABLE = `CREATE TABLE version (
id TEXT,
ver TEXT,
@@ -84,11 +102,21 @@ const GMETA_TABLE = `CREATE TABLE gmeta (
first_key TEXT,
PRIMARY KEY (gid)
);`;
const PMETA_TABLE = `CREATE TABLE pmeta (
gid INT,
"index" INT,
token TEXT,
name TEXT,
width INT,
height INT,
is_original BOOLEAN,
PRIMARY KEY (gid, token)
);`;
export class EhDb {
db;
flock_enabled: boolean = eval('typeof Deno.flock !== "undefined"');
file: Deno.FsFile | undefined;
#file: Deno.FsFile | undefined;
_exist_table: Set<string> = new Set();
#lock_file: string | undefined;
readonly version = new SemVer("1.0.0-0");
@@ -98,7 +126,7 @@ export class EhDb {
if (!this._check_database()) this._create_table();
if (this.flock_enabled) {
this.#lock_file = join(base_path, "db.lock");
this.file = Deno.openSync(this.#lock_file, {
this.#file = Deno.openSync(this.#lock_file, {
create: true,
write: true,
});
@@ -130,6 +158,9 @@ export class EhDb {
if (!this._exist_table.has("gmeta")) {
this.db.execute(GMETA_TABLE);
}
if (!this._exist_table.has("pmeta")) {
this.db.execute(PMETA_TABLE);
}
this._updateExistsTable();
}
_read_version() {
@@ -168,6 +199,12 @@ export class EhDb {
gmeta,
);
}
add_pmeta(pmeta: PMeta) {
this.db.queryEntries(
"INSERT OR REPLACE INTO pmeta VALUES (:gid, :index, :token, :name, :width, :height, :is_original)",
pmeta,
);
}
add_task(task: Task) {
return this.transaction(() => {
this.db.query(
@@ -202,8 +239,8 @@ export class EhDb {
}
close() {
this.db.close();
if (this.file) {
this.file.close();
if (this.#file) {
this.#file.close();
if (this.#lock_file) Deno.removeSync(this.#lock_file);
}
}
@@ -225,24 +262,32 @@ export class EhDb {
}
convert_gmeta(m: GMetaRaw[]): GMeta[] {
return m.map((m) => {
const b = m.expunged === 1;
const b = m.expunged !== 0;
const t = <GMeta> <unknown> m;
t.expunged = b;
return t;
});
}
convert_pmeta(m: PMetaRaw[]): PMeta[] {
return m.map((m) => {
const b = m.is_original !== 0;
const t = <PMeta> <unknown> m;
t.is_original = b;
return t;
});
}
delete_task(task: Task) {
return this.transaction(() => {
this.db.query("DELETE FROM task WHERE id = ?;", [task.id]);
});
}
async flock() {
if (!this.file) return;
await eval(`Deno.flock(${this.file.rid}, true);`);
if (!this.#file) return;
await eval(`Deno.flock(${this.#file.rid}, true);`);
}
async funlock() {
if (!this.file) return;
await eval(`Deno.funlock(${this.file.rid});`);
if (!this.#file) return;
await eval(`Deno.funlock(${this.#file.rid});`);
}
get_gmeta_by_gid(gid: number) {
const s = this.convert_gmeta(
@@ -253,6 +298,24 @@ export class EhDb {
);
return s.length ? s[0] : undefined;
}
get_pmeta_by_index(gid: number, index: number) {
const s = this.convert_pmeta(
this.db.queryEntries<PMetaRaw>(
'SELECT * FROM pmeta WHERE gid = ? AND "index" = ?;',
[gid, index],
),
);
return s.length ? s[0] : undefined;
}
get_pmeta_by_token(gid: number, token: string) {
const s = this.convert_pmeta(
this.db.queryEntries<PMetaRaw>(
"SELECT * FROM pmeta WHERE gid = ? AND token = ?;",
[gid, token],
),
);
return s.length ? s[0] : undefined;
}
get_tasks_by_pid(pid: number) {
return this.transaction(() =>
this.db.queryEntries<Task>("SELECT * FROM task WHERE pid = ?;", [