Skip to content

Commit

Permalink
[idb_shim] add count support on store and index
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jul 22, 2024
1 parent eedbd1b commit 2669990
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 26 deletions.
4 changes: 1 addition & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
version: 2

enable-beta-ecosystems: true

updates:
- package-ecosystem: "pub"
directory: "idb_shim"
schedule:
interval: "monthly"

- package-ecosystem: "pub"
directory: "idb_test"
schedule:
interval: "monthly"

- package-ecosystem: "pub"
directory: "repo_support"
schedule:
interval: "monthly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
6 changes: 5 additions & 1 deletion idb_shim/lib/src/sdb/sdb_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ extension SdbIndexRefExtension<K extends KeyBase, V extends ValueBase,
impl.findRecordsImpl(client,
boundaries: boundaries, offset: offset, limit: limit);

/// Find records.
/// Find record keys.
Future<List<SdbIndexRecordKey<K, V, I>>> findRecordKeys(SdbClient client,
{SdbBoundaries<I>? boundaries, int? offset, int? limit}) =>
impl.findRecordKeysImpl(client,
boundaries: boundaries, offset: offset, limit: limit);

/// Count records.
Future<int> count(SdbClient client, {SdbBoundaries<I>? boundaries}) =>
impl.countImpl(client, boundaries: boundaries);
}

/// Extension on index on 1 field.
Expand Down
23 changes: 23 additions & 0 deletions idb_shim/lib/src/sdb/sdb_index_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,27 @@ class SdbIndexRefImpl<K extends KeyBase, V extends ValueBase,
return SdbIndexRecordKeyImpl<K, V, I>(this, key, indexKey);
}).toList();
}

/// Count records.
Future<int> countImpl(SdbClient client, {SdbBoundaries<I>? boundaries}) =>
client.handleDbOrTxn((db) => dbCountImpl(db, boundaries: boundaries),
(txn) => txnCountImpl(txn, boundaries: boundaries));

/// Count records.
Future<int> dbCountImpl(SdbDatabase db, {SdbBoundaries<I>? boundaries}) {
return db.inStoreTransaction(store, SdbTransactionMode.readOnly, (txn) {
return txnCountImpl(txn.rawImpl, boundaries: boundaries);
});
}

/// Find record keys.
Future<int> txnCountImpl(
SdbTransactionImpl txn, {
SdbBoundaries<I>? boundaries,
}) async {
var idbObjectStore = txn.idbTransaction.objectStore(store.name);
var idbIndex = idbObjectStore.index(name);
var count = idbIndex.count(idbKeyRangeFromBoundaries(boundaries));
return count;
}
}
4 changes: 4 additions & 0 deletions idb_shim/lib/src/sdb/sdb_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ extension SdbStoreRefExtension<K extends KeyBase, V extends ValueBase>
impl.findRecordKeysImpl(client,
boundaries: boundaries, offset: offset, limit: limit);

/// Count records.
Future<int> count(SdbClient client, {SdbBoundaries<K>? boundaries}) =>
impl.countImpl(client, boundaries: boundaries);

/// Record reference.
SdbRecordRef<K, V> record(K key) => SdbRecordRefImpl<K, V>(impl, key);

Expand Down
40 changes: 29 additions & 11 deletions idb_shim/lib/src/sdb/sdb_store_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,11 @@ class SdbStoreRefImpl<K extends KeyBase, V extends ValueBase>
(txn) => txnFindRecordsImpl(txn,
boundaries: boundaries, offset: offset, limit: limit));

/// Find records.
Future<List<SdbRecordKey<K, V>>> findRecordKeysImpl(SdbClient client,
{SdbBoundaries<K>? boundaries, int? offset, int? limit}) =>
client.handleDbOrTxn(
(db) => dbFindRecordKeysImpl(db,
boundaries: boundaries, offset: offset, limit: limit),
(txn) => txnFindRecordKeysImpl(txn,
boundaries: boundaries, offset: offset, limit: limit));

/// Find records.
Future<List<SdbRecordSnapshot<K, V>>> dbFindRecordsImpl(SdbDatabase db,
{SdbBoundaries<K>? boundaries, int? offset, int? limit}) {
return db.inStoreTransaction(this, SdbTransactionMode.readOnly, (txn) {
return txn.findRecords(
return txnFindRecordsImpl(txn.rawImpl,
boundaries: boundaries, offset: offset, limit: limit);
});
}
Expand All @@ -125,11 +116,20 @@ class SdbStoreRefImpl<K extends KeyBase, V extends ValueBase>
.findRecords(boundaries: boundaries, offset: offset, limit: limit);
}

/// Find records.
Future<List<SdbRecordKey<K, V>>> findRecordKeysImpl(SdbClient client,
{SdbBoundaries<K>? boundaries, int? offset, int? limit}) =>
client.handleDbOrTxn(
(db) => dbFindRecordKeysImpl(db,
boundaries: boundaries, offset: offset, limit: limit),
(txn) => txnFindRecordKeysImpl(txn,
boundaries: boundaries, offset: offset, limit: limit));

/// Find record keys.
Future<List<SdbRecordKey<K, V>>> dbFindRecordKeysImpl(SdbDatabase db,
{SdbBoundaries<K>? boundaries, int? offset, int? limit}) {
return db.inStoreTransaction(this, SdbTransactionMode.readOnly, (txn) {
return txn.findRecordKeys(
return txnFindRecordKeysImpl(txn.rawImpl,
boundaries: boundaries, offset: offset, limit: limit);
});
}
Expand All @@ -141,4 +141,22 @@ class SdbStoreRefImpl<K extends KeyBase, V extends ValueBase>
.storeImpl(this)
.findRecordKeys(boundaries: boundaries, offset: offset, limit: limit);
}

/// Count records.
Future<int> countImpl(SdbClient client, {SdbBoundaries<K>? boundaries}) =>
client.handleDbOrTxn((db) => dbCountImpl(db, boundaries: boundaries),
(txn) => txnCountImpl(txn, boundaries: boundaries));

/// Count records.
Future<int> dbCountImpl(SdbDatabase db, {SdbBoundaries<K>? boundaries}) {
return db.inStoreTransaction(this, SdbTransactionMode.readOnly, (txn) {
return txnCountImpl(txn.rawImpl, boundaries: boundaries);
});
}

/// Count records.
Future<int> txnCountImpl(SdbTransactionImpl txn,
{SdbBoundaries<K>? boundaries}) {
return txn.storeImpl(this).count(boundaries: boundaries);
}
}
4 changes: 4 additions & 0 deletions idb_shim/lib/src/sdb/sdb_transaction_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ extension SdbTransactionStoreRefExtension<K extends KeyBase,
_impl.findRecordKeysImpl(
boundaries: boundaries, offset: offset, limit: limit);

/// Count record.
Future<int> count({SdbBoundaries<K>? boundaries}) =>
_impl.countImpl(boundaries: boundaries);

/// store name.
String get name => store.name;
}
Expand Down
5 changes: 5 additions & 0 deletions idb_shim/lib/src/sdb/sdb_transaction_store_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ class SdbTransactionStoreRefImpl<K extends KeyBase, V extends ValueBase>
return SdbRecordKeyImpl<K, V>(store, key);
}).toList();
}

/// Count records.
Future<int> countImpl({SdbBoundaries<K>? boundaries}) async {
return idbObjectStore.count(idbKeyRangeFromBoundaries(boundaries));
}
}

/// Multi store transaction internal extension.
Expand Down
34 changes: 25 additions & 9 deletions idb_test/lib/sdb_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ void simpleDbTest(idb.IdbFactory idbFactory) {
await txn.add({'test': 2});
await txn.add({'test': 3});
});
var records = await testStore.findRecords(db,
boundaries: SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3)));
var boundaries = SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3));
var records = await testStore.findRecords(db, boundaries: boundaries);
expect(records.length, 2);
var keys = await testStore.findRecordKeys(db, boundaries: boundaries);
expect(keys.keys, [1, 2]);
var count = await testStore.count(db, boundaries: boundaries);
expect(count, 2);

await db.close();
});
Expand All @@ -122,10 +126,14 @@ void simpleDbTest(idb.IdbFactory idbFactory) {
await txn.add({'test': 2});
await txn.add({'test': 3});

var records = await testStore.findRecords(txn,
boundaries:
SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3)));
var boundaries =
SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3));
var records = await testStore.findRecords(txn, boundaries: boundaries);
expect(records.length, 2);
var keys = await testStore.findRecordKeys(txn, boundaries: boundaries);
expect(keys.keys, [1, 2]);
var count = await testStore.count(txn, boundaries: boundaries);
expect(count, 2);
});

await db.close();
Expand Down Expand Up @@ -214,13 +222,21 @@ void simpleDbTest(idb.IdbFactory idbFactory) {
await txn.add({'test': 1});
await txn.add({'test': 2});
await txn.add({'test': 3});

var boundaries =
SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3));
var records = await testIndex.findRecords(txn, boundaries: boundaries);
expect(records.length, 2);
var keys = await testIndex.findRecordKeys(txn, boundaries: boundaries);
expect(keys.length, 2);
expect(await testIndex.count(txn, boundaries: boundaries), 2);
});
var records = await testIndex.findRecords(db,
boundaries: SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3)));
var boundaries = SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3));
var records = await testIndex.findRecords(db, boundaries: boundaries);
expect(records.length, 2);
var keys = await testIndex.findRecordKeys(db,
boundaries: SdbBoundaries(SdbLowerBoundary(1), SdbUpperBoundary(3)));
var keys = await testIndex.findRecordKeys(db, boundaries: boundaries);
expect(keys.length, 2);
expect(await testIndex.count(db, boundaries: boundaries), 2);

await db.close();
});
Expand Down
2 changes: 1 addition & 1 deletion idb_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ homepage: https://github.com/tekartik/idb_shim.dart/tree/master/idb_test
publish_to: none

environment:
sdk: '>=3.4.0-0 <4.0.0'
sdk: ^3.4.0

dependencies:
idb_shim: '>=2.4.0-1 <3.0.0'
Expand Down
2 changes: 1 addition & 1 deletion repo_support/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 0.1.0
publish_to: none

environment:
sdk: '>=3.0.0 <4.0.0'
sdk: ^3.4.0

dependencies:
process_run: '>=0.10.0'
Expand Down

0 comments on commit 2669990

Please sign in to comment.