core/src/main/java/com/orientechnologies/orient/core/index/OIndexManagerShared.java
public OIndex<?> createIndex(ODatabaseDocumentInternal database, final String iName, String type,
      final OIndexDefinition indexDefinition, final int[] clusterIdsToIndex, OProgressListener progressListener, ODocument metadata,
      String algorithm) {
    if (database.getTransaction().isActive())
      throw new IllegalStateException("Cannot create a new index inside a transaction");

    final Character c = OSchemaShared.checkFieldNameIfValid(iName);
    if (c != null)
      throw new IllegalArgumentException("Invalid index name '" + iName + "'. Character '" + c + "' is invalid");

    if (indexDefinition == null) {
      throw new IllegalArgumentException("Index definition cannot be null");
    }

    final Locale locale = getServerLocale();
    type = type.toUpperCase(locale);
    if (algorithm == null) {
      algorithm = OIndexes.chooseDefaultIndexAlgorithm(type);
    }

    final String valueContainerAlgorithm = chooseContainerAlgorithm(type);

    final OIndexInternal<?> index;
    acquireExclusiveLock();
    try {

      if (indexes.containsKey(iName))
        throw new OIndexException("Index with name " + iName + " already exists.");

      // manual indexes are always durable
      if (clusterIdsToIndex == null || clusterIdsToIndex.length == 0) {
        if (metadata == null)
          metadata = new ODocument().setTrackingChanges(false);

        final Object durable = metadata.field("durableInNonTxMode");
        if (!(durable instanceof Boolean))
          metadata.field("durableInNonTxMode", true);
        if (metadata.field("trackMode") == null)
          metadata.field("trackMode", "FULL");
      }

      index = OIndexes.createIndex(getStorage(), iName, type, algorithm, valueContainerAlgorithm, metadata, -1);
      if (progressListener == null)
        // ASSIGN DEFAULT PROGRESS LISTENER
        progressListener = new OIndexRebuildOutputListener(index);

      final Set<String> clustersToIndex = findClustersByIds(clusterIdsToIndex, database);
      Object ignoreNullValues = metadata == null ? null : metadata.field("ignoreNullValues");
      if (Boolean.TRUE.equals(ignoreNullValues)) {
        indexDefinition.setNullValuesIgnored(true);
      } else if (Boolean.FALSE.equals(ignoreNullValues)) {
        indexDefinition.setNullValuesIgnored(false);
      } else {
        indexDefinition.setNullValuesIgnored(
            database.getConfiguration().getValueAsBoolean(OGlobalConfiguration.INDEX_IGNORE_NULL_VALUES_DEFAULT));
      }

      // decide which cluster to use ("index" - for automatic and "manindex" for manual)
      final String clusterName = indexDefinition.getClassName() != null ? defaultClusterName : manualClusterName;

      index.create(iName, indexDefinition, clusterName, clustersToIndex, true, progressListener);

      addIndexInternal(index);

      if (metadata != null) {
        final ODocument config = index.getConfiguration();
        config.field("metadata", metadata, OType.EMBEDDED);
      }

      setDirty();
      save();
    } finally {
      releaseExclusiveLock();
    }

    notifyInvolvedClasses(database, clusterIdsToIndex);

    return preProcessBeforeReturn(database, index);
  }
public OIndex<?> createIndex(ODatabaseDocumentInternal database, final String iName, String type,
      final OIndexDefinition indexDefinition, final int[] clusterIdsToIndex, OProgressListener progressListener, ODocument metadata,
      String algorithm) {
    if (database.getTransaction().isActive())
      throw new IllegalStateException("Cannot create a new index inside a transaction");

    final Character c = OSchemaShared.checkFieldNameIfValid(iName);
    if (c != null)
      throw new IllegalArgumentException("Invalid index name '" + iName + "'. Character '" + c + "' is invalid");

    if (indexDefinition == null) {
      throw new IllegalArgumentException("Index definition cannot be null");
    }

    final Locale locale = getServerLocale();
    type = type.toUpperCase(locale);
    if (algorithm == null) {
      algorithm = OIndexes.chooseDefaultIndexAlgorithm(type);
    }

    final String valueContainerAlgorithm = chooseContainerAlgorithm(type);

    final OIndexInternal<?> index;
    acquireExclusiveLock();
    try {

      if (indexes.containsKey(iName))
        throw new OIndexException("Index with name " + iName + " already exists.");

      // manual indexes are always durable
      if (clusterIdsToIndex == null || clusterIdsToIndex.length == 0) {
        if (metadata == null)
          metadata = new ODocument().setTrackingChanges(false);

        final Object durable = metadata.field("durableInNonTxMode");
        if (!(durable instanceof Boolean))
          metadata.field("durableInNonTxMode", true);
        if (metadata.field("trackMode") == null)
          metadata.field("trackMode", "FULL");
      }

      index = OIndexes.createIndex(getStorage(), iName, type, algorithm, valueContainerAlgorithm, metadata, -1);
      if (progressListener == null)
        // ASSIGN DEFAULT PROGRESS LISTENER
        progressListener = new OIndexRebuildOutputListener(index);

      final Set<String> clustersToIndex = findClustersByIds(clusterIdsToIndex, database);
      Object ignoreNullValues = metadata == null ? null : metadata.field("ignoreNullValues");
      if (Boolean.TRUE.equals(ignoreNullValues)) {
        indexDefinition.setNullValuesIgnored(true);
      } else if (Boolean.FALSE.equals(ignoreNullValues)) {
        indexDefinition.setNullValuesIgnored(false);
      } else {
        indexDefinition.setNullValuesIgnored(
            database.getConfiguration().getValueAsBoolean(OGlobalConfiguration.INDEX_IGNORE_NULL_VALUES_DEFAULT));
      }

      // decide which cluster to use ("index" - for automatic and "manindex" for manual)
      final String clusterName = indexDefinition.getClassName() != null ? defaultClusterName : manualClusterName;

      index.create(iName, indexDefinition, clusterName, clustersToIndex, true, progressListener);

      addIndexInternal(index);

      if (metadata != null) {
        final ODocument config = index.getConfiguration();
        config.field("metadata", metadata, OType.EMBEDDED);
      }

      setDirty();
      save();
    } finally {
      releaseExclusiveLock();
    }

    notifyInvolvedClasses(database, clusterIdsToIndex);

    return preProcessBeforeReturn(database, index);
  }
public OIndex < ? > createIndex ( ODatabaseDocumentInternal database , final String iName , String type , final OIndexDefinition indexDefinition , final int [ ] clusterIdsToIndex , OProgressListener progressListener , ODocument metadata , String algorithm ) { if ( database . getTransaction ( ) . isActive ( ) ) throw new IllegalStateException ( "Cannot create a new index inside a transaction" ) ; final Character c = OSchemaShared . checkFieldNameIfValid ( iName ) ; if ( c != null ) throw new IllegalArgumentException ( "Invalid index name '" + iName + "'. Character '" + c + "' is invalid" ) ; if ( indexDefinition == null ) { throw new IllegalArgumentException ( "Index definition cannot be null" ) ; } final Locale locale = getServerLocale ( ) ; type = type . toUpperCase ( locale ) ; if ( algorithm == null ) { algorithm = OIndexes . chooseDefaultIndexAlgorithm ( type ) ; } final String valueContainerAlgorithm = chooseContainerAlgorithm ( type ) ; final OIndexInternal < ? > index ; acquireExclusiveLock ( ) ; try { if ( indexes . containsKey ( iName ) ) throw new OIndexException ( "Index with name " + iName + " already exists." ) ; // manual indexes are always durable if ( clusterIdsToIndex == null || clusterIdsToIndex . length == 0 ) { if ( metadata == null ) metadata = new ODocument ( ) . setTrackingChanges ( false ) ; final Object durable = metadata . field ( "durableInNonTxMode" ) ; if ( ! ( durable instanceof Boolean ) ) metadata . field ( "durableInNonTxMode" , true ) ; if ( metadata . field ( "trackMode" ) == null ) metadata . field ( "trackMode" , "FULL" ) ; } index = OIndexes . createIndex ( getStorage ( ) , iName , type , algorithm , valueContainerAlgorithm , metadata , - 1 ) ; if ( progressListener == null ) // ASSIGN DEFAULT PROGRESS LISTENER progressListener = new OIndexRebuildOutputListener ( index ) ; final Set < String > clustersToIndex = findClustersByIds ( clusterIdsToIndex , database ) ; Object ignoreNullValues = metadata == null ? null : metadata . field ( "ignoreNullValues" ) ; if ( Boolean . TRUE . equals ( ignoreNullValues ) ) { indexDefinition . setNullValuesIgnored ( true ) ; } else if ( Boolean . FALSE . equals ( ignoreNullValues ) ) { indexDefinition . setNullValuesIgnored ( false ) ; } else { indexDefinition . setNullValuesIgnored ( database . getConfiguration ( ) . getValueAsBoolean ( OGlobalConfiguration . INDEX_IGNORE_NULL_VALUES_DEFAULT ) ) ; } // decide which cluster to use ("index" - for automatic and "manindex" for manual) final String clusterName = indexDefinition . getClassName ( ) != null ? defaultClusterName : manualClusterName ; index . create ( iName , indexDefinition , clusterName , clustersToIndex , true , progressListener ) ; addIndexInternal ( index ) ; if ( metadata != null ) { final ODocument config = index . getConfiguration ( ) ; config . field ( "metadata" , metadata , OType . EMBEDDED ) ; } setDirty ( ) ; save ( ) ; } finally { releaseExclusiveLock ( ) ; } notifyInvolvedClasses ( database , clusterIdsToIndex ) ; return preProcessBeforeReturn ( database , index ) ; }
Create a new index.
<p>
May require quite a long time if big amount of data should be indexed.

@param iName             name of index
@param type              index type. Specified by plugged index factories.
@param indexDefinition   metadata that describes index structure
@param clusterIdsToIndex ids of clusters that index should track for changes.
@param progressListener  listener to track task progress.
@param metadata          document with additional properties that can be used by index engine.
@param algorithm         tip to an index factory what algorithm to use

@return a newly created index instance
Create a new index . <p > May require quite a long time if big amount of data should be indexed .