Skip to content

Commit 3cfd012

Browse files
authored
Refactor task backend's low-level data access. (#8710)
1 parent 76a2116 commit 3cfd012

File tree

4 files changed

+236
-90
lines changed

4 files changed

+236
-90
lines changed

app/lib/package/backend.dart

+64
Original file line numberDiff line numberDiff line change
@@ -1990,3 +1990,67 @@ void checkPackageVersionParams(String package, [String? version]) {
19901990
}
19911991
}
19921992
}
1993+
1994+
/// Low-level, narrowly typed data access methods for [Package] entity.
1995+
extension PackageDatastoreDBExt on DatastoreDB {
1996+
_PackageDataAccess get packages => _PackageDataAccess(this);
1997+
}
1998+
1999+
extension PackageTransactionWrapperExt on TransactionWrapper {
2000+
_PackageTransactionDataAcccess get packages =>
2001+
_PackageTransactionDataAcccess(this);
2002+
2003+
_VersionTransactionDataAcccess get versions =>
2004+
_VersionTransactionDataAcccess(this);
2005+
}
2006+
2007+
final class _PackageDataAccess {
2008+
final DatastoreDB _db;
2009+
2010+
_PackageDataAccess(this._db);
2011+
2012+
Future<bool> exists(String name) async {
2013+
final packageKey = _db.emptyKey.append(Package, id: name);
2014+
final package = await _db.lookupOrNull<Package>(packageKey);
2015+
return package != null;
2016+
}
2017+
2018+
Stream<({String name})> listAllNames() async* {
2019+
final query = _db.query<Package>();
2020+
await for (final p in query.run()) {
2021+
yield (name: p.name!);
2022+
}
2023+
}
2024+
2025+
Stream<({String name, DateTime updated})> listUpdatedSince(
2026+
DateTime since) async* {
2027+
final query = _db.query<Package>()
2028+
..filter('updated >', since)
2029+
..order('-updated');
2030+
await for (final p in query.run()) {
2031+
yield (name: p.name!, updated: p.updated!);
2032+
}
2033+
}
2034+
}
2035+
2036+
class _PackageTransactionDataAcccess {
2037+
final TransactionWrapper _tx;
2038+
2039+
_PackageTransactionDataAcccess(this._tx);
2040+
2041+
Future<Package?> lookupOrNull(String name) async {
2042+
final pkgKey = _tx.emptyKey.append(Package, id: name);
2043+
return await _tx.lookupOrNull<Package>(pkgKey);
2044+
}
2045+
}
2046+
2047+
class _VersionTransactionDataAcccess {
2048+
final TransactionWrapper _tx;
2049+
2050+
_VersionTransactionDataAcccess(this._tx);
2051+
2052+
Future<List<PackageVersion>> listVersionsOfPackage(String name) async {
2053+
final pkgKey = _tx.emptyKey.append(Package, id: name);
2054+
return await _tx.query<PackageVersion>(pkgKey).run().toList();
2055+
}
2056+
}

app/lib/shared/datastore.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ final Logger _logger = Logger('pub.datastore_helper');
2121
/// Wrap [Transaction] to avoid exposing [Transaction.commit] and
2222
/// [Transaction.rollback].
2323
class TransactionWrapper {
24+
final DatastoreDB _db;
2425
final Transaction _tx;
2526
bool _mutated = false;
2627

27-
TransactionWrapper._(this._tx);
28+
TransactionWrapper._(this._db, this._tx);
29+
30+
Key get emptyKey => _db.emptyKey;
2831

2932
/// See [Transaction.lookup].
3033
Future<List<T?>> lookup<T extends Model>(List<Key> keys) =>
@@ -111,7 +114,7 @@ Future<T> _withTransaction<T>(
111114
return await db.withTransaction<T>((tx) async {
112115
bool commitAttempted = false;
113116
try {
114-
final wrapper = TransactionWrapper._(tx);
117+
final wrapper = TransactionWrapper._(db, tx);
115118
final retval = await fn(wrapper);
116119
if (wrapper._mutated) {
117120
commitAttempted = true;

0 commit comments

Comments
 (0)