Skip to content

Commit 6dc65bd

Browse files
committed
Remove legacy finalizers
1 parent 53b24c3 commit 6dc65bd

File tree

4 files changed

+44
-150
lines changed

4 files changed

+44
-150
lines changed

sqlite3/lib/src/implementation/database.dart

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,62 +13,20 @@ import '../result_set.dart';
1313
import '../statement.dart';
1414
import 'bindings.dart';
1515
import 'exception.dart';
16-
import 'finalizer.dart';
1716
import 'statement.dart';
1817
import 'utils.dart';
1918

20-
/// Contains the state of a database needed for finalization.
21-
///
22-
/// This is extracted into separate object so that it can be used as a
23-
/// finalization token. It will get disposed when the main database is no longer
24-
/// reachable without being closed.
25-
final class FinalizableDatabase extends FinalizablePart {
26-
final RawSqliteBindings bindings;
27-
final RawSqliteDatabase database;
28-
29-
final List<FinalizableStatement> _statements = [];
30-
final List<void Function()> dartCleanup = [];
31-
32-
FinalizableDatabase(this.bindings, this.database);
33-
34-
@override
35-
void dispose() {
36-
for (final stmt in _statements) {
37-
stmt.dispose();
38-
}
39-
40-
for (final cleanup in dartCleanup.toList()) {
41-
cleanup();
42-
}
43-
44-
final code = database.sqlite3_close_v2();
45-
SqliteException? exception;
46-
if (code != SqlError.SQLITE_OK) {
47-
exception = createExceptionRaw(
48-
bindings,
49-
database,
50-
code,
51-
operation: 'closing database',
52-
);
53-
}
54-
55-
if (exception != null) {
56-
throw exception;
57-
}
58-
}
59-
}
60-
6119
base class DatabaseImplementation implements CommonDatabase {
6220
final RawSqliteBindings bindings;
21+
// Note: Implementations of this have platform-specific finalizers on them.
6322
final RawSqliteDatabase database;
6423

65-
final FinalizableDatabase finalizable;
66-
6724
_StreamHandlers<SqliteUpdate, void Function()>? _updates;
6825
_StreamHandlers<void, void Function()>? _rollbacks;
6926
_StreamHandlers<void, VoidPredicate>? _commits;
7027

71-
var _isClosed = false;
28+
@internal
29+
var isClosed = false;
7230

7331
@override
7432
DatabaseConfig get config => DatabaseConfigImplementation(this);
@@ -91,24 +49,15 @@ base class DatabaseImplementation implements CommonDatabase {
9149
execute('PRAGMA user_version = $value;');
9250
}
9351

94-
DatabaseImplementation(this.bindings, this.database)
95-
: finalizable = FinalizableDatabase(bindings, database) {
96-
disposeFinalizer.attach(this, finalizable, detach: this);
97-
}
52+
DatabaseImplementation(this.bindings, this.database);
9853

9954
@visibleForOverriding
10055
StatementImplementation wrapStatement(String sql, RawSqliteStatement stmt) {
10156
return StatementImplementation(sql, this, stmt);
10257
}
10358

104-
void handleFinalized(StatementImplementation stmt) {
105-
if (!_isClosed) {
106-
finalizable._statements.remove(stmt.finalizable);
107-
}
108-
}
109-
11059
void _ensureOpen() {
111-
if (_isClosed) {
60+
if (isClosed) {
11261
throw StateError('This database has already been closed');
11362
}
11463
}
@@ -289,10 +238,9 @@ base class DatabaseImplementation implements CommonDatabase {
289238

290239
@override
291240
void dispose() {
292-
if (_isClosed) return;
241+
if (isClosed) return;
293242

294-
disposeFinalizer.detach(this);
295-
_isClosed = true;
243+
isClosed = true;
296244

297245
_updates?.close();
298246
_commits?.close();
@@ -302,7 +250,20 @@ base class DatabaseImplementation implements CommonDatabase {
302250
database.sqlite3_commit_hook(null);
303251
database.sqlite3_rollback_hook(null);
304252

305-
finalizable.dispose();
253+
final code = database.sqlite3_close_v2();
254+
SqliteException? exception;
255+
if (code != SqlError.SQLITE_OK) {
256+
exception = createExceptionRaw(
257+
bindings,
258+
database,
259+
code,
260+
operation: 'closing database',
261+
);
262+
}
263+
264+
if (exception != null) {
265+
throw exception;
266+
}
306267
}
307268

308269
@override
@@ -447,11 +408,6 @@ base class DatabaseImplementation implements CommonDatabase {
447408
}
448409

449410
compiler.close();
450-
451-
for (final created in createdStatements) {
452-
finalizable._statements.add(created.finalizable);
453-
}
454-
455411
return createdStatements;
456412
}
457413

@@ -661,7 +617,7 @@ final class _StreamHandlers<T, SyncCallback> {
661617

662618
Stream<T> _generateStream(bool dispatchSynchronously) {
663619
return Stream.multi((newListener) {
664-
if (_database._isClosed) {
620+
if (_database.isClosed) {
665621
newListener.close();
666622
return;
667623
}
@@ -713,7 +669,7 @@ final class _StreamHandlers<T, SyncCallback> {
713669
void _removeAsyncListener(MultiStreamController<T> listener, bool sync) {
714670
_asyncListeners.remove((controller: listener, sync: sync));
715671

716-
if (!hasListener && !_database._isClosed) {
672+
if (!hasListener && !_database.isClosed) {
717673
_unregister();
718674
}
719675
}

sqlite3/lib/src/implementation/finalizer.dart

Lines changed: 0 additions & 37 deletions
This file was deleted.

sqlite3/lib/src/implementation/session.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ import 'utils.dart';
1010

1111
final class SessionImplementation implements Session {
1212
final RawSqliteBindings bindings;
13+
// Note: Implementations of this have platform-specific finalizers on them.
1314
final RawSqliteSession session;
14-
final FinalizableDatabase database;
1515

1616
bool _deleted = false;
1717

18-
SessionImplementation(this.bindings, this.session, this.database) {
19-
database.dartCleanup.add(delete);
20-
}
18+
SessionImplementation(this.bindings, this.session);
2119

2220
static SessionImplementation createSession(
2321
DatabaseImplementation db,
2422
String name,
2523
) {
2624
final bindings = db.bindings;
2725
final result = bindings.sqlite3session_create(db.database, name);
28-
return SessionImplementation(bindings, result, db.finalizable);
26+
return SessionImplementation(bindings, result);
2927
}
3028

3129
void _checkNotDeleted() {
@@ -53,7 +51,6 @@ final class SessionImplementation implements Session {
5351
if (!_deleted) {
5452
_deleted = true;
5553
session.sqlite3session_delete();
56-
database.dartCleanup.remove(delete);
5754
}
5855
}
5956

sqlite3/lib/src/implementation/statement.dart

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,22 @@ import '../statement.dart';
44
import 'bindings.dart';
55
import 'database.dart';
66
import 'exception.dart';
7-
import 'finalizer.dart';
87
import 'utils.dart';
98

10-
final class FinalizableStatement extends FinalizablePart {
11-
final RawSqliteStatement statement;
12-
13-
bool _inResetState = true;
14-
bool _closed = false;
15-
16-
FinalizableStatement(this.statement);
17-
18-
@override
19-
void dispose() {
20-
if (!_closed) {
21-
_closed = true;
22-
_reset();
23-
statement.sqlite3_finalize();
24-
}
25-
}
26-
27-
void _reset() {
28-
if (!_inResetState) {
29-
statement.sqlite3_reset();
30-
_inResetState = true;
31-
}
32-
}
33-
}
34-
359
base class StatementImplementation extends CommonPreparedStatement {
10+
// Note: Implementations of this have platform-specific finalizers on them.
3611
final RawSqliteStatement statement;
3712
final DatabaseImplementation database;
38-
final FinalizableStatement finalizable;
3913

4014
@override
4115
final String sql;
4216
List<Object?>? _latestArguments;
17+
bool _inResetState = true;
18+
bool _closed = false;
4319

4420
_ActiveCursorIterator? _currentCursor;
4521

46-
StatementImplementation(this.sql, this.database, this.statement)
47-
: finalizable = FinalizableStatement(statement);
22+
StatementImplementation(this.sql, this.database, this.statement);
4823

4924
List<String> get _columnNames {
5025
final columnCount = statement.sqlite3_column_count();
@@ -64,7 +39,7 @@ base class StatementImplementation extends CommonPreparedStatement {
6439
}
6540

6641
void _ensureNotFinalized() {
67-
if (finalizable._closed) {
42+
if (_closed || database.isClosed) {
6843
throw StateError('Tried to operate on a released prepared statement');
6944
}
7045
}
@@ -87,7 +62,7 @@ base class StatementImplementation extends CommonPreparedStatement {
8762
void _execute() {
8863
int result;
8964

90-
finalizable._inResetState = false;
65+
_inResetState = false;
9166
// Users should be able to execute statements returning rows, so we should
9267
// call _step() to skip past rows.
9368
do {
@@ -107,7 +82,7 @@ base class StatementImplementation extends CommonPreparedStatement {
10782

10883
ResultSet _selectResults() {
10984
final rows = <List<Object?>>[];
110-
finalizable._inResetState = false;
85+
_inResetState = false;
11186

11287
int columnCount = -1;
11388

@@ -275,17 +250,20 @@ base class StatementImplementation extends CommonPreparedStatement {
275250

276251
@override
277252
void reset() {
278-
finalizable._reset();
253+
if (!_inResetState) {
254+
statement.sqlite3_reset();
255+
_inResetState = true;
256+
}
257+
279258
_currentCursor = null;
280259
}
281260

282261
@override
283262
void dispose() {
284-
if (!finalizable._closed) {
285-
disposeFinalizer.detach(this);
286-
finalizable.dispose();
287-
288-
database.handleFinalized(this);
263+
if (!_closed) {
264+
_closed = true;
265+
reset();
266+
statement.sqlite3_finalize();
289267
}
290268
}
291269

@@ -356,12 +334,12 @@ class _ActiveCursorIterator extends IteratingCursor {
356334

357335
_ActiveCursorIterator(this.statement)
358336
: super(statement._columnNames, statement._tableNames) {
359-
statement.finalizable._inResetState = false;
337+
statement._inResetState = false;
360338
}
361339

362340
@override
363341
bool moveNext() {
364-
if (statement.finalizable._closed || statement._currentCursor != this) {
342+
if (statement._closed || statement._currentCursor != this) {
365343
return false;
366344
}
367345

0 commit comments

Comments
 (0)