Skip to content

Commit f3e0ffe

Browse files
authored
🐛 fix: improve delete orphan chunks when delete files (#4179)
* 🐛 fix: make deleteOrphanChunks parallels * 🐛 fix: make deleteOrphanChunks parallels
1 parent 9aa1712 commit f3e0ffe

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/database/server/models/chunk.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { asc, cosineDistance, count, eq, inArray, sql } from 'drizzle-orm';
22
import { and, desc, isNull } from 'drizzle-orm/expressions';
3+
import { chunk } from 'lodash-es';
34

45
import { serverDB } from '@/database/server';
56
import { ChunkMetadata, FileChunk, SemanticSearchChunk } from '@/types/chunk';
@@ -53,7 +54,15 @@ export class ChunkModel {
5354
const ids = orphanedChunks.map((chunk) => chunk.chunkId);
5455
if (ids.length === 0) return;
5556

56-
await serverDB.delete(chunks).where(inArray(chunks.id, ids));
57+
const list = chunk(ids, 500);
58+
59+
await serverDB.transaction(async (trx) => {
60+
await Promise.all(
61+
list.map(async (chunkIds) => {
62+
await trx.delete(chunks).where(inArray(chunks.id, chunkIds));
63+
}),
64+
);
65+
});
5766
};
5867

5968
findById = async (id: string) => {

0 commit comments

Comments
 (0)