Skip to content

Commit

Permalink
getUnreadByConversationAndMarkRead: Only query incoming messages
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Jun 3, 2022
1 parent ecdc583 commit d753fe8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ts/sql/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2101,13 +2101,15 @@ async function getUnreadByConversationAndMarkRead({
expirationStartTimestamp = $expirationStartTimestamp,
json = json_patch(json, $jsonPatch)
WHERE
conversationId = $conversationId AND
(${_storyIdPredicate(storyId, isGroup)}) AND
isStory IS 0 AND
type IS 'incoming' AND
(
expirationStartTimestamp IS NULL OR
expirationStartTimestamp > $expirationStartTimestamp
) AND
expireTimer > 0 AND
conversationId = $conversationId AND
(${_storyIdPredicate(storyId, isGroup)}) AND
received_at <= $newestUnreadAt;
`
).run({
Expand Down
39 changes: 39 additions & 0 deletions ts/sql/migrations/59-update-expiring-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import type { Database } from 'better-sqlite3';

import type { LoggerType } from '../../types/Logging';

export default function updateToSchemaVersion59(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 59) {
return;
}

db.transaction(() => {
db.exec(
`
DROP INDEX expiring_message_by_conversation_and_received_at;
CREATE INDEX expiring_message_by_conversation_and_received_at
ON messages
(
conversationId,
storyId
expirationStartTimestamp,
expireTimer,
received_at,
)
WHERE isStory IS 0 AND type IS 'incoming';
`
);

db.pragma('user_version = 59');
})();

logger.info('updateToSchemaVersion59: success!');
}
38 changes: 38 additions & 0 deletions ts/test-node/sql_migrations_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2286,4 +2286,42 @@ describe('SQL migrations test', () => {
);
});
});

describe('updateToSchemaVersion59', () => {
it('updates index to make query efficient', () => {
updateToVersion(47);

const items = db
.prepare(
`
EXPLAIN QUERY PLAN
UPDATE messages
INDEXED BY expiring_message_by_conversation_and_received_at
SET
expirationStartTimestamp = 342342,
json = json_patch(json, '{ "something": true }')
WHERE
conversationId = 'conversationId' AND
storyId IS NULL AND
isStory IS 0 AND
type IS 'incoming' AND
(
expirationStartTimestamp IS NULL OR
expirationStartTimestamp > 23423423
) AND
expireTimer > 0 AND
received_at <= 234234;
`
)
.all();
const detail = items.map(item => item.detail).join('\n');

assert.notInclude(detail, 'B-TREE');
assert.notInclude(detail, 'SCAN');
assert.include(
detail,
'SEARCH messages USING INDEX expiring_message_by_conversation_and_received_at (expirationStartTimestamp=? AND expireTimer>?)'
);
});
});
});

0 comments on commit d753fe8

Please sign in to comment.