-
Notifications
You must be signed in to change notification settings - Fork 3
Add sqlite cache #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jkmassel
wants to merge
7
commits into
trunk
Choose a base branch
from
add/sqlite-poc
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add sqlite cache #903
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
720fb28
Get basic sqlite working with updates
jkmassel e7563df
Genuine Sync+Send, and injected delegate
jkmassel 5425497
Fix Swift test warning
jkmassel 60409f3
Add Kotlin background update notifications test
jkmassel 9acf4ef
Use `runTest`
jkmassel 921ce91
Update native/kotlin/api/android/build.gradle.kts
jkmassel 0bedb9c
Update native/kotlin/api/kotlin/src/integrationTest/kotlin/WordPressA…
jkmassel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
native/kotlin/api/kotlin/src/integrationTest/kotlin/WordPressApiCacheTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.parallel.Execution | ||
import org.junit.jupiter.api.parallel.ExecutionMode | ||
import rs.wordpress.cache.kotlin.WordPressApiCache | ||
import rs.wordpress.cache.kotlin.WordPressApiCacheDelegate | ||
import kotlin.test.assertEquals | ||
|
||
@Execution(ExecutionMode.CONCURRENT) | ||
class WordPressApiCacheTest { | ||
|
||
@Test | ||
fun testThatMigrationsWork() = runTest { | ||
assertEquals(2, WordPressApiCache().performMigrations()) | ||
} | ||
|
||
@Test | ||
fun testBackgroundUpdateNotificationsWork() = runTest { | ||
var updateCount = 0 | ||
val delegate = WordPressApiCacheDelegate( | ||
callback = { updateHook -> | ||
updateCount += 1 | ||
} | ||
) | ||
|
||
val cache = WordPressApiCache(delegate = delegate) | ||
cache.startListeningForUpdates() | ||
|
||
val migrationCount = cache.performMigrations() | ||
assertEquals(updateCount, migrationCount) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
native/kotlin/api/kotlin/src/main/kotlin/rs/wordpress/cache/kotlin/WordPressApiCache.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package rs.wordpress.cache.kotlin | ||
|
||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.withContext | ||
import uniffi.wp_api.DatabaseDelegate | ||
import uniffi.wp_api.UpdateHook | ||
import uniffi.wp_api.WpApiCache | ||
import java.nio.file.Path | ||
import java.util.concurrent.Executors | ||
|
||
class WordPressApiCacheLoggingDelegate: DatabaseDelegate { | ||
override fun didUpdate(updateHook: UpdateHook) { | ||
println("Received update: $updateHook") | ||
} | ||
} | ||
class WordPressApiCacheDelegate( | ||
private val callback: (updateHook: UpdateHook) -> Unit | ||
) : DatabaseDelegate { | ||
|
||
override fun didUpdate(updateHook: UpdateHook) { | ||
callback(updateHook) | ||
} | ||
} | ||
|
||
class WordPressApiCache { | ||
private val cache: WpApiCache | ||
private val internalDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher() | ||
private val delegate: DatabaseDelegate? | ||
|
||
// Creates a new in-memory cache | ||
constructor(delegate: WordPressApiCacheDelegate? = null) : this(":memory:", delegate) | ||
|
||
// Creates a new cache at the specified file system URL | ||
constructor(path: Path, delegate: WordPressApiCacheDelegate? = null) : this(path.toString(), delegate) | ||
|
||
// Creates a new cache at the specified path | ||
constructor(string: String, delegate: WordPressApiCacheDelegate? = null) { | ||
this.cache = WpApiCache(string) | ||
this.delegate = delegate | ||
} | ||
|
||
suspend fun performMigrations(): Int = withContext(internalDispatcher) { | ||
cache.performMigrations().toInt() | ||
} | ||
fun startListeningForUpdates() { | ||
if (this.delegate != null) { | ||
this.cache.startListeningForUpdates(this.delegate) | ||
} | ||
} | ||
|
||
fun stopListeningForUpdates() { | ||
this.cache.stopListeningForUpdates() | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
native/swift/Sources/wordpress-api-cache/WordPressApiCache.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Foundation | ||
import WordPressAPIInternal | ||
|
||
public actor WordPressApiCache { | ||
|
||
private let cache: WpApiCache | ||
private let delegate: any DatabaseDelegate | ||
|
||
public struct Notifications { | ||
public static let cacheDidUpdate = Notification.Name("WordPressApiCache.cacheDidUpdate") | ||
|
||
public static func name(for table: String) -> Notification.Name { | ||
Notification.Name(rawValue: "WordPressApiCache.cacheDidUpdate.\(table)") | ||
} | ||
} | ||
|
||
final public class ApiCacheDelegate: DatabaseDelegate { | ||
public init() {} | ||
|
||
public func didUpdate(updateHook: WordPressAPIInternal.UpdateHook) { | ||
let name = Notifications.name(for: updateHook.tableName) | ||
NotificationCenter.default.post(name: name, object: updateHook) | ||
} | ||
} | ||
|
||
/// Creates a new in-memory cache | ||
public init(delegate: DatabaseDelegate = ApiCacheDelegate()) throws { | ||
try self.init(path: ":memory:", delegate: delegate) | ||
} | ||
|
||
/// Creates a new cache at the specified file system URL | ||
public init(url: URL, delegate: DatabaseDelegate = ApiCacheDelegate()) throws { | ||
try self.init(path: url.absoluteString, delegate: delegate) | ||
} | ||
|
||
/// Creates a new cache at the specified path | ||
public init(path: String, delegate: DatabaseDelegate = ApiCacheDelegate()) throws { | ||
self.cache = try WpApiCache(path: path) | ||
self.delegate = delegate | ||
} | ||
|
||
public func performMigrations() async throws -> Int { | ||
return Int(try self.cache.performMigrations()) | ||
} | ||
|
||
public func startListeningForUpdates() { | ||
self.cache.startListeningForUpdates(delegate: self.delegate) | ||
} | ||
|
||
public func stopListeningForUpdates() { | ||
self.cache.stopListeningForUpdates() | ||
} | ||
|
||
deinit { | ||
self.cache.stopListeningForUpdates() | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
native/swift/Tests/wordpress-api-cache/WordPressApiCacheTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
import Testing | ||
import WordPressApiCache | ||
|
||
actor Test { | ||
|
||
private var cache: WordPressApiCache! | ||
private var changeCount = 0 | ||
|
||
init() throws { | ||
self.cache = try WordPressApiCache() | ||
} | ||
|
||
@Test func testMigrationsWork() async throws { | ||
let migrationsPerformed = try await self.cache.performMigrations() | ||
#expect(migrationsPerformed == 2) | ||
} | ||
|
||
@Test func testBackgroundUpdateNotificationsWork() async throws { | ||
let name = WordPressApiCache.Notifications.name(for: "_migrations") | ||
|
||
let handle = Task { | ||
for await _ in NotificationCenter.default.notifications(named: name) { | ||
self.incrementChangeCount() | ||
} | ||
} | ||
|
||
await self.cache.startListeningForUpdates() | ||
let migrationCount = try await self.cache.performMigrations() | ||
|
||
// Wait for NotificationCenter to finish delivery | ||
try await Task.sleep(nanoseconds: 10 * NSEC_PER_MSEC) | ||
|
||
#expect(migrationCount == self.changeCount) | ||
handle.cancel() | ||
} | ||
|
||
func incrementChangeCount() { | ||
self.changeCount += 1 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.