Search in sources :

Example 11 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OClassImpl method addCluster.

@Override
public OClass addCluster(final String clusterNameOrId) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    if (isAbstract()) {
        throw new OSchemaException("Impossible to associate a cluster to an abstract class class");
    }
    acquireSchemaWriteLock();
    try {
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            final String cmd = String.format("alter class `%s` addcluster `%s`", name, clusterNameOrId);
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final int clusterId = owner.createClusterIfNeeded(clusterNameOrId);
            addClusterIdInternal(clusterId);
            final String cmd = String.format("alter class `%s` addcluster `%s`", name, clusterNameOrId);
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
        } else {
            final int clusterId = owner.createClusterIfNeeded(clusterNameOrId);
            addClusterIdInternal(clusterId);
        }
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 12 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OClassImpl method addClusterId.

public OClass addClusterId(final int clusterId) {
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    if (isAbstract()) {
        throw new OSchemaException("Impossible to associate a cluster to an abstract class class");
    }
    acquireSchemaWriteLock();
    try {
        final ODatabaseDocumentInternal database = getDatabase();
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            final String cmd = String.format("alter class `%s` addcluster %d", name, clusterId);
            database.command(new OCommandSQL(cmd)).execute();
        } else if (isDistributedCommand()) {
            final String cmd = String.format("alter class `%s` addcluster %d", name, clusterId);
            final OCommandSQL commandSQL = new OCommandSQL(cmd);
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            addClusterIdInternal(clusterId);
        } else
            addClusterIdInternal(clusterId);
    } finally {
        releaseSchemaWriteLock();
    }
    return this;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 13 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OrientdbEdgeTest method testEdges.

@Test
public void testEdges() throws Exception {
    OrientGraphFactory factory = getGraphFactory();
    OrientBaseGraph g = factory.getNoTx();
    try {
        try {
            g.createEdgeType("some-label");
        } catch (OSchemaException ex) {
            if (!ex.getMessage().contains("exists"))
                throw (ex);
            g.command(new OCommandSQL("delete edge some-label")).execute();
        }
        try {
            g.createVertexType("some-v-label");
        } catch (OSchemaException ex) {
            if (!ex.getMessage().contains("exists"))
                throw (ex);
            g.command(new OCommandSQL("delete vertex some-v-label")).execute();
        }
    } finally {
        g.shutdown();
    }
    OrientGraph t = factory.getTx();
    try {
        Vertex v1 = t.addVertex("class:some-v-label");
        Vertex v2 = t.addVertex("class:some-v-label");
        v1.setProperty("_id", "v1");
        v2.setProperty("_id", "v2");
        OrientEdge edge = t.addEdge(null, v1, v2, "some-label");
        edge.setProperty("some", "thing");
        t.commit();
        t.shutdown();
        t = factory.getTx();
        assertEquals(2, t.countVertices("some-v-label"));
        assertEquals(1, t.countEdges());
        assertNotNull(t.getVertices("_id", "v1").iterator().next());
        assertNotNull(t.getVertices("_id", "v2").iterator().next());
        t.commit();
        t.shutdown();
        t = factory.getTx();
        // works
        assertEquals(1, t.getVertices("_id", "v1").iterator().next().query().labels("some-label").count());
        // NoSuchElementException
        assertNotNull(t.getVertices("_id", "v1").iterator().next().query().labels("some-label").edges().iterator().next());
        t.commit();
    } finally {
        t.shutdown();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Vertex(com.tinkerpop.blueprints.Vertex) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) Test(org.junit.Test)

Example 14 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OrientElement method checkForClassInSchema.

/**
   * Check if a class already exists, otherwise create it at the fly. If a transaction is running commit changes, create the class
   * and begin a new transaction.
   * 
   * @param className
   *          Class's name
   */
protected String checkForClassInSchema(final String className) {
    if (className == null)
        return null;
    OrientBaseGraph graph = getGraph();
    if (graph == null)
        return className;
    final OSchema schema = graph.getRawGraph().getMetadata().getSchema();
    if (!schema.existsClass(className)) {
        // CREATE A NEW CLASS AT THE FLY
        try {
            graph.executeOutsideTx(new OCallable<OClass, OrientBaseGraph>() {

                @Override
                public OClass call(final OrientBaseGraph g) {
                    return schema.createClass(className, schema.getClass(getBaseClassName()));
                }
            }, "Committing the active transaction to create the new type '", className, "' as subclass of '", getBaseClassName(), "'. The transaction will be reopen right after that. To avoid this behavior create the classes outside the transaction");
        } catch (OSchemaException e) {
            if (!schema.existsClass(className))
                throw e;
        }
    } else {
        // CHECK THE CLASS INHERITANCE
        final OClass cls = schema.getClass(className);
        if (!cls.isSubClassOf(getBaseClassName()))
            throw new IllegalArgumentException("Class '" + className + "' is not an instance of " + getBaseClassName());
    }
    return className;
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 15 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class ODatabaseImport method importRecord.

private ORID importRecord() throws Exception {
    String value = jsonReader.readString(OJSONReader.END_OBJECT, true);
    // JUMP EMPTY RECORDS
    while (!value.isEmpty() && value.charAt(0) != '{') {
        value = value.substring(1);
    }
    record = null;
    try {
        try {
            record = ORecordSerializerJSON.INSTANCE.fromString(value, record, null);
        } catch (OSerializationException e) {
            if (e.getCause() instanceof OSchemaException) {
                // EXTRACT CLASS NAME If ANY
                final int pos = value.indexOf("\"@class\":\"");
                if (pos > -1) {
                    final int end = value.indexOf("\"", pos + "\"@class\":\"".length() + 1);
                    final String value1 = value.substring(0, pos + "\"@class\":\"".length());
                    final String clsName = value.substring(pos + "\"@class\":\"".length(), end);
                    final String value2 = value.substring(end);
                    final String newClassName = convertedClassNames.get(clsName);
                    value = value1 + newClassName + value2;
                    // OVERWRITE CLASS NAME WITH NEW NAME
                    record = ORecordSerializerJSON.INSTANCE.fromString(value, record, null);
                }
            } else
                throw OException.wrapException(new ODatabaseImportException("Error on importing record"), e);
        }
        // Incorrect record format , skip this record
        if (record == null || record.getIdentity() == null) {
            OLogManager.instance().warn(this, "Broken record was detected and will be skipped");
            return null;
        }
        if (schemaImported && record.getIdentity().equals(schemaRecordId)) {
            // JUMP THE SCHEMA
            return null;
        }
        // CHECK IF THE CLUSTER IS INCLUDED
        if (includeClusters != null) {
            if (!includeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId()))) {
                jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
                return null;
            }
        } else if (excludeClusters != null) {
            if (excludeClusters.contains(database.getClusterNameById(record.getIdentity().getClusterId())))
                return null;
        }
        if (record.getIdentity().getClusterId() == 0 && record.getIdentity().getClusterPosition() == 1)
            // JUMP INTERNAL RECORDS
            return null;
        if (exporterVersion >= 3) {
            int oridsId = database.getClusterIdByName("ORIDs");
            int indexId = database.getClusterIdByName(OMetadataDefault.CLUSTER_INDEX_NAME);
            if (record.getIdentity().getClusterId() == indexId || record.getIdentity().getClusterId() == oridsId)
                // JUMP INDEX RECORDS
                return null;
        }
        final int manualIndexCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_MANUAL_INDEX_NAME);
        final int internalCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_INTERNAL_NAME);
        final int indexCluster = database.getClusterIdByName(OMetadataDefault.CLUSTER_INDEX_NAME);
        if (exporterVersion >= 4) {
            if (record.getIdentity().getClusterId() == manualIndexCluster)
                // JUMP INDEX RECORDS
                return null;
        }
        if (record.getIdentity().equals(indexMgrRecordId))
            return null;
        final ORID rid = record.getIdentity();
        final int clusterId = rid.getClusterId();
        if ((clusterId != manualIndexCluster && clusterId != internalCluster && clusterId != indexCluster)) {
            ORecordInternal.setVersion(record, 0);
            record.setDirty();
            ORecordInternal.setIdentity(record, new ORecordId());
            if (!preserveRids && record instanceof ODocument && ODocumentInternal.getImmutableSchemaClass(((ODocument) record)) != null)
                record.save();
            else
                record.save(database.getClusterNameById(clusterId));
            if (!rid.equals(record.getIdentity()))
                // SAVE IT ONLY IF DIFFERENT
                exportImportHashTable.put(rid, record.getIdentity());
        }
    } catch (Exception t) {
        if (record != null)
            OLogManager.instance().error(this, "Error importing record " + record.getIdentity() + ". Source line " + jsonReader.getLineNumber() + ", column " + jsonReader.getColumnNumber());
        else
            OLogManager.instance().error(this, "Error importing record. Source line " + jsonReader.getLineNumber() + ", column " + jsonReader.getColumnNumber());
        throw t;
    } finally {
        jsonReader.readNext(OJSONReader.NEXT_IN_ARRAY);
    }
    return record.getIdentity();
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORID(com.orientechnologies.orient.core.id.ORID) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ParseException(java.text.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)26 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)10 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)10 OException (com.orientechnologies.common.exception.OException)7 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)5 OStorage (com.orientechnologies.orient.core.storage.OStorage)5 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)4 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)3 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)3 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)3 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)3 OObjectNotDetachedException (com.orientechnologies.orient.object.db.OObjectNotDetachedException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)2 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)2