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());
}
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;
}
}
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();
}
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();
}
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();
}
Aggregations