Search in sources :

Example 16 with OException

use of com.orientechnologies.common.exception.OException in project orientdb by orientechnologies.

the class OrientJdbcStatement method execute.

@Override
public boolean execute(final String sqlCommand) throws SQLException {
    //    System.out.println("sqlCommand = " + sqlCommand);
    if ("".equals(sqlCommand))
        return false;
    sql = mayCleanForSpark(sqlCommand);
    if (sql.equalsIgnoreCase("select 1")) {
        documents = new ArrayList<ODocument>(1);
        documents.add(new ODocument().field("1", 1));
    } else {
        query = new OCommandSQL(sql);
        try {
            rawResult = executeCommand(query);
            if (rawResult instanceof List<?>) {
                documents = (List<ODocument>) rawResult;
            } else if (rawResult instanceof OIdentifiable) {
                documents = new ArrayList<ODocument>(1);
                documents.add((ODocument) ((OIdentifiable) rawResult).getRecord());
            } else {
                return false;
            }
        } catch (OQueryParsingException e) {
            throw new SQLSyntaxErrorException("Error while parsing query", e);
        } catch (OException e) {
            throw new SQLException("Error while executing query", e);
        }
    }
    resultSet = new OrientJdbcResultSet(this, documents, resultSetType, resultSetConcurrency, resultSetHoldability);
    return true;
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) ArrayList(java.util.ArrayList) List(java.util.List) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) OException(com.orientechnologies.common.exception.OException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with OException

use of com.orientechnologies.common.exception.OException in project orientdb by orientechnologies.

the class OTransactionOptimisticProxy method begin.

@Override
public void begin() {
    super.begin();
    // Needed for keep the exception and insure that all data is read from the socket.
    OException toThrow = null;
    try {
        setUsingLog(channel.readByte() == 1);
        Map<ORecord, byte[]> lazyDeserialize = new IdentityHashMap<ORecord, byte[]>();
        byte lastTxStatus;
        for (lastTxStatus = channel.readByte(); lastTxStatus == 1; lastTxStatus = channel.readByte()) {
            final byte recordStatus = channel.readByte();
            final ORecordId rid = channel.readRID();
            final byte recordType = channel.readByte();
            final ORecordOperation entry = new OTransactionEntryProxy(recordType);
            entry.type = recordStatus;
            switch(recordStatus) {
                case ORecordOperation.CREATED:
                    byte[] content = channel.readBytes();
                    ORecordInternal.fill(entry.getRecord(), rid, 0, null, true);
                    // oNetworkProtocolBinary.fillRecord(rid, content, 0, entry.getRecord(), database);
                    lazyDeserialize.put(entry.getRecord(), content);
                    // SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW RID TO SEND BACK TO THE REQUESTER
                    createdRecords.put(rid.copy(), entry.getRecord());
                    break;
                case ORecordOperation.UPDATED:
                    int version = channel.readVersion();
                    byte[] bytes = channel.readBytes();
                    ORecordInternal.fill(entry.getRecord(), rid, version, null, true);
                    // oNetworkProtocolBinary.fillRecord(rid, bytes, version, entry.getRecord(), database);
                    lazyDeserialize.put(entry.getRecord(), bytes);
                    if (protocolVersion >= 23)
                        ORecordInternal.setContentChanged(entry.getRecord(), channel.readBoolean());
                    break;
                case ORecordOperation.DELETED:
                    // LOAD RECORD TO BE SURE IT HASN'T BEEN DELETED BEFORE + PROVIDE CONTENT FOR ANY HOOK
                    final ORecord rec = rid.getRecord();
                    int deleteVersion = channel.readVersion();
                    if (rec == null)
                        toThrow = new ORecordNotFoundException(rid);
                    else {
                        ORecordInternal.setVersion(rec, deleteVersion);
                        entry.setRecord(rec);
                    }
                    break;
                default:
                    throw new OTransactionException("Unrecognized tx command: " + recordStatus);
            }
            // PUT IN TEMPORARY LIST TO GET FETCHED AFTER ALL FOR CACHE
            tempEntries.put(entry.getRecord().getIdentity(), entry);
        }
        String dbSerializerName = "";
        if (database != null)
            dbSerializerName = database.getSerializer().toString();
        String name = oNetworkProtocolBinary.getRecordSerializerName(connection);
        for (Map.Entry<ORecord, byte[]> entry : lazyDeserialize.entrySet()) {
            ORecord record = entry.getKey();
            final boolean contentChanged = ORecordInternal.isContentChanged(record);
            if (ORecordInternal.getRecordType(record) == ODocument.RECORD_TYPE && !dbSerializerName.equals(name)) {
                ORecordSerializer ser = ORecordSerializerFactory.instance().getFormat(name);
                ser.fromStream(entry.getValue(), record, null);
                record.setDirty();
                ORecordInternal.setContentChanged(record, contentChanged);
            } else {
                record.fromStream(entry.getValue());
                record.setDirty();
                ORecordInternal.setContentChanged(record, contentChanged);
            }
        }
        if (toThrow != null)
            throw toThrow;
        if (lastTxStatus == -1)
            // ABORT TX
            throw new OTransactionAbortedException("Transaction aborted by the client");
        final ODocument remoteIndexEntries = new ODocument(channel.readBytes());
        fillIndexOperations(remoteIndexEntries);
        // FIRE THE TRIGGERS ONLY AFTER HAVING PARSED THE REQUEST
        for (Entry<ORID, ORecordOperation> entry : tempEntries.entrySet()) {
            if (entry.getValue().type == ORecordOperation.UPDATED) {
                // SPECIAL CASE FOR UPDATE: WE NEED TO LOAD THE RECORD AND APPLY CHANGES TO GET WORKING HOOKS (LIKE INDEXES)
                final ORecord record = entry.getValue().record.getRecord();
                final boolean contentChanged = ORecordInternal.isContentChanged(record);
                final ORecord loadedRecord = record.getIdentity().copy().getRecord();
                if (loadedRecord == null)
                    throw new ORecordNotFoundException(record.getIdentity());
                if (ORecordInternal.getRecordType(loadedRecord) == ODocument.RECORD_TYPE && ORecordInternal.getRecordType(loadedRecord) == ORecordInternal.getRecordType(record)) {
                    ((ODocument) loadedRecord).merge((ODocument) record, false, false);
                    loadedRecord.setDirty();
                    ORecordInternal.setContentChanged(loadedRecord, contentChanged);
                    ORecordInternal.setVersion(loadedRecord, record.getVersion());
                    entry.getValue().record = loadedRecord;
                    // SAVE THE RECORD TO RETRIEVE THEM FOR THE NEW VERSIONS TO SEND BACK TO THE REQUESTER
                    updatedRecords.put((ORecordId) entry.getKey(), entry.getValue().getRecord());
                }
            }
            addRecord(entry.getValue().getRecord(), entry.getValue().type, null);
        }
        tempEntries.clear();
        // UNMARSHALL ALL THE RECORD AT THE END TO BE SURE ALL THE RECORD ARE LOADED IN LOCAL TX
        for (ORecord record : createdRecords.values()) {
            unmarshallRecord(record);
            if (record instanceof ODocument) {
                // Force conversion of value to class for trigger default values.
                ODocumentInternal.autoConvertValueToClass(connection.getDatabase(), (ODocument) record);
            }
        }
        for (ORecord record : updatedRecords.values()) unmarshallRecord(record);
    } catch (IOException e) {
        rollback();
        throw OException.wrapException(new OSerializationException("Cannot read transaction record from the network. Transaction aborted"), e);
    }
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) IOException(java.io.IOException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OTransactionAbortedException(com.orientechnologies.orient.core.exception.OTransactionAbortedException) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ORecordSerializer(com.orientechnologies.orient.core.serialization.serializer.record.ORecordSerializer) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 18 with OException

use of com.orientechnologies.common.exception.OException in project orientdb by orientechnologies.

the class ClusterMetadataTest method testMetadataStore.

public void testMetadataStore() throws Exception {
    final int clusterId = database.addCluster("clusterTest");
    OCluster cluster = database.getStorage().getClusterById(clusterId);
    Assert.assertEquals(cluster.recordGrowFactor(), 1.2f);
    Assert.assertEquals(cluster.recordOverflowGrowFactor(), 1.2f);
    Assert.assertEquals(cluster.compression(), OGlobalConfiguration.STORAGE_COMPRESSION_METHOD.getValueAsString());
    database.command(new OCommandSQL("alter cluster clusterTest record_grow_factor 2")).execute();
    database.command(new OCommandSQL("alter cluster clusterTest record_overflow_grow_factor 2")).execute();
    database.command(new OCommandSQL("alter cluster clusterTest compression nothing")).execute();
    Assert.assertEquals(cluster.recordGrowFactor(), 2f);
    Assert.assertEquals(cluster.recordOverflowGrowFactor(), 2f);
    Assert.assertEquals(cluster.compression(), "nothing");
    OStorage storage = database.getStorage();
    database.close();
    storage.close(true, false);
    database.activateOnCurrentThread();
    database.resetInitialization();
    database.open("admin", "admin");
    cluster = database.getStorage().getClusterById(clusterId);
    Assert.assertEquals(cluster.recordGrowFactor(), 2f);
    Assert.assertEquals(cluster.recordOverflowGrowFactor(), 2f);
    Assert.assertEquals(cluster.compression(), "nothing");
    try {
        database.command(new OCommandSQL("alter cluster clusterTest record_grow_factor 0.5")).execute();
        Assert.fail();
    } catch (OException e) {
    }
    try {
        database.command(new OCommandSQL("alter cluster clusterTest record_grow_factor fff")).execute();
        Assert.fail();
    } catch (OException e) {
    }
    try {
        database.command(new OCommandSQL("alter cluster clusterTest record_overflow_grow_factor 0.5")).execute();
        Assert.fail();
    } catch (OException e) {
    }
    try {
        database.command(new OCommandSQL("alter cluster clusterTest record_overflow_grow_factor fff")).execute();
        Assert.fail();
    } catch (OException e) {
    }
    try {
        database.command(new OCommandSQL("alter cluster clusterTest compression dsgfgd")).execute();
        Assert.fail();
    } catch (OException e) {
    }
    Assert.assertEquals(cluster.recordGrowFactor(), 2f);
    Assert.assertEquals(cluster.recordOverflowGrowFactor(), 2f);
    Assert.assertEquals(cluster.compression(), "nothing");
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OStorage(com.orientechnologies.orient.core.storage.OStorage) OCluster(com.orientechnologies.orient.core.storage.OCluster) OException(com.orientechnologies.common.exception.OException)

Example 19 with OException

use of com.orientechnologies.common.exception.OException in project orientdb by orientechnologies.

the class SchemaTest method testOfflineCluster.

public void testOfflineCluster() {
    database.command(new OCommandSQL("create class TestOffline")).execute();
    database.command(new OCommandSQL("insert into TestOffline set status = 'offline'")).execute();
    List<OIdentifiable> result = database.command(new OCommandSQL("select from TestOffline")).execute();
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isEmpty());
    ODocument record = result.get(0).getRecord();
    // TEST NO EFFECTS
    Boolean changed = database.command(new OCommandSQL("alter cluster TestOffline status online")).execute();
    Assert.assertFalse(changed);
    // PUT IT OFFLINE
    changed = database.command(new OCommandSQL("alter cluster TestOffline* status offline")).execute();
    Assert.assertTrue(changed);
    // NO DATA?
    result = database.command(new OCommandSQL("select from TestOffline")).execute();
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isEmpty());
    // TEST NO EFFECTS
    changed = database.command(new OCommandSQL("alter cluster TestOffline* status offline")).execute();
    Assert.assertFalse(changed);
    // TEST SAVING OF OFFLINE STATUS
    // TEST UPDATE - NO EFFECT
    Assert.assertEquals(database.command(new OCommandSQL("update TestOffline set name = 'yeah'")).execute(), 0);
    // TEST DELETE - NO EFFECT
    Assert.assertEquals(database.command(new OCommandSQL("delete from TestOffline")).execute(), 0);
    // TEST CREATE -> EXCEPTION
    try {
        Object res = database.command(new OCommandSQL("insert into TestOffline set name = 'offline', password = 'offline', status = 'ACTIVE'")).execute();
        Assert.assertTrue(false);
    } catch (OException e) {
        Throwable cause = e;
        while (cause.getCause() != null) cause = cause.getCause();
        Assert.assertTrue(cause instanceof OOfflineClusterException);
    }
    // TEST UPDATE RECORD -> EXCEPTION
    try {
        record.field("status", "offline").save();
        Assert.assertTrue(false);
    } catch (OException e) {
        Throwable cause = e;
        while (cause.getCause() != null) cause = cause.getCause();
        Assert.assertTrue(cause instanceof OOfflineClusterException);
    }
    // TEST DELETE RECORD -> EXCEPTION
    try {
        record.delete();
        Assert.assertTrue(false);
    } catch (OOfflineClusterException e) {
        Assert.assertTrue(true);
    }
    // TEST DELETE RECORD -> EXCEPTION
    try {
        record.reload(null, true);
        Assert.assertTrue(false);
    } catch (ORecordNotFoundException e) {
        Assert.assertTrue(e.getCause() instanceof OOfflineClusterException);
    }
    // RESTORE IT ONLINE
    changed = database.command(new OCommandSQL("alter cluster TestOffline status online")).execute();
    Assert.assertTrue(changed);
    result = database.command(new OCommandSQL("select from TestOffline")).execute();
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isEmpty());
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)

Example 20 with OException

use of com.orientechnologies.common.exception.OException in project orientdb by orientechnologies.

the class TruncateClusterTest method testSimpleClusterIsAbsent.

public void testSimpleClusterIsAbsent() {
    final String clusterName = "TruncateClusterIsAbsent";
    final int clusterId = database.addCluster(clusterName);
    final ODocument document = new ODocument();
    document.save(clusterName);
    Assert.assertEquals(database.countClusterElements(clusterId), 1);
    try {
        database.truncateCluster("Wrong" + clusterName);
        Assert.fail();
    } catch (OException e) {
        Assert.assertTrue(true);
    }
    Assert.assertEquals(database.countClusterElements(clusterId), 1);
    database.dropCluster(clusterId, false);
}
Also used : OException(com.orientechnologies.common.exception.OException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OException (com.orientechnologies.common.exception.OException)21 IOException (java.io.IOException)12 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)11 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)5 OIOException (com.orientechnologies.common.io.OIOException)5 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)4 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)3 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)3 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)3 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)3 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)3 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)3 NamingException (javax.naming.NamingException)3 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2