-
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 1 commit
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
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
16 changes: 16 additions & 0 deletions
16
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,16 @@ | ||
package rs.wordpress.api.cache.kotlin | ||
|
||
jkmassel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import org.junit.jupiter.api.Test | ||
oguzkocer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import org.junit.jupiter.api.parallel.Execution | ||
import org.junit.jupiter.api.parallel.ExecutionMode | ||
import rs.wordpress.cache.kotlin.WordPressApiCache | ||
import kotlin.test.assertEquals | ||
|
||
@Execution(ExecutionMode.CONCURRENT) | ||
class WordPressApiCacheTest { | ||
|
||
@Test | ||
fun testThatMigrationsWork() { | ||
oguzkocer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
assertEquals(2, WordPressApiCache().performMigrations()) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
package rs.wordpress.cache.kotlin | ||
|
||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import uniffi.wp_api.DatabaseDelegate | ||
import uniffi.wp_api.UpdateHook | ||
import uniffi.wp_api.WpApiCache | ||
import uniffi.wp_api.setGlobalDelegate | ||
import java.nio.file.Path | ||
import java.util.concurrent.Executors | ||
|
||
class WordPressApiCacheDelegate : DatabaseDelegate { | ||
override fun didUpdate(updateHook: UpdateHook) { | ||
println("Received update: $updateHook") | ||
} | ||
} | ||
|
||
class WordPressApiCache { | ||
private val cache: WpApiCache | ||
private val internalDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher() | ||
private val delegate: DatabaseDelegate = WordPressApiCacheDelegate() | ||
|
||
// Creates a new in-memory cache | ||
constructor() : this(":memory:") | ||
|
||
// Creates a new cache at the specified file system URL | ||
constructor(path: Path) : this(path.toString()) | ||
|
||
// Creates a new cache at the specified path | ||
constructor(string: String) { | ||
this.cache = WpApiCache(string) | ||
} | ||
|
||
fun performMigrations(): Int { | ||
internalDispatcher.run { | ||
return this@WordPressApiCache.cache.performMigrations().toInt() | ||
} | ||
} | ||
oguzkocer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
fun startListeningForUpdates() { | ||
setGlobalDelegate(delegate) | ||
this.cache.startListeningForUpdates() | ||
} | ||
|
||
fun stopListeningForUpdates() { | ||
this.cache.stopListeningForUpdates() | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
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: "WordPressApiCachce.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() { | ||
setGlobalDelegate(delegate: delegate) | ||
self.cache.startListeningForUpdates() | ||
} | ||
|
||
public func stopListeningForUpdates() { | ||
self.cache.stopListeningForUpdates() | ||
} | ||
|
||
deinit { | ||
self.cache.stopListeningForUpdates() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
import Foundation | ||
import Testing | ||
import WordPressApiCache | ||
|
||
struct Test { | ||
|
||
private var cache: WordPressApiCache! | ||
|
||
init() throws { | ||
self.cache = try WordPressApiCache() | ||
} | ||
|
||
@Test func testMigrationsWork() async throws { | ||
let migrationsPerformed = try await self.cache.performMigrations() | ||
#expect(migrationsPerformed == 2) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CREATE TABLE `posts` ( | ||
`rowid` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`post_id` INTEGER NOT NULL, | ||
`context` TEXT COLLATE NOCASE NOT NULL, | ||
`post_author` INTEGER NOT NULL, | ||
`post_date` TEXT COLLATE NOCASE NOT NULL, | ||
`post_content` TEXT COLLATE NOCASE NOT NULL, | ||
`post_title` TEXT COLLATE NOCASE NOT NULL, | ||
`post_excerpt` TEXT COLLATE NOCASE NOT NULL, | ||
`post_status` TEXT COLLATE NOCASE NOT NULL, | ||
`comment_status` TEXT COLLATE NOCASE NOT NULL, | ||
`ping_status` TEXT COLLATE NOCASE NOT NULL, | ||
`post_password` TEXT COLLATE NOCASE DEFAULT NULL, | ||
`post_modified` TEXT COLLATE NOCASE NOT NULL, | ||
`post_parent` INTEGER, | ||
`guid` TEXT COLLATE NOCASE NOT NULL, | ||
`menu_order` INTEGER NOT NULL DEFAULT '0', | ||
`post_type` TEXT COLLATE NOCASE NOT NULL, | ||
`comment_count` INTEGER NOT NULL | ||
) STRICT; | ||
|
||
CREATE UNIQUE INDEX idx_posts_have_unique_post_id_and_context ON posts(post_id, context); |
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.