@@ -135,7 +135,12 @@ protected void finalize() throws Throwable {
135
135
}
136
136
137
137
/**
138
- * If possible, try to close the query once you are done with it to reclaim resources immediately.
138
+ * Closes this query and frees used resources.
139
+ * <p>
140
+ * If possible, call this always once done with this. Otherwise, will be called once this is finalized (e.g. garbage
141
+ * collected).
142
+ * <p>
143
+ * Calling any other methods of this afterwards will throw an exception.
139
144
*/
140
145
public synchronized void close () {
141
146
if (handle != 0 ) {
@@ -256,6 +261,7 @@ public long[] findIds() {
256
261
*/
257
262
@ Nonnull
258
263
public long [] findIds (final long offset , final long limit ) {
264
+ checkOpen ();
259
265
return box .internalCallWithReaderHandle (cursorHandle -> nativeFindIds (handle , cursorHandle , offset , limit ));
260
266
}
261
267
@@ -294,6 +300,7 @@ public PropertyQuery property(Property<T> property) {
294
300
}
295
301
296
302
<R > R callInReadTx (Callable <R > callable ) {
303
+ checkOpen ();
297
304
return store .callInReadTxWithRetry (callable , queryAttempts , INITIAL_RETRY_BACK_OFF_IN_MS , true );
298
305
}
299
306
@@ -308,6 +315,7 @@ <R> R callInReadTx(Callable<R> callable) {
308
315
*/
309
316
public void forEach (final QueryConsumer <T > consumer ) {
310
317
ensureNoComparator ();
318
+ checkOpen (); // findIds also checks, but throw early outside of transaction.
311
319
box .getStore ().runInReadTx (() -> {
312
320
LazyList <T > lazyList = new LazyList <>(box , findIds (), false );
313
321
int size = lazyList .size ();
@@ -384,6 +392,7 @@ void resolveEagerRelation(@Nonnull T entity, EagerRelation<T, ?> eagerRelation)
384
392
385
393
/** Returns the count of Objects matching the query. */
386
394
public long count () {
395
+ checkOpen ();
387
396
ensureNoFilter ();
388
397
return box .internalCallWithReaderHandle (cursorHandle -> nativeCount (handle , cursorHandle ));
389
398
}
@@ -392,6 +401,7 @@ public long count() {
392
401
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
393
402
*/
394
403
public Query <T > setParameter (Property <?> property , String value ) {
404
+ checkOpen ();
395
405
nativeSetParameter (handle , property .getEntityId (), property .getId (), null , value );
396
406
return this ;
397
407
}
@@ -402,6 +412,7 @@ public Query<T> setParameter(Property<?> property, String value) {
402
412
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
403
413
*/
404
414
public Query <T > setParameter (String alias , String value ) {
415
+ checkOpen ();
405
416
nativeSetParameter (handle , 0 , 0 , alias , value );
406
417
return this ;
407
418
}
@@ -410,6 +421,7 @@ public Query<T> setParameter(String alias, String value) {
410
421
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
411
422
*/
412
423
public Query <T > setParameter (Property <?> property , long value ) {
424
+ checkOpen ();
413
425
nativeSetParameter (handle , property .getEntityId (), property .getId (), null , value );
414
426
return this ;
415
427
}
@@ -420,6 +432,7 @@ public Query<T> setParameter(Property<?> property, long value) {
420
432
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
421
433
*/
422
434
public Query <T > setParameter (String alias , long value ) {
435
+ checkOpen ();
423
436
nativeSetParameter (handle , 0 , 0 , alias , value );
424
437
return this ;
425
438
}
@@ -428,6 +441,7 @@ public Query<T> setParameter(String alias, long value) {
428
441
* Sets a parameter previously given to the {@link QueryBuilder} to a new value.
429
442
*/
430
443
public Query <T > setParameter (Property <?> property , double value ) {
444
+ checkOpen ();
431
445
nativeSetParameter (handle , property .getEntityId (), property .getId (), null , value );
432
446
return this ;
433
447
}
@@ -438,6 +452,7 @@ public Query<T> setParameter(Property<?> property, double value) {
438
452
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
439
453
*/
440
454
public Query <T > setParameter (String alias , double value ) {
455
+ checkOpen ();
441
456
nativeSetParameter (handle , 0 , 0 , alias , value );
442
457
return this ;
443
458
}
@@ -481,6 +496,7 @@ public Query<T> setParameter(String alias, boolean value) {
481
496
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
482
497
*/
483
498
public Query <T > setParameters (Property <?> property , long value1 , long value2 ) {
499
+ checkOpen ();
484
500
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , value1 , value2 );
485
501
return this ;
486
502
}
@@ -491,6 +507,7 @@ public Query<T> setParameters(Property<?> property, long value1, long value2) {
491
507
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
492
508
*/
493
509
public Query <T > setParameters (String alias , long value1 , long value2 ) {
510
+ checkOpen ();
494
511
nativeSetParameters (handle , 0 , 0 , alias , value1 , value2 );
495
512
return this ;
496
513
}
@@ -499,6 +516,7 @@ public Query<T> setParameters(String alias, long value1, long value2) {
499
516
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
500
517
*/
501
518
public Query <T > setParameters (Property <?> property , int [] values ) {
519
+ checkOpen ();
502
520
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , values );
503
521
return this ;
504
522
}
@@ -509,6 +527,7 @@ public Query<T> setParameters(Property<?> property, int[] values) {
509
527
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
510
528
*/
511
529
public Query <T > setParameters (String alias , int [] values ) {
530
+ checkOpen ();
512
531
nativeSetParameters (handle , 0 , 0 , alias , values );
513
532
return this ;
514
533
}
@@ -517,6 +536,7 @@ public Query<T> setParameters(String alias, int[] values) {
517
536
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
518
537
*/
519
538
public Query <T > setParameters (Property <?> property , long [] values ) {
539
+ checkOpen ();
520
540
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , values );
521
541
return this ;
522
542
}
@@ -527,6 +547,7 @@ public Query<T> setParameters(Property<?> property, long[] values) {
527
547
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
528
548
*/
529
549
public Query <T > setParameters (String alias , long [] values ) {
550
+ checkOpen ();
530
551
nativeSetParameters (handle , 0 , 0 , alias , values );
531
552
return this ;
532
553
}
@@ -535,6 +556,7 @@ public Query<T> setParameters(String alias, long[] values) {
535
556
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
536
557
*/
537
558
public Query <T > setParameters (Property <?> property , double value1 , double value2 ) {
559
+ checkOpen ();
538
560
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , value1 , value2 );
539
561
return this ;
540
562
}
@@ -545,6 +567,7 @@ public Query<T> setParameters(Property<?> property, double value1, double value2
545
567
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
546
568
*/
547
569
public Query <T > setParameters (String alias , double value1 , double value2 ) {
570
+ checkOpen ();
548
571
nativeSetParameters (handle , 0 , 0 , alias , value1 , value2 );
549
572
return this ;
550
573
}
@@ -553,6 +576,7 @@ public Query<T> setParameters(String alias, double value1, double value2) {
553
576
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
554
577
*/
555
578
public Query <T > setParameters (Property <?> property , String [] values ) {
579
+ checkOpen ();
556
580
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , values );
557
581
return this ;
558
582
}
@@ -563,6 +587,7 @@ public Query<T> setParameters(Property<?> property, String[] values) {
563
587
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
564
588
*/
565
589
public Query <T > setParameters (String alias , String [] values ) {
590
+ checkOpen ();
566
591
nativeSetParameters (handle , 0 , 0 , alias , values );
567
592
return this ;
568
593
}
@@ -571,6 +596,7 @@ public Query<T> setParameters(String alias, String[] values) {
571
596
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
572
597
*/
573
598
public Query <T > setParameters (Property <?> property , String key , String value ) {
599
+ checkOpen ();
574
600
nativeSetParameters (handle , property .getEntityId (), property .getId (), null , key , value );
575
601
return this ;
576
602
}
@@ -581,6 +607,7 @@ public Query<T> setParameters(Property<?> property, String key, String value) {
581
607
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
582
608
*/
583
609
public Query <T > setParameters (String alias , String key , String value ) {
610
+ checkOpen ();
584
611
nativeSetParameters (handle , 0 , 0 , alias , key , value );
585
612
return this ;
586
613
}
@@ -589,6 +616,7 @@ public Query<T> setParameters(String alias, String key, String value) {
589
616
* Sets a parameter previously given to the {@link QueryBuilder} to new values.
590
617
*/
591
618
public Query <T > setParameter (Property <?> property , byte [] value ) {
619
+ checkOpen ();
592
620
nativeSetParameter (handle , property .getEntityId (), property .getId (), null , value );
593
621
return this ;
594
622
}
@@ -599,6 +627,7 @@ public Query<T> setParameter(Property<?> property, byte[] value) {
599
627
* @param alias as defined using {@link QueryBuilder#parameterAlias(String)}.
600
628
*/
601
629
public Query <T > setParameter (String alias , byte [] value ) {
630
+ checkOpen ();
602
631
nativeSetParameter (handle , 0 , 0 , alias , value );
603
632
return this ;
604
633
}
@@ -609,6 +638,7 @@ public Query<T> setParameter(String alias, byte[] value) {
609
638
* @return count of removed Objects
610
639
*/
611
640
public long remove () {
641
+ checkOpen ();
612
642
ensureNoFilter ();
613
643
return box .internalCallWithWriterHandle (cursorHandle -> nativeRemove (handle , cursorHandle ));
614
644
}
@@ -632,6 +662,7 @@ public long remove() {
632
662
* it may be GCed and observers may become stale (won't receive anymore data).
633
663
*/
634
664
public SubscriptionBuilder <List <T >> subscribe () {
665
+ checkOpen ();
635
666
return new SubscriptionBuilder <>(publisher , null );
636
667
}
637
668
@@ -663,6 +694,7 @@ public void publish() {
663
694
* Note: the format of the returned string may change without notice.
664
695
*/
665
696
public String describe () {
697
+ checkOpen ();
666
698
return nativeToString (handle );
667
699
}
668
700
@@ -673,7 +705,17 @@ public String describe() {
673
705
* Note: the format of the returned string may change without notice.
674
706
*/
675
707
public String describeParameters () {
708
+ checkOpen ();
676
709
return nativeDescribeParameters (handle );
677
710
}
678
711
712
+ /**
713
+ * Throws if {@link #close()} has been called for this.
714
+ */
715
+ private void checkOpen () {
716
+ if (handle == 0 ) {
717
+ throw new IllegalStateException ("This query is closed. Build and use a new one." );
718
+ }
719
+ }
720
+
679
721
}
0 commit comments