Java Code Examples for com.orientechnologies.orient.core.metadata.schema.OClass#INDEX_TYPE

The following examples show how to use com.orientechnologies.orient.core.metadata.schema.OClass#INDEX_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FulltextIndexFieldExtension.java    From guice-persist-orient with MIT License 6 votes vote down vote up
private boolean isIndexCorrect(final IndexValidationSupport support, final OClass.INDEX_TYPE type,
                               final FulltextIndex annotation) {
    final OIndex classIndex = support.getIndex();
    final ODocument metadata = classIndex.getConfiguration();
    final Iterable<String> field = metadata.field(STOP_WORDS);
    return support
            .isIndexSigns(metadata.field(INDEX_RADIX),
                    metadata.field(IGNORE_CHARS),
                    metadata.field(SEPARATOR_CHARS),
                    metadata.field(MIN_WORD_LENGTH),
                    Sets.newHashSet(field))
            .matchRequiredSigns(type, annotation.indexRadix(),
                    annotation.ignoreChars(),
                    annotation.separatorChars(),
                    annotation.minWordLength(),
                    Sets.newHashSet(annotation.stopWords()));
}
 
Example 2
Source File: FulltextIndexFieldExtension.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void afterRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                              final Field field, final FulltextIndex annotation) {
    final String property = field.getName();
    final String model = descriptor.schemeClass;
    final String name = MoreObjects.firstNonNull(
            Strings.emptyToNull(annotation.name().trim()), model + '.' + property);
    final OClass clazz = db.getMetadata().getSchema().getClass(model);
    final OIndex<?> classIndex = clazz.getClassIndex(name);
    final OClass.INDEX_TYPE type = annotation.useHashIndex()
            ? OClass.INDEX_TYPE.FULLTEXT_HASH_INDEX : OClass.INDEX_TYPE.FULLTEXT;
    if (!descriptor.initialRegistration && classIndex != null) {
        final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger);

        support.checkTypeCompatible(OClass.INDEX_TYPE.FULLTEXT, OClass.INDEX_TYPE.FULLTEXT_HASH_INDEX);
        support.checkFieldsCompatible(property);

        final boolean correct = isIndexCorrect(support, type, annotation);
        if (!correct) {
            support.dropIndex(db);
        } else {
            // index ok
            return;
        }
    }
    final ODocument metadata = createMetadata(annotation);
    clazz.createIndex(name, type.name(), null, metadata, null, new String[]{property});
    logger.info("Fulltext index '{}' ({} [{}]) {} created", name, model, property, type);
}
 
Example 3
Source File: LuceneIndexFieldExtension.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void afterRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                              final Field field, final LuceneIndex annotation) {
    db.getMetadata().getIndexManager().reload();
    final String property = field.getName();
    final String model = descriptor.schemeClass;
    final String name = MoreObjects.firstNonNull(
            Strings.emptyToNull(annotation.name().trim()), model + '.' + property);
    final OClass clazz = db.getMetadata().getSchema().getClass(model);
    final OIndex<?> classIndex = clazz.getClassIndex(name);
    final OClass.INDEX_TYPE type = OClass.INDEX_TYPE.FULLTEXT;
    if (!descriptor.initialRegistration && classIndex != null) {
        final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger);
        support.checkTypeCompatible(type);
        support.checkFieldsCompatible(property);

        final boolean correct = support
                .isIndexSigns(classIndex.getConfiguration().field("algorithm"), getAnalyzer(classIndex))
                .matchRequiredSigns(type, OLuceneIndexFactory.LUCENE_ALGORITHM, annotation.value().getName());
        if (!correct) {
            support.dropIndex(db);
        } else {
            // index ok
            return;
        }
    }
    final ODocument metadata = createMetadata(annotation);
    SchemeUtils.command(db, "create index %s on %s (%s) %s engine %s metadata %s",
            name, model, property, type.name(), OLuceneIndexFactory.LUCENE_ALGORITHM, metadata.toJSON());
    logger.info("Lucene fulltext index '{}' ({} [{}]) created", name, model, property);
}
 
Example 4
Source File: IndexValidationSupport.java    From guice-persist-orient with MIT License 5 votes vote down vote up
/**
 * Checks that current index type is equal to one of provided types.
 * If not, exception thrown. Type check is important for fulltext and lucene indexes because,
 * most likely, if existing index type is different from that specific index type then index name was set
 * by mistake and to avoid replacing existing index error should be thrown (programmer mistake).
 *
 * @param types allowed index types
 */
public void checkTypeCompatible(final OClass.INDEX_TYPE... types) {
    final Set<String> allowed = Sets.newHashSet(Iterables.transform(Arrays.asList(types),
            new Function<OClass.INDEX_TYPE, String>() {
                @Nonnull
                @Override
                public String apply(@Nonnull final OClass.INDEX_TYPE input) {
                    return input.name();
                }
            }));
    check(allowed.contains(index.getType()),
            "Existing index '%s' (class '%s') type '%s' is incompatible with '%s'. "
                    + "Either drop existing index or rename index.",
            index.getName(), index.getDefinition().getClassName(), index.getType(), Joiner.on(',').join(allowed));
}
 
Example 5
Source File: IndexValidationSupport.java    From guice-persist-orient with MIT License 5 votes vote down vote up
/**
 * Checks current index signs with new signature signs.
 *
 * @param type  index type
 * @param signs signs to check
 * @return true if indexes are equal, false otherwise
 */
public boolean matchRequiredSigns(final OClass.INDEX_TYPE type, final Object... signs) {
    final List<Object> reqsigns = Lists.newArrayList();
    reqsigns.add(type.toString());
    reqsigns.addAll(Arrays.asList(signs));
    Preconditions.checkState(indexSigns.size() == reqsigns.size(), "Incorrect signs count for comparison");
    boolean res = true;
    for (int i = 0; i < indexSigns.size(); i++) {
        if (!Objects.equal(indexSigns.get(i), reqsigns.get(i))) {
            res = false;
            break;
        }
    }
    return res;
}
 
Example 6
Source File: IndexFieldExtension.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void afterRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                              final Field field, final Index annotation) {
    final String property = field.getName();
    final String model = descriptor.schemeClass;
    final String name = MoreObjects.firstNonNull(
            Strings.emptyToNull(annotation.name().trim()), model + '.' + property);
    final OClass clazz = db.getMetadata().getSchema().getClass(model);
    final OIndex<?> classIndex = clazz.getClassIndex(name);
    final OClass.INDEX_TYPE type = annotation.value();
    if (!descriptor.initialRegistration && classIndex != null) {
        final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger);

        support.checkFieldsCompatible(property);

        final boolean correct = support
                .isIndexSigns(classIndex.getDefinition().isNullValuesIgnored())
                .matchRequiredSigns(type, annotation.ignoreNullValues());
        if (!correct) {
            support.dropIndex(db);
        } else {
            // index ok
            return;
        }
    }
    final ODocument metadata = new ODocument()
            .field("ignoreNullValues", annotation.ignoreNullValues());
    clazz.createIndex(name, type.name(), null, metadata, new String[]{property});
    logger.info("Index '{}' ({} [{}]) {} created", name, model, property, type);
}
 
Example 7
Source File: LuceneIndexTypeExtension.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void afterRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                              final CompositeLuceneIndex annotation) {
    db.getMetadata().getIndexManager().reload();
    final String name = Strings.emptyToNull(annotation.name().trim());
    Preconditions.checkArgument(name != null, "Index name required");
    final String model = descriptor.schemeClass;
    final OClass clazz = db.getMetadata().getSchema().getClass(model);
    final OIndex<?> classIndex = clazz.getClassIndex(name);
    final OClass.INDEX_TYPE type = OClass.INDEX_TYPE.FULLTEXT;
    final String[] fields = annotation.fields();
    if (!descriptor.initialRegistration && classIndex != null) {
        final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger);
        support.checkTypeCompatible(type);
        support.checkFieldsCompatible(fields);

        final boolean correct = support
                .isIndexSigns(classIndex.getConfiguration().field("algorithm"), getAnalyzer(classIndex))
                .matchRequiredSigns(type, OLuceneIndexFactory.LUCENE_ALGORITHM, annotation.analyzer().getName());
        if (!correct) {
            support.dropIndex(db);
        } else {
            // index ok
            return;
        }
    }
    final ODocument metadata = createMetadata(annotation);
    SchemeUtils.command(db, "create index %s on %s (%s) %s engine %s metadata %s", name, model,
            Joiner.on(',').join(fields), type.name(), OLuceneIndexFactory.LUCENE_ALGORITHM, metadata.toJSON());
    logger.info("Lucene fulltext index '{}' ({} [{}]) created", name, model, Joiner.on(',').join(fields));
}
 
Example 8
Source File: IndexTypeExtension.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void afterRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                              final CompositeIndex annotation) {
    // single field index definition intentionally allowed (no check)
    final String name = Strings.emptyToNull(annotation.name().trim());
    Preconditions.checkArgument(name != null, "Index name required");
    final String model = descriptor.schemeClass;
    final OClass clazz = db.getMetadata().getSchema().getClass(model);
    final OIndex<?> classIndex = clazz.getClassIndex(name);
    final OClass.INDEX_TYPE type = annotation.type();
    final String[] fields = annotation.fields();
    if (!descriptor.initialRegistration && classIndex != null) {
        final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger);

        support.checkFieldsCompatible(fields);

        final boolean correct = support
                .isIndexSigns(classIndex.getDefinition().isNullValuesIgnored())
                .matchRequiredSigns(type, annotation.ignoreNullValues());
        if (!correct) {
            support.dropIndex(db);
        } else {
            // index ok
            return;
        }
    }
    final ODocument metadata = new ODocument()
            .field("ignoreNullValues", annotation.ignoreNullValues());
    clazz.createIndex(name, type.name(), null, metadata, fields);
    logger.info("Index '{}' ({} [{}]) {} created", name, model, Joiner.on(',').join(fields), type);
}