Skip to content

Commit

Permalink
Merge pull request #1048 from mpretty-cyro/feature/query-optimisation
Browse files Browse the repository at this point in the history
Updated the SQL for checking session id prefixes
  • Loading branch information
RyanRory authored Oct 24, 2024
2 parents 054043b + 532bbd2 commit fac94f9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ internal extension LibSession {
let threadIdsToRemove: [String] = try SessionThread
.filter(!syncedContactIds.contains(SessionThread.Columns.id))
.filter(SessionThread.Columns.variant == SessionThread.Variant.contact)
.filter(!SessionThread.Columns.id.like("\(SessionId.Prefix.blinded15.rawValue)%"))
.filter(!SessionThread.Columns.id.like("\(SessionId.Prefix.blinded25.rawValue)%"))
.filter(
/// Only want to include include standard contact conversations (not blinded conversations)
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.select(.id)
.asRequest(of: String.self)
.fetchAll(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,6 @@ extension LibSession {
let thread: TypedTableAlias<SessionThread> = TypedTableAlias()
let keyPair: TypedTableAlias<ClosedGroupKeyPair> = TypedTableAlias()

let prefixLiteral: SQL = SQL(stringLiteral: "\(SessionId.Prefix.standard.rawValue)%")
let keyPairThreadIdColumnLiteral: SQL = SQL(stringLiteral: ClosedGroupKeyPair.Columns.threadId.name)
let receivedTimestampColumnLiteral: SQL = SQL(stringLiteral: ClosedGroupKeyPair.Columns.receivedTimestamp.name)
let threadIdColumnLiteral: SQL = SQL(stringLiteral: DisappearingMessagesConfiguration.Columns.threadId.name)
Expand Down Expand Up @@ -858,7 +857,10 @@ extension LibSession {
) AS \(LegacyGroupInfo.lastKeyPairKey) ON \(LegacyGroupInfo.lastKeyPairKey).\(keyPairThreadIdColumnLiteral) = \(closedGroup[.threadId])
LEFT JOIN \(DisappearingMessagesConfiguration.self) AS \(LegacyGroupInfo.disappearingConfigKey) ON \(LegacyGroupInfo.disappearingConfigKey).\(threadIdColumnLiteral) = \(closedGroup[.threadId])
WHERE \(SQL("\(closedGroup[.threadId]) LIKE '\(prefixLiteral)'"))
WHERE (
\(closedGroup[.threadId]) > \(SessionId.Prefix.standard.rawValue) AND
\(closedGroup[.threadId]) < \(SessionId.Prefix.standard.endOfRangeString)
)
"""

let legacyGroupInfoNoMembers: [LegacyGroupInfo] = try request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ extension MessageReceiver {
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ extension MessageReceiver {
.select(.id)
.filter(SessionThread.Columns.variant == SessionThread.Variant.contact)
.filter(
SessionThread.Columns.id.like("\(SessionId.Prefix.blinded15.rawValue)%") ||
SessionThread.Columns.id.like("\(SessionId.Prefix.blinded25.rawValue)%")
(
ClosedGroup.Columns.threadId > SessionId.Prefix.blinded15.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.blinded15.endOfRangeString
) ||
(
ClosedGroup.Columns.threadId > SessionId.Prefix.blinded25.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.blinded25.endOfRangeString
)
)
.asRequest(of: String.self)
.fetchSet(db))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ extension MessageSender {
currentUserPublicKey: userPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == userPublicKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public enum PushNotificationAPI {
currentUserPublicKey: currentUserPublicKey,
legacyGroupIds: try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.joining(
required: ClosedGroup.members
.filter(GroupMember.Columns.profileId == currentUserPublicKey)
Expand Down Expand Up @@ -167,7 +170,10 @@ public enum PushNotificationAPI {
getUserHexEncodedPublicKey(db, using: dependencies),
try ClosedGroup
.select(.threadId)
.filter(!ClosedGroup.Columns.threadId.like("\(SessionId.Prefix.group.rawValue)%"))
.filter(
ClosedGroup.Columns.threadId > SessionId.Prefix.standard.rawValue &&
ClosedGroup.Columns.threadId < SessionId.Prefix.standard.endOfRangeString
)
.asRequest(of: String.self)
.fetchSet(db)
)
Expand Down
14 changes: 14 additions & 0 deletions SessionUtilitiesKit/General/SessionId.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ public struct SessionId: Equatable, Hashable, CustomStringConvertible {
case .some: throw SessionIdError.invalidSessionId // Should be covered by above cases
}
}

/// In SQLite it's more efficient to check if an indexed column is `{column} > '05' AND {column} < '06'` then it is to use
/// a wildcard like`{column} LIKE '05%'` as the wildcard can't use the index, so this variable is here to streamline this behaviour
///
/// stringlint:ignore_contents
public var endOfRangeString: String {
switch self {
case .standard: return "06"
case .blinded15: return "16"
case .blinded25: return "26"
case .unblinded: return "01"
case .group: return "04"
}
}
}

public let prefix: Prefix
Expand Down

0 comments on commit fac94f9

Please sign in to comment.