Skip to content

Commit 30efd33

Browse files
committed
implemented .count(), .isEmpty(), .removeAll(); ready for PR
1 parent db87258 commit 30efd33

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

lib/src/bindings/bindings.dart

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class _ObjectBoxBindings {
5858
int Function(Pointer<Void> box, int id, Pointer<Void> data, int size, int mode) obx_box_put;
5959
int Function(Pointer<Void> box, Pointer<Uint64> objects, Pointer<Uint64> ids, int mode) obx_box_put_many;
6060
int Function(Pointer<Void> box, int id) obx_box_remove;
61+
int Function(Pointer<Void> box, Pointer<Uint64> removed) obx_box_remove_all;
62+
63+
// box analytics
64+
int Function(Pointer<Void> box, int limit, Pointer<Uint64> count) obx_box_count;
65+
int Function(Pointer<Void> box, Pointer<Uint8> is_empty) obx_box_is_empty;
6166

6267
// TODO return .asFunction() -> requires properly determined static return type
6368
Pointer<NativeFunction<T>> _fn<T extends Function>(String name) {
@@ -126,6 +131,11 @@ class _ObjectBoxBindings {
126131
obx_box_put = _fn<obx_box_put_native_t>("obx_box_put").asFunction();
127132
obx_box_put_many = _fn<obx_box_put_many_native_t>("obx_box_put_many").asFunction();
128133
obx_box_remove = _fn<obx_box_remove_native_t>("obx_box_remove").asFunction();
134+
obx_box_remove_all = _fn<obx_box_remove_all_native_t>("obx_box_remove_all").asFunction();
135+
136+
// box analytics
137+
obx_box_count = _fn<obx_box_count_native_t>("obx_box_count").asFunction();
138+
obx_box_is_empty = _fn<obx_box_is_empty_native_t>("obx_box_is_empty").asFunction();
129139
}
130140
}
131141

lib/src/bindings/signatures.dart

+3
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ typedef obx_box_put_native_t = Int32 Function(Pointer<Void> box, Uint64 id, Poin
5555
typedef obx_box_put_many_native_t = Int32 Function(
5656
Pointer<Void> box, Pointer<Uint64> objects, Pointer<Uint64> ids, Int32 mode);
5757
typedef obx_box_remove_native_t = Int32 Function(Pointer<Void> box, Uint64 id);
58+
typedef obx_box_remove_all_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint64> removed);
59+
typedef obx_box_count_native_t = Int32 Function(Pointer<Void> box, Uint64 limit, Pointer<Uint64> _count);
60+
typedef obx_box_is_empty_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint8> is_empty);

lib/src/box.dart

+24
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,29 @@ class Box<T> {
166166
return _getMany(() => checkObxPtr(bindings.obx_box_get_all(_cBox), "failed to get all objects from box", true));
167167
}
168168

169+
// same as calling maxCount with limit := 0
170+
int count() {
171+
return maxCount(limit: 0);
172+
}
173+
174+
int maxCount({int limit}) {
175+
Pointer<Uint64> _count = Pointer<Uint64>.allocate();
176+
checkObx(bindings.obx_box_count(_cBox, limit, _count));
177+
return _count.load<int>();
178+
}
179+
180+
bool isEmpty() {
181+
Pointer<Uint8> _isEmpty = Pointer<Uint8>.allocate();
182+
checkObx(bindings.obx_box_is_empty(_cBox, _isEmpty));
183+
return _isEmpty.load<int>() > 0 ? true : false;
184+
}
185+
186+
int removeAll() {
187+
Pointer<Uint64> _removedItems = Pointer<Uint64>.allocate();
188+
checkObx(bindings.obx_box_remove_all(_cBox, _removedItems));
189+
return _removedItems.load<int>();
190+
}
191+
169192
get ptr => _cBox;
193+
170194
}

test/box_test.dart

+12
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ void main() {
9393
expect(fetchedItems[2].text, equals("Two"));
9494
});
9595

96+
test(".count(), .isEmpty(), .removeAll() works", () {
97+
final List<TestEntity> items = ["One", "Two"].map((s) => TestEntity.construct(s)).toList();
98+
final List<int> ids = box.putMany(items);
99+
100+
int count = box.count();
101+
expect(count, greaterThanOrEqualTo(0));
102+
bool isEmpty = box.isEmpty();
103+
expect(isEmpty, equals(false));
104+
int removed = box.removeAll();
105+
expect(removed, greaterThan(0));
106+
});
107+
96108
tearDown(() {
97109
if (store != null) store.close();
98110
store = null;

0 commit comments

Comments
 (0)