use of com.mongodb.client.model.Collation in project mongo-java-driver by mongodb.
the class DB method getCreateCollectionOperation.
@SuppressWarnings("deprecation")
private CreateCollectionOperation getCreateCollectionOperation(final String collectionName, final DBObject options) {
if (options.get("size") != null && !(options.get("size") instanceof Number)) {
throw new IllegalArgumentException("'size' should be Number");
}
if (options.get("max") != null && !(options.get("max") instanceof Number)) {
throw new IllegalArgumentException("'max' should be Number");
}
if (options.get("capped") != null && !(options.get("capped") instanceof Boolean)) {
throw new IllegalArgumentException("'capped' should be Boolean");
}
if (options.get("autoIndexId") != null && !(options.get("autoIndexId") instanceof Boolean)) {
throw new IllegalArgumentException("'autoIndexId' should be Boolean");
}
if (options.get("storageEngine") != null && !(options.get("storageEngine") instanceof DBObject)) {
throw new IllegalArgumentException("'storageEngine' should be DBObject");
}
if (options.get("indexOptionDefaults") != null && !(options.get("indexOptionDefaults") instanceof DBObject)) {
throw new IllegalArgumentException("'indexOptionDefaults' should be DBObject");
}
if (options.get("validator") != null && !(options.get("validator") instanceof DBObject)) {
throw new IllegalArgumentException("'validator' should be DBObject");
}
if (options.get("validationLevel") != null && !(options.get("validationLevel") instanceof String)) {
throw new IllegalArgumentException("'validationLevel' should be String");
}
if (options.get("validationAction") != null && !(options.get("validationAction") instanceof String)) {
throw new IllegalArgumentException("'validationAction' should be String");
}
boolean capped = false;
boolean autoIndex = true;
long sizeInBytes = 0;
long maxDocuments = 0;
Boolean usePowerOfTwoSizes = null;
BsonDocument storageEngineOptions = null;
BsonDocument indexOptionDefaults = null;
BsonDocument validator = null;
ValidationLevel validationLevel = null;
ValidationAction validationAction = null;
if (options.get("capped") != null) {
capped = (Boolean) options.get("capped");
}
if (options.get("size") != null) {
sizeInBytes = ((Number) options.get("size")).longValue();
}
if (options.get("autoIndexId") != null) {
autoIndex = (Boolean) options.get("autoIndexId");
}
if (options.get("max") != null) {
maxDocuments = ((Number) options.get("max")).longValue();
}
if (options.get("usePowerOfTwoSizes") != null) {
usePowerOfTwoSizes = (Boolean) options.get("usePowerOfTwoSizes");
}
if (options.get("storageEngine") != null) {
storageEngineOptions = wrap((DBObject) options.get("storageEngine"));
}
if (options.get("indexOptionDefaults") != null) {
indexOptionDefaults = wrap((DBObject) options.get("indexOptionDefaults"));
}
if (options.get("validator") != null) {
validator = wrap((DBObject) options.get("validator"));
}
if (options.get("validationLevel") != null) {
validationLevel = ValidationLevel.fromString((String) options.get("validationLevel"));
}
if (options.get("validationAction") != null) {
validationAction = ValidationAction.fromString((String) options.get("validationAction"));
}
Collation collation = DBObjectCollationHelper.createCollationFromOptions(options);
return new CreateCollectionOperation(getName(), collectionName, getWriteConcern()).capped(capped).collation(collation).sizeInBytes(sizeInBytes).autoIndex(autoIndex).maxDocuments(maxDocuments).usePowerOf2Sizes(usePowerOfTwoSizes).storageEngineOptions(storageEngineOptions).indexOptionDefaults(indexOptionDefaults).validator(validator).validationLevel(validationLevel).validationAction(validationAction);
}
use of com.mongodb.client.model.Collation in project mongo-java-driver by mongodb.
the class DBTest method shouldCreateCollectionWithTheSetCollation.
@Test
public void shouldCreateCollectionWithTheSetCollation() {
assumeThat(serverVersionAtLeast(3, 4), is(true));
// Given
collection.drop();
Collation collation = Collation.builder().locale("en").caseLevel(true).collationCaseFirst(CollationCaseFirst.OFF).collationStrength(CollationStrength.IDENTICAL).numericOrdering(true).collationAlternate(CollationAlternate.SHIFTED).collationMaxVariable(CollationMaxVariable.SPACE).backwards(true).build();
DBObject options = BasicDBObject.parse("{ collation: { locale: 'en', caseLevel: true, caseFirst: 'off', strength: 5," + "numericOrdering: true, alternate: 'shifted', maxVariable: 'space', backwards: true }}");
// When
database.createCollection(collectionName, options);
BsonDocument collectionCollation = getCollectionInfo(collectionName).getDocument("options").getDocument("collation");
// Then
BsonDocument collationDocument = collation.asDocument();
for (String key : collationDocument.keySet()) {
assertEquals(collationDocument.get(key), collectionCollation.get(key));
}
// When - collation set on the database
database.getCollection(collectionName).drop();
database.createCollection(collectionName, new BasicDBObject("collation", BasicDBObject.parse(collation.asDocument().toJson())));
collectionCollation = getCollectionInfo(collectionName).getDocument("options").getDocument("collation");
// Then
collationDocument = collation.asDocument();
for (String key : collationDocument.keySet()) {
assertEquals(collationDocument.get(key), collectionCollation.get(key));
}
}
use of com.mongodb.client.model.Collation in project morphia by mongodb.
the class DeleteOptionsTest method passThrough.
@Test
public void passThrough() {
Collation collation = Collation.builder().locale("en").caseLevel(true).build();
DBCollectionRemoveOptions options = new DeleteOptions().collation(collation).writeConcern(WriteConcern.JOURNALED).getOptions();
assertEquals(collation, options.getCollation());
assertEquals(WriteConcern.JOURNALED, options.getWriteConcern());
}
use of com.mongodb.client.model.Collation in project morphia by mongodb.
the class FindAndModifyOptionsTest method passThrough.
@Test
public void passThrough() {
Collation collation = Collation.builder().locale("en").caseLevel(true).build();
DBCollectionFindAndModifyOptions options = new FindAndModifyOptions().bypassDocumentValidation(true).collation(collation).getOptions().maxTime(15, TimeUnit.MINUTES).projection(new BasicDBObject("field", "value")).remove(true).returnNew(true).sort(new BasicDBObject("field", -1)).update(new BasicDBObject("$inc", "somefield")).upsert(true).writeConcern(WriteConcern.JOURNALED);
assertTrue(options.getBypassDocumentValidation());
assertEquals(collation, options.getCollation());
assertEquals(15, options.getMaxTime(TimeUnit.MINUTES));
assertEquals(new BasicDBObject("field", "value"), options.getProjection());
assertTrue(options.isRemove());
assertTrue(options.returnNew());
assertEquals(new BasicDBObject("field", -1), options.getSort());
assertEquals(new BasicDBObject("$inc", "somefield"), options.getUpdate());
assertTrue(options.isUpsert());
assertEquals(WriteConcern.JOURNALED, options.getWriteConcern());
}
use of com.mongodb.client.model.Collation in project morphia by mongodb.
the class CountOptionsTest method passThrough.
@Test
public void passThrough() {
Collation collation = Collation.builder().locale("en").caseLevel(true).build();
DBCollectionCountOptions options = new CountOptions().collation(collation).hint("i'm a hint").limit(18).maxTime(15, TimeUnit.MINUTES).readPreference(ReadPreference.secondaryPreferred()).readConcern(ReadConcern.LOCAL).skip(12).getOptions();
assertEquals(collation, options.getCollation());
assertEquals("i'm a hint", options.getHintString());
assertEquals(18, options.getLimit());
assertEquals(15, options.getMaxTime(TimeUnit.MINUTES));
assertEquals(ReadPreference.secondaryPreferred(), options.getReadPreference());
assertEquals(ReadConcern.LOCAL, options.getReadConcern());
assertEquals(12, options.getSkip());
}
Aggregations