refactor: create index to delete

This commit is contained in:
Namekuji 2023-08-12 05:05:51 -04:00
parent 2a05d8ce12
commit e4bfde105b
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
8 changed files with 31 additions and 50 deletions

View file

@ -2,13 +2,13 @@ DROP MATERIALIZED VIEW IF EXISTS reaction_by_id;
DROP MATERIALIZED VIEW IF EXISTS reaction_by_userid; DROP MATERIALIZED VIEW IF EXISTS reaction_by_userid;
DROP INDEX IF EXISTS reaction_by_id; DROP INDEX IF EXISTS reaction_by_id;
DROP TABLE IF EXISTS reaction; DROP TABLE IF EXISTS reaction;
DROP TABLE IF EXISTS deleted_note; DROP INDEX IF EXISTS home_by_id;
DROP TABLE IF EXISTS home_timeline; DROP TABLE IF EXISTS home_timeline;
DROP MATERIALIZED VIEW IF EXISTS local_timeline; DROP MATERIALIZED VIEW IF EXISTS local_timeline;
DROP MATERIALIZED VIEW IF EXISTS global_timeline; DROP MATERIALIZED VIEW IF EXISTS global_timeline;
DROP MATERIALIZED VIEW IF EXISTS note_by_renote_id; DROP MATERIALIZED VIEW IF EXISTS note_by_renote_id;
DROP MATERIALIZED VIEW IF EXISTS note_by_userid; DROP MATERIALIZED VIEW IF EXISTS note_by_userid;
DROP MATERIALIZED VIEW IF EXISTS note_by_id; DROP INDEX IF EXISTS note_by_id;
DROP INDEX IF EXISTS note_by_uri; DROP INDEX IF EXISTS note_by_uri;
DROP INDEX IF EXISTS note_by_url; DROP INDEX IF EXISTS note_by_url;
DROP TABLE IF EXISTS note; DROP TABLE IF EXISTS note;

View file

@ -74,17 +74,7 @@ CREATE TABLE IF NOT EXISTS note ( -- Store all posts
CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri"); CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri");
CREATE INDEX IF NOT EXISTS note_by_url ON note ("url"); CREATE INDEX IF NOT EXISTS note_by_url ON note ("url");
CREATE INDEX IF NOT EXISTS note_by_id ON note ("id");
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_id AS
SELECT * FROM note
WHERE "id" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "userId" IS NOT NULL
AND "userHost" IS NOT NULL
AND "visibility" IS NOT NULL
PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId", "userHost", "visibility")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS
SELECT * FROM note SELECT * FROM note
@ -171,11 +161,7 @@ CREATE TABLE IF NOT EXISTS home_timeline (
PRIMARY KEY (("feedUserId", "createdAtDate"), "createdAt", "userId") PRIMARY KEY (("feedUserId", "createdAtDate"), "createdAt", "userId")
) WITH CLUSTERING ORDER BY ("createdAt" DESC); ) WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE TABLE IF NOT EXISTS deleted_note ( CREATE INDEX IF NOT EXISTS home_by_id ON home_timeline ("id");
"noteId" ascii,
"deletedAt" timestamp,
PRIMARY KEY ("noteId", "deletedAt")
);
CREATE TABLE IF NOT EXISTS reaction ( CREATE TABLE IF NOT EXISTS reaction (
"id" text, "id" text,

View file

@ -45,7 +45,7 @@ export const scyllaQueries = {
byDate: `SELECT * FROM note WHERE "createdAtDate" = ?`, byDate: `SELECT * FROM note WHERE "createdAtDate" = ?`,
byUri: `SELECT * FROM note WHERE "uri" = ?`, byUri: `SELECT * FROM note WHERE "uri" = ?`,
byUrl: `SELECT * FROM note WHERE "url" = ?`, byUrl: `SELECT * FROM note WHERE "url" = ?`,
byId: `SELECT * FROM note_by_id WHERE "id" IN ?`, byId: `SELECT * FROM note WHERE "id" = ?`,
byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`, byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`,
byRenoteId: `SELECT * FROM note_by_renote_id WHERE "renoteId" = ?`, byRenoteId: `SELECT * FROM note_by_renote_id WHERE "renoteId" = ?`,
}, },
@ -110,7 +110,9 @@ export const scyllaQueries = {
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
select: { select: {
byUserAndDate: `SELECT * FROM home_timeline WHERE "feedUserId" = ? AND "createdAtDate" = ?`, byUserAndDate: `SELECT * FROM home_timeline WHERE "feedUserId" = ? AND "createdAtDate" = ?`,
byId: `SELECT * FROM home_timeline WHERE "id" = ?`,
}, },
delete: `DELETE FROM home_timeline WHERE "feedUserId" = ? AND "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ?`,
}, },
localTimeline: { localTimeline: {
select: { select: {
@ -122,10 +124,6 @@ export const scyllaQueries = {
byDate: `SELECT * FROM global_timeline WHERE "createdAtDate" = ?`, byDate: `SELECT * FROM global_timeline WHERE "createdAtDate" = ?`,
}, },
}, },
deletedNote: {
insert: `INSERT INTO deleted_note ("noteId", "deletedAt") VALUES (?, ?)`,
select: `SELECT "noteId" FROM deleted_note`,
},
reaction: { reaction: {
insert: `INSERT INTO reaction insert: `INSERT INTO reaction
("id", "noteId", "userId", "reaction", "emoji", "createdAt") ("id", "noteId", "userId", "reaction", "emoji", "createdAt")

View file

@ -155,15 +155,14 @@ export function parseScyllaNote(row: types.Row): ScyllaNote {
}; };
} }
export interface DeletedNote { export function parseHomeTimeline(
noteId: string; row: types.Row,
deletedAt: string; ): { feedUserId: string } & ScyllaNote {
} const note = parseScyllaNote(row);
export function parseDeletedNote(row: types.Row): DeletedNote {
return { return {
noteId: row.get("noteId"), feedUserId: row.get("feedUserId"),
deletedAt: row.get("deletedAt"), ...note,
}; };
} }
@ -301,18 +300,7 @@ export async function execNotePaginationQuery(
if (result.rowLength > 0) { if (result.rowLength > 0) {
const notes = result.rows.map(parseScyllaNote); const notes = result.rows.map(parseScyllaNote);
const candidates = filter ? await filter(notes) : notes; foundNotes.push(...(filter ? await filter(notes) : notes));
// foundNotes.push(...(filter ? await filter(notes) : notes));
const deletedNotes = await scyllaClient
.execute(`${prepared.deletedNote.select} WHERE "noteId" IN ?`, [
candidates.map(({ id }) => id),
])
.then((result) =>
result.rows.map((row) => parseDeletedNote(row).noteId),
);
foundNotes.push(
...candidates.filter((note) => !deletedNotes.includes(note.id)),
);
untilDate = notes[notes.length - 1].createdAt; untilDate = notes[notes.length - 1].createdAt;
} }

View file

@ -206,7 +206,7 @@ export const NoteRepository = db.getRepository(Note).extend({
if (scyllaClient) { if (scyllaClient) {
const result = await scyllaClient.execute( const result = await scyllaClient.execute(
prepared.note.select.byId, prepared.note.select.byId,
[[noteId]], [noteId],
{ prepare: true }, { prepare: true },
); );
if (result.rowLength > 0) { if (result.rowLength > 0) {

View file

@ -23,7 +23,7 @@ export async function getNote(
if (scyllaClient) { if (scyllaClient) {
const result = await scyllaClient.execute( const result = await scyllaClient.execute(
prepared.note.select.byId, prepared.note.select.byId,
[[noteId]], [noteId],
{ prepare: true }, { prepare: true },
); );
if (result.rowLength > 0) { if (result.rowLength > 0) {

View file

@ -905,7 +905,7 @@ async function insertNote(
if (scyllaClient) { if (scyllaClient) {
const result = await scyllaClient.execute( const result = await scyllaClient.execute(
prepared.note.select.byId, prepared.note.select.byId,
[[insert.id]], [insert.id],
{ prepare: true }, { prepare: true },
); );
if (result.rowLength > 0) { if (result.rowLength > 0) {

View file

@ -22,7 +22,7 @@ import { countSameRenotes } from "@/misc/count-same-renotes.js";
import { registerOrFetchInstanceDoc } from "../register-or-fetch-instance-doc.js"; import { registerOrFetchInstanceDoc } from "../register-or-fetch-instance-doc.js";
import { deliverToRelays } from "../relay.js"; import { deliverToRelays } from "../relay.js";
import meilisearch from "@/db/meilisearch.js"; import meilisearch from "@/db/meilisearch.js";
import { prepared, scyllaClient } from "@/db/scylla.js"; import { parseHomeTimeline, prepared, scyllaClient } from "@/db/scylla.js";
/** /**
* 稿 * 稿
@ -126,10 +126,19 @@ export default async function (
prepare: true, prepare: true,
}, },
); );
await scyllaClient.execute(prepared.deletedNote.insert, [
note.id, const homeTimelines = await scyllaClient
new Date(), .execute(prepared.homeTimeline.select.byId, [note.id], { prepare: true })
]); .then((result) => result.rows.map(parseHomeTimeline));
for (const timeline of homeTimelines) {
// No need to wait
scyllaClient.execute(prepared.homeTimeline.delete, [
timeline.feedUserId,
timeline.createdAtDate,
timeline.createdAt,
timeline.userId,
]);
}
} }
await Notes.delete({ await Notes.delete({