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