Skip to content

Commit

Permalink
refactor: Move caching out of DiscordConnection and do caching in `…
Browse files Browse the repository at this point in the history
…customizers` instead of `transformers`. (#430)
  • Loading branch information
vxern authored Sep 19, 2024
2 parents 1a57ad1 + 0783357 commit f75ddc1
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 398 deletions.
7 changes: 6 additions & 1 deletion scripts/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { loadEnvironment } from "logos:core/loaders/environment";
import { getAvailableMigrations, migrate, rollback } from "logos:core/runners/migrator";
import bun from "bun";
import { DatabaseMetadata } from "logos/models/database-metadata";
import { CacheStore } from "logos/stores/cache";
import { DatabaseStore } from "logos/stores/database";

const log = constants.loggers.feedback;
Expand Down Expand Up @@ -31,7 +32,11 @@ if (values.step !== undefined && !Number.isSafeInteger(Number(values.step))) {
log.info("Checking for migrations...");

const environment = loadEnvironment({ log: constants.loggers.silent });
const database = await DatabaseStore.create({ log: constants.loggers.silent, environment });
const database = DatabaseStore.create({
log: constants.loggers.silent,
environment,
cache: new CacheStore({ log: constants.loggers.silent }),
});
await database.setup({ prefetchDocuments: false });

const availableMigrations = await getAvailableMigrations();
Expand Down
7 changes: 6 additions & 1 deletion scripts/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { loadEnvironment } from "logos:core/loaders/environment";
import { getAvailableMigrations, migrate } from "logos:core/runners/migrator";
import { DatabaseMetadata } from "logos/models/database-metadata";
import { Guild } from "logos/models/guild";
import { CacheStore } from "logos/stores/cache";
import { DatabaseStore } from "logos/stores/database";

const log = constants.loggers.feedback;
Expand Down Expand Up @@ -40,7 +41,11 @@ const bot = Discord.createBot({

bot.start();

const database = await DatabaseStore.create({ log: constants.loggers.silent, environment });
const database = DatabaseStore.create({
log: constants.loggers.silent,
environment,
cache: new CacheStore({ log: constants.loggers.silent }),
});
await database.setup({ prefetchDocuments: false });

const availableMigrations = await getAvailableMigrations();
Expand Down
2 changes: 1 addition & 1 deletion scripts/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ const log = process.env.IS_DEBUG === "true" ? constants.loggers.debug : constant
const environment = loadEnvironment({ log });
const localisations = await loadLocalisations({ log });

const client = await Client.create({ log, environment, localisations });
const client = new Client({ log, environment, localisations });
await client.start();
8 changes: 4 additions & 4 deletions source/library/adapters/databases/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class DocumentSession {
return undefined;
}

this.database.cacheDocument(document);
this.database.cache.cacheDocument(document);

return document;
}
Expand All @@ -68,7 +68,7 @@ abstract class DocumentSession {
continue;
}

this.database.cacheDocument(document);
this.database.cache.cacheDocument(document);
documents.push(document);
}

Expand All @@ -78,7 +78,7 @@ abstract class DocumentSession {
abstract store<M extends Model>(document: M): void | Promise<void>;
async set<M extends Model>(document: M): Promise<M> {
await this.store(document);
this.database.cacheDocument(document);
this.database.cache.cacheDocument(document);

return document;
}
Expand All @@ -87,7 +87,7 @@ abstract class DocumentSession {
// We don't call any methods to delete a document here because we don't actually delete anything from the
// database; we merely *mark* documents as deleted.

this.database.unloadDocument(document);
this.database.cache.unloadDocument(document);
}

abstract query<M extends Model>({ collection }: { collection: Collection }): DocumentQuery<M>;
Expand Down
9 changes: 3 additions & 6 deletions source/library/adapters/databases/ravendb/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "node:fs/promises";
import fs from "node:fs";
import type { Collection } from "logos:constants/database";
import type { Environment } from "logos:core/loaders/environment";
import { DatabaseAdapter, type DocumentConventions } from "logos/adapters/databases/adapter";
Expand Down Expand Up @@ -36,10 +36,7 @@ class RavenDBAdapter extends DatabaseAdapter {
this.#database.conventions.getIdentityProperty = () => undefined;
}

static async tryCreate({
environment,
log,
}: { log: pino.Logger; environment: Environment }): Promise<RavenDBAdapter | undefined> {
static tryCreate({ environment, log }: { log: pino.Logger; environment: Environment }): RavenDBAdapter | undefined {
if (
environment.ravendbHost === undefined ||
environment.ravendbPort === undefined ||
Expand All @@ -51,7 +48,7 @@ class RavenDBAdapter extends DatabaseAdapter {

let certificate: Buffer | undefined;
if (environment.ravendbSecure) {
certificate = await fs.readFile(".cert.pfx");
certificate = fs.readFileSync(".cert.pfx");
}

return new RavenDBAdapter({
Expand Down
68 changes: 32 additions & 36 deletions source/library/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { LavalinkService } from "logos/services/lavalink";
import type { RealtimeUpdateService } from "logos/services/realtime-updates";
import type { StatusService } from "logos/services/status";
import { AdapterStore } from "logos/stores/adapters";
import { CacheStore } from "logos/stores/cache";
import { CommandStore } from "logos/stores/commands";
import { DatabaseStore } from "logos/stores/database";
import { EventStore } from "logos/stores/events";
Expand All @@ -25,13 +26,14 @@ import type pino from "pino";
class Client {
readonly log: pino.Logger;
readonly environment: Environment;
readonly database: DatabaseStore;
readonly volatile?: VolatileStore;
readonly diagnostics: Diagnostics;

readonly #localisations: LocalisationStore;
readonly #commands: CommandStore;
readonly #interactions: InteractionStore;
readonly #cache: CacheStore;
readonly #database: DatabaseStore;
readonly #volatile?: VolatileStore;
readonly #services: ServiceStore;
readonly #events: EventStore;
readonly #journalling: JournallingStore;
Expand All @@ -43,10 +45,6 @@ class Client {
readonly #interactionCollector: InteractionCollector;
readonly #channelDeleteCollector: Collector<"channelDelete">;

get documents(): DatabaseStore["cache"] {
return this.database.cache;
}

get localiseRaw(): LocalisationStore["localiseRaw"] {
return this.#localisations.localiseRaw.bind(this.#localisations);
}
Expand Down Expand Up @@ -183,8 +181,20 @@ class Client {
return this.#events.registerCollector.bind(this.#events);
}

get tryLog(): JournallingStore["tryLog"] {
return this.#journalling.tryLog.bind(this.#journalling);
get entities(): CacheStore["entities"] {
return this.#cache.entities;
}

get documents(): CacheStore["documents"] {
return this.#cache.documents;
}

get database(): DatabaseStore {
return this.#database;
}

get volatile(): VolatileStore | undefined {
return this.#volatile;
}

get lavalinkService(): LavalinkService | undefined {
Expand Down Expand Up @@ -231,6 +241,10 @@ class Client {
return this.#services.getPromptService.bind(this.#services);
}

get tryLog(): JournallingStore["tryLog"] {
return this.#journalling.tryLog.bind(this.#journalling);
}

get registerInteractionCollector(): <Metadata extends string[]>(
collector: InteractionCollector<Metadata>,
) => Promise<void> {
Expand All @@ -245,25 +259,17 @@ class Client {
return this.#connection.bot;
}

get entities(): DiscordConnection["cache"] {
return this.#connection.cache;
}

constructor({
log,
environment,
database,
localisations,
}: {
log: pino.Logger;
environment: Environment;
database: DatabaseStore;
localisations: RawLocalisations;
}) {
this.log = log.child({ name: "Client" });
this.environment = environment;
this.log = log;
this.database = database;
this.volatile = VolatileStore.tryCreate(this);
this.diagnostics = new Diagnostics(this);

this.#localisations = new LocalisationStore({ log, localisations });
Expand All @@ -272,11 +278,19 @@ class Client {
templates: commands,
});
this.#interactions = new InteractionStore(this);
this.#cache = new CacheStore({ log });
this.#database = DatabaseStore.create({ log, environment, cache: this.#cache });
this.#volatile = VolatileStore.tryCreate(this);
this.#services = new ServiceStore(this);
this.#events = new EventStore(this);
this.#journalling = new JournallingStore(this);
this.#adapters = new AdapterStore(this);
this.#connection = new DiscordConnection({ log, environment, events: this.#events.buildEventHandlers() });
this.#connection = new DiscordConnection({
log,
environment,
eventHandlers: this.#events.buildEventHandlers(),
cacheHandlers: this.#cache.buildCacheHandlers(),
});

this.#guildReloadLock = new ActionLock();
this.#guildCreateCollector = new Collector<"guildCreate">();
Expand All @@ -289,24 +303,6 @@ class Client {
this.#channelDeleteCollector = new Collector<"channelDelete">();
}

static async create({
log,
environment,
localisations,
}: {
log: pino.Logger;
environment: Environment;
localisations: RawLocalisations;
}): Promise<Client> {
log = log.child({ name: "Client" });

log.info("Bootstrapping the client...");

const database = await DatabaseStore.create({ log, environment });

return new Client({ log, environment, database, localisations });
}

async #setupCollectors(): Promise<void> {
this.log.info("Setting up event collectors...");

Expand Down
Loading

0 comments on commit f75ddc1

Please sign in to comment.