Skip to content

Commit

Permalink
Multithread db (#915)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent 2f1cc05 commit 1fd21cd
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 293 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Improved background syncing #833
- Update to Xcode 14. #839
- Speed up database using a many connections instead of one. #884
- Fixed the Share Database button in the debug settings. #918
- Fixed incorrect aspect ratio of avatar images. #753
- Fixed an issue that could potentially freeze the UI on startup. #865
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,54 @@
</dict>
<key>ViewDatabasePerformanceTests</key>
<dict>
<key>testCurrentPostsAlgorithmGivenSmallDb()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.152286</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testDiscoverAlgorithmGivenSmallDb()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.137287</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testFeedForIdentity()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.146236</real>
<real>0.010480</real>
<key>baselineIntegrationDisplayName</key>
<string>Jan 17, 2022 at 2:45:41 PM</string>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testFillMessagesGivenSmallDB()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.229490</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testGetFollows()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.125950</real>
<real>0.157553</real>
<key>baselineIntegrationDisplayName</key>
<string>Jan 17, 2022 at 2:45:41 PM</string>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testInsertBig()</key>
Expand All @@ -49,14 +79,24 @@
<string>Jan 17, 2022 at 2:45:41 PM</string>
</dict>
</dict>
<key>testPostsAndContactsAlgorithmGivenSmallDB()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.375271</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
<key>testSimultanousReadsAndWrites()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>2.046612</real>
<real>0.908602</real>
<key>baselineIntegrationDisplayName</key>
<string>Jan 17, 2022 at 2:45:41 PM</string>
<string>Sep 27, 2022 at 2:14:48 PM</string>
</dict>
</dict>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion Source/GoBot/FeedStrategy/NoHopFeedAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class NoHopFeedAlgorithm: NSObject, FeedStrategy {
}

func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
2 changes: 1 addition & 1 deletion Source/GoBot/FeedStrategy/OneHopFeedAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class OneHopFeedAlgorithm: NSObject, FeedStrategy {
}

func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
4 changes: 2 additions & 2 deletions Source/GoBot/FeedStrategy/PostsAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PostsAlgorithm: NSObject, FeedStrategy {
}

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

private func mapQueryToMessage(query: Table, database: ViewDatabase) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
2 changes: 1 addition & 1 deletion Source/GoBot/FeedStrategy/PostsAndContactsAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class PostsAndContactsAlgorithm: NSObject, FeedStrategy {
}

func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
2 changes: 1 addition & 1 deletion Source/GoBot/FeedStrategy/RandomAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class RandomAlgorithm: NSObject, FeedStrategy {
offset: Int?,
onlyUnread: Bool
) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class RecentlyActivePostsAndContactsAlgorithm: NSObject, FeedStrategy {
}

func fetchMessages(database: ViewDatabase, userId: Int64, limit: Int, offset: Int?) throws -> [Message] {
guard let connection = database.getOpenDB() else {
guard let connection = try? database.checkoutConnection() else {
Log.error("db is closed")
return []
}
Expand Down
9 changes: 6 additions & 3 deletions Source/GoBot/GoBotViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ extension GoBotViewController {
if GoBot.shared.bot.isRunning {
_ = GoBot.shared.bot.logout()
}
let dbPath = GoBot.shared.database.currentPath
GoBot.shared.database.close()
try FileManager.default.removeItem(atPath: dbPath)
if let dbPath = GoBot.shared.database.currentPath {
GoBot.shared.database.close()
try FileManager.default.removeItem(atPath: dbPath)
} else {
throw ViewDatabaseError.notOpen
}
} catch {
Log.unexpected(.apiError, "purge error")
Log.optional(error)
Expand Down
Loading

0 comments on commit 1fd21cd

Please sign in to comment.