/** * Creates an instance of {@link OIndexDefinition} for automatic index. * * @param oClass * class which will be indexed * @param fieldNames * list of properties which will be indexed. Format should be '<property> [by key|value]', use 'by key' or 'by value' to * describe how to index maps. By default maps indexed by key * @param types * types of indexed properties * @param collates * @param indexKind * @param algorithm * @return index definition instance */ public static OIndexDefinition createIndexDefinition(final OClass oClass, final List<String> fieldNames, final List<OType> types, List<OCollate> collates, String indexKind, String algorithm) { checkTypes(oClass, fieldNames, types); if (fieldNames.size() == 1) return createSingleFieldIndexDefinition(oClass, fieldNames.get(0), types.get(0), collates == null ? null : collates.get(0), indexKind, algorithm); else return createMultipleFieldIndexDefinition(oClass, fieldNames, types, collates, indexKind, algorithm); }
public List<OType> extractFieldTypes(final String[] fieldNames) { final List<OType> types = new ArrayList<OType>(fieldNames.length); for (String fieldName : fieldNames) { if (!fieldName.equals("@rid")) types.add(getProperty(decodeClassName(OIndexDefinitionFactory.extractFieldName(fieldName))).getType()); else types.add(OType.LINK); } return types; }
private static OPropertyMapIndexDefinition.INDEX_BY extractMapIndexSpecifier(final String fieldName) { String[] fieldNameParts = FILED_NAME_PATTERN.split(fieldName); if (fieldNameParts.length == 1) return OPropertyMapIndexDefinition.INDEX_BY.KEY; if (fieldNameParts.length == 3) { Locale locale = getServerLocale(); if ("by".equals(fieldNameParts[1].toLowerCase(locale))) try { return OPropertyMapIndexDefinition.INDEX_BY.valueOf(fieldNameParts[2].toUpperCase(locale)); } catch (IllegalArgumentException iae) { throw new IllegalArgumentException( "Illegal field name format, should be '<property> [by key|value]' but was '" + fieldName + '\'', iae); } } throw new IllegalArgumentException( "Illegal field name format, should be '<property> [by key|value]' but was '" + fieldName + '\''); }
public OIndex<?> createIndex(final String name, String type, final OProgressListener progressListener, ODocument metadata, String algorithm, final String... fields) { if (type == null) throw new IllegalArgumentException("Index type is null"); type = type.toUpperCase(Locale.ENGLISH); if (fields.length == 0) { throw new OIndexException("List of fields to index cannot be empty."); } final String localName = this.name; final int[] localPolymorphicClusterIds = polymorphicClusterIds; for (final String fieldToIndex : fields) { final String fieldName = decodeClassName(OIndexDefinitionFactory.extractFieldName(fieldToIndex)); if (!fieldName.equals("@rid") && !existsProperty(fieldName)) throw new OIndexException( "Index with name '" + name + "' cannot be created on class '" + localName + "' because the field '" + fieldName + "' is absent in class definition"); } final OIndexDefinition indexDefinition = OIndexDefinitionFactory .createIndexDefinition(this, Arrays.asList(fields), extractFieldTypes(fields), null, type, algorithm); return getDatabase().getMetadata().getIndexManager() .createIndex(name, type, indexDefinition, localPolymorphicClusterIds, progressListener, metadata, algorithm); }
public OIndex build(final ODatabaseDocumentTx db) { checkState(!propertyNames.isEmpty(), "At least one property is required"); checkState(propertyTypes.size() == propertyNames.size(), "A type must be defined for each property"); List<OCollate> collates = null; if (caseInsensitive) { collates = Lists.transform(propertyNames, n -> new OCaseInsensitiveCollate()); } ODocument metadata = new ODocument(); if (ignoreNullValues) { metadata.field("ignoreNullValues", true); } OIndexDefinition indexDefinition = OIndexDefinitionFactory.createIndexDefinition(type, propertyNames, propertyTypes, collates, indexType.name(), null); return db.getMetadata().getIndexManager().createIndex(name, indexType.name(), indexDefinition, type.getPolymorphicClusterIds(), null, metadata.fields() > 0 ? metadata : null); } }
private static OIndexDefinition createSingleFieldIndexDefinition(OClass oClass, final String field, final OType type, OCollate collate, String indexKind, String algorithm) { final String fieldName = OClassImpl.decodeClassName(adjustFieldName(oClass, extractFieldName(field))); final OIndexDefinition indexDefinition; final OType indexType; if (type == OType.EMBEDDEDMAP || type == OType.LINKMAP) { final OPropertyMapIndexDefinition.INDEX_BY indexBy = extractMapIndexSpecifier(field);
private static OIndexDefinition createMultipleFieldIndexDefinition(final OClass oClass, final List<String> fieldsToIndex, final List<OType> types, List<OCollate> collates, String indexKind, String algorithm) { final OIndexFactory factory = OIndexes.getFactory(indexKind, algorithm); final String className = oClass.getName(); final OCompositeIndexDefinition compositeIndex = new OCompositeIndexDefinition(className); for (int i = 0, fieldsToIndexSize = fieldsToIndex.size(); i < fieldsToIndexSize; i++) { OCollate collate = null; if (collates != null) collate = collates.get(i); compositeIndex .addIndex(createSingleFieldIndexDefinition(oClass, fieldsToIndex.get(i), types.get(i), collate, indexKind, algorithm)); } return compositeIndex; }
private OIndex<?> getoIndex(OClass oClass, String[] fields, String engine, ODatabase database, List<OCollate> collatesList, ODocument metadataDoc) { OIndex<?> idx; if ((keyTypes == null || keyTypes.size() == 0) && collatesList == null) { idx = oClass.createIndex(name.getValue(), type.getStringValue(), null, metadataDoc, engine, fields); } else { final List<OType> fieldTypeList; if (keyTypes == null || keyTypes.size() == 0 && fields.length > 0) { for (final String fieldName : fields) { if (!fieldName.equals("@rid") && !oClass.existsProperty(fieldName)) throw new OIndexException( "Index with name : '" + name.getValue() + "' cannot be created on class : '" + oClass.getName() + "' because field: '" + fieldName + "' is absent in class definition."); } fieldTypeList = ((OClassImpl) oClass).extractFieldTypes(fields); } else fieldTypeList = keyTypes.stream().map(x -> OType.valueOf(x.getStringValue())).collect(Collectors.toList()); final OIndexDefinition idxDef = OIndexDefinitionFactory .createIndexDefinition(oClass, Arrays.asList(fields), fieldTypeList, collatesList, type.getStringValue(), null); idx = database.getMetadata().getIndexManager() .createIndex(name.getValue(), type.getStringValue(), idxDef, oClass.getPolymorphicClusterIds(), null, metadataDoc, engine); } return idx; }
.createIndexDefinition(oClass, Arrays.asList(fields), fieldTypeList, collatesList, indexType.toString(), null);