-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMDSQLiteAdapter.cs
More file actions
473 lines (398 loc) · 14.1 KB
/
MDSQLiteAdapter.cs
File metadata and controls
473 lines (398 loc) · 14.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
namespace PowerSync.Common.MDSQLite;
using System;
using System.Collections.Generic;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Nito.AsyncEx;
using PowerSync.Common.DB;
using PowerSync.Common.Utils;
public class MDSQLiteAdapterOptions()
{
public string Name { get; set; } = null!;
public MDSQLiteOptions? SqliteOptions;
}
public class MDSQLiteAdapter : IDBAdapter
{
public string Name => options.Name;
public DBAdapterEvents Events { get; } = new();
// One writer
private MDSQLiteConnection writeConnection = null!;
private readonly AsyncLock writeMutex = new();
// Many readers
private MDSQLiteConnectionPool readPool = null!;
private readonly Task initialized;
protected MDSQLiteAdapterOptions options;
protected RequiredMDSQLiteOptions resolvedOptions;
private CancellationTokenSource? tablesUpdatedCts;
private Task? tablesUpdatedTask;
public MDSQLiteAdapter(MDSQLiteAdapterOptions options)
{
this.options = options;
resolvedOptions = ResolveMDSQLiteOptions(options.SqliteOptions);
initialized = Init();
}
private RequiredMDSQLiteOptions ResolveMDSQLiteOptions(MDSQLiteOptions? options)
{
var defaults = RequiredMDSQLiteOptions.DEFAULT_SQLITE_OPTIONS;
return new RequiredMDSQLiteOptions
{
JournalMode = options?.JournalMode ?? defaults.JournalMode,
Synchronous = options?.Synchronous ?? defaults.Synchronous,
JournalSizeLimit = options?.JournalSizeLimit ?? defaults.JournalSizeLimit,
CacheSizeKb = options?.CacheSizeKb ?? defaults.CacheSizeKb,
TemporaryStorage = options?.TemporaryStorage ?? defaults.TemporaryStorage,
LockTimeoutMs = options?.LockTimeoutMs ?? defaults.LockTimeoutMs,
EncryptionKey = options?.EncryptionKey ?? defaults.EncryptionKey,
Extensions = options?.Extensions ?? defaults.Extensions,
LoadPowerSyncExtension = options?.LoadPowerSyncExtension ?? defaults.LoadPowerSyncExtension,
ReadPoolSize = options?.ReadPoolSize ?? defaults.ReadPoolSize,
};
}
private async Task Init()
{
string[] baseStatements =
[
$"PRAGMA busy_timeout = {resolvedOptions.LockTimeoutMs}",
$"PRAGMA cache_size = -{resolvedOptions.CacheSizeKb}",
$"PRAGMA temp_store = {resolvedOptions.TemporaryStorage}"
];
string[] writeConnectionStatements =
[
.. baseStatements,
$"PRAGMA journal_mode = {resolvedOptions.JournalMode}",
$"PRAGMA journal_size_limit = {resolvedOptions.JournalSizeLimit}",
$"PRAGMA synchronous = {resolvedOptions.Synchronous}",
];
string[] readConnectionStatements =
[
.. baseStatements,
"PRAGMA query_only = true",
];
// Prepare write connection
writeConnection = await OpenConnection(options.Name);
foreach (var statement in writeConnectionStatements)
{
await writeConnection!.Execute(statement);
}
// Prepare read pool and create connection factory
Func<Task<MDSQLiteConnection>> readConnectionFactory = async () =>
{
var readConnection = await OpenConnection(options.Name);
foreach (var statement in readConnectionStatements)
{
await readConnection.Execute(statement);
}
return readConnection;
};
readPool = new MDSQLiteConnectionPool(resolvedOptions.ReadPoolSize, readConnectionFactory);
await readPool.Init();
// Register TablesUpdated listener
tablesUpdatedCts = new CancellationTokenSource();
tablesUpdatedTask = Task.Run(async () =>
{
await foreach (var notification in writeConnection.ListenAsync(tablesUpdatedCts.Token))
{
if (notification.TablesUpdated != null)
{
Events.Emit(notification);
}
}
});
}
protected async Task<MDSQLiteConnection> OpenConnection(string dbFilename)
{
var db = OpenDatabase(dbFilename);
LoadExtensions(db);
var connection = new MDSQLiteConnection(new MDSQLiteConnectionOptions(db));
try
{
await connection.Execute("SELECT powersync_init()");
}
catch (SqliteException ex)
{
// SQLite will throw a very unhelpful "SQLite Error 1: 'The specified
// module could not be found.'" error if uncaught.
throw new SqliteException(
"Failed to initialize PowerSync: powersync_init() is not registered. " +
"Ensure the PowerSync core SQLite extension is loaded. Either set " +
"MDSQLiteOptions.LoadPowerSyncExtension to true (default), or supply " +
"a PowerSync-compatible extension via MDSQLiteOptions.Extensions.",
ex.SqliteErrorCode,
ex.SqliteExtendedErrorCode);
}
return connection;
}
private static SqliteConnection OpenDatabase(string dbFilename)
{
string connectionString = $"Data Source={dbFilename};Pooling=False;";
var connection = new SqliteConnection(connectionString);
connection.Open();
return connection;
}
protected virtual void LoadExtensions(SqliteConnection db)
{
db.EnableExtensions(true);
if (resolvedOptions.LoadPowerSyncExtension)
{
LoadDefaultPowerSyncExtension(db);
}
foreach (var extension in resolvedOptions.Extensions)
{
db.LoadExtension(extension.Path, extension.EntryPoint);
}
}
/// <summary>
/// Loads the bundled PowerSync core SQLite extension. Override on
/// platform-specific adapters (e.g. MAUI iOS/Android) where the native library
/// lives outside the desktop runtime path.
/// </summary>
protected virtual void LoadDefaultPowerSyncExtension(SqliteConnection db)
{
var path = PowerSyncPathResolver.GetNativeLibraryPath(AppContext.BaseDirectory);
db.LoadExtension(path, "sqlite3_powersync_init");
}
public async Task Close()
{
tablesUpdatedCts?.Cancel();
try { tablesUpdatedTask?.Wait(2000); } catch { /* expected */ }
writeConnection?.Close();
await readPool.Close();
Events.Close();
}
public async Task<NonQueryResult> Execute(string query, object?[]? parameters = null)
{
return await WriteLock((ctx) => ctx.Execute(query, parameters));
}
public async Task<NonQueryResult> ExecuteBatch(string query, object?[][]? parameters = null)
{
return await WriteTransaction((ctx) => ctx.ExecuteBatch(query, parameters));
}
public async Task<T[]> GetAll<T>(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.GetAll<T>(sql, parameters));
}
public async Task<dynamic[]> GetAll(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.GetAll(sql, parameters));
}
public async Task<T?> GetOptional<T>(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.GetOptional<T>(sql, parameters));
}
public async Task<dynamic?> GetOptional(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.GetOptional(sql, parameters));
}
public async Task<T> Get<T>(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.Get<T>(sql, parameters));
}
public async Task<dynamic> Get(string sql, object?[]? parameters = null)
{
return await ReadLock((ctx) => ctx.Get(sql, parameters));
}
public async Task<T> ReadTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null)
{
return await ReadLock((ctx) => InternalTransaction(new MDSQLiteTransaction((MDSQLiteConnection)ctx), fn));
}
public async Task<T> ReadLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null)
{
await initialized;
return await readPool.Lease(fn);
}
public async Task WriteLock(Func<ILockContext, Task> fn, DBLockOptions? options = null)
{
await initialized;
using (await writeMutex.LockAsync())
{
await fn(writeConnection);
}
writeConnection.FlushUpdates();
}
public async Task<T> WriteLock<T>(Func<ILockContext, Task<T>> fn, DBLockOptions? options = null)
{
await initialized;
T result;
using (await writeMutex.LockAsync())
{
result = await fn(writeConnection);
}
writeConnection.FlushUpdates();
return result;
}
public async Task WriteTransaction(Func<ITransaction, Task> fn, DBLockOptions? options = null)
{
await WriteLock(ctx => InternalTransaction(new MDSQLiteTransaction(writeConnection), fn));
}
public async Task<T> WriteTransaction<T>(Func<ITransaction, Task<T>> fn, DBLockOptions? options = null)
{
return await WriteLock((ctx) => InternalTransaction(new MDSQLiteTransaction(writeConnection), fn));
}
protected static async Task InternalTransaction(
MDSQLiteTransaction ctx,
Func<ITransaction, Task> fn)
{
await RunTransaction(ctx, () => fn(ctx));
}
protected static async Task<T> InternalTransaction<T>(
MDSQLiteTransaction ctx,
Func<ITransaction, Task<T>> fn)
{
T result = default!;
await RunTransaction(ctx, async () =>
{
result = await fn(ctx);
});
return result;
}
private static async Task RunTransaction(
MDSQLiteTransaction ctx,
Func<Task> action)
{
try
{
await ctx.Begin();
await action();
await ctx.Commit();
}
catch (Exception)
{
// In rare cases, a rollback may fail. Safe to ignore.
try { await ctx.Rollback(); }
catch
{
// Ignore rollback errors
}
throw;
}
}
public async Task RefreshSchema()
{
await initialized;
await writeConnection.RefreshSchema();
await readPool.LeaseAll(async (connections) =>
{
foreach (var conn in connections) await conn.RefreshSchema();
});
}
}
class MDSQLiteConnectionPool
{
private readonly Channel<MDSQLiteConnection> _channel;
private readonly int _poolSize;
private readonly Func<Task<MDSQLiteConnection>> _connectionFactory;
private readonly Task _initialized;
public MDSQLiteConnectionPool(int poolSize, Func<Task<MDSQLiteConnection>> connectionFactory)
{
_channel = Channel.CreateBounded<MDSQLiteConnection>(poolSize);
_poolSize = poolSize;
_connectionFactory = connectionFactory;
_initialized = Initialize();
}
public async Task Init() => await _initialized;
private async Task Initialize()
{
for (int i = 0; i < _poolSize; i++)
{
var connection = await _connectionFactory();
await _channel.Writer.WriteAsync(connection);
}
}
public async Task<T> Lease<T>(Func<MDSQLiteConnection, Task<T>> callback)
{
await _initialized;
var connection = await _channel.Reader.ReadAsync();
try
{
return await callback(connection);
}
finally
{
await _channel.Writer.WriteAsync(connection);
}
}
public async Task LeaseAll(Func<List<MDSQLiteConnection>, Task> callback)
{
await _initialized;
var connections = new List<MDSQLiteConnection>(_poolSize);
for (int i = 0; i < _poolSize; i++)
{
connections.Add(await _channel.Reader.ReadAsync());
}
try
{
await callback(connections);
}
finally
{
foreach (var conn in connections)
{
_channel.Writer.TryWrite(conn);
}
}
}
public async Task Close()
{
await LeaseAll((connections) =>
{
foreach (var conn in connections) conn.Close();
return Task.CompletedTask;
});
_channel.Writer.TryComplete();
}
}
public class MDSQLiteTransaction(MDSQLiteConnection connection) : ITransaction
{
private readonly MDSQLiteConnection connection = connection;
private bool finalized = false;
public async Task Begin()
{
if (finalized) return;
await connection.Execute("BEGIN");
}
public async Task Commit()
{
if (finalized) return;
finalized = true;
await connection.Execute("COMMIT");
}
public async Task Rollback()
{
if (finalized) return;
finalized = true;
await connection.Execute("ROLLBACK");
}
public Task<NonQueryResult> Execute(string query, object?[]? parameters = null)
{
return connection.Execute(query, parameters);
}
public Task<NonQueryResult> ExecuteBatch(string query, object?[][]? parameters = null)
{
return connection.ExecuteBatch(query, parameters);
}
public Task<T[]> GetAll<T>(string sql, object?[]? parameters = null)
{
return connection.GetAll<T>(sql, parameters);
}
public Task<dynamic[]> GetAll(string sql, object?[]? parameters = null)
{
return connection.GetAll(sql, parameters);
}
public Task<T?> GetOptional<T>(string sql, object?[]? parameters = null)
{
return connection.GetOptional<T>(sql, parameters);
}
public Task<dynamic?> GetOptional(string sql, object?[]? parameters = null)
{
return connection.GetOptional(sql, parameters);
}
public Task<T> Get<T>(string sql, object?[]? parameters = null)
{
return connection.Get<T>(sql, parameters);
}
public Task<dynamic> Get(string sql, object?[]? parameters = null)
{
return connection.Get(sql, parameters);
}
}