Search in sources :

Example 1 with NodeDelegate

use of org.sirix.node.delegates.NodeDelegate 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 2 with NodeDelegate

use of org.sirix.node.delegates.NodeDelegate in project sirix by sirixdb.

the class NodePageTest method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws IOException {
    final UnorderedKeyValuePage page1 = new UnorderedKeyValuePage(0L, PageKind.RECORDPAGE, Constants.NULL_ID_LONG, mPageReadTrx);
    assertEquals(0L, page1.getPageKey());
    final NodeDelegate del = new NodeDelegate(0, 1, 0, 0, Optional.of(SirixDeweyID.newRootID()));
    final StructNodeDelegate strucDel = new StructNodeDelegate(del, 12l, 4l, 3l, 1l, 0l);
    final NameNodeDelegate nameDel = new NameNodeDelegate(del, 5, 6, 7, 1);
    final ElementNode node1 = new ElementNode(strucDel, nameDel, new ArrayList<Long>(), HashBiMap.<Long, Long>create(), new ArrayList<Long>(), new QNm("a", "b", "c"));
    node1.insertAttribute(88L, 100);
    node1.insertAttribute(87L, 101);
    node1.insertNamespace(99L);
    node1.insertNamespace(98L);
    assertEquals(0L, node1.getNodeKey());
    page1.setEntry(node1.getNodeKey(), node1);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final DataOutputStream dataOut = new DataOutputStream(out);
    PagePersistenter.serializePage(dataOut, page1, SerializationType.DATA);
    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final UnorderedKeyValuePage page2 = (UnorderedKeyValuePage) PagePersistenter.deserializePage(new DataInputStream(in), mPageReadTrx, SerializationType.DATA);
    // assertEquals(position, out.position());
    final ElementNode element = (ElementNode) page2.getValue(0l);
    assertEquals(0L, page2.getValue(0l).getNodeKey());
    assertEquals(1L, element.getParentKey());
    assertEquals(12L, element.getFirstChildKey());
    assertEquals(3L, element.getLeftSiblingKey());
    assertEquals(4L, element.getRightSiblingKey());
    assertEquals(1, element.getChildCount());
    assertEquals(2, element.getAttributeCount());
    assertEquals(2, element.getNamespaceCount());
    assertEquals(88L, element.getAttributeKey(0));
    assertEquals(87L, element.getAttributeKey(1));
    assertEquals(99L, element.getNamespaceKey(0));
    assertEquals(98L, element.getNamespaceKey(1));
    assertEquals(5, ((NameNode) page2.getValue(0l)).getURIKey());
    assertEquals(6, ((NameNode) page2.getValue(0l)).getPrefixKey());
    assertEquals(7, ((NameNode) page2.getValue(0l)).getLocalNameKey());
    assertEquals(NamePageHash.generateHashForString("xs:untyped"), element.getTypeKey());
}
Also used : QNm(org.brackit.xquery.atomic.QNm) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementNode(org.sirix.node.ElementNode) DataInputStream(java.io.DataInputStream) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) Test(org.junit.Test)

Example 3 with NodeDelegate

use of org.sirix.node.delegates.NodeDelegate in project sirix by sirixdb.

the class NodeFactoryImpl method createPathNode.

@Override
public PathNode createPathNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, final long rightSibKey, final long hash, @Nonnull final QNm name, @Nonnull final Kind kind, @Nonnegative final int level) throws SirixIOException {
    final int uriKey = NamePageHash.generateHashForString(name.getNamespaceURI());
    final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? NamePageHash.generateHashForString(name.getPrefix()) : -1;
    final int localName = name.getLocalName() != null && !name.getLocalName().isEmpty() ? NamePageHash.generateHashForString(name.getLocalName()) : -1;
    final long revision = mPageWriteTrx.getRevisionNumber();
    final NodeDelegate nodeDel = new NodeDelegate(((PathSummaryPage) mPageWriteTrx.getActualRevisionRootPage().getPathSummaryPageReference().getPage()).getMaxNodeKey(0) + 1, parentKey, 0, revision, Optional.<SirixDeweyID>empty());
    final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
    final NameNodeDelegate nameDel = new NameNodeDelegate(nodeDel, uriKey, prefixKey, localName, 0);
    return (PathNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new PathNode(nodeDel, structDel, nameDel, kind, 1, level), PageKind.PATHSUMMARYPAGE, 0, Optional.<UnorderedKeyValuePage>empty());
}
Also used : PathSummaryPage(org.sirix.page.PathSummaryPage) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) PathNode(org.sirix.index.path.summary.PathNode) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate)

Example 4 with NodeDelegate

use of org.sirix.node.delegates.NodeDelegate in project sirix by sirixdb.

the class NodeFactoryImpl method createNamespaceNode.

@Override
public NamespaceNode createNamespaceNode(@Nonnegative final long parentKey, final QNm name, @Nonnegative final long pathNodeKey, final Optional<SirixDeweyID> id) throws SirixIOException {
    final long revision = mPageWriteTrx.getRevisionNumber();
    final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
    final int uriKey = mPageWriteTrx.createNameKey(name.getNamespaceURI(), Kind.NAMESPACE);
    final int prefixKey = name.getPrefix() != null && !name.getPrefix().isEmpty() ? mPageWriteTrx.createNameKey(name.getPrefix(), Kind.NAMESPACE) : -1;
    final NameNodeDelegate nameDel = new NameNodeDelegate(nodeDel, uriKey, prefixKey, -1, pathNodeKey);
    return (NamespaceNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new NamespaceNode(nodeDel, nameDel, name), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : NamespaceNode(org.sirix.node.NamespaceNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate)

Example 5 with NodeDelegate

use of org.sirix.node.delegates.NodeDelegate in project sirix by sirixdb.

the class NodeFactoryImpl method createTextNode.

@Override
public TextNode createTextNode(@Nonnegative final long parentKey, @Nonnegative final long leftSibKey, @Nonnegative final long rightSibKey, @Nonnull final byte[] value, final boolean isCompressed, final Optional<SirixDeweyID> id) throws SirixIOException {
    final long revision = mPageWriteTrx.getRevisionNumber();
    final NodeDelegate nodeDel = new NodeDelegate(mPageWriteTrx.getActualRevisionRootPage().getMaxNodeKey() + 1, parentKey, 0, revision, id);
    final boolean compression = isCompressed && value.length > 10;
    final byte[] compressedValue = compression ? Compression.compress(value, Deflater.HUFFMAN_ONLY) : value;
    final ValNodeDelegate valDel = new ValNodeDelegate(nodeDel, compressedValue, compression);
    final StructNodeDelegate structDel = new StructNodeDelegate(nodeDel, Fixed.NULL_NODE_KEY.getStandardProperty(), rightSibKey, leftSibKey, 0, 0);
    return (TextNode) mPageWriteTrx.createEntry(nodeDel.getNodeKey(), new TextNode(valDel, structDel), PageKind.RECORDPAGE, -1, Optional.<UnorderedKeyValuePage>empty());
}
Also used : StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate) TextNode(org.sirix.node.TextNode) UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) NodeDelegate(org.sirix.node.delegates.NodeDelegate) StructNodeDelegate(org.sirix.node.delegates.StructNodeDelegate) NameNodeDelegate(org.sirix.node.delegates.NameNodeDelegate) ValNodeDelegate(org.sirix.node.delegates.ValNodeDelegate)

Aggregations

NodeDelegate (org.sirix.node.delegates.NodeDelegate)16 StructNodeDelegate (org.sirix.node.delegates.StructNodeDelegate)12 NameNodeDelegate (org.sirix.node.delegates.NameNodeDelegate)11 ValNodeDelegate (org.sirix.node.delegates.ValNodeDelegate)9 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataInputStream (java.io.DataInputStream)6 DataOutputStream (java.io.DataOutputStream)6 Test (org.junit.Test)6 QNm (org.brackit.xquery.atomic.QNm)4 SirixDeweyID (org.sirix.node.SirixDeweyID)3 PageContainer (org.sirix.cache.PageContainer)2 DocumentRootNode (org.sirix.node.DocumentRootNode)2 ElementNode (org.sirix.node.ElementNode)2 PathNode (org.sirix.index.path.summary.PathNode)1 AttributeNode (org.sirix.node.AttributeNode)1 CommentNode (org.sirix.node.CommentNode)1 DeletedNode (org.sirix.node.DeletedNode)1 NamespaceNode (org.sirix.node.NamespaceNode)1