Skip to content

Commit 14a5a89

Browse files
authored
added schema attribute to CollectionPropertiesOptions (arangodb#388)
1 parent 227ca2b commit 14a5a89

File tree

6 files changed

+104
-12
lines changed

6 files changed

+104
-12
lines changed

src/main/java/com/arangodb/internal/velocypack/VPackSerializers.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,33 @@
2020

2121
package com.arangodb.internal.velocypack;
2222

23-
import com.arangodb.entity.*;
24-
import com.arangodb.entity.arangosearch.*;
23+
import com.arangodb.entity.BaseDocument;
24+
import com.arangodb.entity.BaseEdgeDocument;
25+
import com.arangodb.entity.CollectionType;
26+
import com.arangodb.entity.DocumentField;
27+
import com.arangodb.entity.LogLevel;
28+
import com.arangodb.entity.MinReplicationFactor;
29+
import com.arangodb.entity.Permissions;
30+
import com.arangodb.entity.ReplicationFactor;
31+
import com.arangodb.entity.ViewType;
32+
import com.arangodb.entity.arangosearch.ArangoSearchCompression;
33+
import com.arangodb.entity.arangosearch.ArangoSearchProperties;
34+
import com.arangodb.entity.arangosearch.CollectionLink;
35+
import com.arangodb.entity.arangosearch.ConsolidationType;
36+
import com.arangodb.entity.arangosearch.FieldLink;
37+
import com.arangodb.entity.arangosearch.PrimarySort;
38+
import com.arangodb.entity.arangosearch.StoreValuesType;
39+
import com.arangodb.entity.arangosearch.StoredValue;
2540
import com.arangodb.internal.velocystream.internal.AuthenticationRequest;
2641
import com.arangodb.model.CollectionSchema;
2742
import com.arangodb.model.TraversalOptions;
2843
import com.arangodb.model.TraversalOptions.Order;
2944
import com.arangodb.model.arangosearch.ArangoSearchPropertiesOptions;
30-
import com.arangodb.velocypack.*;
45+
import com.arangodb.velocypack.VPackBuilder;
46+
import com.arangodb.velocypack.VPackParser;
47+
import com.arangodb.velocypack.VPackSerializer;
48+
import com.arangodb.velocypack.VPackSlice;
49+
import com.arangodb.velocypack.ValueType;
3150
import com.arangodb.velocystream.Request;
3251

3352
import java.util.Collection;
@@ -247,10 +266,10 @@ private static void serializeFieldLinks(final VPackBuilder builder, final Collec
247266

248267
public static final VPackSerializer<CollectionSchema> COLLECTION_VALIDATION = (builder, attribute, value, context) -> {
249268
VPackParser parser = new VPackParser.Builder().build();
250-
VPackSlice rule = parser.fromJson(value.getRule(), true);
269+
VPackSlice rule = value.getRule() != null ? parser.fromJson(value.getRule(), true) : null;
251270
final Map<String, Object> doc = new HashMap<>();
252271
doc.put("message", value.getMessage());
253-
doc.put("level", value.getLevel().getValue());
272+
doc.put("level", value.getLevel() != null ? value.getLevel().getValue() : null);
254273
doc.put("rule", rule);
255274
context.serialize(builder, attribute, doc);
256275
};

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
package com.arangodb.model;
2222

23-
import com.arangodb.entity.*;
23+
import com.arangodb.entity.CollectionType;
24+
import com.arangodb.entity.KeyOptions;
25+
import com.arangodb.entity.KeyType;
26+
import com.arangodb.entity.MinReplicationFactor;
27+
import com.arangodb.entity.ReplicationFactor;
2428

2529
/**
2630
* @author Mark Vollmary
@@ -330,10 +334,21 @@ public CollectionSchema getSchema() {
330334
}
331335

332336
/**
333-
* @param schema object that specifies the collection level schema for documents
334-
* @since ArangoDB 3.7
337+
* @param schema
338+
* object that specifies the collection level schema for documents
335339
* @return options
340+
* @since ArangoDB 3.7
341+
*/
342+
public CollectionCreateOptions schema(final CollectionSchema schema) {
343+
this.schema = schema;
344+
return this;
345+
}
346+
347+
/**
348+
* @since ArangoDB 3.7
349+
* @deprecated Use {@link #schema(CollectionSchema)} instead.
336350
*/
351+
@Deprecated
337352
public CollectionCreateOptions setSchema(final CollectionSchema schema) {
338353
this.schema = schema;
339354
return this;

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

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class CollectionPropertiesOptions {
2929

3030
private Boolean waitForSync;
3131
private Long journalSize;
32+
private CollectionSchema schema;
3233

3334
public CollectionPropertiesOptions() {
3435
super();
@@ -62,4 +63,18 @@ public CollectionPropertiesOptions journalSize(final Long journalSize) {
6263
return this;
6364
}
6465

66+
public CollectionSchema getSchema() {
67+
return schema;
68+
}
69+
70+
/**
71+
* @param schema object that specifies the collection level schema for documents
72+
* @since ArangoDB 3.7
73+
* @return options
74+
*/
75+
public CollectionPropertiesOptions schema(final CollectionSchema schema) {
76+
this.schema = schema;
77+
return this;
78+
}
79+
6580
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
* @since ArangoDB 3.7
2929
*/
3030
public class CollectionSchema {
31+
32+
/**
33+
* Value to unset the collection schema on properties update {@link com.arangodb.ArangoCollection#changeProperties(CollectionPropertiesOptions)}.
34+
*/
35+
public static final CollectionSchema NULL_SCHEMA = new CollectionSchema()
36+
.setLevel(null)
37+
.setMessage(null)
38+
.setRule(null);
39+
3140
private String rule;
3241
private Level level;
3342
private String message;

src/test/java/com/arangodb/ArangoCollectionTest.java

+37-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.arangodb.entity.ShardEntity;
3838
import com.arangodb.model.CollectionCreateOptions;
3939
import com.arangodb.model.CollectionPropertiesOptions;
40+
import com.arangodb.model.CollectionSchema;
4041
import com.arangodb.model.DocumentCreateOptions;
4142
import com.arangodb.model.DocumentDeleteOptions;
4243
import com.arangodb.model.DocumentExistsOptions;
@@ -2612,14 +2613,47 @@ public void getPropeties() {
26122613
public void changeProperties() {
26132614
final CollectionPropertiesEntity properties = collection.getProperties();
26142615
assertThat(properties.getWaitForSync(), is(notNullValue()));
2616+
if (isAtLeastVersion(3, 7)) {
2617+
assertThat(properties.getSchema(), is(nullValue()));
2618+
}
26152619

2616-
final CollectionPropertiesEntity changedProperties = collection.changeProperties(new CollectionPropertiesOptions()
2617-
.waitForSync(!properties.getWaitForSync()));
2620+
String schemaRule = ("{ " +
2621+
" \"properties\": {" +
2622+
" \"number\": {" +
2623+
" \"type\": \"number\"" +
2624+
" }" +
2625+
" }" +
2626+
" }")
2627+
.replaceAll("\\s", "");
2628+
String schemaMessage = "The document has problems!";
2629+
2630+
CollectionPropertiesOptions updatedOptions = new CollectionPropertiesOptions()
2631+
.waitForSync(!properties.getWaitForSync())
2632+
.schema(new CollectionSchema()
2633+
.setLevel(CollectionSchema.Level.NEW)
2634+
.setMessage(schemaMessage)
2635+
.setRule(schemaRule)
2636+
);
2637+
2638+
final CollectionPropertiesEntity changedProperties = collection.changeProperties(updatedOptions);
26182639
assertThat(changedProperties.getWaitForSync(), is(notNullValue()));
26192640
assertThat(changedProperties.getWaitForSync(), is(!properties.getWaitForSync()));
2641+
if (isAtLeastVersion(3, 7)) {
2642+
assertThat(changedProperties.getSchema(), is(notNullValue()));
2643+
assertThat(changedProperties.getSchema().getLevel(), is(CollectionSchema.Level.NEW));
2644+
assertThat(changedProperties.getSchema().getMessage(), is(schemaMessage));
2645+
assertThat(changedProperties.getSchema().getRule(), is(schemaRule));
2646+
}
26202647

26212648
// revert changes
2622-
collection.changeProperties(new CollectionPropertiesOptions().waitForSync(properties.getWaitForSync()));
2649+
CollectionPropertiesEntity revertedProperties = collection.changeProperties(new CollectionPropertiesOptions()
2650+
.waitForSync(properties.getWaitForSync())
2651+
.schema(CollectionSchema.NULL_SCHEMA)
2652+
);
2653+
if (isAtLeastVersion(3, 7)) {
2654+
assertThat(revertedProperties.getSchema(), is(nullValue()));
2655+
}
2656+
26232657
}
26242658

26252659
@Test

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public void createCollectionWithJsonSchema() {
311311

312312
final CollectionEntity result = db
313313
.createCollection(name, new CollectionCreateOptions()
314-
.setSchema(
314+
.schema(
315315
new CollectionSchema()
316316
.setLevel(CollectionSchema.Level.NEW)
317317
.setMessage(message)

0 commit comments

Comments
 (0)