Skip to content
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

[Web] Flush at the end of each writeLock #77

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2024-11-06

### Changes

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`sqlite_async` - `v0.11.0`](#sqlite_async---v0110)
- [`drift_sqlite_async` - `v0.2.0-alpha.4`](#drift_sqlite_async---v020-alpha4)

Packages with dependency updates only:

> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.

- `drift_sqlite_async` - `v0.2.0-alpha.4`

---

#### `sqlite_async` - `v0.11.0`

- Automatically flush IndexedDB storage to fix durability issues


## 2024-11-01

### Changes
Expand Down
4 changes: 4 additions & 0 deletions packages/drift_sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0-alpha.4

- Update a dependency to the latest release.

## 0.2.0-alpha.3

- Bump `sqlite_async` to v0.10.1
Expand Down
4 changes: 2 additions & 2 deletions packages/drift_sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: drift_sqlite_async
version: 0.2.0-alpha.3
version: 0.2.0-alpha.4
homepage: https://github.com/powersync-ja/sqlite_async.dart
repository: https://github.com/powersync-ja/sqlite_async.dart
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.
Expand All @@ -15,7 +15,7 @@ environment:
sdk: ">=3.0.0 <4.0.0"
dependencies:
drift: ">=2.19.0 <3.0.0"
sqlite_async: ^0.10.1
sqlite_async: ^0.11.0
dev_dependencies:
build_runner: ^2.4.8
drift_dev: ">=2.19.0 <3.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.11.0

- Automatically flush IndexedDB storage to fix durability issues

## 0.10.1

- For database setups not using a shared worker, use a `BroadcastChannel` to share updates across different tabs.
Expand Down
20 changes: 17 additions & 3 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class WebDatabase
@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout}) {
{Duration? lockTimeout,
bool? flush}) {
return writeLock(
(writeContext) =>
internalWriteTransaction(writeContext, (context) async {
Expand All @@ -122,21 +123,25 @@ class WebDatabase
return callback(_ExclusiveTransactionContext(this, writeContext));
}),
debugContext: 'writeTransaction()',
lockTimeout: lockTimeout);
lockTimeout: lockTimeout,
flush: flush);
}

@override

/// Internal writeLock which intercepts transaction context's to verify auto commit is not active
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
if (_mutex case var mutex?) {
return await mutex.lock(() async {
final context = _ExclusiveContext(this);
try {
return await callback(context);
} finally {
context.markClosed();
if (flush != false) {
await this.flush();
}
}
});
} else {
Expand All @@ -148,11 +153,20 @@ class WebDatabase
return await callback(context);
} finally {
context.markClosed();
if (flush != false) {
await this.flush();
}
await _database.customRequest(
CustomDatabaseMessage(CustomDatabaseMessageKind.releaseLock));
}
}
}

@override
Future<void> flush() async {
await isInitialized;
return _database.fileSystem.flush();
}
}

class _SharedContext implements SqliteReadContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,32 @@ class SqliteDatabaseImpl

@override
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext}) async {
{Duration? lockTimeout, String? debugContext, bool? flush}) async {
await isInitialized;
return _runZoned(() {
return _connection.writeLock(callback,
lockTimeout: lockTimeout, debugContext: debugContext);
lockTimeout: lockTimeout, debugContext: debugContext, flush: flush);
}, debugContext: debugContext ?? 'execute()');
}

@override
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout}) async {
{Duration? lockTimeout,
bool? flush}) async {
await isInitialized;
return _runZoned(
() => _connection.writeTransaction(callback, lockTimeout: lockTimeout),
() => _connection.writeTransaction(callback,
lockTimeout: lockTimeout, flush: flush),
debugContext: 'writeTransaction()');
}

@override
Future<void> flush() async {
await isInitialized;
return _connection.flush();
}

@override
Future<void> close() async {
await isInitialized;
Expand Down
30 changes: 30 additions & 0 deletions packages/sqlite_async/lib/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,34 @@ abstract class WebSqliteConnection implements SqliteConnection {
);
return database;
}

/// Same as [SqliteConnection.writeLock].
///
/// Has an additional [flush] (defaults to true). This can be set to false
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext, bool? flush});

/// Same as [SqliteConnection.writeTransaction].
///
/// Has an additional [flush] (defaults to true). This can be set to false
/// to delay flushing changes to the database file, losing durability guarantees.
/// This only has an effect when IndexedDB storage is used.
///
/// See [flush] for details.
Future<T> writeTransaction<T>(
Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout,
bool? flush});

/// Flush changes to the underlying storage.
///
/// When this returns, all changes previously written will be persisted
/// to storage.
///
/// This only has an effect when IndexedDB storage is used.
Future<void> flush();
}
2 changes: 1 addition & 1 deletion packages/sqlite_async/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sqlite_async
description: High-performance asynchronous interface for SQLite on Dart and Flutter.
version: 0.10.1
version: 0.11.0
repository: https://github.com/powersync-ja/sqlite_async.dart
environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down
Loading