/** * Write a list of objects. * * @param buff the target buffer * @param obj the objects * @param len the number of objects to write */ default void write(DataBuffer buff, Object[] obj, int len) { for (int i = 0; i < len; i++) { write(buff, obj[i]); } }
map.getKeyType().write(buff, keys, keyLength);
map.getKeyType().write(buff, keys, keyLength); for (int row = 0; row < keyLength; row++) { valueType.writeMeta(buff, values[row]);
default void writeColumn(DataBuffer buff, Object obj, int columnIndex) { write(buff, obj); }
@Override public void write(DataBuffer buff, Object obj) { VersionedValue v = (VersionedValue) obj; buff.putVarInt(v.vertion); valueType.write(buff, v.value); }
public ByteBuffer write(StorageDataType type, Object obj) { type.write(this, obj); ByteBuffer buff = getAndFlipBuffer(); ByteBuffer v = ByteBuffer.allocateDirect(buff.limit()); v.put(buff); v.flip(); return v; }
public void writeValue(DataBuffer buff, StorageDataType valueType) { if (value == null) { buff.put((byte) 0); } else { buff.put((byte) 1); valueType.write(buff, value); } }
@Override void writeLeaf(DataBuffer buff, boolean remote) { buff.put((byte) PageUtils.PAGE_TYPE_LEAF); writeReplicationHostIds(replicationHostIds, buff); buff.put((byte) (remote ? 1 : 0)); if (!remote) { StorageDataType kt = map.getKeyType(); StorageDataType vt = map.getValueType(); buff.putInt(keys.length); for (int i = 0, len = keys.length; i < len; i++) { kt.write(buff, keys[i]); vt.write(buff, values[i]); } } }
@Override public void transferTo(WritableByteChannel target, K firstKey, K lastKey) throws IOException { ArrayList<PageKey> pageKeys = new ArrayList<>(); getEndpointToPageKeyMap(null, firstKey, lastKey, pageKeys); int size = pageKeys.size(); for (int i = 0; i < size; i++) { PageKey pk = pageKeys.get(i); long pos = pk.pos; try (DataBuffer buff = DataBuffer.create()) { keyType.write(buff, pk.key); buff.put((byte) (pk.first ? 1 : 0)); ByteBuffer pageBuff = null; if (pos > 0) { pageBuff = readPageBuff(pos); int start = pageBuff.position(); int pageLength = pageBuff.getInt(); pageBuff.position(start); pageBuff.limit(start + pageLength); } else { BTreePage p = root.binarySearchLeafPage(pk.key); p.replicatePage(buff, null); } ByteBuffer dataBuff = buff.getAndFlipBuffer(); target.write(dataBuff); if (pageBuff != null) target.write(pageBuff); } } }
@Override public void write(DataBuffer buff, Object obj) { StorageDataType t = getType(obj); if (t != this) { t.write(buff, obj); return; } byte[] data = serialize(obj); // we say they are larger, because these objects // use quite a lot of disk space int size = data.length * 2; // adjust the average size // using an exponential moving average averageSize = (size + 15 * averageSize) / 16; buff.put((byte) TYPE_SERIALIZED_OBJECT).putVarInt(data.length).put(data); }
public void writeForRedo(DataBuffer writeBuffer, MVCCTransactionEngine transactionEngine) { StorageMap<?, ?> map = transactionEngine.getMap(mapName); // 有可能在执行DROP DATABASE时删除了 if (map == null) { return; } int lastPosition = writeBuffer.position(); ValueString.type.write(writeBuffer, mapName); int keyValueLengthStartPos = writeBuffer.position(); writeBuffer.putInt(0); map.getKeyType().write(writeBuffer, key); if (newValue.value == null) writeBuffer.put((byte) 0); else { writeBuffer.put((byte) 1); ((TransactionalValueType) map.getValueType()).valueType.write(writeBuffer, newValue.value); } writeBuffer.putInt(keyValueLengthStartPos, writeBuffer.position() - keyValueLengthStartPos - 4); // 预估一下内存占用大小,当到达一个阈值时方便其他服务线程刷数据到硬盘 int memory = writeBuffer.position() - lastPosition; transactionEngine.incrementEstimatedMemory(mapName, memory); }
@Override public void writeMeta(DataBuffer buff) { buff.putVarLong(tid); buff.putVarInt(logId); if (rowLock) { buff.put((byte) 0); } else { buff.put((byte) 1); byte[] bytes = lockedColumns.toByteArray(); int len = bytes.length; buff.putVarInt(len); for (int i = 0; i < len; i++) { buff.put(bytes[i]); } } if (oldValue == null) { buff.put((byte) 0); } else { buff.put((byte) 1); oldValueType.write(buff, oldValue); } ValueString.type.write(buff, hostAndPort); ValueString.type.write(buff, globalReplicationName); buff.putVarLong(version); }
map.getKeyType().write(buff, keys, keyLength);