Search in sources :

Example 21 with UnorderedKeyValuePage

use of org.sirix.page.UnorderedKeyValuePage in project sirix by sirixdb.

the class PageWriteTrxImpl method prepareRecordPage.

/**
 * Prepare record page.
 *
 * @param recordPageKey the key of the record page
 * @param pageKind the kind of page (used to determine the right subtree)
 * @return {@link PageContainer} instance
 * @throws SirixIOException if an I/O error occurs
 */
private PageContainer prepareRecordPage(@Nonnegative final long recordPageKey, final int index, final PageKind pageKind) throws SirixIOException {
    assert recordPageKey >= 0;
    assert pageKind != null;
    // Get the reference to the unordered key/value page storing the records.
    final PageReference reference = prepareLeafOfTree(mPageRtx.getPageReference(mNewRoot, pageKind, index), recordPageKey, index, pageKind);
    PageContainer pageContainer = mLog.get(reference);
    if (pageContainer.equals(PageContainer.emptyInstance())) {
        if (reference.getKey() == Constants.NULL_ID_LONG) {
            final UnorderedKeyValuePage completePage = new UnorderedKeyValuePage(recordPageKey, pageKind, Constants.NULL_ID_LONG, mPageRtx);
            final UnorderedKeyValuePage modifyPage = mPageRtx.clone(completePage);
            pageContainer = new PageContainer(completePage, modifyPage);
        } else {
            pageContainer = dereferenceRecordPageForModification(reference);
        }
        assert pageContainer != null;
        switch(pageKind) {
            case RECORDPAGE:
            case PATHSUMMARYPAGE:
            case PATHPAGE:
            case CASPAGE:
            case NAMEPAGE:
                appendLogRecord(reference, pageContainer);
                break;
            default:
                throw new IllegalStateException("Page kind not known!");
        }
    }
    return pageContainer;
}
Also used : PageReference(org.sirix.page.PageReference) PageContainer(org.sirix.cache.PageContainer) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage)

Example 22 with UnorderedKeyValuePage

use of org.sirix.page.UnorderedKeyValuePage in project sirix by sirixdb.

the class PageWriteTrxImpl method getRecord.

@Override
public Optional<Record> getRecord(@Nonnegative final long recordKey, final PageKind pageKind, @Nonnegative final int index) throws SirixIOException {
    mPageRtx.assertNotClosed();
    checkArgument(recordKey >= Fixed.NULL_NODE_KEY.getStandardProperty());
    checkNotNull(pageKind);
    // Calculate page.
    final long recordPageKey = mPageRtx.pageKey(recordKey);
    final PageContainer pageCont = prepareRecordPage(recordPageKey, index, pageKind);
    if (pageCont.equals(PageContainer.emptyInstance())) {
        return mPageRtx.getRecord(recordKey, pageKind, index);
    } else {
        Record node = ((UnorderedKeyValuePage) pageCont.getModified()).getValue(recordKey);
        if (node == null) {
            node = ((UnorderedKeyValuePage) pageCont.getComplete()).getValue(recordKey);
        }
        return mPageRtx.checkItemIfDeleted(node);
    }
}
Also used : PageContainer(org.sirix.cache.PageContainer) Record(org.sirix.node.interfaces.Record) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage)

Example 23 with UnorderedKeyValuePage

use of org.sirix.page.UnorderedKeyValuePage in project sirix by sirixdb.

the class XdmResourceManager method beginPageWriteTrx.

@Override
public synchronized PageWriteTrx<Long, Record, UnorderedKeyValuePage> beginPageWriteTrx(@Nonnegative final int revision) throws SirixException {
    assertAccess(revision);
    // Make sure not to exceed available number of write transactions.
    try {
        if (!mWriteSemaphore.tryAcquire(20, TimeUnit.SECONDS)) {
            throw new SirixUsageException("No write transaction available, please close the write transaction first.");
        }
    } catch (final InterruptedException e) {
        throw new SirixThreadedException(e);
    }
    final long currentPageTrxID = mPageTrxIDCounter.incrementAndGet();
    final int lastRev = mLastCommittedUberPage.get().getRevisionNumber();
    final PageWriteTrx<Long, Record, UnorderedKeyValuePage> pageWtx = createPageWriteTransaction(currentPageTrxID, lastRev, lastRev, Abort.NO);
    // Remember page transaction for debugging and safe close.
    if (mPageTrxMap.put(currentPageTrxID, pageWtx) != null) {
        throw new SirixThreadedException("ID generation is bogus because of duplicate ID.");
    }
    return pageWtx;
}
Also used : SirixThreadedException(org.sirix.exception.SirixThreadedException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Record(org.sirix.node.interfaces.Record) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixUsageException(org.sirix.exception.SirixUsageException)

Example 24 with UnorderedKeyValuePage

use of org.sirix.page.UnorderedKeyValuePage in project sirix by sirixdb.

the class LRUCacheTest method test.

@Test
public void test() {
    for (int i = 1; i < CacheTestHelper.PAGES.length; i++) {
        final PageContainer cont = cache.get((long) i);
        final UnorderedKeyValuePage current = (UnorderedKeyValuePage) cont.getComplete();
        assertEquals(CacheTestHelper.PAGES[i][0], current);
    }
    final PageContainer page = cache.get(0L);
    assertNull(page);
}
Also used : UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) Test(org.junit.Test)

Example 25 with UnorderedKeyValuePage

use of org.sirix.page.UnorderedKeyValuePage in project sirix by sirixdb.

the class PathSummaryWriter method processFoundPathNode.

/**
 * Process a found path node.
 *
 * @param oldPathNodeKey key of old path node
 * @param newPathNodeKey key of new path node
 * @param oldNodeKey key of old node
 * @param uriKey key of URI
 * @param prefixKey key of prefix
 * @param localNameKey key of local name
 * @param remove determines if a {@link PathNode} must be removed or not
 * @param type type of operation
 * @throws SirixException if Sirix fails to do so
 */
private void processFoundPathNode(@Nonnegative final long oldPathNodeKey, @Nonnegative final long newPathNodeKey, @Nonnegative final long oldNodeKey, final int uriKey, final int prefixKey, final int localNameKey, final Remove remove, final OPType type) throws SirixException {
    final PathSummaryReader cloned = PathSummaryReader.getInstance(mPageWriteTrx, mNodeRtx.getResourceManager());
    boolean moved = cloned.moveTo(oldPathNodeKey).hasMoved();
    assert moved;
    // Set new reference count of the root.
    if (type != OPType.MOVEDSAMELEVEL) {
        final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
        currNode.setReferenceCount(currNode.getReferences() + cloned.getReferences());
        currNode.setLocalNameKey(localNameKey);
        currNode.setPrefixKey(prefixKey);
        currNode.setURIKey(uriKey);
    }
    // For all old path nodes: Merge paths and adapt reference counts.
    mPathSummaryReader.moveToFirstChild();
    final int oldLevel = cloned.getLevel();
    for (final Axis oldDescendants = new DescendantAxis(cloned); oldDescendants.hasNext(); ) {
        oldDescendants.next();
        // Search for new path entry.
        final Axis axis = new FilterAxis(new LevelOrderAxis.Builder(mPathSummaryReader).filterLevel(cloned.getLevel() - oldLevel).includeSelf().build(), new NameFilter(mPathSummaryReader, Utils.buildName(cloned.getName())), new PathKindFilter(mPathSummaryReader, cloned.getPathKind()), new PathLevelFilter(mPathSummaryReader, cloned.getLevel()));
        if (axis.hasNext()) {
            axis.next();
            // Set new reference count.
            if (type != OPType.MOVEDSAMELEVEL) {
                final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
                currNode.setReferenceCount(currNode.getReferences() + cloned.getReferences());
            }
        } else {
            // Insert new node.
            insertPathAsFirstChild(cloned.getName(), cloned.getPathKind(), mPathSummaryReader.getLevel() + 1);
            // Set new reference count.
            final PathNode currNode = (PathNode) mPageWriteTrx.prepareEntryForModification(mPathSummaryReader.getNodeKey(), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
            currNode.setReferenceCount(cloned.getReferences());
        }
        mPathSummaryReader.moveTo(newPathNodeKey);
    }
    // Set new path nodes of the changed nodes, that is set their PCR
    // references.
    mPathSummaryReader.moveTo(newPathNodeKey);
    mNodeRtx.moveTo(oldNodeKey);
    boolean first = true;
    for (final Axis axis = new DescendantAxis(mNodeRtx, IncludeSelf.YES); axis.hasNext(); ) {
        axis.next();
        if (first && type == OPType.SETNAME) {
            first = false;
        } else if (mNodeRtx.getNode() instanceof ImmutableNameNode) {
            cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
            resetPath(newPathNodeKey, cloned.getLevel());
            if (mNodeRtx.getNode().getKind() == Kind.ELEMENT) {
                final ElementNode element = (ElementNode) mNodeRtx.getCurrentNode();
                for (int i = 0, nspCount = element.getNamespaceCount(); i < nspCount; i++) {
                    mNodeRtx.moveToNamespace(i);
                    cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
                    resetPath(newPathNodeKey, cloned.getLevel());
                    mNodeRtx.moveToParent();
                }
                for (int i = 0, attCount = element.getAttributeCount(); i < attCount; i++) {
                    mNodeRtx.moveToAttribute(i);
                    cloned.moveTo(((NameNode) mNodeRtx.getCurrentNode()).getPathNodeKey());
                    resetPath(newPathNodeKey, cloned.getLevel());
                    mNodeRtx.moveToParent();
                }
            }
        }
    }
    // Then: Remove old nodes.
    if (remove == Remove.YES) {
        mPathSummaryReader.moveTo(oldPathNodeKey);
        removePathSummaryNode(remove);
    }
}
Also used : ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) NameNode(org.sirix.node.interfaces.NameNode) PathLevelFilter(org.sirix.axis.filter.PathLevelFilter) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) ElementNode(org.sirix.node.ElementNode) FilterAxis(org.sirix.axis.filter.FilterAxis) NameFilter(org.sirix.axis.filter.NameFilter) ImmutableNameNode(org.sirix.node.interfaces.immutable.ImmutableNameNode) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) FilterAxis(org.sirix.axis.filter.FilterAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis) PathKindFilter(org.sirix.axis.filter.PathKindFilter)

Aggregations

UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)42 StructNode (org.sirix.node.interfaces.StructNode)14 ElementNode (org.sirix.node.ElementNode)12 NameNode (org.sirix.node.interfaces.NameNode)11 Node (org.sirix.node.interfaces.Node)11 AttributeNode (org.sirix.node.AttributeNode)9 CommentNode (org.sirix.node.CommentNode)9 NamespaceNode (org.sirix.node.NamespaceNode)9 PINode (org.sirix.node.PINode)9 TextNode (org.sirix.node.TextNode)9 NodeDelegate (org.sirix.node.delegates.NodeDelegate)9 ValueNode (org.sirix.node.interfaces.ValueNode)9 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)9 Record (org.sirix.node.interfaces.Record)8 NameNodeDelegate (org.sirix.node.delegates.NameNodeDelegate)7 StructNodeDelegate (org.sirix.node.delegates.StructNodeDelegate)7 ValNodeDelegate (org.sirix.node.delegates.ValNodeDelegate)7 Axis (org.sirix.api.Axis)6 DescendantAxis (org.sirix.axis.DescendantAxis)6 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)6