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();
}
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);
}
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;
}
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);
}
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);
}
Aggregations