Search in sources :

Example 1 with ODatabaseImport

use of com.orientechnologies.orient.core.db.tool.ODatabaseImport in project serverless by bluenimble.

the class OrientDatabase method imp.

@Override
public void imp(Set<String> entities, InputStream in, Map<ExchangeOption, Boolean> options, final ExchangeListener listener) throws DatabaseException {
    OCommandOutputListener olistener = new OCommandOutputListener() {

        @Override
        public void onMessage(String iText) {
            if (listener != null) {
                listener.onMessage(iText);
            }
        }
    };
    ODatabaseImport _import = null;
    try {
        _import = new ODatabaseImport(db, in, olistener);
    } catch (IOException e) {
        throw new DatabaseException(e.getMessage(), e);
    }
    _import.setIncludeInfo(option(options, ExchangeOption.info, false));
    _import.setIncludeClusterDefinitions(option(options, ExchangeOption.entities, false));
    _import.setIncludeSchema(option(options, ExchangeOption.schema, false));
    _import.setIncludeIndexDefinitions(option(options, ExchangeOption.indexes, false));
    _import.setIncludeManualIndexes(option(options, ExchangeOption.indexes, false));
    _import.setPreserveRids(option(options, ExchangeOption.ids, false));
    _import.setDeleteRIDMapping(false);
    if (entities != null && !entities.isEmpty()) {
        _import.setIncludeClasses(entities);
    }
    _import.importDatabase();
    _import.close();
}
Also used : ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) IOException(java.io.IOException) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener) DatabaseException(com.bluenimble.platform.db.DatabaseException)

Example 2 with ODatabaseImport

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

the class AutomaticBackupTest method testExport.

// // @Test
// //TODO: move to EE test suite
// public void testIncrementalBackup() throws IOException, ClassNotFoundException, MalformedObjectNameException,
// InstanceAlreadyExistsException, NotCompliantMBeanException, MBeanRegistrationException {
// if (new File(BACKUPDIR + "/" + DBNAME).exists())
// OFileUtils.deleteRecursively(new File(BACKUPDIR + "/" + DBNAME));
// 
// final OAutomaticBackup aBackup = new OAutomaticBackup();
// 
// final OServerParameterConfiguration[] config = new OServerParameterConfiguration[] {
// new OServerParameterConfiguration("firstTime",
// new SimpleDateFormat("HH:mm:ss").format(new Date(System.currentTimeMillis() + 2000))),
// new OServerParameterConfiguration("delay", "1d"), new OServerParameterConfiguration("mode", "INCREMENTAL_BACKUP"),
// new OServerParameterConfiguration("target.directory", BACKUPDIR) };
// 
// aBackup.config(server, config);
// 
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// 
// aBackup.sendShutdown();
// 
// final ODatabaseDocumentTx database2 = new ODatabaseDocumentTx(URL2);
// if (database2.exists())
// database2.open("admin", "admin").drop();
// database2.create(BACKUPDIR + "/" + DBNAME);
// 
// Assert.assertEquals(database2.countClass("TestBackup"), 1);
// }
@Test
public void testExport() throws IOException, ClassNotFoundException, MalformedObjectNameException, InstanceAlreadyExistsException, NotCompliantMBeanException, MBeanRegistrationException {
    if (new File(BACKUPDIR + "/fullExport.json.gz").exists())
        new File(BACKUPDIR + "/fullExport.json.gz").delete();
    final OAutomaticBackup aBackup = new OAutomaticBackup();
    final OServerParameterConfiguration[] config = new OServerParameterConfiguration[] { new OServerParameterConfiguration("firstTime", new SimpleDateFormat("HH:mm:ss").format(new Date(System.currentTimeMillis() + 2000))), new OServerParameterConfiguration("delay", "1d"), new OServerParameterConfiguration("mode", "EXPORT"), new OServerParameterConfiguration("target.directory", BACKUPDIR), new OServerParameterConfiguration("target.fileName", "fullExport.json.gz") };
    aBackup.config(server, config);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    aBackup.sendShutdown();
    final ODatabaseDocumentTx database2 = new ODatabaseDocumentTx(URL2);
    if (database2.exists())
        database2.open("admin", "admin").drop();
    database2.create();
    new ODatabaseImport(database2, BACKUPDIR + "/fullExport.json.gz", null).importDatabase();
    Assert.assertEquals(database2.countClass("TestBackup"), 1);
}
Also used : ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) OServerParameterConfiguration(com.orientechnologies.orient.server.config.OServerParameterConfiguration) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ODatabaseImport

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

the class OServerCommandPostImportDatabase method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    if (!iRequest.isMultipart) {
        database = getProfiledDatabaseInstance(iRequest);
        try {
            ODatabaseImport importer = new ODatabaseImport(database, new ByteArrayInputStream(iRequest.content.getBytes("UTF8")), this);
            for (Map.Entry<String, String> option : iRequest.getParameters().entrySet()) importer.setOption(option.getKey(), option.getValue());
            importer.importDatabase();
            iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, "{\"responseText\": \"Database imported Correctly, see server log for more informations.\"}", null);
        } catch (Exception e) {
            iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, e.getMessage() + ": " + e.getCause() != null ? e.getCause().getMessage() : "", OHttpUtils.CONTENT_JSON, "{\"responseText\": \"" + e.getMessage() + ": " + (e.getCause() != null ? e.getCause().getMessage() : "") + "\"}", null);
        } finally {
            if (database != null)
                database.close();
            database = null;
        }
    } else if (iRequest.multipartStream == null || iRequest.multipartStream.available() <= 0) {
        iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Content stream is null or empty", OHttpUtils.CONTENT_TEXT_PLAIN, "Content stream is null or empty", null);
    } else {
        database = getProfiledDatabaseInstance(iRequest);
        try {
            parse(iRequest, iResponse, new OHttpMultipartContentBaseParser(), new OHttpMultipartDatabaseImportContentParser(), database);
            ODatabaseImport importer = new ODatabaseImport(database, importData, this);
            for (Map.Entry<String, String> option : iRequest.getParameters().entrySet()) importer.setOption(option.getKey(), option.getValue());
            importer.importDatabase();
            iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, "{\"responseText\": \"Database imported Correctly, see server log for more informations.\"}", null);
        } catch (Exception e) {
            iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, e.getMessage() + ": " + e.getCause() != null ? e.getCause().getMessage() : "", OHttpUtils.CONTENT_JSON, "{\"responseText\": \"" + e.getMessage() + ": " + (e.getCause() != null ? e.getCause().getMessage() : "") + "\"}", null);
        } finally {
            if (database != null)
                database.close();
            database = null;
            if (importData != null)
                importData.close();
            importData = null;
        }
    }
    return false;
}
Also used : OHttpMultipartDatabaseImportContentParser(com.orientechnologies.orient.server.network.protocol.http.multipart.OHttpMultipartDatabaseImportContentParser) ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) HashMap(java.util.HashMap) Map(java.util.Map) OHttpMultipartContentBaseParser(com.orientechnologies.orient.server.network.protocol.http.multipart.OHttpMultipartContentBaseParser)

Example 4 with ODatabaseImport

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

the class LuceneAutomaticBackupRestoreTest method shouldExportImport.

@Test
public void shouldExportImport() throws IOException, InterruptedException {
    List<?> query = databaseDocumentTx.query(new OSQLSynchQuery<Object>("select from City where name lucene 'Rome'"));
    Assert.assertEquals(query.size(), 1);
    String jsonConfig = OIOUtils.readStreamAsString(getClass().getClassLoader().getResourceAsStream("automatic-backup.json"));
    ODocument doc = new ODocument().fromJSON(jsonConfig);
    doc.field("enabled", true);
    doc.field("targetFileName", "${DBNAME}.json");
    doc.field("targetDirectory", BACKUPDIR);
    doc.field("mode", "EXPORT");
    doc.field("dbInclude", new String[] { "LuceneAutomaticBackupRestoreTest" });
    doc.field("firstTime", new SimpleDateFormat("HH:mm:ss").format(new Date(System.currentTimeMillis() + 2000)));
    OIOUtils.writeFile(new File(tempFolder.getRoot().getAbsolutePath() + "/config/automatic-backup.json"), doc.toJSON());
    final OAutomaticBackup aBackup = new OAutomaticBackup();
    final OServerParameterConfiguration[] config = new OServerParameterConfiguration[] {};
    aBackup.config(server, config);
    final CountDownLatch latch = new CountDownLatch(1);
    aBackup.registerListener(new OAutomaticBackup.OAutomaticBackupListener() {

        @Override
        public void onBackupCompleted(String database) {
            latch.countDown();
        }
    });
    latch.await();
    aBackup.sendShutdown();
    // RESTORE
    databaseDocumentTx.drop();
    databaseDocumentTx.create();
    GZIPInputStream stream = new GZIPInputStream(new FileInputStream(BACKUFILE + ".json.gz"));
    new ODatabaseImport(databaseDocumentTx, stream, new OCommandOutputListener() {

        @Override
        public void onMessage(String s) {
        }
    }).importDatabase();
    databaseDocumentTx.close();
    // VERIFY
    databaseDocumentTx.open("admin", "admin");
    assertThat(databaseDocumentTx.countClass("City")).isEqualTo(1);
    OIndex<?> index = databaseDocumentTx.getMetadata().getIndexManager().getIndex("City.name");
    assertThat(index).isNotNull();
    assertThat(index.getType()).isEqualTo(OClass.INDEX_TYPE.FULLTEXT.name());
    assertThat(databaseDocumentTx.query(new OSQLSynchQuery<Object>("select from City where name lucene 'Rome'"))).hasSize(1);
}
Also used : OAutomaticBackup(com.orientechnologies.orient.server.handler.OAutomaticBackup) CountDownLatch(java.util.concurrent.CountDownLatch) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) OServerParameterConfiguration(com.orientechnologies.orient.server.config.OServerParameterConfiguration) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with ODatabaseImport

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

the class LuceneExportImportTest method testExportImport.

@Test
public void testExportImport() {
    String file = "./target/exportTest.json";
    List<?> query = db.query(new OSQLSynchQuery<Object>("select from City where name lucene 'Rome'"));
    assertThat(query).hasSize(1);
    try {
        // export the DB
        new ODatabaseExport(db, file, new OCommandOutputListener() {

            @Override
            public void onMessage(String s) {
            }
        }).exportDatabase();
        db.drop();
        db.create();
        GZIPInputStream stream = new GZIPInputStream(new FileInputStream(file + ".gz"));
        // import
        new ODatabaseImport(db, stream, new OCommandOutputListener() {

            @Override
            public void onMessage(String s) {
                System.out.println(s);
            }
        }).importDatabase();
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    db.commit();
    long city = db.countClass("City");
    Assert.assertEquals(city, 1);
    OIndex<?> index = db.getMetadata().getIndexManager().getIndex("City.name");
    Assert.assertNotNull(index);
    assertThat(index.getSize()).isEqualTo(1L);
    Assert.assertEquals(index.getType(), "FULLTEXT");
    Assert.assertEquals(index.getAlgorithm(), "LUCENE");
    query = db.query(new OSQLSynchQuery<Object>("select from City "));
    assertThat(query.size()).isEqualTo(1);
    query = db.query(new OSQLSynchQuery<Object>("select from City where name lucene 'rome'"));
    assertThat(query).hasSize(1);
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ODatabaseImport(com.orientechnologies.orient.core.db.tool.ODatabaseImport) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener) ODatabaseExport(com.orientechnologies.orient.core.db.tool.ODatabaseExport) Test(org.junit.Test)

Aggregations

ODatabaseImport (com.orientechnologies.orient.core.db.tool.ODatabaseImport)9 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)5 File (java.io.File)5 IOException (java.io.IOException)4 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)3 ODatabaseExport (com.orientechnologies.orient.core.db.tool.ODatabaseExport)3 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)2 OServerParameterConfiguration (com.orientechnologies.orient.server.config.OServerParameterConfiguration)2 FileInputStream (java.io.FileInputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 DatabaseException (com.bluenimble.platform.db.DatabaseException)1 OIOException (com.orientechnologies.common.io.OIOException)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 OObjectDatabaseTx (com.orientechnologies.orient.object.db.OObjectDatabaseTx)1