@@ -9,7 +9,6 @@ import 'dart:typed_data';
9
9
10
10
import 'package:ffi/ffi.dart' ;
11
11
import 'package:meta/meta.dart' ;
12
- import 'package:objectbox/src/native/version.dart' ;
13
12
import 'package:path/path.dart' as path;
14
13
15
14
import '../common.dart' ;
@@ -22,6 +21,7 @@ import 'bindings/helpers.dart';
22
21
import 'box.dart' ;
23
22
import 'model.dart' ;
24
23
import 'sync.dart' ;
24
+ import 'version.dart' ;
25
25
26
26
part 'observable.dart' ;
27
27
@@ -43,19 +43,26 @@ class Store {
43
43
HashMap <int , Type >? _entityTypeById;
44
44
final _boxes = HashMap <Type , Box >();
45
45
46
- /// Configuration of this for use with [Store._attachByConfiguration] .
46
+ /// Configuration of this.
47
+ /// Is null if this is a minimal store.
48
+ /// Can be used with [Store._attachByConfiguration] .
47
49
late final StoreConfiguration ? _configuration;
48
50
49
- /// May be null for minimal store, access via [_modelDefinition] with null check.
50
- final ModelDefinition ? _defs;
51
51
Stream <List <Type >>? _entityChanges;
52
52
53
53
/// Should be cleared when this closes to free native resources.
54
54
final _reader = ReaderWithCBuffer ();
55
55
Transaction ? _tx;
56
56
57
57
/// Path to the database directory.
58
- final String directoryPath;
58
+ String get directoryPath {
59
+ final configuration = _configuration;
60
+ if (configuration != null ) {
61
+ return configuration.directoryPath;
62
+ } else {
63
+ throw StateError ("A minimal store does not have a directory path." );
64
+ }
65
+ }
59
66
60
67
/// Absolute path to the database directory, used for open check.
61
68
final String _absoluteDirectoryPath;
@@ -69,9 +76,6 @@ class Store {
69
76
/// remove [_absoluteDirectoryPath] from [_openStoreDirectories] .
70
77
final bool _closesNativeStore;
71
78
72
- /// Default value for string query conditions [caseSensitive] argument.
73
- final bool _queriesCaseSensitiveDefault;
74
-
75
79
static String _safeDirectoryPath (String ? path) =>
76
80
(path == null || path.isEmpty) ? defaultDirectoryPath : path;
77
81
@@ -157,10 +161,7 @@ class Store {
157
161
int ? debugFlags,
158
162
bool queriesCaseSensitiveDefault = true ,
159
163
String ? macosApplicationGroup})
160
- : _defs = modelDefinition,
161
- _closesNativeStore = true ,
162
- _queriesCaseSensitiveDefault = queriesCaseSensitiveDefault,
163
- directoryPath = _safeDirectoryPath (directory),
164
+ : _closesNativeStore = true ,
164
165
_absoluteDirectoryPath =
165
166
path.context.canonicalize (_safeDirectoryPath (directory)) {
166
167
try {
@@ -181,13 +182,14 @@ class Store {
181
182
}
182
183
_checkStoreDirectoryNotOpen ();
183
184
final model = Model (modelDefinition.model);
185
+ final safeDirectoryPath = _safeDirectoryPath (directory);
184
186
185
187
final opt = C .opt ();
186
188
checkObxPtr (opt, 'failed to create store options' );
187
189
188
190
try {
189
191
checkObx (C .opt_model (opt, model.ptr));
190
- final cStr = directoryPath .toNativeUtf8 ();
192
+ final cStr = safeDirectoryPath .toNativeUtf8 ();
191
193
try {
192
194
checkObx (C .opt_directory (opt, cStr.cast ()));
193
195
} finally {
@@ -211,8 +213,8 @@ class Store {
211
213
}
212
214
if (debugLogs) {
213
215
print (
214
- "Opening store (C lib V${libraryVersion ()})... path=$directoryPath "
215
- " isOpen=${isOpen (directoryPath )}" );
216
+ "Opening store (C lib V${libraryVersion ()})... path=$safeDirectoryPath "
217
+ " isOpen=${isOpen (safeDirectoryPath )}" );
216
218
}
217
219
218
220
_cStore = C .store_open (opt);
@@ -227,7 +229,8 @@ class Store {
227
229
_reference.setUint64 (1 * _int64Size, _ptr.address);
228
230
229
231
_openStoreDirectories.add (_absoluteDirectoryPath);
230
- _attachConfiguration (_cStore);
232
+ _attachConfiguration (_cStore, modelDefinition, safeDirectoryPath,
233
+ queriesCaseSensitiveDefault);
231
234
_attachFinalizer ();
232
235
} catch (e) {
233
236
_reader.clear ();
@@ -272,14 +275,12 @@ class Store {
272
275
/// ...
273
276
/// }
274
277
/// ```
275
- Store .fromReference (this ._defs , this ._reference,
278
+ Store .fromReference (ModelDefinition modelDefinition , this ._reference,
276
279
{bool queriesCaseSensitiveDefault = true })
277
280
:
278
281
// Must not close native store twice, only original store is allowed to.
279
282
_closesNativeStore = false ,
280
- directoryPath = '' ,
281
- _absoluteDirectoryPath = '' ,
282
- _queriesCaseSensitiveDefault = queriesCaseSensitiveDefault {
283
+ _absoluteDirectoryPath = '' {
283
284
// see [reference] for serialization order
284
285
final readPid = _reference.getUint64 (0 * _int64Size);
285
286
if (readPid != pid) {
@@ -293,7 +294,8 @@ class Store {
293
294
'Given native pointer is empty' );
294
295
}
295
296
296
- _attachConfiguration (_cStore);
297
+ _attachConfiguration (
298
+ _cStore, modelDefinition, '' , queriesCaseSensitiveDefault);
297
299
}
298
300
299
301
/// Creates a Store clone with minimal functionality given a pointer address
@@ -304,11 +306,8 @@ class Store {
304
306
///
305
307
/// See [_clone] for details.
306
308
Store ._minimal (int ptrAddress, {bool queriesCaseSensitiveDefault = true })
307
- : _defs = null ,
308
- _closesNativeStore = true ,
309
- directoryPath = '' ,
310
- _absoluteDirectoryPath = '' ,
311
- _queriesCaseSensitiveDefault = queriesCaseSensitiveDefault {
309
+ : _closesNativeStore = true ,
310
+ _absoluteDirectoryPath = '' {
312
311
if (ptrAddress == 0 ) {
313
312
throw ArgumentError .value (
314
313
ptrAddress, 'ptrAddress' , 'Given native pointer address is invalid' );
@@ -329,11 +328,9 @@ class Store {
329
328
/// its own lifetime and must also be closed (e.g. before an isolate exits).
330
329
/// The actual underlying store is only closed when the last store instance
331
330
/// is closed (e.g. when the app exits).
332
- Store .attach (this ._defs , String ? directoryPath,
331
+ Store .attach (ModelDefinition modelDefinition , String ? directoryPath,
333
332
{bool queriesCaseSensitiveDefault = true })
334
333
: _closesNativeStore = true ,
335
- _queriesCaseSensitiveDefault = queriesCaseSensitiveDefault,
336
- directoryPath = _safeDirectoryPath (directoryPath),
337
334
_absoluteDirectoryPath =
338
335
path.context.canonicalize (_safeDirectoryPath (directoryPath)) {
339
336
try {
@@ -343,12 +340,12 @@ class Store {
343
340
// overlap.
344
341
_checkStoreDirectoryNotOpen ();
345
342
346
- final pathCStr = this .directoryPath.toNativeUtf8 ();
343
+ final safeDirectoryPath = _safeDirectoryPath (directoryPath);
344
+ final pathCStr = safeDirectoryPath.toNativeUtf8 ();
347
345
try {
348
346
if (debugLogs) {
349
347
final isOpen = C .store_is_open (pathCStr.cast ());
350
- print (
351
- 'Attaching to store... path=${this .directoryPath } isOpen=$isOpen ' );
348
+ print ('Attaching to store... path=$safeDirectoryPath isOpen=$isOpen ' );
352
349
}
353
350
_cStore = C .store_attach (pathCStr.cast ());
354
351
} finally {
@@ -361,7 +358,8 @@ class Store {
361
358
// Not setting _reference as this is a replacement for obtaining a store
362
359
// via reference.
363
360
364
- _attachConfiguration (_cStore);
361
+ _attachConfiguration (_cStore, modelDefinition, safeDirectoryPath,
362
+ queriesCaseSensitiveDefault);
365
363
_attachFinalizer ();
366
364
} catch (e) {
367
365
_reader.clear ();
@@ -377,12 +375,8 @@ class Store {
377
375
/// so [close] this immediately when done using. Closing this will only close
378
376
/// the underlying store if it is not opened elsewhere.
379
377
Store ._attachByConfiguration (StoreConfiguration configuration)
380
- : _defs = configuration.modelDefinition,
381
- _closesNativeStore = true ,
382
- directoryPath = configuration.directoryPath,
383
- _absoluteDirectoryPath = '' ,
384
- _queriesCaseSensitiveDefault =
385
- configuration.queriesCaseSensitiveDefault {
378
+ : _closesNativeStore = true ,
379
+ _absoluteDirectoryPath = '' {
386
380
try {
387
381
Pointer <OBX_store >? storePtr = C .store_attach_id (configuration.id);
388
382
_checkStorePointer (storePtr);
@@ -424,10 +418,11 @@ class Store {
424
418
}
425
419
}
426
420
427
- void _attachConfiguration (Pointer <OBX_store > storePtr) {
421
+ void _attachConfiguration (Pointer <OBX_store > storePtr, ModelDefinition model,
422
+ String directoryPath, bool queriesCaseSensitiveDefault) {
428
423
int id = C .store_id (storePtr);
429
- _configuration = StoreConfiguration ._(id, _modelDefinition, directoryPath,
430
- queriesCaseSensitiveDefault : _queriesCaseSensitiveDefault );
424
+ _configuration = StoreConfiguration ._(
425
+ id, model, directoryPath, queriesCaseSensitiveDefault );
431
426
}
432
427
433
428
/// Attach a finalizer (using Dart C API) so when garbage collected, most
@@ -544,7 +539,7 @@ class Store {
544
539
}
545
540
546
541
EntityDefinition <T > _entityDef <T >() {
547
- final binding = _modelDefinition .bindings[T ];
542
+ final binding = configuration ().modelDefinition .bindings[T ];
548
543
if (binding == null ) {
549
544
throw ArgumentError ('Unknown entity type ' + T .toString ());
550
545
}
@@ -802,14 +797,6 @@ class Store {
802
797
@pragma ('vm:prefer-inline' )
803
798
Pointer <OBX_store > get _ptr =>
804
799
isClosed () ? throw StateError ('Store is closed' ) : _cStore;
805
-
806
- /// Returns the ModelDefinition of this store, or throws if
807
- /// this is a minimal store.
808
- ModelDefinition get _modelDefinition {
809
- final model = _defs;
810
- if (model == null ) throw StateError ('Minimal store does not have a model' );
811
- return model;
812
- }
813
800
}
814
801
815
802
/// This hides away methods from the public API
@@ -860,7 +847,7 @@ class InternalStoreAccess {
860
847
static Map <int , Type > entityTypeById (Store store) {
861
848
if (store._entityTypeById == null ) {
862
849
store._entityTypeById = HashMap <int , Type >();
863
- store._modelDefinition .bindings.forEach (
850
+ store.configuration ().modelDefinition .bindings.forEach (
864
851
(Type entity, EntityDefinition entityDef) =>
865
852
store._entityTypeById! [entityDef.model.id.id] = entity);
866
853
}
@@ -882,7 +869,8 @@ class InternalStoreAccess {
882
869
883
870
/// String query case-sensitive default
884
871
@pragma ('vm:prefer-inline' )
885
- static bool queryCS (Store store) => store._queriesCaseSensitiveDefault;
872
+ static bool queryCS (Store store) =>
873
+ store.configuration ().queriesCaseSensitiveDefault;
886
874
887
875
/// The low-level pointer to this store.
888
876
@pragma ('vm:prefer-inline' )
0 commit comments