Search in sources :

Example 16 with ODatabase

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

the class TransactionAtomicTest method testTransactionPreListenerRollback.

@Test
public void testTransactionPreListenerRollback() throws IOException {
    ODocument record1 = new ODocument();
    record1.field("value", "This is the first version").save();
    final ODatabaseListener listener = new ODatabaseListener() {

        @Override
        public void onAfterTxCommit(ODatabase iDatabase) {
        }

        @Override
        public void onAfterTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxBegin(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeTxCommit(ODatabase iDatabase) {
            throw new RuntimeException("Rollback test");
        }

        @Override
        public void onBeforeTxRollback(ODatabase iDatabase) {
        }

        @Override
        public void onClose(ODatabase iDatabase) {
        }

        @Override
        public void onBeforeCommand(OCommandRequestText iCommand, OCommandExecutor executor) {
        }

        @Override
        public void onAfterCommand(OCommandRequestText iCommand, OCommandExecutor executor, Object result) {
        }

        @Override
        public void onCreate(ODatabase iDatabase) {
        }

        @Override
        public void onDelete(ODatabase iDatabase) {
        }

        @Override
        public void onOpen(ODatabase iDatabase) {
        }

        @Override
        public boolean onCorruptionRepairDatabase(ODatabase iDatabase, final String iReason, String iWhatWillbeFixed) {
            return true;
        }
    };
    database.registerListener(listener);
    database.begin();
    try {
        database.commit();
        Assert.assertTrue(false);
    } catch (OTransactionException e) {
        Assert.assertTrue(true);
    } finally {
        database.unregisterListener(listener);
    }
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutor(com.orientechnologies.orient.core.command.OCommandExecutor) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 17 with ODatabase

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

the class GiantFileTest method storeFileData.

private static void storeFileData(final ODocument fileDoc, final File file) throws Exception {
    // To avoid overwriting a stored file, DataChunks must be null.
    final List<ORID> existingChunks = fileDoc.field("DataChunks");
    if (existingChunks != null) {
        final String fileName = fileDoc.field("FileName");
        throw new RuntimeException("File record already has data; overwrite not allowed! fileName: " + fileName);
    }
    // TODO: is this assumption ok?
    // Get the currently open database for this thread and set intent.
    final ODatabase database = ODatabaseRecordThreadLocal.INSTANCE.get();
    database.declareIntent(new OIntentMassiveInsert());
    // Insert File data.
    final long fileSize = file.length();
    final FileInputStream in = new FileInputStream(file);
    try {
        final int CHUNK_SIZE = 81920;
        int bufferedBytes;
        final byte[] buffer = new byte[CHUNK_SIZE];
        byte currentPercent = 0;
        final int fullChunks = (int) (fileSize / CHUNK_SIZE);
        final long fullChunksSize = fullChunks * CHUNK_SIZE;
        final int totalChunks;
        if (fileSize > fullChunksSize) {
            totalChunks = fullChunks + 1;
        } else {
            totalChunks = fullChunks;
        }
        final List<ORID> chunkRids = new ArrayList<ORID>(totalChunks);
        // Make only one ORecordBytes instance and reuse it for every chunk,
        // to reduce heap garbage.
        final ORecordBytes chunk = new ORecordBytes();
        // Handle the full chunks.
        for (int page = 0; page < fullChunks; page++) {
            // Read a full chunk of data from the file into a buffer.
            bufferedBytes = 0;
            while (bufferedBytes < buffer.length) {
                final int bytesRead = in.read(buffer, bufferedBytes, buffer.length - bufferedBytes);
                if (bytesRead == -1) {
                    throw new Exception("Reached end of file prematurely. (File changed while reading?) fileName=" + file.getAbsolutePath());
                }
                bufferedBytes += bytesRead;
            }
            // Save the chunk to the database.
            final long saveStartTime = System.currentTimeMillis();
            chunk.reset(buffer);
            chunk.save();
            final long saveMs = System.currentTimeMillis() - saveStartTime;
            // Log the amount of time taken by the save.
            System.out.printf("Saved chunk %d in %d ms.\n", page, saveMs);
            // Save the chunk's record ID in the list.
            // Have to copy() the ORID or else every chunk in the list gets the same last ORID.
            // This is because we are using the chunk.reset(); approach to reduce garbage objects.
            chunkRids.add(chunk.getIdentity().copy());
            // Only report progress if it has changed.
            final byte percent = (byte) ((page + 1) * 100 / totalChunks);
            if (percent > currentPercent) {
                System.out.printf("Progress: %d%%\n", percent);
                currentPercent = percent;
            }
        }
        // Handle the final partial chunk (if any).
        if (fullChunks < totalChunks) {
            final int remainder = (int) (fileSize - fullChunksSize);
            // Read the remaining data from the file into a buffer.
            bufferedBytes = 0;
            while (bufferedBytes < remainder) {
                final int bytesRead = in.read(buffer, bufferedBytes, remainder - bufferedBytes);
                if (bytesRead == -1) {
                    throw new Exception("Reached end of file prematurely. (File changed while reading?) fileName=" + file.getAbsolutePath());
                }
                bufferedBytes += bytesRead;
            }
            // Save the chunk to the database.
            final long saveStartTime = System.currentTimeMillis();
            chunk.reset(Arrays.copyOf(buffer, remainder));
            chunk.save();
            final long saveMs = System.currentTimeMillis() - saveStartTime;
            // Log the amount of time taken by the save.
            System.out.printf("Saved partial chunk %d in %d ms.\n", fullChunks, saveMs);
            // Save the chunk's record ID in the list.
            chunkRids.add(chunk.getIdentity());
        }
        // Should be no more data, so validate this.
        final int b = in.read();
        if (b != -1) {
            throw new Exception("File changed while saving to database! fileName=" + file.getAbsolutePath());
        }
        // Report 100% progress if we haven't already.
        if (currentPercent < 100) {
            System.out.println("Progress: 100%");
        }
        // Save the list of chunk references.
        final long saveChunkListStartTime = System.currentTimeMillis();
        fileDoc.field("DataChunks", chunkRids);
        fileDoc.save();
        final long saveChunkListMs = System.currentTimeMillis() - saveChunkListStartTime;
        // Log the amount of time taken to save the list of chunk RIDs.
        System.out.printf("Saved list of %d chunk RIDs in %d ms.\n", chunkRids.size(), saveChunkListMs);
    } finally {
        database.declareIntent(null);
        in.close();
    }
}
Also used : ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ArrayList(java.util.ArrayList) ODatabase(com.orientechnologies.orient.core.db.ODatabase) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ORID(com.orientechnologies.orient.core.id.ORID)

Example 18 with ODatabase

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

the class OAESEncryptionTest method testCreatedAESEncryptedCluster.

public void testCreatedAESEncryptedCluster() {
    final String buildDirectory = System.getProperty("buildDirectory", ".");
    final String dbPath = buildDirectory + File.separator + DBNAME_CLUSTERTEST;
    OFileUtils.deleteRecursively(new File(dbPath));
    final ODatabase db = new ODatabaseDocumentTx("plocal:" + dbPath);
    db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
    db.create();
    try {
        db.command(new OCommandSQL("create class TestEncryption")).execute();
        db.command(new OCommandSQL("alter class TestEncryption encryption aes")).execute();
        db.command(new OCommandSQL("insert into TestEncryption set name = 'Jay'")).execute();
        List result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        db.close();
        db.open("admin", "admin");
        OStorage storage = ((ODatabaseDocumentInternal) db).getStorage();
        db.close();
        storage.close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        storage = ((ODatabaseDocumentInternal) db).getStorage();
        db.close();
        storage.close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "invalidPassword");
        try {
            db.open("admin", "admin");
            storage = ((ODatabaseDocumentInternal) db).getStorage();
            db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
            result = db.query(new OSQLSynchQuery<ODocument>("select from OUser"));
            Assert.assertFalse(result.isEmpty());
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.close();
            storage.close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA=-");
        try {
            db.open("admin", "admin");
            storage = ((ODatabaseDocumentInternal) db).getStorage();
            db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.activateOnCurrentThread();
            db.close();
            storage.close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.activateOnCurrentThread();
        if (db.isClosed())
            db.open("admin", "admin");
        db.drop();
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) List(java.util.List) File(java.io.File) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 19 with ODatabase

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

the class OAESEncryptionTest method testCreatedAESEncryptedDatabase.

public void testCreatedAESEncryptedDatabase() {
    String buildDirectory = System.getProperty("buildDirectory", ".");
    final String dbPath = buildDirectory + File.separator + DBNAME_DATABASETEST;
    OFileUtils.deleteRecursively(new File(dbPath));
    final ODatabase db = new ODatabaseDocumentTx("plocal:" + dbPath);
    db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_METHOD.getKey(), "aes");
    db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
    db.create();
    try {
        db.command(new OCommandSQL("create class TestEncryption")).execute();
        db.command(new OCommandSQL("insert into TestEncryption set name = 'Jay'")).execute();
        List result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        db.close();
        db.open("admin", "admin");
        OStorage storage = ((ODatabaseDocumentInternal) db).getStorage();
        db.close();
        storage.close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        storage = ((ODatabaseDocumentInternal) db).getStorage();
        db.close();
        storage.close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "invalidPassword");
        try {
            db.open("admin", "admin");
            storage = ((ODatabaseDocumentInternal) db).getStorage();
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.activateOnCurrentThread();
            db.close();
            storage.close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA=-");
        try {
            db.open("admin", "admin");
            storage = ((ODatabaseDocumentInternal) db).getStorage();
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.activateOnCurrentThread();
            db.close();
            storage.close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
    } finally {
        db.activateOnCurrentThread();
        if (db.isClosed())
            db.open("admin", "admin");
        db.drop();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OStorage(com.orientechnologies.orient.core.storage.OStorage) List(java.util.List) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) File(java.io.File) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 20 with ODatabase

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

the class ODESEncryptionTest method testCreatedDESEncryptedCluster.

public void testCreatedDESEncryptedCluster() {
    OFileUtils.deleteRecursively(new File("target/" + DBNAME_CLUSTERTEST));
    final ODatabase db = new ODatabaseDocumentTx("plocal:target/" + DBNAME_CLUSTERTEST);
    db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
    db.create();
    try {
        db.command(new OCommandSQL("create class TestEncryption")).execute();
        db.command(new OCommandSQL("alter class TestEncryption encryption des")).execute();
        db.command(new OCommandSQL("insert into TestEncryption set name = 'Jay'")).execute();
        List result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        db.close();
        db.open("admin", "admin");
        db.close();
        Orient.instance().getStorage(DBNAME_CLUSTERTEST).close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
        db.close();
        Orient.instance().getStorage(DBNAME_CLUSTERTEST).close(true, false);
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "invalidPassword");
        try {
            db.open("admin", "admin");
            db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
            result = db.query(new OSQLSynchQuery<ODocument>("select from OUser"));
            Assert.assertFalse(result.isEmpty());
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.activateOnCurrentThread();
            db.close();
            Orient.instance().getStorage(DBNAME_CLUSTERTEST).close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA=-");
        try {
            db.open("admin", "admin");
            db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
            Assert.fail();
        } catch (OSecurityException e) {
            Assert.assertTrue(true);
        } finally {
            db.activateOnCurrentThread();
            db.close();
            Orient.instance().getStorage(DBNAME_CLUSTERTEST).close(true, false);
        }
        db.setProperty(OGlobalConfiguration.STORAGE_ENCRYPTION_KEY.getKey(), "T1JJRU5UREJfSVNfQ09PTA==");
        db.open("admin", "admin");
        result = db.query(new OSQLSynchQuery<ODocument>("select from TestEncryption"));
        Assert.assertEquals(result.size(), 1);
    } finally {
        db.activateOnCurrentThread();
        if (db.exists())
            db.drop();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) List(java.util.List) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) ODatabase(com.orientechnologies.orient.core.db.ODatabase) File(java.io.File) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabase (com.orientechnologies.orient.core.db.ODatabase)29 OInvalidIndexEngineIdException (com.orientechnologies.orient.core.exception.OInvalidIndexEngineIdException)13 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)9 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)8 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)7 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)6 File (java.io.File)6 List (java.util.List)6 OSecurityException (com.orientechnologies.orient.core.exception.OSecurityException)5 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OStorage (com.orientechnologies.orient.core.storage.OStorage)3 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)2 OIndexRIDContainer (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OIndexRIDContainer)2 ORID (com.orientechnologies.orient.core.id.ORID)2 IOException (java.io.IOException)2 OCommandExecutor (com.orientechnologies.orient.core.command.OCommandExecutor)1 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 ODatabaseListener (com.orientechnologies.orient.core.db.ODatabaseListener)1