Skip to content

Commit 1cd4e4f

Browse files
Merge branch '203-new-db-size-apis' into 'dev'
Store: add dbSize and dbSizeOnDisk, deprecate sizeOnDisk See merge request objectbox/objectbox-java!132
2 parents e76e08c + b5991dd commit 1cd4e4f

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ static native void nativeRegisterCustomType(long store, int entityId, int proper
194194

195195
static native boolean nativeIsObjectBrowserAvailable();
196196

197-
native long nativeSizeOnDisk(long store);
197+
native long nativeDbSize(long store);
198+
199+
native long nativeDbSizeOnDisk(long store);
198200

199201
native long nativeValidate(long store, long pageLimit, boolean checkLeafLevel);
200202

@@ -484,9 +486,31 @@ public static long sysProcStatusKb(String key) {
484486
* The size in bytes occupied by the data file on disk.
485487
*
486488
* @return 0 if the size could not be determined (does not throw unless this store was already closed)
489+
* @deprecated Use {@link #getDbSize()} or {@link #getDbSizeOnDisk()} instead which properly handle in-memory databases.
487490
*/
491+
@Deprecated
488492
public long sizeOnDisk() {
489-
return nativeSizeOnDisk(getNativeStore());
493+
return getDbSize();
494+
}
495+
496+
/**
497+
* Get the size of this store. For a disk-based store type, this corresponds to the size on disk, and for the
498+
* in-memory store type, this is roughly the used memory bytes occupied by the data.
499+
*
500+
* @return The size in bytes of the database, or 0 if the file does not exist or some error occurred.
501+
*/
502+
public long getDbSize() {
503+
return nativeDbSize(getNativeStore());
504+
}
505+
506+
/**
507+
* The size in bytes occupied by the database on disk (if any).
508+
*
509+
* @return The size in bytes of the database on disk, or 0 if the underlying database is in-memory only
510+
* or the size could not be determined.
511+
*/
512+
public long getDbSizeOnDisk() {
513+
return nativeDbSizeOnDisk(getNativeStore());
490514
}
491515

492516
/**

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public void testClose() {
114114

115115
// Methods using the native store should throw.
116116
assertThrowsStoreIsClosed(store::sizeOnDisk);
117+
assertThrowsStoreIsClosed(store::getDbSize);
118+
assertThrowsStoreIsClosed(store::getDbSizeOnDisk);
117119
assertThrowsStoreIsClosed(store::beginTx);
118120
assertThrowsStoreIsClosed(store::beginReadTx);
119121
assertThrowsStoreIsClosed(store::isReadOnly);
@@ -306,9 +308,14 @@ private Callable<String> createTestCallable(final int[] countHolder) {
306308

307309
@Test
308310
public void testSizeOnDisk() {
309-
long size = store.sizeOnDisk();
310311
// Note: initial database does have a non-zero (file) size.
311-
assertTrue(size > 0);
312+
long legacySizeOnDisk = store.sizeOnDisk();
313+
assertTrue(legacySizeOnDisk > 0);
314+
315+
assertTrue(store.getDbSize() > 0);
316+
317+
long sizeOnDisk = store.getDbSizeOnDisk();
318+
assertEquals(IN_MEMORY ? 0 : 12288, sizeOnDisk);
312319
}
313320

314321
@Test

0 commit comments

Comments
 (0)