@Override public int fromStream(final byte[] content, int offset) { final int size = OIntegerSerializer.INSTANCE.deserializeNative(content, offset); offset += OIntegerSerializer.INT_SIZE; dirtyPages = new HashSet<ODirtyPage>(); for (int i = 0; i < size; i++) { long pageIndex = OLongSerializer.INSTANCE.deserializeNative(content, offset); offset += OLongSerializer.LONG_SIZE; String fileName = stringSerializer.deserializeNativeObject(content, offset); offset += stringSerializer.getObjectSize(fileName); long segment = OLongSerializer.INSTANCE.deserializeNative(content, offset); offset += OLongSerializer.LONG_SIZE; long position = OLongSerializer.INSTANCE.deserializeNative(content, offset); offset += OLongSerializer.LONG_SIZE; dirtyPages.add( new ODirtyPage(fileName, pageIndex, new OLogSequenceNumber(segment, position))); } return offset; }
@Override public int serialize(byte[] stream, int offset) { OByteSerializer.INSTANCE.serializeLiteral(TYPE, stream, offset); OIntegerSerializer.INSTANCE.serializeLiteral( value, stream, offset + OByteSerializer.BYTE_SIZE); return OByteSerializer.BYTE_SIZE + OIntegerSerializer.INT_SIZE; }
@Override public int deserialize(byte[] stream, int offset) { final long fileId = OLongSerializer.INSTANCE.deserializeLiteral(stream, offset); offset += OLongSerializer.LONG_SIZE; final long pageIndex = OLongSerializer.INSTANCE.deserializeLiteral(stream, offset); offset += OLongSerializer.LONG_SIZE; final int pageOffset = OIntegerSerializer.INSTANCE.deserializeLiteral(stream, offset); offset += OIntegerSerializer.INT_SIZE; // Cached bag size. Not used after 1.7.5 offset += OIntegerSerializer.INT_SIZE; if (fileId == -1) collectionPointer = null; else collectionPointer = new OBonsaiCollectionPointer(fileId, new OBonsaiBucketPointer(pageIndex, pageOffset)); this.size = -1; changes.putAll(ChangeSerializationHelper.INSTANCE.deserializeChanges(stream, offset)); return offset; }
public static ONodeId generateUniqueId() { final long clusterPosition = random.nextLong(Long.MAX_VALUE); final int[] chunks = new int[CHUNKS_SIZE]; final byte[] uuid = new byte[16]; secureRandom.nextBytes(uuid); chunks[0] = (int) (clusterPosition >>> 32); chunks[1] = (int) clusterPosition; chunks[2] = OIntegerSerializer.INSTANCE.deserialize(uuid, 0); chunks[3] = OIntegerSerializer.INSTANCE.deserialize(uuid, 4); chunks[4] = OIntegerSerializer.INSTANCE.deserialize(uuid, 8); chunks[5] = OIntegerSerializer.INSTANCE.deserialize(uuid, 12); return new ONodeId(chunks, 1); }
public byte[] chunksToByteArray() { final byte[] bytes = new byte[NODE_SIZE_BYTES]; int pos = 0; for (int i = 0; i < CHUNKS_SIZE; i++) { OIntegerSerializer.INSTANCE.serialize(chunks[i], bytes, pos); pos += OIntegerSerializer.INT_SIZE; } return bytes; }
@Override public int serialize(byte[] stream, int offset, UUID ownerUuid) { for (int i = 0; i < entriesLength; i++) { final Object entry = entries[i]; if (entry instanceof OIdentifiable) { final OIdentifiable identifiable = (OIdentifiable) entry; if (identifiable instanceof ORecord) { final ORecord record = (ORecord) identifiable; if (record.isDirty() || record.getIdentity().isNew()) { record.save(); } } } } if (!deserialized) { System.arraycopy(serializedContent, 0, stream, offset, serializedContent.length); if (contentWasChanged) { OIntegerSerializer.INSTANCE.serialize(size, stream, offset); offset += serializedContent.length; } else { offset += serializedContent.length; return offset; } } else { OIntegerSerializer.INSTANCE.serialize(size, stream, offset); offset += OIntegerSerializer.INT_SIZE; } for (Object entry : entries) { if (entry instanceof OIdentifiable) { OLinkSerializer.INSTANCE.serialize((OIdentifiable) entry, stream, offset); offset += OLinkSerializer.RID_SIZE; } } return offset; }
@Override public int deserialize(byte[] stream, int offset) { final int contentSize = getSerializedSize(stream, offset); this.size = OIntegerSerializer.INSTANCE.deserialize(stream, offset); this.serializedContent = new byte[contentSize]; System.arraycopy(stream, offset, this.serializedContent, 0, contentSize); deserialized = false; return offset + contentSize; }
public Change deserializeChange(final byte[] stream, final int offset) { int value = OIntegerSerializer.INSTANCE.deserializeLiteral( stream, offset + OByteSerializer.BYTE_SIZE); switch (OByteSerializer.INSTANCE.deserializeLiteral(stream, offset)) { case AbsoluteChange.TYPE: return new AbsoluteChange(value); case DiffChange.TYPE: return new DiffChange(value); default: throw new IllegalArgumentException("Change type is incorrect"); } }
public static ONodeId fromStream(byte[] content, int start) { final int[] chunks = new int[CHUNKS_SIZE]; int pos = start; for (int i = 0; i < CHUNKS_SIZE; i++) { chunks[i] = OIntegerSerializer.INSTANCE.deserialize(content, pos); pos += OIntegerSerializer.INT_SIZE; } final int signum = content[pos]; return new ONodeId(chunks, signum); }
public byte[] toStream() { final byte[] bytes = new byte[SERIALIZED_SIZE]; int pos = 0; for (int i = 0; i < CHUNKS_SIZE; i++) { OIntegerSerializer.INSTANCE.serialize(chunks[i], bytes, pos); pos += OIntegerSerializer.INT_SIZE; } bytes[pos] = (byte) signum; return bytes; }
private byte[] createValue(int value, int byteArraySize) { byte[] binaryValue = new byte[byteArraySize]; byte[] intValue = new byte[4]; OIntegerSerializer.INSTANCE.serializeLiteral(value, intValue, 0); int offset = 0; while (offset < byteArraySize) { System.arraycopy( intValue, 0, binaryValue, offset, Math.min(byteArraySize - offset, intValue.length)); offset += intValue.length; } return binaryValue; }
public <K> void serializeChanges( Map<K, Change> changes, OBinarySerializer<K> keySerializer, byte[] stream, int offset) { OIntegerSerializer.INSTANCE.serializeLiteral(changes.size(), stream, offset); offset += OIntegerSerializer.INT_SIZE; for (Map.Entry<K, Change> entry : changes.entrySet()) { K key = entry.getKey(); if (((OIdentifiable) key).getIdentity().isTemporary()) key = ((OIdentifiable) key).getRecord(); keySerializer.serialize(key, stream, offset); offset += keySerializer.getObjectSize(key); offset += entry.getValue().serialize(stream, offset); } }
private void flushPage(long fileId, long pageIndex, ODirectMemoryPointer dataPointer) throws IOException { if (writeAheadLog != null) { OLogSequenceNumber lsn = ODurablePage.getLogSequenceNumberFromPage(dataPointer); OLogSequenceNumber flushedLSN = writeAheadLog.getFlushedLSN(); if (flushedLSN == null || flushedLSN.compareTo(lsn) < 0) writeAheadLog.flush(); } final byte[] content = dataPointer.get(0, pageSize); OLongSerializer.INSTANCE.serializeNative(MAGIC_NUMBER, content, 0); final int crc32 = calculatePageCrc(content); OIntegerSerializer.INSTANCE.serializeNative(crc32, content, OLongSerializer.LONG_SIZE); final OFileClassic fileClassic = files.get(fileId); fileClassic.write(pageIndex * pageSize, content); if (syncOnPageFlush) fileClassic.synch(); }
public Map<OIdentifiable, Change> deserializeChanges(final byte[] stream, int offset) { final int count = OIntegerSerializer.INSTANCE.deserializeLiteral(stream, offset); offset += OIntegerSerializer.INT_SIZE; final HashMap<OIdentifiable, Change> res = new HashMap<OIdentifiable, Change>(); for (int i = 0; i < count; i++) { ORecordId rid = OLinkSerializer.INSTANCE.deserialize(stream, offset); offset += OLinkSerializer.RID_SIZE; Change change = ChangeSerializationHelper.INSTANCE.deserializeChange(stream, offset); offset += Change.SIZE; final OIdentifiable identifiable; if (rid.isTemporary() && rid.getRecord() != null) identifiable = rid.getRecord(); else identifiable = rid; res.put(identifiable, change); } return res; }
private void doDeserialization() { if (deserialized) return; int offset = 0; int entriesSize = OIntegerSerializer.INSTANCE.deserialize(serializedContent, offset); offset += OIntegerSerializer.INT_SIZE; for (int i = 0; i < entriesSize; i++) { ORID rid = OLinkSerializer.INSTANCE.deserialize(serializedContent, offset); offset += OLinkSerializer.RID_SIZE; OIdentifiable identifiable; if (rid.isTemporary()) identifiable = rid.getRecord(); else identifiable = rid; addEntry(identifiable); } deserialized = true; }
@Override public int toStream(final byte[] content, int offset) { OIntegerSerializer.INSTANCE.serializeNative(dirtyPages.size(), content, offset); offset += OIntegerSerializer.INT_SIZE; for (ODirtyPage dirtyPage : dirtyPages) { OLongSerializer.INSTANCE.serializeNative(dirtyPage.getPageIndex(), content, offset); offset += OLongSerializer.LONG_SIZE; stringSerializer.serializeNativeObject(dirtyPage.getFileName(), content, offset); offset += stringSerializer.getObjectSize(dirtyPage.getFileName()); OLongSerializer.INSTANCE.serializeNative(dirtyPage.getLsn().getSegment(), content, offset); offset += OLongSerializer.LONG_SIZE; OLongSerializer.INSTANCE.serializeNative(dirtyPage.getLsn().getPosition(), content, offset); offset += OLongSerializer.LONG_SIZE; } return offset; }
@Override public int getSerializedSize(byte[] stream, int offset) { return OIntegerSerializer.INSTANCE.deserialize(stream, offset) * OLinkSerializer.RID_SIZE + OIntegerSerializer.INT_SIZE; }
@Override public int serialize(byte[] stream, int offset, UUID ownerUuid) { for (Map.Entry<OIdentifiable, OModifiableInteger> entry : newEntries.entrySet()) { OIdentifiable identifiable = entry.getKey(); assert identifiable instanceof ORecord; Change c = changes.get(identifiable); final int delta = entry.getValue().intValue(); if (c == null) changes.put(identifiable, new DiffChange(delta)); else c.applyDiff(delta); } newEntries.clear(); final ORecordSerializationContext context; boolean remoteMode = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy; if (remoteMode) { context = null; } else context = ORecordSerializationContext.getContext(); // make sure that we really save underlying record. if (collectionPointer == null) { if (context != null) { final int clusterId = getHighLevelDocClusterId(); assert clusterId > -1; collectionPointer = ODatabaseRecordThreadLocal.INSTANCE .get() .getSbTreeCollectionManager() .createSBTree(clusterId, ownerUuid); } } OBonsaiCollectionPointer collectionPointer; if (this.collectionPointer != null) collectionPointer = this.collectionPointer; else { collectionPointer = OBonsaiCollectionPointer.INVALID; } OLongSerializer.INSTANCE.serializeLiteral(collectionPointer.getFileId(), stream, offset); offset += OLongSerializer.LONG_SIZE; OBonsaiBucketPointer rootPointer = collectionPointer.getRootPointer(); OLongSerializer.INSTANCE.serializeLiteral(rootPointer.getPageIndex(), stream, offset); offset += OLongSerializer.LONG_SIZE; OIntegerSerializer.INSTANCE.serializeLiteral(rootPointer.getPageOffset(), stream, offset); offset += OIntegerSerializer.INT_SIZE; // Keep this section for binary compatibility with versions older then 1.7.5 OIntegerSerializer.INSTANCE.serializeLiteral(size, stream, offset); offset += OIntegerSerializer.INT_SIZE; if (context == null) { ChangeSerializationHelper.INSTANCE.serializeChanges( changes, OLinkSerializer.INSTANCE, stream, offset); } else { context.push(new ORidBagUpdateSerializationOperation(changes, collectionPointer)); // 0-length serialized list of changes OIntegerSerializer.INSTANCE.serializeLiteral(0, stream, offset); offset += OIntegerSerializer.INT_SIZE; } return offset; }
public OPageDataVerificationError[] checkStoredPages( OCommandOutputListener commandOutputListener) { final int notificationTimeOut = 5000; final List<OPageDataVerificationError> errors = new ArrayList<OPageDataVerificationError>(); synchronized (syncObject) { for (long fileId : files.keySet()) { OFileClassic fileClassic = files.get(fileId); boolean fileIsCorrect; try { if (commandOutputListener != null) commandOutputListener.onMessage("Flashing file " + fileClassic.getName() + "... "); flush(fileId); if (commandOutputListener != null) commandOutputListener.onMessage( "Start verification of content of " + fileClassic.getName() + "file ..."); long time = System.currentTimeMillis(); long filledUpTo = fileClassic.getFilledUpTo(); fileIsCorrect = true; for (long pos = 0; pos < filledUpTo; pos += pageSize) { boolean checkSumIncorrect = false; boolean magicNumberIncorrect = false; byte[] data = new byte[pageSize]; fileClassic.read(pos, data, data.length); long magicNumber = OLongSerializer.INSTANCE.deserializeNative(data, 0); if (magicNumber != MAGIC_NUMBER) { magicNumberIncorrect = true; if (commandOutputListener != null) commandOutputListener.onMessage( "Error: Magic number for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " does not much !!!"); fileIsCorrect = false; } final int storedCRC32 = OIntegerSerializer.INSTANCE.deserializeNative(data, OLongSerializer.LONG_SIZE); final int calculatedCRC32 = calculatePageCrc(data); if (storedCRC32 != calculatedCRC32) { checkSumIncorrect = true; if (commandOutputListener != null) commandOutputListener.onMessage( "Error: Checksum for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " is incorrect !!!"); fileIsCorrect = false; } if (magicNumberIncorrect || checkSumIncorrect) errors.add( new OPageDataVerificationError( magicNumberIncorrect, checkSumIncorrect, pos / pageSize, fileClassic.getName())); if (commandOutputListener != null && System.currentTimeMillis() - time > notificationTimeOut) { time = notificationTimeOut; commandOutputListener.onMessage((pos / pageSize) + " pages were processed ..."); } } } catch (IOException ioe) { if (commandOutputListener != null) commandOutputListener.onMessage( "Error: Error during processing of file " + fileClassic.getName() + ". " + ioe.getMessage()); fileIsCorrect = false; } if (!fileIsCorrect) { if (commandOutputListener != null) commandOutputListener.onMessage( "Verification of file " + fileClassic.getName() + " is finished with errors."); } else { if (commandOutputListener != null) commandOutputListener.onMessage( "Verification of file " + fileClassic.getName() + " is successfully finished."); } } return errors.toArray(new OPageDataVerificationError[errors.size()]); } }