@@ -5,6 +5,7 @@ import 'common.dart';
5
5
import "store.dart" ;
6
6
import "bindings/bindings.dart" ;
7
7
import "bindings/constants.dart" ;
8
+ import "bindings/data_visitor.dart" ;
8
9
import "bindings/flatbuffers.dart" ;
9
10
import "bindings/helpers.dart" ;
10
11
import "bindings/structs.dart" ;
@@ -24,8 +25,9 @@ class Box<T> {
24
25
ModelEntity _modelEntity;
25
26
ObjectReader <T > _entityReader;
26
27
OBXFlatbuffersManager <T > _fbManager;
28
+ final bool _supportsBytesArrays;
27
29
28
- Box (this ._store) {
30
+ Box (this ._store) : _supportsBytesArrays = bindings. obx_supports_bytes_array () == 1 {
29
31
EntityDefinition <T > entityDefs = _store.entityDef <T >();
30
32
_modelEntity = entityDefs.model;
31
33
_entityReader = entityDefs.reader;
@@ -160,34 +162,63 @@ class Box<T> {
160
162
}
161
163
}
162
164
163
- List <T > _getMany (bool allowMissing, Pointer <OBX_bytes_array > Function () cCall) {
165
+ List <T > _getMany (
166
+ bool allowMissing, Pointer <OBX_bytes_array > Function () cGetArray, void Function (DataVisitor ) cVisit) {
164
167
return _store.runInTransaction (TxMode .Read , () {
165
- final bytesArray = cCall ();
166
- try {
167
- return _fbManager.unmarshalArray (bytesArray, allowMissing: allowMissing);
168
- } finally {
169
- bindings.obx_bytes_array_free (bytesArray);
168
+ if (_supportsBytesArrays) {
169
+ final bytesArray = cGetArray ();
170
+ try {
171
+ return _fbManager.unmarshalArray (bytesArray, allowMissing: allowMissing);
172
+ } finally {
173
+ bindings.obx_bytes_array_free (bytesArray);
174
+ }
175
+ } else {
176
+ final results = < T > [];
177
+ final visitor = DataVisitor ((Pointer <Uint8 > dataPtr, int length) {
178
+ if (dataPtr == null || dataPtr.address == 0 || length == 0 ) {
179
+ if (allowMissing) {
180
+ results.add (null );
181
+ return true ;
182
+ } else {
183
+ throw Exception ('Object not found' );
184
+ }
185
+ }
186
+ final bytes = dataPtr.asTypedList (length);
187
+ results.add (_fbManager.unmarshal (bytes));
188
+ return true ;
189
+ });
190
+
191
+ try {
192
+ cVisit (visitor);
193
+ } finally {
194
+ visitor.close ();
195
+ }
196
+ return results;
170
197
}
171
198
});
172
199
}
173
200
174
201
/// Returns a list of [ids.length] Objects of type T, each corresponding to the location of its ID in [ids] .
175
- /// Non-existant IDs become null.
202
+ /// Non-existent IDs become null.
176
203
List <T > getMany (List <int > ids) {
177
204
if (ids.isEmpty) return [];
178
205
179
- const bool allowMissing = true ; // returns null if null is encountered in the data found
206
+ const bool allowMissing = true ; // result includes null if an object is missing
180
207
return OBX_id_array .executeWith (
181
208
ids,
182
- (ptr) => _getMany (allowMissing,
183
- () => checkObxPtr (bindings.obx_box_get_many (_cBox, ptr), "failed to get many objects from box" )));
209
+ (ptr) => _getMany (
210
+ allowMissing,
211
+ () => checkObxPtr (bindings.obx_box_get_many (_cBox, ptr), "failed to get many objects from box" ),
212
+ (DataVisitor visitor) => checkObx (bindings.obx_box_visit_many (_cBox, ptr, visitor.fn, visitor.userData))));
184
213
}
185
214
186
215
/// Returns all stored objects in this Box.
187
216
List <T > getAll () {
188
217
const bool allowMissing = false ; // throw if null is encountered in the data found
189
218
return _getMany (
190
- allowMissing, () => checkObxPtr (bindings.obx_box_get_all (_cBox), "failed to get all objects from box" ));
219
+ allowMissing,
220
+ () => checkObxPtr (bindings.obx_box_get_all (_cBox), "failed to get all objects from box" ),
221
+ (DataVisitor visitor) => checkObx (bindings.obx_box_visit_all (_cBox, visitor.fn, visitor.userData)));
191
222
}
192
223
193
224
/// Returns a builder to create queries for Object matching supplied criteria.
0 commit comments