Periodically optimize FTS table
This commit is contained in:
parent
e8ed8b3400
commit
5dfdde998b
7 changed files with 131 additions and 0 deletions
|
@ -360,6 +360,12 @@ export type GetAllStoriesResultType = ReadonlyArray<
|
|||
}
|
||||
>;
|
||||
|
||||
export type FTSOptimizationStateType = Readonly<{
|
||||
changes: number;
|
||||
steps: number;
|
||||
done?: boolean;
|
||||
}>;
|
||||
|
||||
export type DataInterface = {
|
||||
close: () => Promise<void>;
|
||||
removeDB: () => Promise<void>;
|
||||
|
@ -704,6 +710,10 @@ export type DataInterface = {
|
|||
getMaxMessageCounter(): Promise<number | undefined>;
|
||||
|
||||
getStatisticsForLogging(): Promise<Record<string, string>>;
|
||||
|
||||
optimizeFTS: (
|
||||
state?: FTSOptimizationStateType
|
||||
) => Promise<FTSOptimizationStateType | undefined>;
|
||||
};
|
||||
|
||||
export type ServerInterface = DataInterface & {
|
||||
|
|
|
@ -78,6 +78,7 @@ import type {
|
|||
DeleteSentProtoRecipientOptionsType,
|
||||
DeleteSentProtoRecipientResultType,
|
||||
EmojiType,
|
||||
FTSOptimizationStateType,
|
||||
GetAllStoriesResultType,
|
||||
GetConversationRangeCenteredOnMessageResultType,
|
||||
GetKnownMessageAttachmentsResultType,
|
||||
|
@ -341,6 +342,8 @@ const dataInterface: ServerInterface = {
|
|||
|
||||
getStatisticsForLogging,
|
||||
|
||||
optimizeFTS,
|
||||
|
||||
// Server-only
|
||||
|
||||
initialize,
|
||||
|
@ -5405,6 +5408,47 @@ async function removeKnownDraftAttachments(
|
|||
return Object.keys(lookup);
|
||||
}
|
||||
|
||||
// Default value of 'automerge'.
|
||||
// See: https://www.sqlite.org/fts5.html#the_automerge_configuration_option
|
||||
const OPTIMIZE_FTS_PAGE_COUNT = 4;
|
||||
|
||||
// This query is incremental. It gets the `state` from the return value of
|
||||
// previous `optimizeFTS` call. When `state.done` is `true` - optimization is
|
||||
// complete.
|
||||
async function optimizeFTS(
|
||||
state?: FTSOptimizationStateType
|
||||
): Promise<FTSOptimizationStateType | undefined> {
|
||||
// See https://www.sqlite.org/fts5.html#the_merge_command
|
||||
let pageCount = OPTIMIZE_FTS_PAGE_COUNT;
|
||||
if (state === undefined) {
|
||||
pageCount = -pageCount;
|
||||
}
|
||||
|
||||
const db = getInstance();
|
||||
const { changes } = prepare(
|
||||
db,
|
||||
`
|
||||
INSERT INTO messages_fts(messages_fts, rank) VALUES ('merge', $pageCount);
|
||||
`
|
||||
).run({ pageCount });
|
||||
|
||||
if (state === undefined) {
|
||||
return {
|
||||
changes,
|
||||
steps: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const { changes: prevChanges, steps } = state;
|
||||
|
||||
if (Math.abs(changes - prevChanges) < 2) {
|
||||
return { changes, steps, done: true };
|
||||
}
|
||||
|
||||
// More work is needed.
|
||||
return { changes, steps: steps + 1 };
|
||||
}
|
||||
|
||||
async function getJobsInQueue(queueType: string): Promise<Array<StoredJob>> {
|
||||
const db = getInstance();
|
||||
return getJobsInQueueSync(db, queueType);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue