Skip to content

Commit 7758a14

Browse files
committed
added .remove(), .removeMany() methods and seperate unit tests
1 parent 30efd33 commit 7758a14

File tree

5 files changed

+94
-20
lines changed

5 files changed

+94
-20
lines changed

lib/src/bindings/bindings.dart

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class _ObjectBoxBindings {
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;
6161
int Function(Pointer<Void> box, Pointer<Uint64> removed) obx_box_remove_all;
62+
int Function(Pointer<Void> box, Pointer<Uint64> ids, Pointer<Uint64> removed) obx_box_remove_many;
6263

6364
// box analytics
6465
int Function(Pointer<Void> box, int limit, Pointer<Uint64> count) obx_box_count;
@@ -132,6 +133,7 @@ class _ObjectBoxBindings {
132133
obx_box_put_many = _fn<obx_box_put_many_native_t>("obx_box_put_many").asFunction();
133134
obx_box_remove = _fn<obx_box_remove_native_t>("obx_box_remove").asFunction();
134135
obx_box_remove_all = _fn<obx_box_remove_all_native_t>("obx_box_remove_all").asFunction();
136+
obx_box_remove_many = _fn<obx_box_remove_many_native_t>("obx_box_remove_many").asFunction();
135137

136138
// box analytics
137139
obx_box_count = _fn<obx_box_count_native_t>("obx_box_count").asFunction();

lib/src/bindings/signatures.dart

+1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ 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);
5858
typedef obx_box_remove_all_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint64> removed);
59+
typedef obx_box_remove_many_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint64> ids, Pointer<Uint64> removed);
5960
typedef obx_box_count_native_t = Int32 Function(Pointer<Void> box, Uint64 limit, Pointer<Uint64> _count);
6061
typedef obx_box_is_empty_native_t = Int32 Function(Pointer<Void> box, Pointer<Uint8> is_empty);

lib/src/box.dart

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import "dart:ffi";
22

3+
import 'package:objectbox/src/common.dart';
4+
35
import "store.dart";
46
import "bindings/bindings.dart";
57
import "bindings/constants.dart";
@@ -166,27 +168,54 @@ class Box<T> {
166168
return _getMany(() => checkObxPtr(bindings.obx_box_get_all(_cBox), "failed to get all objects from box", true));
167169
}
168170

169-
// same as calling maxCount with limit := 0
170-
int count() {
171-
return maxCount(limit: 0);
172-
}
173-
174-
int maxCount({int limit}) {
171+
int count({int limit: 0}) {
175172
Pointer<Uint64> _count = Pointer<Uint64>.allocate();
176-
checkObx(bindings.obx_box_count(_cBox, limit, _count));
177-
return _count.load<int>();
173+
try {
174+
checkObx(bindings.obx_box_count(_cBox, limit, _count));
175+
return _count.load<int>();
176+
} finally {
177+
_count.free();
178+
}
178179
}
179180

180181
bool isEmpty() {
181182
Pointer<Uint8> _isEmpty = Pointer<Uint8>.allocate();
182-
checkObx(bindings.obx_box_is_empty(_cBox, _isEmpty));
183-
return _isEmpty.load<int>() > 0 ? true : false;
183+
try {
184+
checkObx(bindings.obx_box_is_empty(_cBox, _isEmpty));
185+
return _isEmpty.load<int>() > 0 ? true : false;
186+
} finally {
187+
_isEmpty.free();
188+
}
189+
}
190+
191+
bool remove(int id) {
192+
try {
193+
checkObx(bindings.obx_box_remove(_cBox, id));
194+
} on ObjectBoxException catch (ex) {
195+
if (ex.raw_msg == "code 404") return false;
196+
else throw(ex);
197+
}
198+
return true;
199+
}
200+
201+
int removeMany(List<int> ids) {
202+
Pointer<Uint64> _removedIds = Pointer<Uint64>.allocate();
203+
try {
204+
checkObx(bindings.obx_box_remove_many(_cBox, IDArray(ids).ptr, _removedIds));
205+
return _removedIds.load<int>();
206+
} finally {
207+
_removedIds.free();
208+
}
184209
}
185210

186211
int removeAll() {
187212
Pointer<Uint64> _removedItems = Pointer<Uint64>.allocate();
188-
checkObx(bindings.obx_box_remove_all(_cBox, _removedItems));
189-
return _removedItems.load<int>();
213+
try {
214+
checkObx(bindings.obx_box_remove_all(_cBox, _removedItems));
215+
return _removedItems.load<int>();
216+
} finally {
217+
_removedItems.free();
218+
}
190219
}
191220

192221
get ptr => _cBox;

lib/src/common.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ Version versionLib() {
2727
}
2828
}
2929

30-
class ObjectBoxException {
30+
class ObjectBoxException implements Exception {
3131
final String message;
32+
final String raw_msg;
3233

33-
ObjectBoxException(msg) : message = "ObjectBoxException: " + msg;
34+
ObjectBoxException(msg) : message = "ObjectBoxException: " + msg, raw_msg = msg;
3435

3536
String toString() => message;
3637
}

test/box_test.dart

+47-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ void main() {
1919
Store store;
2020
Box box;
2121

22+
final List<TestEntity> simple_items = ["One", "Two", "Three", "Four", "Five", "Six"].map((s) => TestEntity.construct(s)).toList();
23+
2224
group("box", () {
2325
setUp(() {
2426
store = Store([TestEntity_OBXDefs]);
@@ -93,16 +95,55 @@ void main() {
9395
expect(fetchedItems[2].text, equals("Two"));
9496
});
9597

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-
98+
test(".count() works", () {
99+
List<int> ids = box.putMany(simple_items);
100100
int count = box.count();
101-
expect(count, greaterThanOrEqualTo(0));
101+
expect(count, equals(6));
102+
//add more
103+
ids.addAll(box.putMany(simple_items));
104+
count = box.count();
105+
expect(count, equals(12));
106+
});
107+
108+
test(".isEmpty() works", () {
102109
bool isEmpty = box.isEmpty();
110+
expect(isEmpty, equals(true));
111+
//check complementary
112+
final List<int> ids = box.putMany(simple_items);
113+
isEmpty = box.isEmpty();
103114
expect(isEmpty, equals(false));
115+
});
116+
117+
test(".remove(id) works", () {
118+
final List<int> ids = box.putMany(simple_items);
119+
//check if single id remove works
120+
box.remove(ids[1]);
121+
expect(box.count(), equals(5));
122+
//check what happens if id already deleted -> throws OBJBOXEX 404
123+
bool success = box.remove(ids[1]);
124+
expect(box.count(), equals(5));
125+
expect(success, equals(false));
126+
});
127+
128+
test(".removeMany(ids) works", () {
129+
final List<int> ids = box.putMany(simple_items);
130+
expect(box.count(), equals(6));
131+
box.removeMany(ids.sublist(4));
132+
expect(box.count(), equals(4));
133+
//again test what happens if ids already deleted
134+
box.removeMany(ids.sublist(4));
135+
expect(box.count(), equals(4));
136+
});
137+
138+
test(".removeAll() works", () {
139+
List<int> ids = box.putMany(simple_items);
104140
int removed = box.removeAll();
105-
expect(removed, greaterThan(0));
141+
expect(removed, equals(6));
142+
//try with different number of items
143+
List<TestEntity> items = ["one", "two", "three"].map((s) => TestEntity.construct(s)).toList();
144+
ids.addAll(box.putMany(items));
145+
removed = box.removeAll();
146+
expect(removed, equals(3));
106147
});
107148

108149
tearDown(() {

0 commit comments

Comments
 (0)