Skip to content

Commit

Permalink
feat: Added count() functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Sep 5, 2023
1 parent e6a656d commit 5c2f188
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 127 deletions.
48 changes: 28 additions & 20 deletions packages/katana_model/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: "58826e40314219b223f4723dd4205845040161cdc2df3e6a1cdceed5d8165084"
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
url: "https://pub.dev"
source: hosted
version: "63.0.0"
version: "64.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: f85566ec7b3d25cbea60f7dd4f157c5025f2f19233ca4feeed33b616c78a26a3
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
url: "https://pub.dev"
source: hosted
version: "6.1.0"
version: "6.2.0"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -109,10 +109,10 @@ packages:
dependency: transitive
description:
name: built_value
sha256: "598a2a682e2a7a90f08ba39c0aaa9374c5112340f0a2e275f61b59389543d166"
sha256: ff627b645b28fb8bdb69e645f910c2458fd6b65f6585c3a53e0626024897dedf
url: "https://pub.dev"
source: hosted
version: "8.6.1"
version: "8.6.2"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -149,10 +149,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.2"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -329,21 +329,21 @@ packages:
path: "../../katana"
relative: true
source: path
version: "2.3.3"
version: "2.4.1"
katana_listenables_annotation:
dependency: "direct overridden"
description:
path: "../../katana_listenables_annotation"
relative: true
source: path
version: "2.0.13"
version: "2.0.15"
katana_model:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "2.11.8"
version: "2.11.11"
lints:
dependency: transitive
description:
Expand All @@ -364,18 +364,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.16"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -473,10 +473,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
sprintf:
dependency: transitive
description:
Expand Down Expand Up @@ -529,10 +529,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.0"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -581,6 +581,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
web_socket_channel:
dependency: transitive
description:
Expand All @@ -598,4 +606,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=3.1.0-185.0.dev <4.0.0"
13 changes: 5 additions & 8 deletions packages/katana_model/lib/adapter/runtime_model_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,11 @@ class RuntimeModelAdapter extends ModelAdapter {
}

@override
Future<int> loadCollectionCount(
ModelAdapterCollectionQuery query, {
Iterable? retreivedList,
}) async {
if (retreivedList != null) {
return retreivedList.length;
}
final data = await database.loadCollection(query, prefix: prefix);
Future<int> loadCollectionCount(ModelAdapterCollectionQuery query) async {
final data = await database.loadCollection(
query.copyWith(query: query.query.remove(ModelQueryFilterType.limit)),
prefix: prefix,
);
return data.length;
}

Expand Down
1 change: 1 addition & 0 deletions packages/katana_model/lib/katana_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ part 'src/model_query.dart';
part 'src/model_locale.dart';
part 'src/collection_base.dart';
part 'src/listenable_listener.dart';
part 'src/async_count_value.dart';
part 'src/model_batch.dart';
part 'src/model_transaction.dart';
part 'src/model_field_value.dart';
Expand Down
21 changes: 21 additions & 0 deletions packages/katana_model/lib/src/async_count_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
part of katana_model;

/// Class for asynchronously retrieving values returned from [CollectionBase.count].
///
/// You can get the number of elements in the collection from [value].
///
/// [CollectionBase.count]から返される値を非同期で取得するためのクラスです。
///
/// [value]からコレクションの要素数を取得できます。
class AsyncCountValue extends ChangeNotifier implements ValueListenable<int?> {
AsyncCountValue._(Future<int> future) {
future.then((value) {
_value = value;
notifyListeners();
});
}

@override
int? get value => _value;
int? _value;
}
20 changes: 20 additions & 0 deletions packages/katana_model/lib/src/collection_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ abstract class CollectionBase<TModel extends DocumentBase>
return loaded;
}

/// Get the number of elements in all collections existing in the database.
///
/// [AsyncCountValue] is returned, wait a moment and then retrieve the value from [AsyncCountValue.value].
///
/// データベース上に存在するすべてのコレクションの要素数を取得します。
///
/// [AsyncCountValue]が返されるので少し待ったあと[AsyncCountValue.value]から値を取得してください。
AsyncCountValue count() {
_count ??= AsyncCountValue._(_countRequest());
return _count!;
}

Future<int> _countRequest() async {
final count = await modelQuery.adapter.loadCollectionCount(databaseQuery);
notifyListeners();
return count;
}

AsyncCountValue? _count;

/// {@macro model_transaction}
///
/// ```dart
Expand Down
9 changes: 1 addition & 8 deletions packages/katana_model/lib/src/model_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,10 @@ abstract class ModelAdapter {
///
/// Pass [query] to the platform set by the adapter to get the number of collections.
///
/// [retreivedList] stores data that has already been retrieved.
///
/// 検索条件を含めてDBに保存されているすべての要素の数を返します。
///
/// アダプターで設定されたプラットフォームに[query]を渡してコレクションの数を取得します。
///
/// [retreivedList]にはすでに取得したデータを格納します。
Future<int> loadCollectionCount(
ModelAdapterCollectionQuery query, {
Iterable? retreivedList,
});
Future<int> loadCollectionCount(ModelAdapterCollectionQuery query);

/// By passing the [query] and the [value] to be stored, the data is stored on the platform set by the adapter.
///
Expand Down
9 changes: 9 additions & 0 deletions packages/katana_model/lib/src/model_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ class CollectionModelQuery extends ModelQuery {
return _copyWithAddingFilter(filters: []);
}

/// Remove filters related to [type].
///
/// [type]に関連するフィルタを取り除きます。
CollectionModelQuery remove(ModelQueryFilterType type) {
return _copyWithAddingFilter(filters: [
...filters.where((e) => e.type != type),
]);
}

CollectionModelQuery _copyWithAddingFilter({
List<ModelQueryFilter>? filters,
}) {
Expand Down
2 changes: 1 addition & 1 deletion packages/katana_model/lib/src/no_sql_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class NoSqlDatabase {
if (paths.isEmpty) {
continue;
}
_appliedInitialValue.add(tmp.key);
_appliedInitialValue.add(path);
data._writeToPath(paths, 0, Map.from(value));
}
}
Expand Down
44 changes: 26 additions & 18 deletions packages/katana_model/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: "58826e40314219b223f4723dd4205845040161cdc2df3e6a1cdceed5d8165084"
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
url: "https://pub.dev"
source: hosted
version: "63.0.0"
version: "64.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: f85566ec7b3d25cbea60f7dd4f157c5025f2f19233ca4feeed33b616c78a26a3
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
url: "https://pub.dev"
source: hosted
version: "6.1.0"
version: "6.2.0"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -109,10 +109,10 @@ packages:
dependency: transitive
description:
name: built_value
sha256: "598a2a682e2a7a90f08ba39c0aaa9374c5112340f0a2e275f61b59389543d166"
sha256: ff627b645b28fb8bdb69e645f910c2458fd6b65f6585c3a53e0626024897dedf
url: "https://pub.dev"
source: hosted
version: "8.6.1"
version: "8.6.2"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -149,10 +149,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.2"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -340,7 +340,7 @@ packages:
path: "../katana"
relative: true
source: path
version: "2.3.3"
version: "2.4.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -369,10 +369,10 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -582,10 +582,10 @@ packages:
dependency: "direct dev"
description:
name: test
sha256: b9a384c4b9c4966dbf7215e7c033a78db1da7e5dcaf8da9232c5f24735f65652
sha256: "9b0dd8e36af4a5b1569029949d50a52cb2a2a2fdaa20cebb96e6603b9ae241f9"
url: "https://pub.dev"
source: hosted
version: "1.24.5"
version: "1.24.6"
test_api:
dependency: transitive
description:
Expand All @@ -598,10 +598,10 @@ packages:
dependency: transitive
description:
name: test_core
sha256: c6a536288535efef8526eea8adfa4e25fdd2849fa7f457ecb2a52099998ce8f7
sha256: "4bef837e56375537055fdbbbf6dd458b1859881f4c7e6da936158f77d61ab265"
url: "https://pub.dev"
source: hosted
version: "0.5.5"
version: "0.5.6"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -646,10 +646,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "0fae432c85c4ea880b33b497d32824b97795b04cdaa74d270219572a1f50268d"
sha256: c538be99af830f478718b51630ec1b6bee5e74e52c8a802d328d9e71d35d2583
url: "https://pub.dev"
source: hosted
version: "11.9.0"
version: "11.10.0"
watcher:
dependency: transitive
description:
Expand All @@ -658,6 +658,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
web_socket_channel:
dependency: transitive
description:
Expand All @@ -683,5 +691,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=3.1.0-185.0.dev <4.0.0"
flutter: ">=2.0.0"
Loading

0 comments on commit 5c2f188

Please sign in to comment.