Search in sources :

Example 1 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class UnorderedKeyValuePage method serialize.

@Override
public void serialize(final DataOutput out, final SerializationType type) throws IOException {
    if (!mAddedReferences) {
        addReferences();
    }
    // Write page key.
    putVarLong(out, mRecordPageKey);
    // Write dewey IDs.
    if (mResourceConfig.mDeweyIDsStored && mPersistenter instanceof NodePersistenter) {
        final NodePersistenter persistenter = (NodePersistenter) mPersistenter;
        out.writeInt(mDeweyIDs.size());
        final List<SirixDeweyID> ids = new ArrayList<>(mDeweyIDs.keySet());
        ids.sort((SirixDeweyID first, SirixDeweyID second) -> Integer.valueOf(first.toBytes().length).compareTo(second.toBytes().length));
        final PeekingIterator<SirixDeweyID> iter = Iterators.peekingIterator(ids.iterator());
        SirixDeweyID id = null;
        if (iter.hasNext()) {
            id = iter.next();
            persistenter.serializeDeweyID(out, Kind.ELEMENT, id, Optional.empty(), mResourceConfig);
            serializeDeweyRecord(id, out);
        }
        while (iter.hasNext()) {
            final SirixDeweyID nextDeweyID = iter.next();
            persistenter.serializeDeweyID(out, Kind.ELEMENT, id, Optional.of(nextDeweyID), mResourceConfig);
            serializeDeweyRecord(nextDeweyID, out);
            id = nextDeweyID;
        }
    }
    // Write normal entries.
    out.writeInt(mSlots.size());
    for (final Entry<Long, byte[]> entry : mSlots.entrySet()) {
        putVarLong(out, entry.getKey());
        final byte[] data = entry.getValue();
        final int length = data.length;
        out.writeInt(length);
        out.write(data);
    }
    // Write overlong entries.
    out.writeInt(mReferences.size());
    for (final Map.Entry<Long, PageReference> entry : mReferences.entrySet()) {
        // Write record ID.
        out.writeLong(entry.getKey());
        // Write key in persistent storage.
        out.writeLong(entry.getValue().getKey());
    }
    // Write previous reference if it has any reference.
    final boolean hasPreviousReference = mPreviousPageRefKey != Constants.NULL_ID_LONG;
    out.writeBoolean(hasPreviousReference);
    if (hasPreviousReference) {
        out.writeLong(mPreviousPageRefKey);
    }
    out.writeByte(mPageKind.getID());
}
Also used : ArrayList(java.util.ArrayList) SirixDeweyID(org.sirix.node.SirixDeweyID) NodePersistenter(org.sirix.node.interfaces.NodePersistenter) Utils.getVarLong(org.sirix.node.Utils.getVarLong) Utils.putVarLong(org.sirix.node.Utils.putVarLong) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class AVLTreeWriter method index.

/**
 * Checks if the specified token is already indexed; if yes, returns its reference. Otherwise,
 * creates a new index entry and returns a reference of the indexed token.
 *
 * @param key token to be indexed
 * @param value node key references
 * @param move determines if AVLNode cursor must be moved to document root/root node or not
 * @return indexed node key references
 * @throws SirixIOException if an I/O error occurs
 */
@SuppressWarnings("unchecked")
public V index(final K key, final V value, final MoveCursor move) throws SirixIOException {
    if (move == MoveCursor.TO_DOCUMENT_ROOT) {
        moveToDocumentRoot();
    }
    final RevisionRootPage root = mPageWriteTrx.getActualRevisionRootPage();
    if (mAVLTreeReader.getAVLNode() == null && ((DocumentRootNode) getNode()).getFirstChildKey() == Fixed.NULL_NODE_KEY.getStandardProperty()) {
        // Index is empty.. create root node.
        final long nodeKey = getNewNodeKey(root);
        final AVLNode<K, V> treeRoot = (AVLNode<K, V>) mPageWriteTrx.createEntry(nodeKey, new AVLNode<>(key, value, new NodeDelegate(nodeKey, Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), 0, 0, Optional.<SirixDeweyID>empty())), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
        final DocumentRootNode document = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
        document.setFirstChildKey(treeRoot.getNodeKey());
        document.incrementChildCount();
        document.incrementDescendantCount();
        return value;
    }
    if (move == MoveCursor.TO_DOCUMENT_ROOT || mAVLTreeReader.getAVLNode() == null) {
        moveToDocumentRoot();
        moveToFirstChild();
    }
    AVLNode<K, V> node = mAVLTreeReader.getAVLNode();
    while (true) {
        final int c = key.compareTo(node.getKey());
        if (c == 0) {
            if (!value.equals(node.getValue())) {
                final AVLNode<K, V> avlNode = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
                avlNode.setValue(value);
            }
            return node.getValue();
        }
        final boolean moved = c < 0 ? moveToFirstChild().hasMoved() : moveToLastChild().hasMoved();
        if (moved) {
            node = mAVLTreeReader.getAVLNode();
            continue;
        }
        final long nodeKey = getNewNodeKey(root);
        final AVLNode<K, V> child = (AVLNode<K, V>) mPageWriteTrx.createEntry(nodeKey, new AVLNode<>(key, value, new NodeDelegate(nodeKey, node.getNodeKey(), 0, 0, Optional.<SirixDeweyID>empty())), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
        node = (AVLNode<K, V>) mPageWriteTrx.prepareEntryForModification(node.getNodeKey(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
        if (c < 0) {
            node.setLeftChildKey(child.getNodeKey());
            adjust(child);
        } else {
            node.setRightChildKey(child.getNodeKey());
            adjust(child);
        }
        final DocumentRootNode document = (DocumentRootNode) mPageWriteTrx.prepareEntryForModification(Fixed.DOCUMENT_NODE_KEY.getStandardProperty(), mAVLTreeReader.mPageKind, mAVLTreeReader.mIndex, Optional.<UnorderedKeyValuePage>empty());
        document.incrementDescendantCount();
        return value;
    }
}
Also used : DocumentRootNode(org.sirix.node.DocumentRootNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) SirixDeweyID(org.sirix.node.SirixDeweyID) NodeDelegate(org.sirix.node.delegates.NodeDelegate) RevisionRootPage(org.sirix.page.RevisionRootPage)

Example 3 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class UpdateTest method testSecondMoveToFirstChild.

@Test
public void testSecondMoveToFirstChild() throws SirixException {
    final XdmNodeWriteTrx wtx = holder.getResourceManager().beginNodeWriteTrx();
    DocumentCreater.create(wtx);
    wtx.moveTo(5);
    wtx.moveSubtreeToFirstChild(4);
    testSecondMoveToFirstChild(wtx);
    wtx.commit();
    testSecondMoveToFirstChild(wtx);
    wtx.close();
    final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
    testSecondMoveToFirstChild(rtx);
    rtx.moveToDocumentRoot();
    final Builder<SirixDeweyID> builder = ImmutableSet.<SirixDeweyID>builder();
    final ImmutableSet<SirixDeweyID> ids = builder.add(new SirixDeweyID("1")).add(new SirixDeweyID("1.3")).add(new SirixDeweyID("1.3.0.3")).add(new SirixDeweyID("1.3.1.3")).add(new SirixDeweyID("1.3.5")).add(new SirixDeweyID("1.3.5.3")).add(new SirixDeweyID("1.3.5.5")).add(new SirixDeweyID("1.3.7")).add(new SirixDeweyID("1.3.9")).add(new SirixDeweyID("1.3.9.1.3")).add(new SirixDeweyID("1.3.9.3")).add(new SirixDeweyID("1.3.9.5")).add(new SirixDeweyID("1.3.11")).build();
    test(ids.iterator(), new NonStructuralWrapperAxis(new DescendantAxis(rtx, IncludeSelf.YES)));
    rtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) NonStructuralWrapperAxis(org.sirix.axis.NonStructuralWrapperAxis) SirixDeweyID(org.sirix.node.SirixDeweyID) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Example 4 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class UpdateTest method testSecondMoveSubtreeToRightSibling.

@Test
public void testSecondMoveSubtreeToRightSibling() throws SirixException {
    final XdmNodeWriteTrx wtx = holder.getResourceManager().beginNodeWriteTrx();
    DocumentCreater.create(wtx);
    wtx.moveTo(9);
    wtx.moveSubtreeToRightSibling(5);
    // wtx.moveTo(5);
    // wtx.moveSubtreeToRightSibling(4);
    testSecondMoveSubtreeToRightSibling(wtx);
    wtx.commit();
    testSecondMoveSubtreeToRightSibling(wtx);
    wtx.close();
    final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
    testSecondMoveSubtreeToRightSibling(rtx);
    rtx.moveToDocumentRoot();
    final Builder<SirixDeweyID> builder = ImmutableSet.<SirixDeweyID>builder();
    builder.add(new SirixDeweyID("1")).add(new SirixDeweyID("1.3")).add(new SirixDeweyID("1.3.0.3")).add(new SirixDeweyID("1.3.1.3")).add(new SirixDeweyID("1.3.3")).add(new SirixDeweyID("1.3.5")).add(new SirixDeweyID("1.3.5.5")).add(new SirixDeweyID("1.3.5.7")).add(new SirixDeweyID("1.3.7")).add(new SirixDeweyID("1.3.9")).add(new SirixDeweyID("1.3.9.1.3")).add(new SirixDeweyID("1.3.9.3")).add(new SirixDeweyID("1.3.9.5")).add(new SirixDeweyID("1.3.11")).build();
    // test(ids.iterator(), new NonStructuralWrapperAxis(new DescendantAxis(rtx,
    // IncludeSelf.YES)));
    rtx.moveToDocumentRoot();
    for (final long nodeKey : new NonStructuralWrapperAxis(new DescendantAxis(rtx, IncludeSelf.YES))) {
        System.out.println(nodeKey + ": " + rtx.getDeweyID());
    }
    rtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) NonStructuralWrapperAxis(org.sirix.axis.NonStructuralWrapperAxis) SirixDeweyID(org.sirix.node.SirixDeweyID) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Example 5 with SirixDeweyID

use of org.sirix.node.SirixDeweyID in project sirix by sirixdb.

the class UpdateTest method testThirdMoveToFirstChild.

@Test
public void testThirdMoveToFirstChild() throws SirixException {
    final XdmNodeWriteTrx wtx = holder.getResourceManager().beginNodeWriteTrx();
    DocumentCreater.create(wtx);
    wtx.moveTo(5);
    wtx.moveSubtreeToFirstChild(11);
    testThirdMoveToFirstChild(wtx);
    wtx.commit();
    testThirdMoveToFirstChild(wtx);
    wtx.close();
    final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
    testThirdMoveToFirstChild(rtx);
    rtx.moveToDocumentRoot();
    final Builder<SirixDeweyID> builder = ImmutableSet.<SirixDeweyID>builder();
    final ImmutableSet<SirixDeweyID> ids = builder.add(new SirixDeweyID("1")).add(new SirixDeweyID("1.3")).add(new SirixDeweyID("1.3.0.3")).add(new SirixDeweyID("1.3.1.3")).add(new SirixDeweyID("1.3.3")).add(new SirixDeweyID("1.3.5")).add(new SirixDeweyID("1.3.5.2.3")).add(new SirixDeweyID("1.3.5.3")).add(new SirixDeweyID("1.3.5.5")).add(new SirixDeweyID("1.3.7")).add(new SirixDeweyID("1.3.9")).add(new SirixDeweyID("1.3.9.1.3")).add(new SirixDeweyID("1.3.9.5")).add(new SirixDeweyID("1.3.11")).build();
    test(ids.iterator(), new NonStructuralWrapperAxis(new DescendantAxis(rtx, IncludeSelf.YES)));
    rtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) NonStructuralWrapperAxis(org.sirix.axis.NonStructuralWrapperAxis) SirixDeweyID(org.sirix.node.SirixDeweyID) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Aggregations

SirixDeweyID (org.sirix.node.SirixDeweyID)25 StructNode (org.sirix.node.interfaces.StructNode)14 SirixUsageException (org.sirix.exception.SirixUsageException)10 ElementNode (org.sirix.node.ElementNode)6 TextNode (org.sirix.node.TextNode)6 Test (org.junit.Test)5 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)5 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)5 DescendantAxis (org.sirix.axis.DescendantAxis)5 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)5 CommentNode (org.sirix.node.CommentNode)4 PINode (org.sirix.node.PINode)4 Node (org.sirix.node.interfaces.Node)4 QNm (org.brackit.xquery.atomic.QNm)3 AttributeNode (org.sirix.node.AttributeNode)3 NamespaceNode (org.sirix.node.NamespaceNode)3 NodeDelegate (org.sirix.node.delegates.NodeDelegate)3 NameNode (org.sirix.node.interfaces.NameNode)3 ValueNode (org.sirix.node.interfaces.ValueNode)3