Search in sources :

Example 1 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class DatabaseImpl method getResourceManager.

// //////////////////////////////////////////////////////////
// END resource name <=> ID handling ////////////////////////
// //////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////
// START DB-Operations //////////////////////////////////////
// //////////////////////////////////////////////////////////
@Override
public synchronized ResourceManager getResourceManager(final ResourceManagerConfiguration resourceManagerConfig) throws SirixException {
    final Path resourceFile = mDBConfig.getFile().resolve(DatabaseConfiguration.DatabasePaths.DATA.getFile()).resolve(resourceManagerConfig.getResource());
    if (!Files.exists(resourceFile)) {
        throw new SirixUsageException("Resource could not be opened (since it was not created?) at location", resourceFile.toString());
    }
    if (mResourceStore.hasOpenResourceManager(resourceFile))
        return mResourceStore.getOpenResourceManager(resourceFile);
    final ResourceConfiguration resourceConfig = ResourceConfiguration.deserialize(resourceFile);
    // Resource of must be associated to this database.
    assert resourceConfig.mPath.getParent().getParent().equals(mDBConfig.getFile());
    if (!mBufferManagers.containsKey(resourceFile))
        mBufferManagers.put(resourceFile, new BufferManagerImpl());
    final ResourceManager resourceManager = mResourceStore.openResource(this, resourceConfig, resourceManagerConfig, mBufferManagers.get(resourceFile), resourceFile);
    return resourceManager;
}
Also used : Path(java.nio.file.Path) BufferManagerImpl(org.sirix.cache.BufferManagerImpl) ResourceManager(org.sirix.api.ResourceManager) SirixUsageException(org.sirix.exception.SirixUsageException) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration)

Example 2 with SirixUsageException

use of org.sirix.exception.SirixUsageException 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 3 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertTextAsFirstChild.

@Override
public XdmNodeWriteTrx insertTextAsFirstChild(final String value) throws SirixException {
    checkNotNull(value);
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && !value.isEmpty()) {
            checkAccessAndCommit();
            final long pathNodeKey = getCurrentNode().getNodeKey();
            final long parentKey = getCurrentNode().getNodeKey();
            final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
            final long rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
            // Update value in case of adjacent text nodes.
            if (hasNode(rightSibKey)) {
                moveTo(rightSibKey);
                if (getCurrentNode().getKind() == Kind.TEXT) {
                    setValue(new StringBuilder(value).append(getValue()).toString());
                    adaptHashedWithUpdate(getCurrentNode().getHash());
                    return this;
                }
                moveTo(parentKey);
            }
            // Insert new text node if no adjacent text nodes are found.
            final byte[] textValue = getBytes(value);
            final Optional<SirixDeweyID> id = newFirstChildID();
            final TextNode node = mNodeFactory.createTextNode(parentKey, leftSibKey, rightSibKey, textValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            // Index text value.
            mIndexController.notifyChange(ChangeType.INSERT, node, pathNodeKey);
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode or TextNode!");
        }
    } finally {
        unLock();
    }
}
Also used : TextNode(org.sirix.node.TextNode) SirixDeweyID(org.sirix.node.SirixDeweyID) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 4 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method setValue.

@Override
public XdmNodeWriteTrx setValue(final String value) throws SirixException {
    checkNotNull(value);
    acquireLock();
    try {
        if (getCurrentNode() instanceof ValueNode) {
            checkAccessAndCommit();
            // XDM).
            if (value.isEmpty()) {
                remove();
                return this;
            }
            // Remove old value from indexes.
            mIndexController.notifyChange(ChangeType.DELETE, getNode(), getPathNodeKey());
            final long oldHash = mNodeReader.getCurrentNode().hashCode();
            final byte[] byteVal = getBytes(value);
            final ValueNode node = (ValueNode) getPageTransaction().prepareEntryForModification(mNodeReader.getCurrentNode().getNodeKey(), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
            node.setValue(byteVal);
            mNodeReader.setCurrentNode(node);
            adaptHashedWithUpdate(oldHash);
            // Index new value.
            mIndexController.notifyChange(ChangeType.INSERT, getNode(), getPathNodeKey());
            return this;
        } else {
            throw new SirixUsageException("setValue(String) is not allowed if current node is not an IValNode implementation!");
        }
    } finally {
        unLock();
    }
}
Also used : ValueNode(org.sirix.node.interfaces.ValueNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 5 with SirixUsageException

use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertElementAsLeftSibling.

@Override
public XdmNodeWriteTrx insertElementAsLeftSibling(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && getCurrentNode().getKind() != Kind.DOCUMENT) {
            checkAccessAndCommit();
            final long key = getCurrentNode().getNodeKey();
            moveToParent();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            moveTo(key);
            final long parentKey = getCurrentNode().getParentKey();
            final long leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
            final long rightSibKey = getCurrentNode().getNodeKey();
            final Optional<SirixDeweyID> id = newLeftSiblingID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASLEFTSIBLING, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an StructuralNode (either Text or Element)!");
        }
    } finally {
        unLock();
    }
}
Also used : SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

SirixUsageException (org.sirix.exception.SirixUsageException)22 StructNode (org.sirix.node.interfaces.StructNode)13 SirixDeweyID (org.sirix.node.SirixDeweyID)10 ElementNode (org.sirix.node.ElementNode)8 TextNode (org.sirix.node.TextNode)7 Node (org.sirix.node.interfaces.Node)6 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)6 CommentNode (org.sirix.node.CommentNode)5 PINode (org.sirix.node.PINode)5 NameNode (org.sirix.node.interfaces.NameNode)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)5 QNm (org.brackit.xquery.atomic.QNm)4 SirixThreadedException (org.sirix.exception.SirixThreadedException)4 AttributeNode (org.sirix.node.AttributeNode)4 NamespaceNode (org.sirix.node.NamespaceNode)4 Optional (java.util.Optional)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Axis (org.sirix.api.Axis)2 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)2