Search in sources :

Example 6 with DatabaseConfiguration

use of org.sirix.access.conf.DatabaseConfiguration 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;
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 7 with DatabaseConfiguration

use of org.sirix.access.conf.DatabaseConfiguration 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());
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) SubtreeListener(org.brackit.xquery.node.parser.SubtreeListener) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) AbstractTemporalNode(org.brackit.xquery.xdm.AbstractTemporalNode) ResourceManager(org.sirix.api.ResourceManager) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException)

Example 8 with DatabaseConfiguration

use of org.sirix.access.conf.DatabaseConfiguration 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());
    }
}
Also used : SirixRuntimeException(org.sirix.exception.SirixRuntimeException) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database)

Example 9 with DatabaseConfiguration

use of org.sirix.access.conf.DatabaseConfiguration 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;
}
Also used : SirixRuntimeException(org.sirix.exception.SirixRuntimeException) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) DocumentException(org.brackit.xquery.xdm.DocumentException) Database(org.sirix.api.Database)

Example 10 with DatabaseConfiguration

use of org.sirix.access.conf.DatabaseConfiguration in project sirix by sirixdb.

the class WikipediaImportTest method testWikipediaImport.

@Test
public void testWikipediaImport() throws Exception {
    Databases.truncateDatabase(new DatabaseConfiguration(PATHS.PATH2.getFile()));
    // Create necessary element nodes.
    final String NSP_URI = "";
    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    final StartElement timestamp = eventFactory.createStartElement(new QName(NSP_URI, "timestamp", XMLConstants.DEFAULT_NS_PREFIX), null, null);
    final StartElement page = eventFactory.createStartElement(new QName(NSP_URI, "page", XMLConstants.DEFAULT_NS_PREFIX), null, null);
    final StartElement rev = eventFactory.createStartElement(new QName(NSP_URI, "revision", XMLConstants.DEFAULT_NS_PREFIX), null, null);
    final StartElement id = eventFactory.createStartElement(new QName(NSP_URI, "id", XMLConstants.DEFAULT_NS_PREFIX), null, null);
    final StartElement text = eventFactory.createStartElement(new QName(NSP_URI, "text", XMLConstants.DEFAULT_NS_PREFIX), null, null);
    // Create list.
    final List<StartElement> list = new LinkedList<StartElement>();
    list.add(timestamp);
    list.add(page);
    list.add(rev);
    list.add(id);
    list.add(text);
    // Invoke import.
    new WikipediaImport(WIKIPEDIA, PATHS.PATH2.getFile()).importData(DateBy.HOURS, list);
    XMLSerializer.main(PATHS.PATH2.getFile().toAbsolutePath().toString(), PATHS.PATH3.getFile().toAbsolutePath().toString());
    final StringBuilder actual = TestHelper.readFile(PATHS.PATH3.getFile().toAbsolutePath(), false);
    final StringBuilder expected = TestHelper.readFile(EXPECTED, false);
    assertEquals("XML files match", expected.toString(), actual.toString());
}
Also used : StartElement(javax.xml.stream.events.StartElement) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLEventFactory(javax.xml.stream.XMLEventFactory) QName(javax.xml.namespace.QName) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)24 Database (org.sirix.api.Database)19 ResourceManager (org.sirix.api.ResourceManager)11 Path (java.nio.file.Path)10 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)7 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixRuntimeException (org.sirix.exception.SirixRuntimeException)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 SirixException (org.sirix.exception.SirixException)4 IOException (java.io.IOException)3 QName (javax.xml.namespace.QName)3 ResourceConfiguration (org.sirix.access.conf.ResourceConfiguration)3 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)3 LinkedList (java.util.LinkedList)2 XMLEventFactory (javax.xml.stream.XMLEventFactory)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 StartElement (javax.xml.stream.events.StartElement)2 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)2 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)2 Ignore (org.junit.Ignore)2