@@ -220,6 +220,7 @@ public static boolean isSyncServerAvailable() {
220
220
221
221
private final File directory ;
222
222
private final String canonicalPath ;
223
+ /** Reference to the native store. Should probably get through {@link #getNativeStore()} instead. */
223
224
private final long handle ;
224
225
private final Map <Class <?>, String > dbNameByClass = new HashMap <>();
225
226
private final Map <Class <?>, Integer > entityTypeIdByClass = new HashMap <>();
@@ -467,11 +468,12 @@ public static long sysProcStatusKb(String key) {
467
468
* @return 0 if the size could not be determined (does not throw unless this store was already closed)
468
469
*/
469
470
public long sizeOnDisk () {
470
- checkOpen ();
471
- return nativeSizeOnDisk (handle );
471
+ return nativeSizeOnDisk (getNativeStore ());
472
472
}
473
473
474
474
/**
475
+ * Closes this if this is finalized.
476
+ * <p>
475
477
* Explicitly call {@link #close()} instead to avoid expensive finalization.
476
478
*/
477
479
@ SuppressWarnings ("deprecation" ) // finalize()
@@ -481,8 +483,11 @@ protected void finalize() throws Throwable {
481
483
super .finalize ();
482
484
}
483
485
486
+ /**
487
+ * Verifies this has not been {@link #close() closed}.
488
+ */
484
489
private void checkOpen () {
485
- if (closed ) {
490
+ if (isClosed () ) {
486
491
throw new IllegalStateException ("Store is closed" );
487
492
}
488
493
}
@@ -533,13 +538,12 @@ <T> EntityInfo<T> getEntityInfo(Class<T> entityClass) {
533
538
*/
534
539
@ Internal
535
540
public Transaction beginTx () {
536
- checkOpen ();
537
541
// Because write TXs are typically not cached, initialCommitCount is not as relevant than for read TXs.
538
542
int initialCommitCount = commitCount ;
539
543
if (debugTxWrite ) {
540
544
System .out .println ("Begin TX with commit count " + initialCommitCount );
541
545
}
542
- long nativeTx = nativeBeginTx (handle );
546
+ long nativeTx = nativeBeginTx (getNativeStore () );
543
547
if (nativeTx == 0 ) throw new DbException ("Could not create native transaction" );
544
548
545
549
Transaction tx = new Transaction (this , nativeTx , initialCommitCount );
@@ -555,7 +559,6 @@ public Transaction beginTx() {
555
559
*/
556
560
@ Internal
557
561
public Transaction beginReadTx () {
558
- checkOpen ();
559
562
// initialCommitCount should be acquired before starting the tx. In race conditions, there is a chance the
560
563
// commitCount is already outdated. That's OK because it only gives a false positive for an TX being obsolete.
561
564
// In contrast, a false negative would make a TX falsely not considered obsolete, and thus readers would not be
@@ -565,7 +568,7 @@ public Transaction beginReadTx() {
565
568
if (debugTxRead ) {
566
569
System .out .println ("Begin read TX with commit count " + initialCommitCount );
567
570
}
568
- long nativeTx = nativeBeginReadTx (handle );
571
+ long nativeTx = nativeBeginReadTx (getNativeStore () );
569
572
if (nativeTx == 0 ) throw new DbException ("Could not create native read transaction" );
570
573
571
574
Transaction tx = new Transaction (this , nativeTx , initialCommitCount );
@@ -575,6 +578,9 @@ public Transaction beginReadTx() {
575
578
return tx ;
576
579
}
577
580
581
+ /**
582
+ * If this was {@link #close() closed}.
583
+ */
578
584
public boolean isClosed () {
579
585
return closed ;
580
586
}
@@ -584,8 +590,7 @@ public boolean isClosed() {
584
590
* If true the schema is not updated and write transactions are not possible.
585
591
*/
586
592
public boolean isReadOnly () {
587
- checkOpen ();
588
- return nativeIsReadOnly (handle );
593
+ return nativeIsReadOnly (getNativeStore ());
589
594
}
590
595
591
596
/**
@@ -665,7 +670,7 @@ private void checkThreadTermination() {
665
670
* Note: If false is returned, any number of files may have been deleted before the failure happened.
666
671
*/
667
672
public boolean deleteAllFiles () {
668
- if (!closed ) {
673
+ if (!isClosed () ) {
669
674
throw new IllegalStateException ("Store must be closed" );
670
675
}
671
676
return deleteAllFiles (directory );
@@ -765,8 +770,7 @@ public static boolean deleteAllFiles(@Nullable File baseDirectoryOrNull, @Nullab
765
770
* </ul>
766
771
*/
767
772
public void removeAllObjects () {
768
- checkOpen ();
769
- nativeDropAllData (handle );
773
+ nativeDropAllData (getNativeStore ());
770
774
}
771
775
772
776
@ Internal
@@ -1049,8 +1053,7 @@ public <R> void callInTxAsync(final Callable<R> callable, @Nullable final TxCall
1049
1053
* @return String that is typically logged by the application.
1050
1054
*/
1051
1055
public String diagnose () {
1052
- checkOpen ();
1053
- return nativeDiagnose (handle );
1056
+ return nativeDiagnose (getNativeStore ());
1054
1057
}
1055
1058
1056
1059
/**
@@ -1069,13 +1072,11 @@ public long validate(long pageLimit, boolean checkLeafLevel) {
1069
1072
if (pageLimit < 0 ) {
1070
1073
throw new IllegalArgumentException ("pageLimit must be zero or positive" );
1071
1074
}
1072
- checkOpen ();
1073
- return nativeValidate (handle , pageLimit , checkLeafLevel );
1075
+ return nativeValidate (getNativeStore (), pageLimit , checkLeafLevel );
1074
1076
}
1075
1077
1076
1078
public int cleanStaleReadTransactions () {
1077
- checkOpen ();
1078
- return nativeCleanStaleReadTransactions (handle );
1079
+ return nativeCleanStaleReadTransactions (getNativeStore ());
1079
1080
}
1080
1081
1081
1082
/**
@@ -1090,11 +1091,6 @@ public void closeThreadResources() {
1090
1091
// activeTx is cleaned up in finally blocks, so do not free them here
1091
1092
}
1092
1093
1093
- @ Internal
1094
- long internalHandle () {
1095
- return handle ;
1096
- }
1097
-
1098
1094
/**
1099
1095
* A {@link io.objectbox.reactive.DataObserver} can be subscribed to data changes using the returned builder.
1100
1096
* The observer is supplied via {@link SubscriptionBuilder#observer(DataObserver)} and will be notified once a
@@ -1146,8 +1142,7 @@ public String startObjectBrowser() {
1146
1142
@ Nullable
1147
1143
public String startObjectBrowser (int port ) {
1148
1144
verifyObjectBrowserNotRunning ();
1149
- checkOpen ();
1150
- String url = nativeStartObjectBrowser (handle , null , port );
1145
+ String url = nativeStartObjectBrowser (getNativeStore (), null , port );
1151
1146
if (url != null ) {
1152
1147
objectBrowserPort = port ;
1153
1148
}
@@ -1158,14 +1153,13 @@ public String startObjectBrowser(int port) {
1158
1153
@ Nullable
1159
1154
public String startObjectBrowser (String urlToBindTo ) {
1160
1155
verifyObjectBrowserNotRunning ();
1161
- checkOpen ();
1162
1156
int port ;
1163
1157
try {
1164
1158
port = new URL (urlToBindTo ).getPort (); // Gives -1 if not available
1165
1159
} catch (MalformedURLException e ) {
1166
1160
throw new RuntimeException ("Can not start Object Browser at " + urlToBindTo , e );
1167
1161
}
1168
- String url = nativeStartObjectBrowser (handle , urlToBindTo , 0 );
1162
+ String url = nativeStartObjectBrowser (getNativeStore () , urlToBindTo , 0 );
1169
1163
if (url != null ) {
1170
1164
objectBrowserPort = port ;
1171
1165
}
@@ -1178,8 +1172,7 @@ public synchronized boolean stopObjectBrowser() {
1178
1172
throw new IllegalStateException ("ObjectBrowser has not been started before" );
1179
1173
}
1180
1174
objectBrowserPort = 0 ;
1181
- checkOpen ();
1182
- return nativeStopObjectBrowser (handle );
1175
+ return nativeStopObjectBrowser (getNativeStore ());
1183
1176
}
1184
1177
1185
1178
@ Experimental
@@ -1204,8 +1197,7 @@ private void verifyObjectBrowserNotRunning() {
1204
1197
* This for example allows central error handling or special logging for database-related exceptions.
1205
1198
*/
1206
1199
public void setDbExceptionListener (@ Nullable DbExceptionListener dbExceptionListener ) {
1207
- checkOpen ();
1208
- nativeSetDbExceptionListener (handle , dbExceptionListener );
1200
+ nativeSetDbExceptionListener (getNativeStore (), dbExceptionListener );
1209
1201
}
1210
1202
1211
1203
@ Internal
@@ -1234,18 +1226,19 @@ public TxCallback<?> internalFailedReadTxAttemptCallback() {
1234
1226
}
1235
1227
1236
1228
void setDebugFlags (int debugFlags ) {
1237
- checkOpen ();
1238
- nativeSetDebugFlags (handle , debugFlags );
1229
+ nativeSetDebugFlags (getNativeStore (), debugFlags );
1239
1230
}
1240
1231
1241
1232
long panicModeRemoveAllObjects (int entityId ) {
1242
- checkOpen ();
1243
- return nativePanicModeRemoveAllObjects (handle , entityId );
1233
+ return nativePanicModeRemoveAllObjects (getNativeStore (), entityId );
1244
1234
}
1245
1235
1246
1236
/**
1247
- * If you want to use the same ObjectBox store using the C API, e.g. via JNI, this gives the required pointer,
1248
- * which you have to pass on to obx_store_wrap().
1237
+ * Gets the reference to the native store. Can be used with the C API to use the same store, e.g. via JNI, by
1238
+ * passing it on to {@code obx_store_wrap()}.
1239
+ * <p>
1240
+ * Throws if the store is closed.
1241
+ * <p>
1249
1242
* The procedure is like this:<br>
1250
1243
* 1) you create a BoxStore on the Java side<br>
1251
1244
* 2) you call this method to get the native store pointer<br>
0 commit comments