Skip to content

Commit 6cefcba

Browse files
authored
[DE-541] forceOneShardAttributeValue (#491)
1 parent 502a119 commit 6cefcba

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
## [6.22.0] - 2023-03-07
10+
11+
- added support to `forceOneShardAttributeValue` query parameter (DE-541)
12+
913
## [6.21.0] - 2023-03-07
1014

1115
- added `x-arango-driver` header (DE-479)

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,28 @@ public AqlQueryOptions shardIds(final String... shardIds) {
403403
return this;
404404
}
405405

406+
407+
public String getForceOneShardAttributeValue() {
408+
return options != null ? options.forceOneShardAttributeValue : null;
409+
}
410+
411+
/**
412+
* @param forceOneShardAttributeValue This query option can be used in complex queries in case the query optimizer
413+
* cannot automatically detect that the query can be limited to only a single
414+
* server (e.g. in a disjoint smart graph case).
415+
* <p/>
416+
* If the option is set incorrectly, i.e. to a wrong shard key value, then the
417+
* query may be shipped to a wrong DB server and may not return results (i.e.
418+
* empty result set).
419+
* <p/>
420+
* Use at your own risk.
421+
* @return options
422+
*/
423+
public AqlQueryOptions forceOneShardAttributeValue(final String forceOneShardAttributeValue) {
424+
getOptions().forceOneShardAttributeValue = forceOneShardAttributeValue;
425+
return this;
426+
}
427+
406428
private Options getOptions() {
407429
if (options == null) {
408430
options = new Options();
@@ -429,6 +451,7 @@ public static class Options implements Serializable {
429451
private Collection<String> shardIds;
430452
private Double maxRuntime;
431453
private Boolean fillBlockCache;
454+
private String forceOneShardAttributeValue;
432455

433456
protected Optimizer getOptimizer() {
434457
if (optimizer == null) {

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

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

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

0 commit comments

Comments
 (0)