mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-23 13:29:02 +08:00
feat: add index for snapshots (#8163)
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- The primary key for the `snapshots` table will be changed. If it partially fails, the table could be left without primary key constraint.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "snapshots" DROP CONSTRAINT "snapshots_pkey",
|
||||||
|
ADD CONSTRAINT "snapshots_pkey" PRIMARY KEY ("workspace_id", "guid");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE INDEX "snapshots_workspace_id_updated_at_idx" ON "snapshots"("workspace_id", "updated_at");
|
||||||
@@ -262,7 +262,8 @@ model Snapshot {
|
|||||||
// we need to clear all hanging updates and snapshots before enable the foreign key on workspaceId
|
// we need to clear all hanging updates and snapshots before enable the foreign key on workspaceId
|
||||||
// workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
|
// workspace Workspace @relation(fields: [workspaceId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@id([id, workspaceId])
|
@@id([workspaceId, id])
|
||||||
|
@@index([workspaceId, updatedAt])
|
||||||
@@map("snapshots")
|
@@map("snapshots")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -143,8 +143,6 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSpaceDocTimestamps(workspaceId: string, after?: number) {
|
async getSpaceDocTimestamps(workspaceId: string, after?: number) {
|
||||||
// TODO(@forehalo): do we need a [Clock] table to store the last seen time of each doc?
|
|
||||||
// SLOW if query DB in large workspace by `updatedAt: { gt: after }`
|
|
||||||
const snapshots = await this.db.snapshot.findMany({
|
const snapshots = await this.db.snapshot.findMany({
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -152,6 +150,13 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
|
...(after
|
||||||
|
? {
|
||||||
|
updatedAt: {
|
||||||
|
gt: new Date(after),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -176,9 +181,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
const result: Record<string, number> = {};
|
const result: Record<string, number> = {};
|
||||||
|
|
||||||
snapshots.forEach(s => {
|
snapshots.forEach(s => {
|
||||||
if (!after || s.updatedAt.getTime() > after) {
|
result[s.id] = s.updatedAt.getTime();
|
||||||
result[s.id] = s.updatedAt.getTime();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
updates.forEach(u => {
|
updates.forEach(u => {
|
||||||
@@ -378,7 +381,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
protected async getDocSnapshot(workspaceId: string, docId: string) {
|
protected async getDocSnapshot(workspaceId: string, docId: string) {
|
||||||
const snapshot = await this.db.snapshot.findUnique({
|
const snapshot = await this.db.snapshot.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
id: docId,
|
id: docId,
|
||||||
},
|
},
|
||||||
@@ -414,7 +417,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
//
|
//
|
||||||
// ii. Prisma doesn't support `upsert` with additional `where` condition along side unique constraint.
|
// ii. Prisma doesn't support `upsert` with additional `where` condition along side unique constraint.
|
||||||
// In our case, we need to manually check the `updatedAt` to avoid overriding the newer snapshot.
|
// In our case, we need to manually check the `updatedAt` to avoid overriding the newer snapshot.
|
||||||
// where: { id_workspaceId: {}, updatedAt: { lt: updatedAt } }
|
// where: { workspaceId_id: {}, updatedAt: { lt: updatedAt } }
|
||||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
try {
|
try {
|
||||||
const result: { updatedAt: Date }[] = await this.db.$queryRaw`
|
const result: { updatedAt: Date }[] = await this.db.$queryRaw`
|
||||||
@@ -432,7 +435,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
// seq: true,
|
// seq: true,
|
||||||
// },
|
// },
|
||||||
// where: {
|
// where: {
|
||||||
// id_workspaceId: {
|
// workspaceId_id: {
|
||||||
// workspaceId,
|
// workspaceId,
|
||||||
// id: guid,
|
// id: guid,
|
||||||
// },
|
// },
|
||||||
@@ -571,7 +574,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
seq: true,
|
seq: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
id: guid,
|
id: guid,
|
||||||
},
|
},
|
||||||
@@ -594,7 +597,7 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
|
|||||||
seq: true,
|
seq: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
id: guid,
|
id: guid,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ export class Guid1698398506533 {
|
|||||||
}),
|
}),
|
||||||
db.snapshot.update({
|
db.snapshot.update({
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
id: docId.guid,
|
id: docId.guid,
|
||||||
workspaceId: doc.workspaceId,
|
workspaceId: doc.workspaceId,
|
||||||
},
|
},
|
||||||
@@ -93,7 +93,7 @@ export class Guid1698398506533 {
|
|||||||
// just modify the id the required one
|
// just modify the id the required one
|
||||||
await db.snapshot.update({
|
await db.snapshot.update({
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
workspaceId: doc.workspaceId,
|
workspaceId: doc.workspaceId,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ test('should have sequential update number', async t => {
|
|||||||
// fake the seq num is about to overflow
|
// fake the seq num is about to overflow
|
||||||
await db.snapshot.update({
|
await db.snapshot.update({
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
id: '2',
|
id: '2',
|
||||||
workspaceId: '2',
|
workspaceId: '2',
|
||||||
},
|
},
|
||||||
@@ -273,7 +273,7 @@ test('should not update snapshot if doc is outdated', async t => {
|
|||||||
// fake the snapshot is a lot newer
|
// fake the snapshot is a lot newer
|
||||||
await db.snapshot.update({
|
await db.snapshot.update({
|
||||||
where: {
|
where: {
|
||||||
id_workspaceId: {
|
workspaceId_id: {
|
||||||
workspaceId: '2',
|
workspaceId: '2',
|
||||||
id: '1',
|
id: '1',
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user