Example #1
0
  private List<Index> collectTopLevelIndexes(final MappedClass mc) {
    List<Index> list = new ArrayList<Index>();
    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));
        }
      }
    }
    return list;
  }
Example #2
0
 private void calculateWeights(
     final Index index, final com.mongodb.client.model.IndexOptions indexOptions) {
   Document weights = new Document();
   for (Field field : index.fields()) {
     if (field.weight() != -1) {
       if (field.type() != IndexType.TEXT) {
         throw new MappingException(
             "Weight values only apply to text indexes: " + Arrays.toString(index.fields()));
       }
       weights.put(field.value(), field.weight());
     }
   }
   if (!weights.isEmpty()) {
     indexOptions.weights(weights);
   }
 }
Example #3
0
 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;
 }
Example #4
0
  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;
  }