fix: use userId as primary key

This commit is contained in:
Namekuji 2023-08-10 01:09:37 -04:00
parent bfc96e3c8a
commit 5828fa7a14
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
6 changed files with 18 additions and 24 deletions

View file

@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS note ( -- Models timeline
"reactions" map<text, int>, -- Reactions
"noteEdit" set<frozen<note_edit_history>>, -- Edit History
"updatedAt" timestamp,
PRIMARY KEY ("createdAtDate", "createdAt", "id")
PRIMARY KEY ("createdAtDate", "createdAt", "userId")
) WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE INDEX IF NOT EXISTS note_by_uri ON note ("uri");
@ -80,7 +80,8 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_id AS
WHERE "id" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
PRIMARY KEY ("id", "createdAt", "createdAtDate")
AND "userId" IS NOT NULL
PRIMARY KEY ("id", "createdAt", "createdAtDate", "userId")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS
@ -88,8 +89,7 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_user_id AS
WHERE "userId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "id" IS NOT NULL
PRIMARY KEY ("userId", "createdAt", "createdAtDate", "id")
PRIMARY KEY ("userId", "createdAt", "createdAtDate")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_renote_id AS
@ -97,8 +97,8 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS note_by_renote_id AS
WHERE "renoteId" IS NOT NULL
AND "createdAt" IS NOT NULL
AND "createdAtDate" IS NOT NULL
AND "id" IS NOT NULL
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "id")
AND "userId" IS NOT NULL
PRIMARY KEY ("renoteId", "createdAt", "createdAtDate", "userId")
WITH CLUSTERING ORDER BY ("createdAt" DESC);
CREATE TABLE IF NOT EXISTS reaction (

View file

@ -115,20 +115,20 @@ export const prepared = {
byUserId: `SELECT * FROM note_by_user_id WHERE "userId" IN ?`,
byRenoteId: `SELECT * FROM note_by_renote_id WHERE "renoteId" = ?`,
},
delete: `DELETE FROM note WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ?`,
delete: `DELETE FROM note WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ?`,
update: {
renoteCount: `UPDATE note SET
"renoteCount" = ?,
"score" = ?
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
repliesCount: `UPDATE note SET
"repliesCount" = ?
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
reactions: `UPDATE note SET
"emojis" = ?,
"reactions" = ?,
"score" = ?
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "id" = ? IF EXISTS`,
WHERE "createdAtDate" = ? AND "createdAt" = ? AND "userId" = ? IF EXISTS`,
},
},
reaction: {
@ -327,15 +327,9 @@ export async function execNotePaginationQuery(
if (result.rowLength === 0) {
// Reached the end of partition. Queries posts created one day before.
scannedEmptyPartitions++;
untilDate = new Date(
untilDate.getFullYear(),
untilDate.getMonth(),
untilDate.getDate() - 1,
23,
59,
59,
999,
);
const yesterday = new Date(untilDate.getTime() - 86400000);
yesterday.setUTCHours(23, 59, 59, 999);
untilDate = yesterday;
if (sinceDate && untilDate < sinceDate) break;
continue;

View file

@ -685,7 +685,7 @@ async function incRenoteCount(renote: Note) {
const score = isNaN(renote.score) ? 0 : renote.score;
await scyllaClient.execute(
prepared.note.update.renoteCount,
[count + 1, score + 1, renote.createdAt, renote.createdAt, renote.id],
[count + 1, score + 1, renote.createdAt, renote.createdAt, renote.userId],
{ prepare: true },
);
} else {
@ -1002,7 +1002,7 @@ async function saveReply(reply: Note) {
const count = isNaN(reply.repliesCount) ? 0 : reply.repliesCount;
await scyllaClient.execute(
prepared.note.update.repliesCount,
[count + 1, reply.createdAt, reply.createdAt, reply.id],
[count + 1, reply.createdAt, reply.createdAt, reply.userId],
{ prepare: true },
);
} else {

View file

@ -119,7 +119,7 @@ export default async function (
if (scyllaClient) {
const date = new Date(note.createdAt.getTime());
await scyllaClient.execute(prepared.note.delete, [date, date, note.id], {
await scyllaClient.execute(prepared.note.delete, [date, date, note.userId], {
prepare: true,
});
}

View file

@ -116,7 +116,7 @@ export default async (
(note.score ?? 0) + 1,
date,
date,
note.id,
note.userId,
],
{ prepare: true },
);

View file

@ -67,7 +67,7 @@ export default async (
Math.max((note.score ?? 0) - 1, 0),
date,
date,
note.id,
note.userId,
],
{ prepare: true },
);