refactor: fix followgraph / add indexes
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Kieran 2024-01-18 22:39:18 +00:00
parent 6eef8c7fef
commit ba3e901e9b
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
6 changed files with 33 additions and 27 deletions

View File

@ -2,13 +2,13 @@ import "./index.css";
import "@szhsin/react-menu/dist/index.css";
import "@/assets/fonts/inter.css";
import { encodeTLVEntries } from "@snort/system";
import { encodeTLVEntries, socialGraphInstance } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import { StrictMode } from "react";
import * as ReactDOM from "react-dom/client";
import { createBrowserRouter, RouteObject, RouterProvider } from "react-router-dom";
import { initRelayWorker, preload } from "@/Cache";
import { initRelayWorker, preload, Relay } from "@/Cache";
import { ThreadRoute } from "@/Components/Event/Thread";
import { IntlProvider } from "@/Components/IntlProvider/IntlProvider";
import { db } from "@/Db";
@ -69,6 +69,11 @@ async function initSite() {
}
setupWebLNWalletConfig(Wallets);
Relay.sql("select json from events where kind = ?", [3]).then(res => {
for (const [json] of res) {
socialGraphInstance.handleEvent(JSON.parse(json as string));
}
});
return null;
}

View File

@ -153,9 +153,6 @@
"2ukA4d": {
"defaultMessage": "{n} hours"
},
"2zJXeA": {
"defaultMessage": "Profiles"
},
"39AHJm": {
"defaultMessage": "Sign Up"
},

View File

@ -1,5 +1,5 @@
import { removeUndefined, throwIfOffline } from "@snort/shared";
import { mapEventToProfile, NostrEvent, NostrSystem, ProfileLoaderService } from "@snort/system";
import { mapEventToProfile, NostrEvent, NostrSystem, ProfileLoaderService, socialGraphInstance } from "@snort/system";
import { EventsCache, Relay, RelayMetrics, SystemDb, UserCache, UserRelays } from "@/Cache";
import { LoginStore } from "@/Utils/Login";
@ -29,6 +29,7 @@ System.on("event", (_, ev) => {
Relay.event(ev);
EventsCache.discover(ev);
UserCache.discover(ev);
socialGraphInstance.handleEvent(ev);
});
/**

View File

@ -50,7 +50,6 @@
"2mcwT8": "New Note",
"2oCF7O": "Followed by friends of friends",
"2ukA4d": "{n} hours",
"2zJXeA": "Profiles",
"39AHJm": "Sign Up",
"3KNMbJ": "Articles",
"3QwfJR": "~{amount}",

View File

@ -60,7 +60,7 @@ export class WorkerRelayInterface {
return (await this.#workerRpc<object, Array<Array<any>>>("sql", { sql, params })).result;
}
#workerRpc<T, R>(cmd: string, args?: T, timeout = 30_000) {
#workerRpc<T, R>(cmd: string, args?: T) {
const id = uuid();
const msg = {
id,
@ -71,20 +71,14 @@ export class WorkerRelayInterface {
return new Promise<{
result: R;
port: MessagePort | undefined;
}>((resolve, reject) => {
let t: ReturnType<typeof setTimeout>;
}>(resolve => {
this.#commandQueue.set(id, (v, port) => {
clearTimeout(t);
const cmdReply = v as WorkerMessage<R>;
resolve({
result: cmdReply.args,
port: port.length > 0 ? port[0] : undefined,
});
});
t = setTimeout(() => {
reject("timeout");
this.#commandQueue.delete(id);
}, timeout);
});
}
}

View File

@ -66,21 +66,22 @@ export class WorkerRelay extends EventEmitter<WorkerRelayEvents> {
this.#migrate_v1();
this.#log("Migrated to v1");
}
if (version < 2) {
this.#migrate_v2();
this.#log("Migrated to v2");
}
}
/**
* Insert an event to the database
*/
event(ev: NostrEvent) {
let eventInserted = false;
this.#db?.transaction(db => {
eventInserted = this.#insertEvent(db, ev);
});
if (eventInserted) {
if (this.#insertEvent(this.#db!, ev)) {
this.#log(`Inserted: kind=${ev.kind},authors=${ev.pubkey},id=${ev.id}`);
this.emit("event", [ev]);
return true;
}
return eventInserted;
return false;
}
/**
@ -96,13 +97,11 @@ export class WorkerRelay extends EventEmitter<WorkerRelayEvents> {
eventBatch(evs: Array<NostrEvent>) {
const start = unixNowMs();
let eventsInserted: Array<NostrEvent> = [];
this.#db?.transaction(db => {
for (const ev of evs) {
if (this.#insertEvent(db, ev)) {
eventsInserted.push(ev);
}
for (const ev of evs) {
if (this.#insertEvent(this.#db!, ev)) {
eventsInserted.push(ev);
}
});
}
if (eventsInserted.length > 0) {
this.#log(`Inserted Batch: ${eventsInserted.length}/${evs.length}, ${(unixNowMs() - start).toLocaleString()}ms`);
this.emit("event", eventsInserted);
@ -114,6 +113,7 @@ export class WorkerRelay extends EventEmitter<WorkerRelayEvents> {
db.exec(`delete from events where id in (${this.#repeatParams(ids.length)})`, {
bind: ids,
});
this.#log("Deleted", ids, db.changes());
}
#insertEvent(db: Database, ev: NostrEvent) {
@ -326,4 +326,14 @@ export class WorkerRelay extends EventEmitter<WorkerRelayEvents> {
});
});
}
#migrate_v2() {
this.#db?.transaction(db => {
db.exec("CREATE INDEX pubkey_kind_IDX ON events (pubkey,kind)");
db.exec("CREATE INDEX pubkey_created_IDX ON events (pubkey,created)");
db.exec("insert into __migration values(2, ?)", {
bind: [new Date().getTime() / 1000],
});
});
}
}