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

implemented .count(), .isEmpty(), .removeAll() #42

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions lib/src/bindings/bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class _ObjectBoxBindings {
int Function(Pointer<Void> box, int id, Pointer<Void> data, int size, int mode) obx_box_put;
int Function(Pointer<Void> box, Pointer<Uint64> objects, Pointer<Uint64> ids, int mode) obx_box_put_many;
int Function(Pointer<Void> box, int id) obx_box_remove;
int Function(Pointer<Void> box, Pointer<Uint64> removed) obx_box_remove_all;

// box analytics
int Function(Pointer<Void> box, int limit, Pointer<Uint64> count) obx_box_count;
int Function(Pointer<Void> box, Pointer<Uint8> is_empty) obx_box_is_empty;

// TODO return .asFunction() -> requires properly determined static return type
Pointer<NativeFunction<T>> _fn<T extends Function>(String name) {
Expand Down Expand Up @@ -126,6 +131,11 @@ class _ObjectBoxBindings {
obx_box_put = _fn<obx_box_put_native_t>("obx_box_put").asFunction();
obx_box_put_many = _fn<obx_box_put_many_native_t>("obx_box_put_many").asFunction();
obx_box_remove = _fn<obx_box_remove_native_t>("obx_box_remove").asFunction();
obx_box_remove_all = _fn<obx_box_remove_all_native_t>("obx_box_remove_all").asFunction();

// box analytics
obx_box_count = _fn<obx_box_count_native_t>("obx_box_count").asFunction();
obx_box_is_empty = _fn<obx_box_is_empty_native_t>("obx_box_is_empty").asFunction();
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/bindings/signatures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ typedef obx_box_put_native_t = Int32 Function(Pointer<Void> box, Uint64 id, Poin
typedef obx_box_put_many_native_t = Int32 Function(
Pointer<Void> box, Pointer<Uint64> objects, Pointer<Uint64> ids, Int32 mode);
typedef obx_box_remove_native_t = Int32 Function(Pointer<Void> box, Uint64 id);
typedef obx_box_remove_all_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint64> removed);
typedef obx_box_count_native_t = Int32 Function(Pointer<Void> box, Uint64 limit, Pointer<Uint64> _count);
typedef obx_box_is_empty_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint8> is_empty);
24 changes: 24 additions & 0 deletions lib/src/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,29 @@ class Box<T> {
return _getMany(() => checkObxPtr(bindings.obx_box_get_all(_cBox), "failed to get all objects from box", true));
}

// same as calling maxCount with limit := 0
int count() {
return maxCount(limit: 0);
}

int maxCount({int limit}) {
Pointer<Uint64> _count = Pointer<Uint64>.allocate();
checkObx(bindings.obx_box_count(_cBox, limit, _count));
return _count.load<int>();
}
vaind marked this conversation as resolved.
Show resolved Hide resolved

bool isEmpty() {
Pointer<Uint8> _isEmpty = Pointer<Uint8>.allocate();
checkObx(bindings.obx_box_is_empty(_cBox, _isEmpty));
return _isEmpty.load<int>() > 0 ? true : false;
vaind marked this conversation as resolved.
Show resolved Hide resolved
}

int removeAll() {
Pointer<Uint64> _removedItems = Pointer<Uint64>.allocate();
checkObx(bindings.obx_box_remove_all(_cBox, _removedItems));
return _removedItems.load<int>();
}

get ptr => _cBox;

}
12 changes: 12 additions & 0 deletions test/box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ void main() {
expect(fetchedItems[2].text, equals("Two"));
});

test(".count(), .isEmpty(), .removeAll() works", () {
final List<TestEntity> items = ["One", "Two"].map((s) => TestEntity.construct(s)).toList();
final List<int> ids = box.putMany(items);

int count = box.count();
expect(count, greaterThanOrEqualTo(0));
bool isEmpty = box.isEmpty();
expect(isEmpty, equals(false));
int removed = box.removeAll();
expect(removed, greaterThan(0));
});

vaind marked this conversation as resolved.
Show resolved Hide resolved
tearDown(() {
if (store != null) store.close();
store = null;
Expand Down