Parallelize SQL queries

This commit is contained in:
Fedor Indutny 2024-07-22 11:16:33 -07:00 committed by GitHub
parent 86b4da1ec2
commit c64762858e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
178 changed files with 3377 additions and 3618 deletions

View file

@ -1,17 +1,16 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { sql } from '../util';
import type { WritableDB } from '../Interface';
import { getOurUuid } from './41-uuid-keys';
export const version = 1020;
export function updateToSchemaVersion1020(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 1020) {

View file

@ -1,17 +1,14 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { isValidUuid } from '../../util/isValidUuid';
import { assertSync } from '../../util/assert';
import Helpers from '../../textsecure/Helpers';
import { createOrUpdate, getById, removeById } from '../util';
import type { EmptyQuery, Query } from '../util';
import type { ItemKeyType } from '../Interface';
import type { ItemKeyType, ReadableDB, WritableDB } from '../Interface';
export function getOurUuid(db: Database): string | undefined {
export function getOurUuid(db: ReadableDB): string | undefined {
const UUID_ID: ItemKeyType = 'uuid_id';
const row: { json: string } | undefined = db
@ -30,7 +27,7 @@ export function getOurUuid(db: Database): string | undefined {
export default function updateToSchemaVersion41(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 41) {
@ -92,8 +89,8 @@ export default function updateToSchemaVersion41(
db.prepare('DELETE FROM preKeys').run().changes,
].reduce((a: number, b: number): number => a + b);
assertSync(removeById<string>(db, 'items', 'identityKey'));
assertSync(removeById<string>(db, 'items', 'registrationId'));
removeById<string>(db, 'items', 'identityKey');
removeById<string>(db, 'items', 'registrationId');
return keyCount;
};
@ -104,36 +101,34 @@ export default function updateToSchemaVersion41(
publicKey: string;
};
const identityKey = assertSync(
getById<string, { value: IdentityKeyType }>(db, 'items', 'identityKey')
const identityKey = getById<string, { value: IdentityKeyType }>(
db,
'items',
'identityKey'
);
type RegistrationId = number;
const registrationId = assertSync(
getById<string, { value: RegistrationId }>(db, 'items', 'registrationId')
const registrationId = getById<string, { value: RegistrationId }>(
db,
'items',
'registrationId'
);
if (identityKey) {
assertSync(
createOrUpdate<ItemKeyType>(db, 'items', {
id: 'identityKeyMap',
value: {
[ourUuid]: identityKey.value,
},
})
);
createOrUpdate<ItemKeyType>(db, 'items', {
id: 'identityKeyMap',
value: {
[ourUuid]: identityKey.value,
},
});
}
if (registrationId) {
assertSync(
createOrUpdate<ItemKeyType>(db, 'items', {
id: 'registrationIdMap',
value: {
[ourUuid]: registrationId.value,
},
})
);
createOrUpdate<ItemKeyType>(db, 'items', {
id: 'registrationIdMap',
value: {
[ourUuid]: registrationId.value,
},
});
}
db.exec(

View file

@ -1,15 +1,14 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import { batchMultiVarQuery } from '../util';
import type { ArrayQuery } from '../util';
import type { WritableDB } from '../Interface';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion42(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 42) {

View file

@ -1,7 +1,6 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import { omit } from 'lodash';
import type { LoggerType } from '../../types/Logging';
@ -16,6 +15,7 @@ import {
objectToJSON,
} from '../util';
import type { EmptyQuery, Query } from '../util';
import type { WritableDB } from '../Interface';
type MessageType = Readonly<{
id: string;
@ -35,7 +35,7 @@ type ConversationType = Readonly<{
export default function updateToSchemaVersion43(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 43) {

View file

@ -1,15 +1,14 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { getOurUuid } from './41-uuid-keys';
import type { WritableDB } from '../Interface';
import type { Query } from '../util';
export default function updateToSchemaVersion47(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 47) {

View file

@ -1,19 +1,14 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { isRecord } from '../../util/isRecord';
import {
getJobsInQueueSync,
getMessageByIdSync,
insertJobSync,
} from '../Server';
import type { WritableDB } from '../Interface';
import { getJobsInQueue, getMessageById, insertJob } from '../Server';
export default function updateToSchemaVersion51(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 51) {
@ -26,7 +21,7 @@ export default function updateToSchemaVersion51(
);
// First, make sure that reactions job data has a type and conversationId
const reactionsJobs = getJobsInQueueSync(db, 'reactions');
const reactionsJobs = getJobsInQueue(db, 'reactions');
deleteJobsInQueue.run({ queueType: 'reactions' });
reactionsJobs.forEach(job => {
@ -47,7 +42,7 @@ export default function updateToSchemaVersion51(
return;
}
const message = getMessageByIdSync(db, messageId);
const message = getMessageById(db, messageId);
if (!message) {
logger.warn(
`updateToSchemaVersion51: Unable to find message for reaction job ${id}`
@ -73,11 +68,11 @@ export default function updateToSchemaVersion51(
},
};
insertJobSync(db, newJob);
insertJob(db, newJob);
});
// Then make sure all normal send job data has a type
const normalSendJobs = getJobsInQueueSync(db, 'normal send');
const normalSendJobs = getJobsInQueue(db, 'normal send');
deleteJobsInQueue.run({ queueType: 'normal send' });
normalSendJobs.forEach(job => {
@ -99,7 +94,7 @@ export default function updateToSchemaVersion51(
},
};
insertJobSync(db, newJob);
insertJob(db, newJob);
});
db.pragma('user_version = 51');

View file

@ -1,15 +1,15 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { getJobsInQueueSync, insertJobSync } from '../Server';
import { getJobsInQueue, insertJob } from '../Server';
import type { WritableDB } from '../Interface';
import { isRecord } from '../../util/isRecord';
import { isIterable } from '../../util/iterables';
export default function updateToSchemaVersion55(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 55) {
@ -22,7 +22,7 @@ export default function updateToSchemaVersion55(
);
// First, make sure that report spam job data has e164 and serverGuids
const reportSpamJobs = getJobsInQueueSync(db, 'report spam');
const reportSpamJobs = getJobsInQueue(db, 'report spam');
deleteJobsInQueue.run({ queueType: 'report spam' });
reportSpamJobs.forEach(job => {
@ -59,7 +59,7 @@ export default function updateToSchemaVersion55(
},
};
insertJobSync(db, newJob);
insertJob(db, newJob);
});
db.pragma('user_version = 55');

View file

@ -1,19 +1,14 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import type { LoggerType } from '../../types/Logging';
import { isRecord } from '../../util/isRecord';
import {
getJobsInQueueSync,
getMessageByIdSync,
insertJobSync,
} from '../Server';
import type { WritableDB } from '../Interface';
import { getJobsInQueue, getMessageById, insertJob } from '../Server';
export default function updateToSchemaVersion78(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 78) {
@ -47,7 +42,7 @@ export default function updateToSchemaVersion78(
];
for (const queue of queues) {
const prevJobs = getJobsInQueueSync(db, queue.queueType);
const prevJobs = getJobsInQueue(db, queue.queueType);
deleteJobsInQueue.run({ queueType: queue.queueType });
prevJobs.forEach(job => {
@ -67,7 +62,7 @@ export default function updateToSchemaVersion78(
return;
}
const message = getMessageByIdSync(db, messageId);
const message = getMessageById(db, messageId);
if (!message) {
logger.warn(
`updateToSchemaVersion78: Unable to find message for ${queue.queueType} job ${id}`
@ -121,7 +116,7 @@ export default function updateToSchemaVersion78(
},
};
insertJobSync(db, newJob);
insertJob(db, newJob);
});
}

View file

@ -1,7 +1,6 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from '@signalapp/better-sqlite3';
import { callIdFromEra } from '@signalapp/ringrtc';
import Long from 'long';
import { v4 as generateUuid } from 'uuid';
@ -20,7 +19,7 @@ import {
callHistoryDetailsSchema,
} from '../../types/CallDisposition';
import { CallMode } from '../../types/Calling';
import type { MessageType, ConversationType } from '../Interface';
import type { WritableDB, MessageType, ConversationType } from '../Interface';
import { strictAssert } from '../../util/assert';
import { missingCaseError } from '../../util/missingCaseError';
import { isAciString } from '../../util/isAciString';
@ -188,7 +187,7 @@ function convertLegacyCallDetails(
export default function updateToSchemaVersion89(
currentVersion: number,
db: Database,
db: WritableDB,
logger: LoggerType
): void {
if (currentVersion >= 89) {

View file

@ -15,6 +15,7 @@ import {
jsonToObject,
} from '../util';
import type { Query, EmptyQuery } from '../util';
import type { WritableDB } from '../Interface';
import updateToSchemaVersion41 from './41-uuid-keys';
import updateToSchemaVersion42 from './42-stale-reactions';
@ -2075,7 +2076,7 @@ export function enableFTS5SecureDelete(db: Database, logger: LoggerType): void {
}
}
export function updateSchema(db: Database, logger: LoggerType): void {
export function updateSchema(db: WritableDB, logger: LoggerType): void {
const sqliteVersion = getSQLiteVersion(db);
const sqlcipherVersion = getSQLCipherVersion(db);
const startingVersion = getUserVersion(db);