use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.
the class IndexHelper method calculateKeys.
BsonDocument calculateKeys(final MappedClass mc, final Index index) {
BsonDocument keys = new BsonDocument();
for (Field field : index.fields()) {
String path;
try {
path = findField(mc, index.options(), new ArrayList<String>(asList(field.value().split("\\."))));
} catch (Exception e) {
path = field.value();
String message = format("The path '%s' can not be validated against '%s' and may represent an invalid index", path, mc.getClazz().getName());
if (!index.options().disableValidation()) {
throw new MappingException(message);
}
LOG.warning(message);
}
keys.putAll(toBsonDocument(path, field.type().toIndexValue()));
}
return keys;
}
use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.
the class IndexHelper method collectTopLevelIndexes.
private List<Index> collectTopLevelIndexes(final MappedClass mc) {
List<Index> list = new ArrayList<Index>();
if (mc != null) {
final List<Indexes> annotations = mc.getAnnotations(Indexes.class);
if (annotations != null) {
for (final Indexes indexes : annotations) {
for (final Index index : indexes.value()) {
Index updated = index;
if (index.fields().length == 0) {
LOG.warning(format("This index on '%s' is using deprecated configuration options. Please update to use the " + "fields value on @Index: %s", mc.getClazz().getName(), index.toString()));
updated = new IndexBuilder().migrate(index);
}
List<Field> fields = new ArrayList<Field>();
for (Field field : updated.fields()) {
fields.add(new FieldBuilder().value(findField(mc, index.options(), asList(field.value().split("\\.")))).type(field.type()).weight(field.weight()));
}
list.add(replaceFields(updated, fields));
}
}
}
list.addAll(collectTopLevelIndexes(mc.getSuperClass()));
}
return list;
}
use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.
the class IndexBuilder method parseFieldsString.
private List<Field> parseFieldsString(final String str) {
List<Field> fields = new ArrayList<Field>();
final String[] parts = str.split(",");
for (String s : parts) {
s = s.trim();
IndexType dir = IndexType.ASC;
if (s.startsWith("-")) {
dir = IndexType.DESC;
s = s.substring(1).trim();
}
fields.add(new FieldBuilder().value(s).type(dir));
}
return fields;
}
use of org.mongodb.morphia.annotations.Field in project morphia by mongodb.
the class IndexHelper method collectNestedIndexes.
private List<Index> collectNestedIndexes(final MappedClass mc, final List<MappedClass> parentMCs) {
List<Index> list = new ArrayList<Index>();
for (final MappedField mf : mc.getPersistenceFields()) {
if (!mf.isTypeMongoCompatible() && !mf.hasAnnotation(Reference.class) && !mf.hasAnnotation(Serialized.class) && !mf.hasAnnotation(NotSaved.class) && !mf.isTransient()) {
final List<MappedClass> parents = new ArrayList<MappedClass>(parentMCs);
parents.add(mc);
List<MappedClass> classes = new ArrayList<MappedClass>();
MappedClass mappedClass = mapper.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass());
classes.add(mappedClass);
classes.addAll(mapper.getSubTypes(mappedClass));
for (MappedClass aClass : classes) {
for (Index index : collectIndexes(aClass, parents)) {
List<Field> fields = new ArrayList<Field>();
for (Field field : index.fields()) {
fields.add(new FieldBuilder().value(field.value().equals("$**") ? field.value() : mf.getNameToStore() + "." + field.value()).type(field.type()).weight(field.weight()));
}
list.add(new IndexBuilder(index).fields(fields));
}
}
}
}
return list;
}
Aggregations