@Override public String languageOverride() { return get("languageOverride"); }
private IndexOptionsBuilder indexOptions() { return new IndexOptionsBuilder() .name("index_name") .background(true) .collation(collation()) .disableValidation(true) .dropDups(true) .expireAfterSeconds(42) .language("en") .languageOverride("de") .sparse(true) .unique(true); }
@Test @SuppressWarnings("deprecation") public void normalizeIndexed() { Indexed indexed = new IndexedBuilder() .value(IndexDirection.DESC) .options(new IndexOptionsBuilder().name("index_name") .background(true) .dropDups(true) .expireAfterSeconds(42) .sparse(true) .unique(true)); Index converted = indexHelper.convert(indexed, "oldstyle"); assertEquals(converted.options().name(), "index_name"); assertTrue(converted.options().background()); assertTrue(converted.options().dropDups()); assertTrue(converted.options().sparse()); assertTrue(converted.options().unique()); assertEquals(new FieldBuilder().value("oldstyle").type(IndexType.DESC), converted.fields()[0]); }
@Test public void textPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Text text = new TextBuilder() .value(4) .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, indexHelper.convert(text, "text"), false); findPartialIndex(BasicDBObject.parse(text.options().partialFilter())); }
IndexBuilder migrate(final Index index) { return fields(parseFieldsString(index.value())) .options(new IndexOptionsBuilder().migrate(index)); } }
@Test public void findField() { MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); assertEquals("indexName", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), singletonList("indexName"))); assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nested", "name"))); assertEquals("nest.name", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "name"))); try { assertEquals("nest.whatsit", indexHelper.findField(mappedClass, new IndexOptionsBuilder(), asList("nest", "whatsit"))); fail("Should have failed on the bad index path"); } catch (MappingException e) { // alles ist gut } assertEquals("nest.whatsit.nested.more.deeply.than.the.object.model", indexHelper.findField(mappedClass, new IndexOptionsBuilder().disableValidation(true), asList("nest", "whatsit", "nested", "more", "deeply", "than", "the", "object", "model"))); }
public IndexOptionsBuilder(final IndexOptions original, final String prefix) { super(original); if(!"".equals(original.partialFilter())) { final Document parse = Document.parse(original.partialFilter()); final Document filter = new Document(); for (final Entry<String, Object> entry : parse.entrySet()) { filter.put(prefix + "." + entry.getKey(), entry.getValue()); } partialFilter(filter.toJson()); } }
IndexBuilder(final Index original, final String prefix) { super(original); fields(updateFieldsWithPrefix(prefix, original)); options(new IndexOptionsBuilder(original.options(), prefix)); }
@Test public void indexOptionsConversion() { IndexOptionsBuilder indexOptions = indexOptions(); com.mongodb.client.model.IndexOptions options = indexHelper.convert(indexOptions, false); assertEquals("index_name", options.getName()); assertTrue(options.isBackground()); assertTrue(options.isUnique()); assertTrue(options.isSparse()); assertEquals(Long.valueOf(42), options.getExpireAfter(TimeUnit.SECONDS)); assertEquals("en", options.getDefaultLanguage()); assertEquals("de", options.getLanguageOverride()); assertEquals(indexHelper.convert(indexOptions.collation()), options.getCollation()); assertTrue(indexHelper.convert(indexOptions, true).isBackground()); assertTrue(indexHelper.convert(indexOptions.background(false), true).isBackground()); assertTrue(indexHelper.convert(indexOptions.background(true), true).isBackground()); assertTrue(indexHelper.convert(indexOptions.background(true), false).isBackground()); assertFalse(indexHelper.convert(indexOptions.background(false), false).isBackground()); }
@Test @SuppressWarnings("deprecation") public void convertTextIndex() { TextBuilder text = new TextBuilder() .value(4) .options(new IndexOptionsBuilder() .name("index_name") .background(true) .dropDups(true) .expireAfterSeconds(42) .sparse(true) .unique(true)); Index index = indexHelper.convert(text, "search_field"); assertEquals(index.options().name(), "index_name"); assertTrue(index.options().background()); assertTrue(index.options().dropDups()); assertTrue(index.options().sparse()); assertTrue(index.options().unique()); assertEquals(new FieldBuilder() .value("search_field") .type(IndexType.TEXT) .weight(4), index.fields()[0]); }
@Test public void indexedPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Indexed indexed = new IndexedBuilder() .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, indexHelper.convert(indexed, "text"), false); findPartialIndex(BasicDBObject.parse(indexed.options().partialFilter())); }
@SuppressWarnings("deprecation") Index convert(final Indexed indexed, final String nameToStore) { if (indexed.dropDups() || indexed.options().dropDups()) { LOG.warn("Support for dropDups has been removed from the server. Please remove this setting."); } final Map<String, Object> newOptions = extractOptions(indexed.options()); if (!extractOptions(indexed).isEmpty() && !newOptions.isEmpty()) { throw new MappingException("Mixed usage of deprecated @Indexed values with the new @IndexOption values is not " + "allowed. Please migrate all settings to @IndexOptions"); } List<Field> fields = Collections.<Field>singletonList(new FieldBuilder() .value(nameToStore) .type(fromValue(indexed.value().toIndexValue()))); return newOptions.isEmpty() ? new IndexBuilder() .options(new IndexOptionsBuilder() .migrate(indexed)) .fields(fields) : new IndexBuilder() .options(indexed.options()) .fields(fields); }
@Test public void calculateBadKeys() { MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); IndexBuilder index = new IndexBuilder() .fields(new FieldBuilder() .value("texting") .type(IndexType.TEXT) .weight(1), new FieldBuilder() .value("nest") .type(IndexType.DESC)); try { indexHelper.calculateKeys(mappedClass, index); fail("Validation should have failed on the bad key"); } catch (MappingException e) { // all good } index.options(new IndexOptionsBuilder().disableValidation(true)); indexHelper.calculateKeys(mappedClass, index); }
@Test public void indexPartialFilters() { MongoCollection<Document> collection = getDatabase().getCollection("indexes"); MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class); Index index = new IndexBuilder() .fields(new FieldBuilder().value("text")) .options(new IndexOptionsBuilder() .partialFilter("{ name : { $gt : 13 } }")); indexHelper.createIndex(collection, mappedClass, index, false); findPartialIndex(BasicDBObject.parse(index.options().partialFilter())); } @Test
@Override public boolean background() { return get("background"); }
@Override public boolean disableValidation() { return get("disableValidation"); }
@Override public int expireAfterSeconds() { return get("expireAfterSeconds"); }
@Override public Collation collation() { return get("collation"); }
@Override public boolean dropDups() { return get("dropDups"); }
@Override public String language() { return get("language"); }