mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-07 20:31:33 +08:00
fix: potential flaky issues (#2733)
(cherry picked from commit 935b4f847c)
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import { SqliteConnection } from '@affine/native';
|
import { SqliteConnection } from '@affine/native';
|
||||||
import assert from 'assert';
|
|
||||||
|
|
||||||
import { logger } from '../logger';
|
import { logger } from '../logger';
|
||||||
|
|
||||||
@@ -25,11 +24,15 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
const { db } = this;
|
const { db } = this;
|
||||||
this.db = null;
|
this.db = null;
|
||||||
await db?.close();
|
await db?.close();
|
||||||
|
logger.info(`[SQLiteAdapter:${this.role}]`, 'destroyed:', this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
async addBlob(key: string, data: Uint8Array) {
|
async addBlob(key: string, data: Uint8Array) {
|
||||||
try {
|
try {
|
||||||
assert(this.db, `${this.path} is not connected`);
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this.db.addBlob(key, data);
|
await this.db.addBlob(key, data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('addBlob', error);
|
logger.error('addBlob', error);
|
||||||
@@ -38,7 +41,10 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
|
|
||||||
async getBlob(key: string) {
|
async getBlob(key: string) {
|
||||||
try {
|
try {
|
||||||
assert(this.db, `${this.path} is not connected`);
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const blob = await this.db.getBlob(key);
|
const blob = await this.db.getBlob(key);
|
||||||
return blob?.data;
|
return blob?.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -49,7 +55,10 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
|
|
||||||
async deleteBlob(key: string) {
|
async deleteBlob(key: string) {
|
||||||
try {
|
try {
|
||||||
assert(this.db, `${this.path} is not connected`);
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this.db.deleteBlob(key);
|
await this.db.deleteBlob(key);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`${this.path} delete blob failed`, error);
|
logger.error(`${this.path} delete blob failed`, error);
|
||||||
@@ -58,7 +67,10 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
|
|
||||||
async getBlobKeys() {
|
async getBlobKeys() {
|
||||||
try {
|
try {
|
||||||
assert(this.db, `${this.path} is not connected`);
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
return await this.db.getBlobKeys();
|
return await this.db.getBlobKeys();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`getBlobKeys failed`, error);
|
logger.error(`getBlobKeys failed`, error);
|
||||||
@@ -68,7 +80,10 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
|
|
||||||
async getUpdates() {
|
async getUpdates() {
|
||||||
try {
|
try {
|
||||||
assert(this.db, `${this.path} is not connected`);
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
return await this.db.getUpdates();
|
return await this.db.getUpdates();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('getUpdates', error);
|
logger.error('getUpdates', error);
|
||||||
@@ -80,8 +95,11 @@ export abstract class BaseSQLiteAdapter {
|
|||||||
async addUpdateToSQLite(db: SqliteConnection, updates: Uint8Array[]) {
|
async addUpdateToSQLite(db: SqliteConnection, updates: Uint8Array[]) {
|
||||||
// batch write instead write per key stroke?
|
// batch write instead write per key stroke?
|
||||||
try {
|
try {
|
||||||
|
if (!this.db) {
|
||||||
|
logger.warn(`${this.path} is not connected`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
await db.connect();
|
|
||||||
await db.insertUpdates(updates);
|
await db.insertUpdates(updates);
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`[SQLiteAdapter][${this.role}] addUpdateToSQLite`,
|
`[SQLiteAdapter][${this.role}] addUpdateToSQLite`,
|
||||||
|
|||||||
@@ -85,10 +85,6 @@ function getWorkspaceDB$(id: string) {
|
|||||||
try {
|
try {
|
||||||
await db.destroy();
|
await db.destroy();
|
||||||
db$Map.delete(id);
|
db$Map.delete(id);
|
||||||
logger.info(
|
|
||||||
'[ensureSQLiteDB] db connection destroyed',
|
|
||||||
db.workspaceId
|
|
||||||
);
|
|
||||||
return db;
|
return db;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('[ensureSQLiteDB] destroy db failed', err);
|
logger.error('[ensureSQLiteDB] destroy db failed', err);
|
||||||
|
|||||||
@@ -306,9 +306,17 @@ export async function moveDBFile(
|
|||||||
|
|
||||||
// remove the old db file, but we don't care if it fails
|
// remove the old db file, but we don't care if it fails
|
||||||
if (meta.secondaryDBPath) {
|
if (meta.secondaryDBPath) {
|
||||||
fs.remove(meta.secondaryDBPath).catch(err => {
|
await fs
|
||||||
logger.error(`[moveDBFile] remove ${meta.secondaryDBPath} failed`, err);
|
.remove(meta.secondaryDBPath)
|
||||||
});
|
.then(() => {
|
||||||
|
logger.info(`[moveDBFile] removed ${meta.secondaryDBPath}`);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
logger.error(
|
||||||
|
`[moveDBFile] remove ${meta.secondaryDBPath} failed`,
|
||||||
|
err
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// update meta
|
// update meta
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ export const registerUpdater = async () => {
|
|||||||
|
|
||||||
_autoUpdater = autoUpdater;
|
_autoUpdater = autoUpdater;
|
||||||
|
|
||||||
if (!_autoUpdater) {
|
// skip auto update in dev mode
|
||||||
|
if (!_autoUpdater || isDev) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user