Skip to content

Commit df01e61

Browse files
Merge branch '97-db-file-size' into 'main'
Add Store dbFileSize See merge request objectbox/objectbox-dart!72
2 parents 783c28f + 6159891 commit df01e61

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

objectbox/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
```
4949
See the `Store` documentation for details.
5050
* Add `Store.removeDbFiles()` to conveniently delete database files or an in-memory database.
51+
* Add `Store.dbFileSize()` to get the size in bytes of the main database file or memory occupied by
52+
an in-memory database.
5153

5254
## 2.4.0 (2023-12-13)
5355

objectbox/lib/src/native/store.dart

+26-31
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ class Store implements Finalizable {
219219
ArgumentError.value(macosApplicationGroup, 'macosApplicationGroup',
220220
'Must be at most 20 characters long');
221221
}
222-
final cStr = macosApplicationGroup.toNativeUtf8();
223-
try {
224-
C.posix_sem_prefix_set(cStr.cast());
225-
} finally {
226-
malloc.free(cStr);
227-
}
222+
withNativeString(macosApplicationGroup, C.posix_sem_prefix_set);
228223
}
229224
_checkStoreDirectoryNotOpen();
230225
final model = Model(modelDefinition.model);
@@ -235,12 +230,8 @@ class Store implements Finalizable {
235230

236231
try {
237232
checkObx(C.opt_model(opt, model.ptr));
238-
final cStr = safeDirectoryPath.toNativeUtf8();
239-
try {
240-
checkObx(C.opt_directory(opt, cStr.cast()));
241-
} finally {
242-
malloc.free(cStr);
243-
}
233+
checkObx(withNativeString(
234+
safeDirectoryPath, (cStr) => C.opt_directory(opt, cStr)));
244235
if (maxDBSizeInKB != null && maxDBSizeInKB > 0) {
245236
C.opt_max_db_size_in_kb(opt, maxDBSizeInKB);
246237
}
@@ -389,16 +380,13 @@ class Store implements Finalizable {
389380
_checkStoreDirectoryNotOpen();
390381

391382
final safeDirectoryPath = _safeDirectoryPath(directoryPath);
392-
final pathCStr = safeDirectoryPath.toNativeUtf8();
393-
try {
383+
withNativeString(safeDirectoryPath, (cStr) {
394384
if (debugLogs) {
395-
final isOpen = C.store_is_open(pathCStr.cast());
385+
final isOpen = C.store_is_open(cStr);
396386
print('Attaching to store... path=$safeDirectoryPath isOpen=$isOpen');
397387
}
398-
_cStore = C.store_attach(pathCStr.cast());
399-
} finally {
400-
malloc.free(pathCStr);
401-
}
388+
_cStore = C.store_attach(cStr);
389+
});
402390

403391
checkObxPtr(_cStore,
404392
'could not attach to the store at given path - please ensure it was opened before');
@@ -496,12 +484,24 @@ class Store implements Finalizable {
496484
/// For Dart Native apps, pass null to use the [defaultDirectoryPath].
497485
static bool isOpen(String? directoryPath) {
498486
final path = _safeDirectoryPath(directoryPath);
499-
final cStr = path.toNativeUtf8();
500-
try {
501-
return C.store_is_open(cStr.cast());
502-
} finally {
503-
malloc.free(cStr);
504-
}
487+
return withNativeString(path, C.store_is_open);
488+
}
489+
490+
/// Returns the file size in bytes of the main database file for the given
491+
/// [directoryPath], or 0 if the file does not exist or some error occurred.
492+
///
493+
/// For in-memory databases, it is supported to pass the [inMemoryPrefix] and
494+
/// the identifier. The rough size in bytes of the in-memory database will be
495+
/// reported instead.
496+
///
497+
/// For Flutter apps, the default [directoryPath] can be obtained with
498+
/// `(await defaultStoreDirectory()).path` from `objectbox_flutter_libs`
499+
/// (or `objectbox_sync_flutter_libs`).
500+
///
501+
/// For Dart Native apps, pass null to use the [defaultDirectoryPath].
502+
static int dbFileSize(String? directoryPath) {
503+
final path = _safeDirectoryPath(directoryPath);
504+
return withNativeString(path, C.db_file_size);
505505
}
506506

507507
/// Danger zone! This will delete all files in the given directory!
@@ -519,12 +519,7 @@ class Store implements Finalizable {
519519
/// For Dart Native apps, pass null to use the [defaultDirectoryPath].
520520
static void removeDbFiles(String? directoryPath) {
521521
final path = _safeDirectoryPath(directoryPath);
522-
final cStr = path.toNativeUtf8();
523-
try {
524-
checkObx(C.remove_db_files(cStr.cast()));
525-
} finally {
526-
malloc.free(cStr);
527-
}
522+
checkObx(withNativeString(path, C.remove_db_files));
528523
}
529524

530525
/// Returns a store reference you can use to create a new store instance with

objectbox_test/test/store_test.dart

+6
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ void main() {
291291
Directory('store').deleteSync(recursive: true);
292292
});
293293

294+
test('store dbFileSize', () {
295+
final testEnv = TestEnv("db-file-size");
296+
expect(Store.dbFileSize(testEnv.dbDirPath), isPositive);
297+
testEnv.closeAndDelete();
298+
});
299+
294300
test('store maxDBSizeInKB', () {
295301
final testDir = Directory('db-size-test');
296302
if (testDir.existsSync()) testDir.deleteSync(recursive: true);

0 commit comments

Comments
 (0)