public static int calculateDataSize(Data data) { return addByteArrayLengthHeader(data.totalSize()); }
public static int calculateDataSize(Data data) { return addByteArrayLengthHeader(data.totalSize()); }
@Override public void storeAll(Map<Long, Data> map) { if (!enabled) { return; } final Map<Long, Object> objectMap = createHashMap(map.size()); if (binary) { // WARNING: we can't pass original Data to the user // TODO: @mm - is there really an advantage of using binary storeAll? // since we need to do array copy for each item. for (Map.Entry<Long, Data> entry : map.entrySet()) { Data value = entry.getValue(); byte[] copy = Arrays.copyOf(value.toByteArray(), value.totalSize()); objectMap.put(entry.getKey(), copy); } } else { for (Map.Entry<Long, Data> entry : map.entrySet()) { objectMap.put(entry.getKey(), serializationService.toObject(entry.getValue())); } } store.storeAll(objectMap); }
@Override public void storeAll(Map<Long, Data> map) { if (!enabled) { return; } final Map<Long, Object> objectMap = createHashMap(map.size()); if (binary) { // WARNING: we can't pass original Data to the user // TODO: @mm - is there really an advantage of using binary storeAll? // since we need to do array copy for each item. for (Map.Entry<Long, Data> entry : map.entrySet()) { Data value = entry.getValue(); byte[] copy = Arrays.copyOf(value.toByteArray(), value.totalSize()); objectMap.put(entry.getKey(), copy); } } else { for (Map.Entry<Long, Data> entry : map.entrySet()) { objectMap.put(entry.getKey(), serializationService.toObject(entry.getValue())); } } store.storeAll(objectMap); }
@Override public void store(Long key, Data value) { if (!enabled) { return; } final Object actualValue; if (binary) { // WARNING: we can't pass original Data to the user actualValue = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { actualValue = serializationService.toObject(value); } store.store(key, actualValue); }
@Override public void store(Long key, Data value) { if (!enabled) { return; } final Object actualValue; if (binary) { // WARNING: we can't pass original Data to the user actualValue = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { actualValue = serializationService.toObject(value); } store.store(key, actualValue); }
private void writeKeySet(FileOutputStream fos, FileChannel outChannel, Iterator<K> iterator) throws IOException { while (iterator.hasNext()) { K key = iterator.next(); Data dataKey = serializationService.toData(key); if (dataKey != null) { int dataSize = dataKey.totalSize(); writeInt(fos, dataSize); int position = 0; int remaining = dataSize; while (remaining > 0) { int transferredCount = Math.min(BUFFER_SIZE - buf.position(), remaining); ensureBufHasRoom(fos, transferredCount); buf.put(dataKey.toByteArray(), position, transferredCount); position += transferredCount; remaining -= transferredCount; } lastWrittenBytes += INT_SIZE_IN_BYTES + dataSize; lastKeyCount++; } flushLocalBuffer(outChannel); } }
public MessageFlyweight set(Data data) { int length = data.totalSize(); set(length); data.copyTo(buffer.byteArray(), index); index += length; return this; }
public MessageFlyweight set(Data data) { int length = data.totalSize(); set(length); data.copyTo(buffer.byteArray(), index); index += length; return this; }
@Override public void writeData(Data data) throws IOException { int len = data == null ? NULL_ARRAY_LENGTH : data.totalSize(); writeInt(len); if (len > 0) { ensureAvailable(len); data.copyTo(buffer, pos); pos += len; } }
@Override public void writeData(Data data) throws IOException { int len = data == null ? NULL_ARRAY_LENGTH : data.totalSize(); writeInt(len); if (len > 0) { ensureAvailable(len); data.copyTo(buffer, pos); pos += len; } }
private void writeKeySet(FileOutputStream fos, FileChannel outChannel, Iterator<K> iterator) throws IOException { while (iterator.hasNext()) { K key = iterator.next(); Data dataKey = serializationService.toData(key); if (dataKey != null) { int dataSize = dataKey.totalSize(); writeInt(fos, dataSize); int position = 0; int remaining = dataSize; while (remaining > 0) { int transferredCount = Math.min(BUFFER_SIZE - buf.position(), remaining); ensureBufHasRoom(fos, transferredCount); buf.put(dataKey.toByteArray(), position, transferredCount); position += transferredCount; remaining -= transferredCount; } lastWrittenBytes += INT_SIZE_IN_BYTES + dataSize; lastKeyCount++; } flushLocalBuffer(outChannel); } }
@Override @SuppressWarnings("unchecked") public void store(long sequence, Data value) { final Object actualValue; if (isBinaryFormat()) { // WARNING: we can't pass original byte array to the user actualValue = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { // here we deserialize the object again (the first time is in the actual ring buffer). // if we are certain that the user cannot abuse the object, we can provide him with a reference to the // stored value actualValue = serializationService.toObject(value); } store.store(sequence, actualValue); }
@Override @SuppressWarnings("unchecked") public void storeAll(long firstItemSequence, Data[] items) { final Object[] storedItems = new Object[items.length]; for (int i = 0; i < items.length; i++) { final Data value = items[i]; if (isBinaryFormat()) { // more defensive copying in the case of binary format storedItems[i] = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { // in the case of object format we have again deserialization. We could possibly pass the user the reference // to the item in the RB store if it is safe storedItems[i] = serializationService.toObject(value); } } store.storeAll(firstItemSequence, storedItems); }
@Override @SuppressWarnings("unchecked") public void store(long sequence, Data value) { final Object actualValue; if (isBinaryFormat()) { // WARNING: we can't pass original byte array to the user actualValue = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { // here we deserialize the object again (the first time is in the actual ring buffer). // if we are certain that the user cannot abuse the object, we can provide him with a reference to the // stored value actualValue = serializationService.toObject(value); } store.store(sequence, actualValue); }
@Override @SuppressWarnings("unchecked") public void storeAll(long firstItemSequence, Data[] items) { final Object[] storedItems = new Object[items.length]; for (int i = 0; i < items.length; i++) { final Data value = items[i]; if (isBinaryFormat()) { // more defensive copying in the case of binary format storedItems[i] = Arrays.copyOf(value.toByteArray(), value.totalSize()); } else { // in the case of object format we have again deserialization. We could possibly pass the user the reference // to the item in the RB store if it is safe storedItems[i] = serializationService.toObject(value); } } store.storeAll(firstItemSequence, storedItems); }
Packet toNormalResponsePacket(long callId, int backupAcks, boolean urgent, Object value) { byte[] bytes; boolean isData = value instanceof Data; if (isData) { Data data = (Data) value; int dataLengthInBytes = data.totalSize(); bytes = new byte[OFFSET_DATA_PAYLOAD + dataLengthInBytes]; writeInt(bytes, OFFSET_DATA_LENGTH, dataLengthInBytes, useBigEndian); // this is a crucial part. If data is NativeMemoryData, instead of calling Data.toByteArray which causes a // byte-array to be created and a intermediate copy of the data, we immediately copy the NativeMemoryData // into the bytes for the packet. data.copyTo(bytes, OFFSET_DATA_PAYLOAD); } else if (value == null) { // since there are many 'null' responses we optimize this case as well. bytes = new byte[OFFSET_NOT_DATA + INT_SIZE_IN_BYTES]; writeInt(bytes, OFFSET_NOT_DATA, CONSTANT_TYPE_NULL, useBigEndian); } else { // for regular object we currently can't guess how big the bytes will be; so we just hand it // over to the serializationService to deal with it. The negative part is that this does lead to // an intermediate copy of the data. bytes = serializationService.toBytes(value, OFFSET_NOT_DATA, false); } writeResponsePrologueBytes(bytes, NORMAL_RESPONSE, callId, urgent); // backup-acks (will fit in a byte) bytes[OFFSET_BACKUP_ACKS] = (byte) backupAcks; // isData bytes[OFFSET_IS_DATA] = (byte) (isData ? 1 : 0); //the remaining part of the byte array is already filled, so we are done. return newResponsePacket(bytes, urgent); }
Packet toNormalResponsePacket(long callId, int backupAcks, boolean urgent, Object value) { byte[] bytes; boolean isData = value instanceof Data; if (isData) { Data data = (Data) value; int dataLengthInBytes = data.totalSize(); bytes = new byte[OFFSET_DATA_PAYLOAD + dataLengthInBytes]; writeInt(bytes, OFFSET_DATA_LENGTH, dataLengthInBytes, useBigEndian); // this is a crucial part. If data is NativeMemoryData, instead of calling Data.toByteArray which causes a // byte-array to be created and a intermediate copy of the data, we immediately copy the NativeMemoryData // into the bytes for the packet. data.copyTo(bytes, OFFSET_DATA_PAYLOAD); } else if (value == null) { // since there are many 'null' responses we optimize this case as well. bytes = new byte[OFFSET_NOT_DATA + INT_SIZE_IN_BYTES]; writeInt(bytes, OFFSET_NOT_DATA, CONSTANT_TYPE_NULL, useBigEndian); } else { // for regular object we currently can't guess how big the bytes will be; so we just hand it // over to the serializationService to deal with it. The negative part is that this does lead to // an intermediate copy of the data. bytes = serializationService.toBytes(value, OFFSET_NOT_DATA, false); } writeResponsePrologueBytes(bytes, NORMAL_RESPONSE, callId, urgent); // backup-acks (will fit in a byte) bytes[OFFSET_BACKUP_ACKS] = (byte) backupAcks; // isData bytes[OFFSET_IS_DATA] = (byte) (isData ? 1 : 0); //the remaining part of the byte array is already filled, so we are done. return newResponsePacket(bytes, urgent); }