Skip to content

Commit 71639cd

Browse files
committed
Store: add debugLogs, improved attach fail msg
1 parent ae6bab7 commit 71639cd

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

objectbox/lib/src/native/store.dart

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:typed_data';
99

1010
import 'package:ffi/ffi.dart';
1111
import 'package:meta/meta.dart';
12+
import 'package:objectbox/src/native/version.dart';
1213
import 'package:path/path.dart' as path;
1314

1415
import '../common.dart';
@@ -30,6 +31,10 @@ class Store {
3031
/// Path of the default directory, currently 'objectbox'.
3132
static const String defaultDirectoryPath = 'objectbox';
3233

34+
/// Enables a couple of debug logs.
35+
/// This meant for tests only; do not enable for releases!
36+
static bool debugLogs = false;
37+
3338
late final Pointer<OBX_store> _cStore;
3439
HashMap<int, Type>? _entityTypeById;
3540
final _boxes = HashMap<Type, Box>();
@@ -130,6 +135,11 @@ class Store {
130135
C.opt_free(opt);
131136
rethrow;
132137
}
138+
if (debugLogs) {
139+
print('Opening store (C lib V${libraryVersion()})... path=$directory'
140+
' isOpen=${isOpen(directory)}');
141+
}
142+
133143
_cStore = C.store_open(opt);
134144

135145
_checkStorePointer(_cStore);
@@ -233,12 +243,17 @@ class Store {
233243
final path = _safeDirectoryPath(directoryPath);
234244
final pathCStr = path.toNativeUtf8();
235245
try {
246+
if (debugLogs) {
247+
final isOpen = C.store_is_open(pathCStr.cast());
248+
print('Attaching to store... path=$path isOpen=$isOpen');
249+
}
236250
_cStore = C.store_attach(pathCStr.cast());
237251
} finally {
238252
malloc.free(pathCStr);
239253
}
240254

241-
_checkStorePointer(_cStore);
255+
checkObxPtr(_cStore,
256+
'could not attach to the store at given path - please ensure it was opened before');
242257

243258
// Not setting reference as this is a replacement for obtaining a store
244259
// via reference.

objectbox/test/isolates_test.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void main() {
5656
/// Work with a single store across multiple isolates using
5757
/// the directory path to attach to an existing store.
5858
test('single store using attach', () async {
59+
Store.debugLogs = true;
5960
await testUsingStoreFromIsolate(storeCreatorAttach, (env) => env.dir.path);
6061
});
6162
}
@@ -64,8 +65,10 @@ void main() {
6465
Store storeCreatorFromRef(dynamic msg) =>
6566
Store.fromReference(getObjectBoxModel(), msg as ByteData);
6667

67-
Store storeCreatorAttach(dynamic msg) =>
68-
Store.attach(getObjectBoxModel(), msg as String);
68+
Store storeCreatorAttach(dynamic msg) {
69+
Store.debugLogs = true;
70+
return Store.attach(getObjectBoxModel(), msg as String);
71+
}
6972

7073
class IsolateInitMessage {
7174
SendPort sendPort;
@@ -105,6 +108,8 @@ Future<void> testUsingStoreFromIsolate(Store Function(dynamic) storeCreator,
105108

106109
// Pass the store to the isolate
107110
final env = TestEnv('isolates');
111+
expect(Store.isOpen('testdata-isolates'), true);
112+
108113
expect(await call(storeRefGetter(env)), equals('store set'));
109114

110115
{

0 commit comments

Comments
 (0)