Search in sources :

Example 1 with ODatabaseInternal

use of com.orientechnologies.orient.core.db.ODatabaseInternal in project wicket-orientdb by OrienteerBAP.

the class OrientDbWebApplication method init.

@Override
protected void init() {
    super.init();
    Orient.instance().registerThreadDatabaseFactory(new DefaultODatabaseThreadLocalFactory(this));
    Orient.instance().addDbLifecycleListener(new ODatabaseLifecycleListener() {

        private ORecordHook createHook(Class<? extends ORecordHook> clazz, ODatabaseInternal iDatabase) {
            if (!(iDatabase instanceof ODatabaseDocument))
                return null;
            try {
                return (ORecordHook) clazz.getConstructor(ODatabaseDocument.class).newInstance(iDatabase);
            } catch (Exception e) {
                try {
                    return (ORecordHook) clazz.newInstance();
                } catch (Exception e1) {
                    throw new IllegalStateException("Can't initialize hook " + clazz.getName(), e);
                }
            }
        }

        @Override
        public void onOpen(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
        }

        @Override
        public void onCreate(ODatabaseInternal iDatabase) {
            registerHooks(iDatabase);
            // Fix for "feature" appeared in OrientDB 2.1.1
            // Issue: https://github.com/orientechnologies/orientdb/issues/4906
            fixOrientDBRights(iDatabase);
        }

        public void registerHooks(ODatabaseInternal iDatabase) {
            Set<ORecordHook> hooks = iDatabase.getHooks().keySet();
            List<Class<? extends ORecordHook>> hooksToRegister = new ArrayList<Class<? extends ORecordHook>>(getOrientDbSettings().getORecordHooks());
            for (ORecordHook hook : hooks) {
                if (hooksToRegister.contains(hook.getClass()))
                    hooksToRegister.remove(hook.getClass());
            }
            for (Class<? extends ORecordHook> oRecordHookClass : hooksToRegister) {
                ORecordHook hook = createHook(oRecordHookClass, iDatabase);
                if (hook != null) {
                    if (hook instanceof IHookPosition) {
                        iDatabase.registerHook(hook, ((IHookPosition) hook).getPosition());
                    } else {
                        iDatabase.registerHook(hook);
                    }
                }
            }
        }

        @Override
        public void onClose(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        @Override
        public void onDrop(ODatabaseInternal iDatabase) {
        /*NOP*/
        }

        public PRIORITY getPriority() {
            return PRIORITY.REGULAR;
        }

        @Override
        public void onCreateClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onDropClass(ODatabaseInternal iDatabase, OClass iClass) {
        /*NOP*/
        }

        @Override
        public void onLocalNodeConfigurationRequest(ODocument arg0) {
        /*NOP*/
        }
    });
    getRequestCycleListeners().add(newTransactionRequestCycleListener());
    getRequestCycleListeners().add(new OrientDefaultExceptionsHandlingListener());
    getSecuritySettings().setAuthorizationStrategy(new WicketOrientDbAuthorizationStrategy(this));
    getApplicationListeners().add(new IApplicationListener() {

        @Override
        public void onAfterInitialized(Application application) {
            Orient.instance().startup();
            Orient.instance().removeShutdownHook();
        }

        @Override
        public void onBeforeDestroyed(Application application) {
            Orient.instance().shutdown();
        }
    });
    getAjaxRequestTargetListeners().add(new FixFormEncTypeListener());
    // workaround to support changing system users passwords in web interface
    getOrientDbSettings().getORecordHooks().add(OUserCatchPasswordHook.class);
    PropertyResolver.setLocator(this, new ODocumentPropertyLocator(new PropertyResolver.CachingPropertyLocator(new PropertyResolver.DefaultPropertyLocator())));
}
Also used : Set(java.util.Set) ODocumentPropertyLocator(ru.ydn.wicket.wicketorientdb.utils.ODocumentPropertyLocator) IHookPosition(ru.ydn.wicket.wicketorientdb.components.IHookPosition) PropertyResolver(org.apache.wicket.core.util.lang.PropertyResolver) FixFormEncTypeListener(ru.ydn.wicket.wicketorientdb.utils.FixFormEncTypeListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ArrayList(java.util.ArrayList) List(java.util.List) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordHook(com.orientechnologies.orient.core.hook.ORecordHook) InvocationTargetException(java.lang.reflect.InvocationTargetException) ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) WicketOrientDbAuthorizationStrategy(ru.ydn.wicket.wicketorientdb.security.WicketOrientDbAuthorizationStrategy) IApplicationListener(org.apache.wicket.IApplicationListener) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) AuthenticatedWebApplication(org.apache.wicket.authroles.authentication.AuthenticatedWebApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 2 with ODatabaseInternal

use of com.orientechnologies.orient.core.db.ODatabaseInternal in project orientdb by orientechnologies.

the class OClassImpl method firePropertyNameMigration.

public void firePropertyNameMigration(final ODatabaseDocument database, final String propertyName, final String newPropertyName, final OType type) {
    final boolean strictSQL = ((ODatabaseInternal) database).getStorage().getConfiguration().isStrictSql();
    database.query(new OSQLAsynchQuery<Object>("select from " + getEscapedName(name, strictSQL) + " where " + getEscapedName(propertyName, strictSQL) + " is not null ", new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            final ODocument record = ((OIdentifiable) iRecord).getRecord();
            record.setFieldType(propertyName, type);
            record.field(newPropertyName, record.field(propertyName), type);
            database.save(record);
            return true;
        }

        @Override
        public void end() {
        }

        @Override
        public Object getResult() {
            return null;
        }
    }));
}
Also used : ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with ODatabaseInternal

use of com.orientechnologies.orient.core.db.ODatabaseInternal in project orientdb by orientechnologies.

the class OIndexManagerShared method createIndex.

/**
 * 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
 */
public OIndex<?> createIndex(final String iName, String type, final OIndexDefinition indexDefinition, final int[] clusterIdsToIndex, OProgressListener progressListener, ODocument metadata, String algorithm) {
    if (getDatabase().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");
    ODatabaseInternal database = getDatabase();
    OStorage storage = database.getStorage();
    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.toLowerCase(locale)))
            throw new OIndexException("Index with name " + iName.toLowerCase(locale) + " 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(getDatabase(), iName, type, algorithm, valueContainerAlgorithm, metadata, -1);
        if (progressListener == null)
            // ASSIGN DEFAULT PROGRESS LISTENER
            progressListener = new OIndexRebuildOutputListener(index);
        final Set<String> clustersToIndex = findClustersByIds(clusterIdsToIndex, database);
        if (indexDefinition != null) {
            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(OGlobalConfiguration.INDEX_IGNORE_NULL_VALUES_DEFAULT.getValueAsBoolean());
            }
        }
        // decide which cluster to use ("index" - for automatic and "manindex" for manual)
        final String clusterName = indexDefinition != null && 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(clusterIdsToIndex);
    if (OGlobalConfiguration.INDEX_FLUSH_AFTER_CREATE.getValueAsBoolean())
        storage.synch();
    return preProcessBeforeReturn(index);
}
Also used : OStorage(com.orientechnologies.orient.core.storage.OStorage) ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ODatabaseInternal

use of com.orientechnologies.orient.core.db.ODatabaseInternal in project orientdb by orientechnologies.

the class OClassImpl method fireDatabaseMigration.

public void fireDatabaseMigration(final ODatabaseDocument database, final String propertyName, final OType type) {
    final boolean strictSQL = ((ODatabaseInternal) database).getStorage().getConfiguration().isStrictSql();
    database.query(new OSQLAsynchQuery<Object>("select from " + getEscapedName(name, strictSQL) + " where " + getEscapedName(propertyName, strictSQL) + ".type() <> \"" + type.name() + "\"", new OCommandResultListener() {

        @Override
        public boolean result(Object iRecord) {
            final ODocument record = ((OIdentifiable) iRecord).getRecord();
            record.field(propertyName, record.field(propertyName), type);
            database.save(record);
            return true;
        }

        @Override
        public void end() {
        }

        @Override
        public Object getResult() {
            return null;
        }
    }));
}
Also used : ODatabaseInternal(com.orientechnologies.orient.core.db.ODatabaseInternal) OCommandResultListener(com.orientechnologies.orient.core.command.OCommandResultListener) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseInternal (com.orientechnologies.orient.core.db.ODatabaseInternal)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OCommandResultListener (com.orientechnologies.orient.core.command.OCommandResultListener)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OStorage (com.orientechnologies.orient.core.storage.OStorage)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Application (org.apache.wicket.Application)1 IApplicationListener (org.apache.wicket.IApplicationListener)1 AuthenticatedWebApplication (org.apache.wicket.authroles.authentication.AuthenticatedWebApplication)1 PropertyResolver (org.apache.wicket.core.util.lang.PropertyResolver)1 WebApplication (org.apache.wicket.protocol.http.WebApplication)1 IHookPosition (ru.ydn.wicket.wicketorientdb.components.IHookPosition)1 WicketOrientDbAuthorizationStrategy (ru.ydn.wicket.wicketorientdb.security.WicketOrientDbAuthorizationStrategy)1