Skip to content

Commit 56b3ac6

Browse files
committed
support to forceOneShardAttributeValue query parameter (DE-541)
1 parent 9676dcd commit 56b3ac6

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

core/src/main/java/com/arangodb/model/AqlQueryOptions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,27 @@ public AqlQueryOptions shardIds(final String... shardIds) {
433433
return this;
434434
}
435435

436+
public String getForceOneShardAttributeValue() {
437+
return options != null ? options.forceOneShardAttributeValue : null;
438+
}
439+
440+
/**
441+
* @param forceOneShardAttributeValue This query option can be used in complex queries in case the query optimizer
442+
* cannot automatically detect that the query can be limited to only a single
443+
* server (e.g. in a disjoint smart graph case).
444+
* <p/>
445+
* If the option is set incorrectly, i.e. to a wrong shard key value, then the
446+
* query may be shipped to a wrong DB server and may not return results (i.e.
447+
* empty result set).
448+
* <p/>
449+
* Use at your own risk.
450+
* @return options
451+
*/
452+
public AqlQueryOptions forceOneShardAttributeValue(final String forceOneShardAttributeValue) {
453+
getOptions().forceOneShardAttributeValue = forceOneShardAttributeValue;
454+
return this;
455+
}
456+
436457
public Options getOptions() {
437458
if (options == null) {
438459
options = new Options();
@@ -487,6 +508,7 @@ public static final class Options {
487508
private Collection<String> shardIds;
488509
private Double maxRuntime;
489510
private Boolean fillBlockCache;
511+
private String forceOneShardAttributeValue;
490512

491513
public Boolean getFailOnWarning() {
492514
return failOnWarning;

driver/src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,38 @@ void queryWithWarning(ArangoDB arangoDB) {
915915
@ParameterizedTest(name = "{index}")
916916
@MethodSource("dbs")
917917
void queryStream(ArangoDatabase db) {
918-
if (isAtLeastVersion(3, 4)) {
919-
final ArangoCursor<Void> cursor = db
920-
.query("FOR i IN 1..2 RETURN i", Void.class, new AqlQueryOptions().stream(true).count(true));
921-
assertThat((Object) cursor).isNotNull();
922-
assertThat(cursor.getCount()).isNull();
923-
}
918+
final ArangoCursor<Void> cursor = db
919+
.query("FOR i IN 1..2 RETURN i", Void.class, new AqlQueryOptions().stream(true).count(true));
920+
assertThat((Object) cursor).isNotNull();
921+
assertThat(cursor.getCount()).isNull();
922+
}
923+
924+
@ParameterizedTest(name = "{index}")
925+
@MethodSource("dbs")
926+
void queryForceOneShardAttributeValue(ArangoDatabase db) {
927+
assumeTrue(isAtLeastVersion(3, 10));
928+
assumeTrue(isCluster());
929+
assumeTrue(isEnterprise());
930+
931+
String cname = "forceOneShardAttr-" + UUID.randomUUID();
932+
db.createCollection(cname, new CollectionCreateOptions()
933+
.shardKeys("foo")
934+
.numberOfShards(3));
935+
ArangoCollection col = db.collection(cname);
936+
BaseDocument doc = new BaseDocument();
937+
doc.addAttribute("foo", "bar");
938+
col.insertDocument(doc);
939+
940+
ArangoCursor<BaseDocument> c1 = db
941+
.query("FOR d IN @@c RETURN d", BaseDocument.class, Collections.singletonMap("@c", cname),
942+
new AqlQueryOptions().forceOneShardAttributeValue("bar"));
943+
assertThat(c1.hasNext()).isTrue();
944+
assertThat(c1.next().getAttribute("foo")).isEqualTo("bar");
945+
946+
ArangoCursor<BaseDocument> c2 = db
947+
.query("FOR d IN @@c RETURN d", BaseDocument.class, Collections.singletonMap("@c", cname),
948+
new AqlQueryOptions().forceOneShardAttributeValue("ooo"));
949+
assertThat(c2.hasNext()).isFalse();
924950
}
925951

926952
@ParameterizedTest(name = "{index}")

0 commit comments

Comments
 (0)