Skip to content

Commit 1fd21cd

Browse files
mplorentzmartindsqrabble
authored
Multithread db (#915)
* Open a new database connection for every query * Update open and close functions on ViewDatabase to support many connections * Update CHANGELOG * Set PRAGMAs on every connection * Fix codacy issue * Fix testMessageCount Co-authored-by: Martin Dutra <[email protected]> Co-authored-by: rabble <[email protected]>
1 parent 2f1cc05 commit 1fd21cd

11 files changed

+190
-293
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Improved background syncing #833
1212
- Update to Xcode 14. #839
13+
- Speed up database using a many connections instead of one. #884
1314
- Fixed the Share Database button in the debug settings. #918
1415
- Fixed incorrect aspect ratio of avatar images. #753
1516
- Fixed an issue that could potentially freeze the UI on startup. #865

Planetary.xcodeproj/xcshareddata/xcbaselines/5308168421C46A52005C48ED.xcbaseline/F4FEEB87-46B8-45A9-A507-1C8C8A7C28F6.plist

+46-6
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,54 @@
1919
</dict>
2020
<key>ViewDatabasePerformanceTests</key>
2121
<dict>
22+
<key>testCurrentPostsAlgorithmGivenSmallDb()</key>
23+
<dict>
24+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
25+
<dict>
26+
<key>baselineAverage</key>
27+
<real>0.152286</real>
28+
<key>baselineIntegrationDisplayName</key>
29+
<string>Sep 27, 2022 at 2:14:48 PM</string>
30+
</dict>
31+
</dict>
32+
<key>testDiscoverAlgorithmGivenSmallDb()</key>
33+
<dict>
34+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
35+
<dict>
36+
<key>baselineAverage</key>
37+
<real>0.137287</real>
38+
<key>baselineIntegrationDisplayName</key>
39+
<string>Sep 27, 2022 at 2:14:48 PM</string>
40+
</dict>
41+
</dict>
2242
<key>testFeedForIdentity()</key>
2343
<dict>
2444
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
2545
<dict>
2646
<key>baselineAverage</key>
27-
<real>0.146236</real>
47+
<real>0.010480</real>
2848
<key>baselineIntegrationDisplayName</key>
29-
<string>Jan 17, 2022 at 2:45:41 PM</string>
49+
<string>Sep 27, 2022 at 2:14:48 PM</string>
50+
</dict>
51+
</dict>
52+
<key>testFillMessagesGivenSmallDB()</key>
53+
<dict>
54+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
55+
<dict>
56+
<key>baselineAverage</key>
57+
<real>0.229490</real>
58+
<key>baselineIntegrationDisplayName</key>
59+
<string>Sep 27, 2022 at 2:14:48 PM</string>
3060
</dict>
3161
</dict>
3262
<key>testGetFollows()</key>
3363
<dict>
3464
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
3565
<dict>
3666
<key>baselineAverage</key>
37-
<real>0.125950</real>
67+
<real>0.157553</real>
3868
<key>baselineIntegrationDisplayName</key>
39-
<string>Jan 17, 2022 at 2:45:41 PM</string>
69+
<string>Sep 27, 2022 at 2:14:48 PM</string>
4070
</dict>
4171
</dict>
4272
<key>testInsertBig()</key>
@@ -49,14 +79,24 @@
4979
<string>Jan 17, 2022 at 2:45:41 PM</string>
5080
</dict>
5181
</dict>
82+
<key>testPostsAndContactsAlgorithmGivenSmallDB()</key>
83+
<dict>
84+
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
85+
<dict>
86+
<key>baselineAverage</key>
87+
<real>0.375271</real>
88+
<key>baselineIntegrationDisplayName</key>
89+
<string>Sep 27, 2022 at 2:14:48 PM</string>
90+
</dict>
91+
</dict>
5292
<key>testSimultanousReadsAndWrites()</key>
5393
<dict>
5494
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
5595
<dict>
5696
<key>baselineAverage</key>
57-
<real>2.046612</real>
97+
<real>0.908602</real>
5898
<key>baselineIntegrationDisplayName</key>
59-
<string>Jan 17, 2022 at 2:45:41 PM</string>
99+
<string>Sep 27, 2022 at 2:14:48 PM</string>
60100
</dict>
61101
</dict>
62102
</dict>

Source/GoBot/FeedStrategy/NoHopFeedAlgorithm.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class NoHopFeedAlgorithm: NSObject, FeedStrategy {
243243
}
244244

245245
func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
246-
guard let connection = database.getOpenDB() else {
246+
guard let connection = try? database.checkoutConnection() else {
247247
Log.error("db is closed")
248248
return []
249249
}

Source/GoBot/FeedStrategy/OneHopFeedAlgorithm.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class OneHopFeedAlgorithm: NSObject, FeedStrategy {
304304
}
305305

306306
func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
307-
guard let connection = database.getOpenDB() else {
307+
guard let connection = try? database.checkoutConnection() else {
308308
Log.error("db is closed")
309309
return []
310310
}

Source/GoBot/FeedStrategy/PostsAlgorithm.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class PostsAlgorithm: NSObject, FeedStrategy {
7171
}
7272

7373
func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
74-
guard let connection = database.getOpenDB() else {
74+
guard let connection = try? database.checkoutConnection() else {
7575
Log.error("db is closed")
7676
return []
7777
}
@@ -219,7 +219,7 @@ class PostsAlgorithm: NSObject, FeedStrategy {
219219
}
220220

221221
private func mapQueryToMessage(query: Table, database: ViewDatabase) throws -> [Message] {
222-
guard let connection = database.getOpenDB() else {
222+
guard let connection = try? database.checkoutConnection() else {
223223
Log.error("db is closed")
224224
return []
225225
}

Source/GoBot/FeedStrategy/PostsAndContactsAlgorithm.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class PostsAndContactsAlgorithm: NSObject, FeedStrategy {
259259
}
260260

261261
func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
262-
guard let connection = database.getOpenDB() else {
262+
guard let connection = try? database.checkoutConnection() else {
263263
Log.error("db is closed")
264264
return []
265265
}

Source/GoBot/FeedStrategy/RandomAlgorithm.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class RandomAlgorithm: NSObject, FeedStrategy {
320320
offset: Int?,
321321
onlyUnread: Bool
322322
) throws -> [Message] {
323-
guard let connection = database.getOpenDB() else {
323+
guard let connection = try? database.checkoutConnection() else {
324324
Log.error("db is closed")
325325
return []
326326
}

Source/GoBot/FeedStrategy/RecentlyActivePostsAndContactsAlgorithm.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class RecentlyActivePostsAndContactsAlgorithm: NSObject, FeedStrategy {
200200
}
201201

202202
func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
203-
guard let connection = database.getOpenDB() else {
203+
guard let connection = try? database.checkoutConnection() else {
204204
Log.error("db is closed")
205205
return []
206206
}

Source/GoBot/GoBotViewController.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,12 @@ extension GoBotViewController {
252252
if GoBot.shared.bot.isRunning {
253253
_ = GoBot.shared.bot.logout()
254254
}
255-
let dbPath = GoBot.shared.database.currentPath
256-
GoBot.shared.database.close()
257-
try FileManager.default.removeItem(atPath: dbPath)
255+
if let dbPath = GoBot.shared.database.currentPath {
256+
GoBot.shared.database.close()
257+
try FileManager.default.removeItem(atPath: dbPath)
258+
} else {
259+
throw ViewDatabaseError.notOpen
260+
}
258261
} catch {
259262
Log.unexpected(.apiError, "purge error")
260263
Log.optional(error)

0 commit comments

Comments
 (0)