use of org.sirix.api.Database in project sirix by sirixdb.
the class Databases method openDatabase.
/**
* Open database. A database can be opened only once (even across JVMs). Afterwards a singleton
* instance bound to the {@link File} is returned.
*
* @param file determines where the database is located sessionConf a
* {@link ResourceManagerConfiguration} object to set up the session
* @return {@link Database} instance.
* @throws SirixIOException if an I/O exception occurs
* @throws SirixUsageException if Sirix is not used properly
* @throws NullPointerException if {@code file} is {@code null}
*/
public static synchronized Database openDatabase(final Path file) throws SirixUsageException, SirixIOException {
checkNotNull(file);
if (!Files.exists(file)) {
throw new SirixUsageException("DB could not be opened (since it was not created?) at location", file.toString());
}
final DatabaseConfiguration config = DatabaseConfiguration.deserialize(file);
if (config == null) {
throw new IllegalStateException("Configuration may not be null!");
}
final Database database = new DatabaseImpl(config);
putDatabase(file, database);
return database;
}
use of org.sirix.api.Database in project sirix by sirixdb.
the class DBStore method create.
public TemporalCollection<?> create(final String collName, final Optional<String> optResName, final SubtreeParser parser) throws DocumentException {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(collName));
try {
Databases.truncateDatabase(dbConf);
Databases.createDatabase(dbConf);
try (final Database database = Databases.openDatabase(dbConf.getFile())) {
mDatabases.add(database);
final String resName = optResName.isPresent() ? optResName.get() : new StringBuilder(3).append("resource").append(database.listResources().size() + 1).toString();
database.createResource(ResourceConfiguration.newBuilder(resName, dbConf).useDeweyIDs(true).useTextCompression(true).buildPathSummary(true).storageType(mStorageType).build());
final DBCollection collection = new DBCollection(collName, database);
mCollections.put(database, collection);
try (final ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(resName).build());
final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
parser.parse(new SubtreeBuilder(collection, wtx, Insert.ASFIRSTCHILD, Collections.<SubtreeListener<? super AbstractTemporalNode<DBNode>>>emptyList()));
wtx.commit();
}
return collection;
}
} catch (final SirixException e) {
throw new DocumentException(e.getCause());
}
}
use of org.sirix.api.Database in project sirix by sirixdb.
the class DBStore method create.
@Override
public TemporalCollection<?> create(final String name) throws DocumentException {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(name));
try {
if (Databases.createDatabase(dbConf)) {
throw new DocumentException("Document with name %s exists!", name);
}
final Database database = Databases.openDatabase(dbConf.getFile());
mDatabases.add(database);
final DBCollection collection = new DBCollection(name, database);
mCollections.put(database, collection);
return collection;
} catch (final SirixRuntimeException e) {
throw new DocumentException(e.getCause());
}
}
use of org.sirix.api.Database in project sirix by sirixdb.
the class DBStore method lookup.
@Override
public TemporalCollection<?> lookup(final String name) throws DocumentException {
final DatabaseConfiguration dbConf = new DatabaseConfiguration(mLocation.resolve(name));
if (Databases.existsDatabase(dbConf)) {
try {
final Database database = Databases.openDatabase(dbConf.getFile());
final Optional<Database> storedCollection = mDatabases.stream().findFirst().filter((Database db) -> db.equals(database));
if (storedCollection.isPresent()) {
return mCollections.get(storedCollection.get());
}
mDatabases.add(database);
final DBCollection collection = new DBCollection(name, database);
mCollections.put(database, collection);
return collection;
} catch (final SirixRuntimeException e) {
throw new DocumentException(e.getCause());
}
}
return null;
}
use of org.sirix.api.Database in project sirix by sirixdb.
the class WorkerHelperTest method testClose.
/**
* This method tests
* {@link WorkerHelper#closeWTX(boolean, NodeWriteTrx, Session, Database)}
*/
@Test(expected = IllegalStateException.class)
public void testClose() throws SirixException {
Database database = Databases.openDatabase(DBFILE.getParentFile());
Session session = database.getSession(new SessionConfiguration.Builder(DBFILE.getName()).build());
final NodeWriteTrx wtx = session.beginNodeWriteTrx();
WorkerHelper.closeWTX(false, wtx, session, database);
wtx.commit();
database = Databases.openDatabase(DBFILE.getParentFile());
session = database.getSession(new SessionConfiguration.Builder(DBFILE.getName()).build());
final NodeReadTrx rtx = session.beginNodeReadTrx();
WorkerHelper.closeRTX(rtx, session, database);
rtx.moveTo(11);
}
Aggregations