Search in sources :

Example 1 with NodePersistenter

use of org.sirix.node.interfaces.NodePersistenter 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 NodePersistenter

use of org.sirix.node.interfaces.NodePersistenter in project sirix by sirixdb.

the class UnorderedKeyValuePage method addReferences.

// Add references to OverflowPages.
private void addReferences() throws IOException {
    final boolean storeDeweyIDs = mPageReadTrx.getResourceManager().getResourceConfig().mDeweyIDsStored;
    final List<Entry<Long, Record>> entries = sort();
    final Iterator<Entry<Long, Record>> it = entries.iterator();
    while (it.hasNext()) {
        final Entry<Long, Record> entry = it.next();
        final Record record = entry.getValue();
        final long recordID = record.getNodeKey();
        if (mSlots.get(recordID) == null) {
            // Must be either a normal record or one which requires an
            // Overflow page.
            final ByteArrayOutputStream output = new ByteArrayOutputStream();
            final DataOutput out = new DataOutputStream(output);
            mPersistenter.serialize(out, record, mPageReadTrx);
            final byte[] data = output.toByteArray();
            if (data.length > PageConstants.MAX_RECORD_SIZE) {
                final PageReference reference = new PageReference();
                reference.setPage(new OverflowPage(data));
                mReferences.put(recordID, reference);
            } else {
                if (storeDeweyIDs && mPersistenter instanceof NodePersistenter && record instanceof Node && ((Node) record).getDeweyID().isPresent() && record.getNodeKey() != 0)
                    mDeweyIDs.put(((Node) record).getDeweyID().get(), record.getNodeKey());
                mSlots.put(recordID, data);
            }
        }
    }
    mAddedReferences = true;
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) Node(org.sirix.node.interfaces.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NodePersistenter(org.sirix.node.interfaces.NodePersistenter) Entry(java.util.Map.Entry) Utils.getVarLong(org.sirix.node.Utils.getVarLong) Utils.putVarLong(org.sirix.node.Utils.putVarLong) Record(org.sirix.node.interfaces.Record)

Aggregations

Utils.getVarLong (org.sirix.node.Utils.getVarLong)2 Utils.putVarLong (org.sirix.node.Utils.putVarLong)2 NodePersistenter (org.sirix.node.interfaces.NodePersistenter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 SirixDeweyID (org.sirix.node.SirixDeweyID)1 Node (org.sirix.node.interfaces.Node)1 Record (org.sirix.node.interfaces.Record)1