Skip to content

Commit f3fd1a5

Browse files
Shubhamgreenrobot-team
authored andcommitted
Query: add new key value conditions for String-key maps #153
1 parent ceb55c1 commit f3fd1a5

File tree

5 files changed

+557
-38
lines changed

5 files changed

+557
-38
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Notable changes to the ObjectBox Java library.
44

55
For more insights into what changed in the ObjectBox C++ core, [check the ObjectBox C changelog](https://github.com/objectbox/objectbox-c/blob/main/CHANGELOG.md).
66

7+
## 4.1.1 - in development
8+
9+
- Add new query conditions `equalKeyValue`, `greaterKeyValue`, `lessKeyValue`, `lessOrEqualKeyValue`, and
10+
`greaterOrEqualKeyValue` that are helpful to write complex queries for [string maps](https://docs.objectbox.io/advanced/custom-types#flex-properties).
11+
These methods support `String`, `long` and `double` data types for the values in the string map.
12+
- Deprecate the `containsKeyValue` condition, use the new `equalKeyValue` condition instead.
13+
714
## 4.1.0 - 2025-01-30
815

916
- Vector Search: add new `VectorDistanceType.GEO` distance type to perform vector searches on geographical coordinates.

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

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2025 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@
4040
import io.objectbox.query.PropertyQueryConditionImpl.StringCondition;
4141
import io.objectbox.query.PropertyQueryConditionImpl.StringCondition.Operation;
4242
import io.objectbox.query.PropertyQueryConditionImpl.StringStringCondition;
43+
import io.objectbox.query.PropertyQueryConditionImpl.StringLongCondition;
44+
import io.objectbox.query.PropertyQueryConditionImpl.StringDoubleCondition;
4345
import io.objectbox.query.Query;
4446
import io.objectbox.query.QueryBuilder.StringOrder;
4547

@@ -496,21 +498,161 @@ public PropertyQueryCondition<ENTITY> containsElement(String value, StringOrder
496498
* For a String-key map property, matches if at least one key and value combination equals the given values
497499
* using {@link StringOrder#CASE_SENSITIVE StringOrder#CASE_SENSITIVE}.
498500
*
501+
* @deprecated Use the {@link #equalKeyValue(String, String, StringOrder)} condition instead.
502+
*
499503
* @see #containsKeyValue(String, String, StringOrder)
500504
*/
505+
@Deprecated
501506
public PropertyQueryCondition<ENTITY> containsKeyValue(String key, String value) {
502-
return new StringStringCondition<>(this, StringStringCondition.Operation.CONTAINS_KEY_VALUE,
507+
return new StringStringCondition<>(this, StringStringCondition.Operation.EQUAL_KEY_VALUE,
503508
key, value, StringOrder.CASE_SENSITIVE);
504509
}
505510

506511
/**
512+
* @deprecated Use the {@link #equalKeyValue(String, String, StringOrder)} condition instead.
507513
* @see #containsKeyValue(String, String)
508514
*/
515+
@Deprecated
509516
public PropertyQueryCondition<ENTITY> containsKeyValue(String key, String value, StringOrder order) {
510-
return new StringStringCondition<>(this, StringStringCondition.Operation.CONTAINS_KEY_VALUE,
517+
return new StringStringCondition<>(this, StringStringCondition.Operation.EQUAL_KEY_VALUE,
518+
key, value, order);
519+
}
520+
521+
/**
522+
* For a String-key map property, matches the combination where the key and value of at least one map entry is equal
523+
* to the given {@code key} and {@code value}.
524+
*/
525+
public PropertyQueryCondition<ENTITY> equalKeyValue(String key, String value, StringOrder order) {
526+
return new StringStringCondition<>(this, StringStringCondition.Operation.EQUAL_KEY_VALUE,
527+
key, value, order);
528+
}
529+
530+
/**
531+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
532+
* than the given {@code key} and {@code value}.
533+
*/
534+
public PropertyQueryCondition<ENTITY> greaterKeyValue(String key, String value, StringOrder order) {
535+
return new StringStringCondition<>(this, StringStringCondition.Operation.GREATER_KEY_VALUE,
536+
key, value, order);
537+
}
538+
539+
/**
540+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
541+
* than or equal to the given {@code key} and {@code value}.
542+
*/
543+
public PropertyQueryCondition<ENTITY> greaterOrEqualKeyValue(String key, String value, StringOrder order) {
544+
return new StringStringCondition<>(this, StringStringCondition.Operation.GREATER_EQUALS_KEY_VALUE,
511545
key, value, order);
512546
}
513547

548+
/**
549+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
550+
* than the given {@code key} and {@code value}.
551+
*/
552+
public PropertyQueryCondition<ENTITY> lessKeyValue(String key, String value, StringOrder order) {
553+
return new StringStringCondition<>(this, StringStringCondition.Operation.LESS_KEY_VALUE,
554+
key, value, order);
555+
}
556+
557+
/**
558+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
559+
* than or equal to the given {@code key} and {@code value}.
560+
*/
561+
public PropertyQueryCondition<ENTITY> lessOrEqualKeyValue(String key, String value, StringOrder order) {
562+
return new StringStringCondition<>(this, StringStringCondition.Operation.LESS_EQUALS_KEY_VALUE,
563+
key, value, order);
564+
}
565+
566+
/**
567+
* For a String-key map property, matches the combination where the key and value of at least one map entry is equal
568+
* to the given {@code key} and {@code value}.
569+
*/
570+
public PropertyQueryCondition<ENTITY> equalKeyValue(String key, long value) {
571+
return new StringLongCondition<>(this, StringLongCondition.Operation.EQUAL_KEY_VALUE,
572+
key, value);
573+
}
574+
575+
/**
576+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
577+
* than the given {@code key} and {@code value}.
578+
*/
579+
public PropertyQueryCondition<ENTITY> greaterKeyValue(String key, long value) {
580+
return new StringLongCondition<>(this, StringLongCondition.Operation.GREATER_KEY_VALUE,
581+
key, value);
582+
}
583+
584+
/**
585+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
586+
* than or equal to the given {@code key} and {@code value}.
587+
*/
588+
public PropertyQueryCondition<ENTITY> greaterOrEqualKeyValue(String key, long value) {
589+
return new StringLongCondition<>(this, StringLongCondition.Operation.GREATER_EQUALS_KEY_VALUE,
590+
key, value);
591+
}
592+
593+
/**
594+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
595+
* than the given {@code key} and {@code value}.
596+
*/
597+
public PropertyQueryCondition<ENTITY> lessKeyValue(String key, long value) {
598+
return new StringLongCondition<>(this, StringLongCondition.Operation.LESS_KEY_VALUE,
599+
key, value);
600+
}
601+
602+
/**
603+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
604+
* than or equal to the given {@code key} and {@code value}.
605+
*/
606+
public PropertyQueryCondition<ENTITY> lessOrEqualKeyValue(String key, long value) {
607+
return new StringLongCondition<>(this, StringLongCondition.Operation.LESS_EQUALS_KEY_VALUE,
608+
key, value);
609+
}
610+
611+
/**
612+
* For a String-key map property, matches the combination where the key and value of at least one map entry is equal
613+
* to the given {@code key} and {@code value}.
614+
*/
615+
public PropertyQueryCondition<ENTITY> equalKeyValue(String key, double value) {
616+
return new StringDoubleCondition<>(this, StringDoubleCondition.Operation.EQUAL_KEY_VALUE,
617+
key, value);
618+
}
619+
620+
/**
621+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
622+
* than the given {@code key} and {@code value}.
623+
*/
624+
public PropertyQueryCondition<ENTITY> greaterKeyValue(String key, double value) {
625+
return new StringDoubleCondition<>(this, StringDoubleCondition.Operation.GREATER_KEY_VALUE,
626+
key, value);
627+
}
628+
629+
/**
630+
* For a String-key map property, matches the combination where the key and value of at least one map entry is greater
631+
* than or equal to the given {@code key} and {@code value}.
632+
*/
633+
public PropertyQueryCondition<ENTITY> greaterOrEqualKeyValue(String key, double value) {
634+
return new StringDoubleCondition<>(this, StringDoubleCondition.Operation.GREATER_EQUALS_KEY_VALUE,
635+
key, value);
636+
}
637+
638+
/**
639+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
640+
* than the given {@code key} and {@code value}.
641+
*/
642+
public PropertyQueryCondition<ENTITY> lessKeyValue(String key, double value) {
643+
return new StringDoubleCondition<>(this, StringDoubleCondition.Operation.LESS_KEY_VALUE,
644+
key, value);
645+
}
646+
647+
/**
648+
* For a String-key map property, matches the combination where the key and value of at least one map entry is less
649+
* than or equal to the given {@code key} and {@code value}.
650+
*/
651+
public PropertyQueryCondition<ENTITY> lessOrEqualKeyValue(String key, double value) {
652+
return new StringDoubleCondition<>(this, StringDoubleCondition.Operation.LESS_EQUALS_KEY_VALUE,
653+
key, value);
654+
}
655+
514656
/**
515657
* Creates a starts with condition using {@link StringOrder#CASE_SENSITIVE StringOrder#CASE_SENSITIVE}.
516658
*

objectbox-java/src/main/java/io/objectbox/query/PropertyQueryConditionImpl.java

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2020-2025 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -375,7 +375,11 @@ public static class StringStringCondition<T> extends PropertyQueryConditionImpl<
375375
private final StringOrder order;
376376

377377
public enum Operation {
378-
CONTAINS_KEY_VALUE
378+
EQUAL_KEY_VALUE,
379+
GREATER_KEY_VALUE,
380+
GREATER_EQUALS_KEY_VALUE,
381+
LESS_KEY_VALUE,
382+
LESS_EQUALS_KEY_VALUE
379383
}
380384

381385
public StringStringCondition(Property<T> property, Operation op, String leftValue, String rightValue, StringOrder order) {
@@ -388,8 +392,92 @@ public StringStringCondition(Property<T> property, Operation op, String leftValu
388392

389393
@Override
390394
void applyCondition(QueryBuilder<T> builder) {
391-
if (op == Operation.CONTAINS_KEY_VALUE) {
392-
builder.containsKeyValue(property, leftValue, rightValue, order);
395+
if (op == Operation.EQUAL_KEY_VALUE) {
396+
builder.equalKeyValue(property, leftValue, rightValue, order);
397+
} else if (op == Operation.GREATER_KEY_VALUE) {
398+
builder.greaterKeyValue(property, leftValue, rightValue, order);
399+
} else if (op == Operation.GREATER_EQUALS_KEY_VALUE) {
400+
builder.greaterOrEqualKeyValue(property, leftValue, rightValue, order);
401+
} else if (op == Operation.LESS_KEY_VALUE) {
402+
builder.lessKeyValue(property, leftValue, rightValue, order);
403+
} else if (op == Operation.LESS_EQUALS_KEY_VALUE) {
404+
builder.lessOrEqualKeyValue(property, leftValue, rightValue, order);
405+
} else {
406+
throw new UnsupportedOperationException(op + " is not supported with two String values");
407+
}
408+
}
409+
}
410+
411+
public static class StringLongCondition<T> extends PropertyQueryConditionImpl<T> {
412+
private final Operation op;
413+
private final String leftValue;
414+
private final long rightValue;
415+
416+
public enum Operation {
417+
EQUAL_KEY_VALUE,
418+
GREATER_KEY_VALUE,
419+
GREATER_EQUALS_KEY_VALUE,
420+
LESS_KEY_VALUE,
421+
LESS_EQUALS_KEY_VALUE
422+
}
423+
424+
public StringLongCondition(Property<T> property, Operation op, String leftValue, long rightValue) {
425+
super(property);
426+
this.op = op;
427+
this.leftValue = leftValue;
428+
this.rightValue = rightValue;
429+
}
430+
431+
@Override
432+
void applyCondition(QueryBuilder<T> builder) {
433+
if (op == Operation.EQUAL_KEY_VALUE) {
434+
builder.equalKeyValue(property, leftValue, rightValue);
435+
} else if (op == Operation.GREATER_KEY_VALUE) {
436+
builder.greaterKeyValue(property, leftValue, rightValue);
437+
} else if (op == Operation.GREATER_EQUALS_KEY_VALUE) {
438+
builder.greaterOrEqualKeyValue(property, leftValue, rightValue);
439+
} else if (op == Operation.LESS_KEY_VALUE) {
440+
builder.lessKeyValue(property, leftValue, rightValue);
441+
} else if (op == Operation.LESS_EQUALS_KEY_VALUE) {
442+
builder.lessOrEqualKeyValue(property, leftValue, rightValue);
443+
} else {
444+
throw new UnsupportedOperationException(op + " is not supported with two String values");
445+
}
446+
}
447+
}
448+
449+
public static class StringDoubleCondition<T> extends PropertyQueryConditionImpl<T> {
450+
private final Operation op;
451+
private final String leftValue;
452+
private final double rightValue;
453+
454+
public enum Operation {
455+
EQUAL_KEY_VALUE,
456+
GREATER_KEY_VALUE,
457+
GREATER_EQUALS_KEY_VALUE,
458+
LESS_KEY_VALUE,
459+
LESS_EQUALS_KEY_VALUE
460+
}
461+
462+
public StringDoubleCondition(Property<T> property, Operation op, String leftValue, double rightValue) {
463+
super(property);
464+
this.op = op;
465+
this.leftValue = leftValue;
466+
this.rightValue = rightValue;
467+
}
468+
469+
@Override
470+
void applyCondition(QueryBuilder<T> builder) {
471+
if (op == Operation.EQUAL_KEY_VALUE) {
472+
builder.equalKeyValue(property, leftValue, rightValue);
473+
} else if (op == Operation.GREATER_KEY_VALUE) {
474+
builder.greaterKeyValue(property, leftValue, rightValue);
475+
} else if (op == Operation.GREATER_EQUALS_KEY_VALUE) {
476+
builder.greaterOrEqualKeyValue(property, leftValue, rightValue);
477+
} else if (op == Operation.LESS_KEY_VALUE) {
478+
builder.lessKeyValue(property, leftValue, rightValue);
479+
} else if (op == Operation.LESS_EQUALS_KEY_VALUE) {
480+
builder.lessOrEqualKeyValue(property, leftValue, rightValue);
393481
} else {
394482
throw new UnsupportedOperationException(op + " is not supported with two String values");
395483
}

0 commit comments

Comments
 (0)