-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPowerSyncDatabaseImpl.kt
More file actions
542 lines (476 loc) · 18.1 KB
/
Copy pathPowerSyncDatabaseImpl.kt
File metadata and controls
542 lines (476 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package com.powersync.db
import co.touchlab.kermit.Logger
import com.powersync.ExperimentalPowerSyncAPI
import com.powersync.PowerSyncDatabase
import com.powersync.PowerSyncException
import com.powersync.bucket.BucketStorage
import com.powersync.bucket.BucketStorageImpl
import com.powersync.bucket.StreamPriority
import com.powersync.connectors.PowerSyncBackendConnector
import com.powersync.db.crud.CrudBatch
import com.powersync.db.crud.CrudEntry
import com.powersync.db.crud.CrudRow
import com.powersync.db.crud.CrudTransaction
import com.powersync.db.driver.SQLiteConnectionLease
import com.powersync.db.driver.SQLiteConnectionPool
import com.powersync.db.internal.ConnectionContext
import com.powersync.db.internal.InternalDatabaseImpl
import com.powersync.db.internal.InternalTable
import com.powersync.db.internal.PowerSyncTransaction
import com.powersync.db.internal.PowerSyncVersion
import com.powersync.db.schema.Schema
import com.powersync.sync.CoreSyncStatus
import com.powersync.sync.StreamingSyncClient
import com.powersync.sync.SyncOptions
import com.powersync.sync.SyncStatus
import com.powersync.sync.SyncStatusData
import com.powersync.sync.SyncStream
import com.powersync.utils.JsonParam
import com.powersync.utils.JsonUtil
import com.powersync.utils.throttle
import com.powersync.utils.toJsonObject
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.completeWith
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.time.Duration.Companion.milliseconds
/**
* A PowerSync managed database.
*
* Use one instance per database file.
*
* Use [PowerSyncDatabase.connect] to connect to the PowerSync service, to keep the local database in sync with the remote database.
*
* All changes to local tables are automatically recorded, whether connected or not. Once connected, the changes are uploaded.
*/
@OptIn(ExperimentalPowerSyncAPI::class)
internal class PowerSyncDatabaseImpl(
var schema: Schema,
val scope: CoroutineScope,
pool: SQLiteConnectionPool,
val logger: Logger,
private val activeDatabaseGroup: Pair<ActiveDatabaseResource, Any>,
) : PowerSyncDatabase {
companion object {
internal val streamConflictMessage =
"""
Another PowerSync client is already connected to this database.
Multiple connections to the same database should be avoided.
Please check your PowerSync client instantiation logic.
This connection attempt will be queued and will only be executed after
currently connecting clients are disconnected.
""".trimIndent()
}
override val identifier: String
get() = activeDatabaseGroup.first.group.identifier
private val resource = activeDatabaseGroup.first
private val streams = StreamTracker(this)
private val internalDb = InternalDatabaseImpl(pool, logger)
internal val bucketStorage: BucketStorage = BucketStorageImpl(internalDb, logger)
override var closed = false
/**
* The current sync status.
*/
override val currentStatus: SyncStatus = SyncStatus()
private val mutex = Mutex()
private var syncSupervisorJob: Job? = null
// This is set before the initialization job completes
private lateinit var powerSyncVersion: String
private val initializeResult = CompletableDeferred<Unit>()
private val initializeJob =
scope.launch {
initializeResult.completeWith(runCatching { initialize() })
}
private suspend fun initialize() {
val sqliteVersion = internalDb.get("SELECT sqlite_version()") { it.getString(0)!! }
logger.d { "SQLiteVersion: $sqliteVersion" }
powerSyncVersion = internalDb.get("SELECT powersync_rs_version()") { it.getString(0)!! }
checkVersion(powerSyncVersion)
logger.d { "PowerSyncVersion: $powerSyncVersion" }
internalDb.writeTransactionAsync { tx ->
tx.getOptionalAsync("SELECT powersync_init()") {}
}
updateSchemaInternal(schema)
resolveOfflineSyncStatus()
}
private suspend fun waitReady() {
initializeResult.await()
}
override suspend fun updateSchema(schema: Schema) =
runWrapped {
waitReady()
updateSchemaInternal(schema)
}
private suspend fun updateSchemaInternal(schema: Schema) {
mutex.withLock {
if (this.syncSupervisorJob != null) {
throw PowerSyncException(
"Cannot update schema while connected",
cause = Exception("PowerSync client is already connected"),
)
}
val schemaJson = JsonUtil.json.encodeToString(schema)
internalDb.updateSchema(schemaJson)
this.schema = schema
}
}
override suspend fun connect(
connector: PowerSyncBackendConnector,
crudThrottleMs: Long,
retryDelayMs: Long,
params: Map<String, JsonParam?>,
options: SyncOptions,
appMetadata: Map<String, String>,
) {
waitReady()
mutex.withLock {
disconnectInternal()
connectInternal(crudThrottleMs) { scope ->
StreamingSyncClient(
bucketStorage = bucketStorage,
connector = connector,
uploadCrud = suspend { connector.uploadData(this) },
retryDelayMs = retryDelayMs,
logger = logger,
params = params.toJsonObject(),
uploadScope = scope,
options = options,
schema = schema,
activeSubscriptions = streams.currentlyReferencedStreams,
appMetadata = appMetadata,
)
}
}
}
private fun connectInternal(
crudThrottleMs: Long,
createStream: (CoroutineScope) -> StreamingSyncClient,
) {
val db = this
val job = SupervisorJob(scope.coroutineContext[Job])
syncSupervisorJob = job
var activeStream: StreamingSyncClient? = null
scope.launch(job) {
// Create the stream in this scope so that everything launched by the stream is bound to
// this coroutine scope that can be cancelled independently.
val stream = createStream(this)
activeStream = stream
launch {
// Get a global lock for checking mutex maps
val streamMutex = resource.group.syncMutex
// Poke the streaming mutex to see if another client is using it
var obtainedLock = false
try {
// This call will throw if the lock is already held by this db client.
// We should never reach that point since we disconnect before connecting.
obtainedLock = streamMutex.tryLock(db)
if (!obtainedLock) {
// The mutex is held already by another PowerSync instance (owner).
// (The tryLock should throw if this client already holds the lock).
logger.w(streamConflictMessage)
}
} catch (_: IllegalStateException) {
logger.e { "The streaming sync client did not disconnect before connecting" }
}
// This effectively queues operations
if (!obtainedLock) {
// This will throw a CancellationException if the job was cancelled while waiting.
streamMutex.lock(db)
}
// We have a lock if we reached here
try {
ensureActive()
stream.streamingSync()
} finally {
streamMutex.unlock(db)
}
}
launch {
currentStatus.trackOther(stream.status)
}
launch {
internalDb
.updatesOnTables()
.filter { it.contains(InternalTable.CRUD.toString()) }
.throttle(crudThrottleMs.milliseconds)
.collect {
stream.triggerCrudUploadAsync().join()
}
}
}
job.invokeOnCompletion {
if (it is DisconnectRequestedException) {
activeStream?.invalidateCredentials()
}
}
}
override suspend fun getCrudBatch(limit: Int): CrudBatch? {
waitReady()
if (!bucketStorage.hasCrud()) {
return null
}
var entries =
internalDb.getAll(
"SELECT id, tx_id, data FROM ps_crud ORDER BY id ASC LIMIT ?",
listOf(limit.toLong() + 1),
) {
CrudEntry.fromRow(
CrudRow(
id = it.getString("id"),
data = it.getString("data"),
txId = it.getLongOptional("tx_id")?.toInt(),
),
)
}
if (entries.isEmpty()) {
return null
}
val hasMore = entries.size > limit
if (hasMore) {
entries = entries.dropLast(1)
}
return CrudBatch(entries, hasMore, complete = { writeCheckpoint ->
handleWriteCheckpoint(entries.last().clientId, writeCheckpoint)
})
}
override fun getCrudTransactions(): Flow<CrudTransaction> =
flow {
waitReady()
var lastItemId = -1
// Note: We try to avoid filtering on tx_id here because there's no index on that column.
// Starting at the first entry we want and then joining by rowid is more efficient. This is
// sound because there can't be concurrent write transactions, so transaction ids are
// increasing when we iterate over rowids.
val query =
"""
WITH RECURSIVE crud_entries AS (
SELECT id, tx_id, data FROM ps_crud WHERE id = (SELECT min(id) FROM ps_crud WHERE id > ?)
UNION ALL
SELECT ps_crud.id, ps_crud.tx_id, ps_crud.data FROM ps_crud
INNER JOIN crud_entries ON crud_entries.id + 1 = rowid
WHERE crud_entries.tx_id = ps_crud.tx_id
)
SELECT * FROM crud_entries;
""".trimIndent()
while (true) {
val items = getAll(query, listOf(lastItemId), bucketStorage::mapCrudEntry)
if (items.isEmpty()) {
break
}
val txId = items[0].transactionId
val lastId = items.last().clientId
lastItemId = lastId
emit(
CrudTransaction(
crud = items,
transactionId = items[0].transactionId,
complete = { writeCheckpoint ->
logger.i {
"[CrudTransaction::complete] Completing transaction $txId (client ids until <=$lastId) with checkpoint $writeCheckpoint"
}
handleWriteCheckpoint(lastId, writeCheckpoint)
},
),
)
}
}
override fun syncStream(
name: String,
parameters: Map<String, JsonParam>?,
): SyncStream = PendingStream(streams, name, parameters)
override suspend fun getPowerSyncVersion(): String {
// The initialization sets powerSyncVersion.
waitReady()
return powerSyncVersion
}
override suspend fun <T> useConnection(
readOnly: Boolean,
block: suspend (SQLiteConnectionLease) -> T,
): T {
waitReady()
return internalDb.useConnection(readOnly, block)
}
override suspend fun <RowType : Any> get(
sql: String,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType,
): RowType {
waitReady()
return internalDb.get(sql, parameters, mapper)
}
override suspend fun <RowType : Any> getAll(
sql: String,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType,
): List<RowType> {
waitReady()
return internalDb.getAll(sql, parameters, mapper)
}
override suspend fun <RowType : Any> getOptional(
sql: String,
parameters: List<Any?>?,
mapper: (SqlCursor) -> RowType,
): RowType? {
waitReady()
return internalDb.getOptional(sql, parameters, mapper)
}
override fun onChange(
tables: Set<String>,
throttleMs: Long,
triggerImmediately: Boolean,
): Flow<Set<String>> =
flow {
waitReady()
emitAll(
internalDb.onChange(tables, throttleMs, triggerImmediately),
)
}
override fun <RowType : Any> watch(
sql: String,
parameters: List<Any?>?,
throttleMs: Long,
mapper: (SqlCursor) -> RowType,
): Flow<List<RowType>> =
flow {
waitReady()
emitAll(internalDb.watch(sql, parameters, throttleMs, mapper))
}
override suspend fun execute(
sql: String,
parameters: List<Any?>?,
): Long {
waitReady()
return internalDb.execute(sql, parameters)
}
private suspend fun handleWriteCheckpoint(
lastTransactionId: Int,
writeCheckpoint: String?,
) {
internalDb.writeTransactionAsync { transaction ->
transaction.executeAsync(
"DELETE FROM ps_crud WHERE id <= ?",
listOf(lastTransactionId.toLong()),
)
if (writeCheckpoint != null && !bucketStorage.hasCrud(transaction)) {
transaction.executeAsync(
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
listOf(writeCheckpoint),
)
} else {
transaction.executeAsync(
"UPDATE ps_buckets SET target_op = CAST(? AS INTEGER) WHERE name='\$local'",
listOf(bucketStorage.getMaxOpId()),
)
}
}
}
override suspend fun disconnect() {
waitReady()
mutex.withLock { disconnectInternal() }
}
private suspend fun disconnectInternal() {
val syncJob = syncSupervisorJob
if (syncJob != null && syncJob.isActive) {
// Using this exception type will also make the sync job invalidate credentials.
syncJob.cancel(DisconnectRequestedException())
syncJob.join()
syncSupervisorJob = null
}
currentStatus.update {
copy(
connected = false,
connecting = false,
downloading = false,
downloadProgress = null,
)
}
}
override suspend fun disconnectAndClear(
clearLocal: Boolean,
soft: Boolean,
) {
disconnect()
var flags = 0
if (clearLocal) {
flags = flags or 1 // MASK_CLEAR_LOCAL
}
if (soft) {
flags = flags or 2 // MASK_SOFT_CLEAR
}
internalDb.writeTransactionAsync { tx ->
tx.getOptionalAsync("SELECT powersync_clear(?)", listOf(flags)) {}
}
currentStatus.update { copy(lastSyncedAt = null, hasSynced = false) }
}
internal suspend fun resolveOfflineSyncStatusIfNotConnected() {
mutex.withLock {
if (syncSupervisorJob == null) {
// Not connected or connecting
resolveOfflineSyncStatus()
}
}
}
private suspend fun resolveOfflineSyncStatus() {
val offlineSyncStatus =
internalDb.get("SELECT powersync_offline_sync_status()") {
JsonUtil.json.decodeFromString<CoreSyncStatus>(it.getString(0)!!)
}
currentStatus.update {
applyCoreChanges(offlineSyncStatus)
}
}
override suspend fun waitForFirstSync() = waitForFirstSyncImpl(null)
override suspend fun waitForFirstSync(priority: StreamPriority) = waitForFirstSyncImpl(priority)
private suspend fun waitForFirstSyncImpl(priority: StreamPriority?) {
val predicate: (SyncStatusData) -> Boolean =
if (priority == null) {
{ it.hasSynced == true }
} else {
{ it.statusForPriority(priority).hasSynced == true }
}
waitForStatusMatching(predicate)
}
internal suspend fun waitForStatusMatching(predicate: (SyncStatusData) -> Boolean) {
if (predicate(currentStatus)) {
return
}
currentStatus.asFlow().first(predicate)
}
override suspend fun close() =
runWrapped {
mutex.withLock {
if (closed) {
return@withLock
}
initializeJob.cancelAndJoin()
disconnectInternal()
internalDb.close()
resource.dispose()
closed = true
}
}
/**
* Check that a supported version of the powersync extension is loaded.
*/
private fun checkVersion(powerSyncVersion: String) {
val version = PowerSyncVersion.parse(powerSyncVersion)
if (version < PowerSyncVersion.MINIMUM) {
PowerSyncVersion.mismatchError(powerSyncVersion)
}
}
}
internal class DisconnectRequestedException : CancellationException("disconnect() called")