Extract settings from database

This commit is contained in:
Andras Schmelczer 2025-02-19 21:32:40 +00:00
parent aef5952c4d
commit 614e4a780a
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
20 changed files with 344 additions and 319 deletions

View file

@ -1,186 +0,0 @@
import type { SyncSettings } from "./sync-settings";
import { DEFAULT_SETTINGS } from "./sync-settings";
import type {
DocumentId,
DocumentMetadata,
RelativePath,
VaultUpdateId
} from "./document-metadata";
import { Logger } from "src/tracing/logger";
interface StoredDatabase {
documents: Map<RelativePath, DocumentMetadata>;
settings: SyncSettings;
lastSeenUpdateId: VaultUpdateId | undefined;
}
// Todo: split it into settings and documents
export class Database {
private _documents = new Map<RelativePath, DocumentMetadata>();
private _settings: SyncSettings;
private _lastSeenUpdateId: VaultUpdateId | undefined;
private readonly onSettingsChangeHandlers: ((
newSettings: SyncSettings,
oldSettings: SyncSettings
) => void)[] = [];
public constructor(
initialState: Partial<StoredDatabase> | undefined,
private readonly saveData: (data: unknown) => Promise<void>
) {
initialState ??= {};
if (
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
Object.prototype.hasOwnProperty.call(initialState, "documents") &&
initialState.documents
) {
for (const [relativePath, metadata] of Object.entries(
initialState.documents
)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
this._documents.set(relativePath, metadata as DocumentMetadata);
}
}
Logger.getInstance().debug(`Loaded ${this._documents.size} documents`);
this._settings = {
...DEFAULT_SETTINGS,
...(initialState.settings ?? {})
};
Logger.getInstance().debug(
`Loaded settings: ${JSON.stringify(this._settings, null, 2)}`
);
this._lastSeenUpdateId = initialState.lastSeenUpdateId;
Logger.getInstance().debug(
`Loaded last seen update id: ${this._lastSeenUpdateId}`
);
}
public getDocuments(): Map<RelativePath, DocumentMetadata> {
return this._documents;
}
public getSettings(): SyncSettings {
return this._settings;
}
public async setSettings(value: SyncSettings): Promise<void> {
const oldSettings = this._settings;
this._settings = value;
this.onSettingsChangeHandlers.forEach((handler) => {
handler(value, oldSettings);
});
await this.save();
}
public addOnSettingsChangeHandlers(
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
): void {
this.onSettingsChangeHandlers.push(handler);
}
public async setSetting<T extends keyof SyncSettings>(
key: T,
value: SyncSettings[T]
): Promise<void> {
const newSettings = { ...this._settings, [key]: value };
Logger.getInstance().debug(
`Setting ${key} to ${value}, new settings: ${JSON.stringify(
newSettings,
null,
2
)}`
);
await this.setSettings(newSettings);
}
public getLastSeenUpdateId(): VaultUpdateId | undefined {
return this._lastSeenUpdateId;
}
public async setLastSeenUpdateId(
value: VaultUpdateId | undefined
): Promise<void> {
this._lastSeenUpdateId = value;
await this.save();
}
public async resetSyncState(): Promise<void> {
this._documents = new Map();
this._lastSeenUpdateId = 0;
await this.save();
}
public getDocumentByDocumentId(
documentId: DocumentId
): [RelativePath, DocumentMetadata] | undefined {
return [...this._documents.entries()].find(
([_, metadata]) => metadata.documentId === documentId
);
}
public async setDocument({
documentId,
relativePath,
parentVersionId,
hash
}: {
documentId: DocumentId;
relativePath: RelativePath;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this._documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async moveDocument({
documentId,
oldRelativePath,
relativePath,
parentVersionId,
hash
}: {
documentId: DocumentId;
oldRelativePath: RelativePath;
relativePath: RelativePath;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this._documents.delete(oldRelativePath);
this._documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async removeDocument(relativePath: RelativePath): Promise<void> {
this._documents.delete(relativePath);
await this.save();
}
public getDocument(
relativePath: RelativePath
): DocumentMetadata | undefined {
return this._documents.get(relativePath);
}
private async save(): Promise<void> {
await this.saveData({
documents: Object.fromEntries(this._documents.entries()),
settings: this._settings,
lastSeenUpdateId: this._lastSeenUpdateId
});
}
}

View file

@ -1,9 +0,0 @@
export type VaultUpdateId = number;
export type DocumentId = string;
export type RelativePath = string;
export interface DocumentMetadata {
parentVersionId: VaultUpdateId;
documentId: DocumentId;
hash: string;
}

View file

@ -1,25 +0,0 @@
import { LogLevel } from "src/tracing/logger";
export interface SyncSettings {
remoteUri: string;
token: string;
vaultName: string;
fetchChangesUpdateIntervalMs: number;
syncConcurrency: number;
isSyncEnabled: boolean;
displayNoopSyncEvents: boolean;
minimumLogLevel: LogLevel;
maxFileSizeMB: number;
}
export const DEFAULT_SETTINGS: SyncSettings = {
remoteUri: "",
token: "",
vaultName: "default",
fetchChangesUpdateIntervalMs: 1000,
syncConcurrency: 1,
isSyncEnabled: false,
displayNoopSyncEvents: false,
minimumLogLevel: LogLevel.INFO,
maxFileSizeMB: 10
};

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "src/database/document-metadata";
import type { RelativePath } from "src/persistence/database";
export interface FileOperations {
listAllFiles: () => Promise<RelativePath[]>;

View file

@ -1,13 +1,14 @@
export { applyRemoteChangesLocally } from "./sync-operations/apply-remote-changes-locally";
export {
Database,
type RelativePath,
type DocumentId,
type VaultUpdateId,
type DocumentMetadata
} from "./database/document-metadata";
} from "./persistence/database";
export { Database } from "./database/database";
export { Settings, type SyncSettings } from "./persistence/settings";
export {
SyncService,
@ -32,12 +33,6 @@ export { type FileOperations } from "./file-operations";
import init from "sync_lib";
import wasmBin from "sync_lib/sync_lib_bg.wasm";
export const initialize = async (): Promise<void> => {
await init(
// eslint-disable-next-line
(wasmBin as any).default // it is loaded as a base64 string by webpack
);
};
export {
isFileTypeMergable,
mergeText,
@ -46,3 +41,10 @@ export {
merge,
isBinary
} from "sync_lib";
export const initialize = async (): Promise<void> => {
await init(
// eslint-disable-next-line
(wasmBin as any).default // it is loaded as a base64 string by webpack
);
};

View file

@ -0,0 +1,130 @@
export type VaultUpdateId = number;
export type DocumentId = string;
export type RelativePath = string;
export interface DocumentMetadata {
parentVersionId: VaultUpdateId;
documentId: DocumentId;
hash: string;
}
import { Logger } from "src/tracing/logger";
export interface StoredDatabase {
documents: Map<RelativePath, DocumentMetadata>;
lastSeenUpdateId: VaultUpdateId | undefined;
}
export class Database {
private documents = new Map<RelativePath, DocumentMetadata>();
private lastSeenUpdateId: VaultUpdateId | undefined;
public constructor(
initialState: Partial<StoredDatabase> | undefined,
private readonly saveData: (data: unknown) => Promise<void>
) {
initialState ??= {};
if (initialState.documents) {
for (const [relativePath, metadata] of Object.entries(
initialState.documents
)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
this.documents.set(relativePath, metadata as DocumentMetadata);
}
}
Logger.getInstance().debug(`Loaded ${this.documents.size} documents`);
this.lastSeenUpdateId = initialState.lastSeenUpdateId;
Logger.getInstance().debug(
`Loaded last seen update id: ${this.lastSeenUpdateId}`
);
}
public getDocuments(): Map<RelativePath, DocumentMetadata> {
return this.documents;
}
public getLastSeenUpdateId(): VaultUpdateId | undefined {
return this.lastSeenUpdateId;
}
public async setLastSeenUpdateId(
value: VaultUpdateId | undefined
): Promise<void> {
this.lastSeenUpdateId = value;
await this.save();
}
public async resetSyncState(): Promise<void> {
this.documents = new Map();
this.lastSeenUpdateId = 0;
await this.save();
}
public getDocumentByDocumentId(
documentId: DocumentId
): [RelativePath, DocumentMetadata] | undefined {
return [...this.documents.entries()].find(
([_, metadata]) => metadata.documentId === documentId
);
}
public async setDocument({
documentId,
relativePath,
parentVersionId,
hash
}: {
documentId: DocumentId;
relativePath: RelativePath;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this.documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async moveDocument({
documentId,
oldRelativePath,
relativePath,
parentVersionId,
hash
}: {
documentId: DocumentId;
oldRelativePath: RelativePath;
relativePath: RelativePath;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this.documents.delete(oldRelativePath);
this.documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async removeDocument(relativePath: RelativePath): Promise<void> {
this.documents.delete(relativePath);
await this.save();
}
public getDocument(
relativePath: RelativePath
): DocumentMetadata | undefined {
return this.documents.get(relativePath);
}
private async save(): Promise<void> {
await this.saveData({
documents: Object.fromEntries(this.documents.entries()),
lastSeenUpdateId: this.lastSeenUpdateId
});
}
}

View file

@ -0,0 +1,86 @@
import { Logger, LogLevel } from "src/tracing/logger";
export interface SyncSettings {
remoteUri: string;
token: string;
vaultName: string;
fetchChangesUpdateIntervalMs: number;
syncConcurrency: number;
isSyncEnabled: boolean;
displayNoopSyncEvents: boolean;
minimumLogLevel: LogLevel;
maxFileSizeMB: number;
}
const DEFAULT_SETTINGS: SyncSettings = {
remoteUri: "",
token: "",
vaultName: "default",
fetchChangesUpdateIntervalMs: 1000,
syncConcurrency: 1,
isSyncEnabled: false,
displayNoopSyncEvents: false,
minimumLogLevel: LogLevel.INFO,
maxFileSizeMB: 10
};
export class Settings {
private settings: SyncSettings;
private readonly onSettingsChangeHandlers: ((
newSettings: SyncSettings,
oldSettings: SyncSettings
) => void)[] = [];
public constructor(
initialState: Partial<SyncSettings> | undefined,
private readonly saveData: (data: unknown) => Promise<void>
) {
this.settings = {
...DEFAULT_SETTINGS,
...(initialState ?? {})
};
Logger.getInstance().debug(
`Loaded settings: ${JSON.stringify(this.settings, null, 2)}`
);
}
public getSettings(): SyncSettings {
return this.settings;
}
public async setSettings(value: SyncSettings): Promise<void> {
const oldSettings = this.settings;
this.settings = value;
this.onSettingsChangeHandlers.forEach((handler) => {
handler(value, oldSettings);
});
await this.save();
}
public addOnSettingsChangeHandlers(
handler: (settings: SyncSettings, oldSettings: SyncSettings) => void
): void {
this.onSettingsChangeHandlers.push(handler);
}
public async setSetting<T extends keyof SyncSettings>(
key: T,
value: SyncSettings[T]
): Promise<void> {
const newSettings = { ...this.settings, [key]: value };
Logger.getInstance().debug(
`Setting ${key} to ${value}, new settings: ${JSON.stringify(
newSettings,
null,
2
)}`
);
await this.setSettings(newSettings);
}
private async save(): Promise<void> {
await this.saveData(this.settings);
}
}

View file

@ -1,15 +1,15 @@
import type { Client } from "openapi-fetch";
import createClient from "openapi-fetch";
import type { components, paths } from "./types"; // Generated by openapi-typescript
import type { Database } from "../database/database";
import type { SyncSettings } from "../database/sync-settings";
import type {
DocumentId,
RelativePath,
VaultUpdateId
} from "src/database/document-metadata";
} from "../persistence/database";
import { Logger } from "src/tracing/logger";
import { retriedFetch } from "src/utils/retried-fetch";
import type { SyncSettings } from "dist/types";
import type { Settings } from "src/persistence/settings";
export interface CheckConnectionResult {
isSuccessful: boolean;
@ -19,10 +19,10 @@ export class SyncService {
private client: Client<paths>;
private clientWithoutRetries: Client<paths>;
public constructor(private readonly database: Database) {
this.createClient(database.getSettings());
public constructor(private readonly settings: Settings) {
this.createClient(settings.getSettings());
database.addOnSettingsChangeHandlers((s) => {
settings.addOnSettingsChangeHandlers((s) => {
this.createClient(s);
});
}
@ -43,7 +43,7 @@ export class SyncService {
const response = await this.clientWithoutRetries.GET("/ping", {
params: {
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
}
}
});
@ -80,10 +80,10 @@ export class SyncService {
{
params: {
path: {
vault_id: this.database.getSettings().vaultName
vault_id: this.settings.getSettings().vaultName
},
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
}
},
// eslint-disable-next-line
@ -130,11 +130,11 @@ export class SyncService {
{
params: {
path: {
vault_id: this.database.getSettings().vaultName,
vault_id: this.settings.getSettings().vaultName,
document_id: documentId
},
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
}
},
// eslint-disable-next-line
@ -171,11 +171,11 @@ export class SyncService {
{
params: {
path: {
vault_id: this.database.getSettings().vaultName,
vault_id: this.settings.getSettings().vaultName,
document_id: documentId
},
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
}
},
body: {
@ -206,11 +206,11 @@ export class SyncService {
{
params: {
path: {
vault_id: this.database.getSettings().vaultName,
vault_id: this.settings.getSettings().vaultName,
document_id: documentId
},
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
}
}
}
@ -235,10 +235,10 @@ export class SyncService {
const response = await this.client.GET("/vaults/{vault_id}/documents", {
params: {
path: {
vault_id: this.database.getSettings().vaultName
vault_id: this.settings.getSettings().vaultName
},
header: {
authorization: `Bearer ${this.database.getSettings().token}`
authorization: `Bearer ${this.settings.getSettings().token}`
},
query: {
since_update_id: since

View file

@ -1,20 +1,23 @@
import type { Database } from "../database/database";
import type { Database } from "../persistence/database";
import type { SyncService } from "src/services/sync-service";
import { Logger } from "src/tracing/logger";
import type { Syncer } from "./syncer";
import type { Settings } from "src/persistence/settings";
let isRunning = false;
export async function applyRemoteChangesLocally({
settings,
database,
syncService,
syncer
}: {
settings: Settings;
database: Database;
syncService: SyncService;
syncer: Syncer;
}): Promise<void> {
if (!database.getSettings().isSyncEnabled) {
if (!settings.getSettings().isSyncEnabled) {
Logger.getInstance().debug(
`Syncing is disabled, not fetching remote changes`
);

View file

@ -1,4 +1,4 @@
import { RelativePath } from "../database/document-metadata";
import { RelativePath } from "../persistence/database";
import {
tryLockDocument,
waitForDocumentLock,

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "../database/document-metadata";
import type { RelativePath } from "../persistence/database";
const locked = new Set<RelativePath>();
const waiters = new Map<RelativePath, (() => void)[]>();

View file

@ -1,8 +1,9 @@
import type { Database } from "../database/database";
import type {
Database,
DocumentMetadata,
RelativePath
} from "src/database/document-metadata";
} from "../persistence/database";
import type { FileOperations } from "src/file-operations";
import type { SyncService } from "src/services/sync-service";
import { Logger } from "src/tracing/logger";
@ -13,6 +14,7 @@ import PQueue from "p-queue";
import { EMPTY_HASH, hash } from "src/utils/hash";
import type { components } from "src/services/types";
import { deserialize } from "src/utils/deserialize";
import type { Settings } from "src/persistence/settings";
export class Syncer {
private readonly remainingOperationsListeners: ((
@ -25,16 +27,17 @@ export class Syncer {
public constructor(
private readonly database: Database,
private readonly settings: Settings,
private readonly syncService: SyncService,
private readonly operations: FileOperations,
private readonly history: SyncHistory
) {
this.syncQueue = new PQueue({
concurrency: database.getSettings().syncConcurrency
concurrency: settings.getSettings().syncConcurrency
});
database.addOnSettingsChangeHandlers((settings) => {
this.syncQueue.concurrency = settings.syncConcurrency;
settings.addOnSettingsChangeHandlers((newSettings) => {
this.syncQueue.concurrency = newSettings.syncConcurrency;
});
this.syncQueue.on("active", () => {
@ -91,7 +94,7 @@ export class Syncer {
return;
}
if (!this.database.getSettings().isSyncEnabled) {
if (!this.settings.getSettings().isSyncEnabled) {
Logger.getInstance().debug(
`Syncing is disabled, not uploading local changes`
);
@ -229,13 +232,13 @@ export class Syncer {
(await this.operations.getFileSize(relativePath)) /
1024 /
1024 >
this.database.getSettings().maxFileSizeMB
this.settings.getSettings().maxFileSizeMB
) {
this.history.addHistoryEntry({
status: SyncStatus.ERROR,
relativePath,
message: `File size exceeds the maximum file size limit of ${
this.database.getSettings().maxFileSizeMB
this.settings.getSettings().maxFileSizeMB
}MB`,
type: SyncType.CREATE
});
@ -332,13 +335,13 @@ export class Syncer {
(await this.operations.getFileSize(relativePath)) /
1024 /
1024 >
this.database.getSettings().maxFileSizeMB
this.settings.getSettings().maxFileSizeMB
) {
this.history.addHistoryEntry({
status: SyncStatus.ERROR,
relativePath,
message: `File size exceeds the maximum file size limit of ${
this.database.getSettings().maxFileSizeMB
this.settings.getSettings().maxFileSizeMB
}MB`,
type: SyncType.CREATE
});
@ -648,7 +651,7 @@ export class Syncer {
syncSource: SyncSource,
fn: () => Promise<void>
): Promise<void> {
if (!this.database.getSettings().isSyncEnabled) {
if (!this.settings.getSettings().isSyncEnabled) {
Logger.getInstance().info(
`Syncing is disabled, not syncing ${relativePath}`
);

View file

@ -12,7 +12,7 @@ const LOG_LEVEL_ORDER = {
[LogLevel.ERROR]: 3
};
class LogLine {
export class LogLine {
public timestamp = new Date();
public constructor(
public level: LogLevel,
@ -46,19 +46,16 @@ export class Logger {
public info(message: string): void {
console.info(message);
this.pushMessage(message, LogLevel.INFO);
}
public warn(message: string): void {
console.warn(message);
this.pushMessage(message, LogLevel.WARNING);
}
public error(message: string): void {
console.error(message);
this.pushMessage(message, LogLevel.ERROR);
}

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "src/database/document-metadata";
import { RelativePath } from "src/persistence/database";
import { Logger } from "./logger";
export interface CommonHistoryEntry {