Skip to content

Commit 3d2f25e

Browse files
greenrobotgreenrobot-team
authored andcommitted
Store: add debugLogs, improved attach fail msg
1 parent 7e1e319 commit 3d2f25e

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
@@ -55,6 +55,7 @@ void main() {
5555
/// Work with a single store across multiple isolates using
5656
/// the directory path to attach to an existing store.
5757
test('single store using attach', () async {
58+
Store.debugLogs = true;
5859
await testUsingStoreFromIsolate(storeCreatorAttach, (env) => env.dir.path);
5960
});
6061
}
@@ -63,8 +64,10 @@ void main() {
6364
Store storeCreatorFromRef(dynamic msg) =>
6465
Store.fromReference(getObjectBoxModel(), msg as ByteData);
6566

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

6972
class IsolateInitMessage {
7073
SendPort sendPort;
@@ -101,6 +104,8 @@ Future<void> testUsingStoreFromIsolate(Store Function(dynamic) storeCreator,
101104

102105
// Pass the store to the isolate
103106
final env = TestEnv('isolates');
107+
expect(Store.isOpen('testdata-isolates'), true);
108+
104109
expect(await call(storeRefGetter(env)), equals('store set'));
105110

106111
{

0 commit comments

Comments
 (0)