/** * Read a list of objects. * * @param buff the target buffer * @param obj the objects * @param len the number of objects to read */ default void read(ByteBuffer buff, Object[] obj, int len) { for (int i = 0; i < len; i++) { obj[i] = read(buff); } }
buff = expandPage(buff, type, start, pageLength); map.getKeyType().read(buff, keys, keyLength); values = new Object[keyLength]; StorageDataType valueType = map.getValueType();
buff = expandPage(buff, type, start, pageLength); map.getKeyType().read(buff, keys, keyLength); values = new Object[keyLength]; StorageDataType valueType = map.getValueType();
public static Object readValue(ByteBuffer buff, StorageDataType valueType) { Object value = null; if (buff.get() == 1) { value = valueType.read(buff); } return value; }
oldValue = (TransactionalValue) oldValueType.read(buff);
@Override public void readColumn(ByteBuffer buff, Object obj, int columnIndex) { VersionedValue v = (VersionedValue) obj; Value[] columns = v.value.getList(); if (columnIndex >= 0 && columnIndex < columns.length) { Value value = (Value) valueType.read(buff); columns[columnIndex] = value; } }
@Deprecated void transferFrom(ReadableByteChannel src, long position, long count) throws IOException { ByteBuffer buff = ByteBuffer.allocateDirect((int) count); src.read(buff); buff.position((int) position); int pos = 0; while (buff.remaining() > 0) { int pageLength = buff.getInt(); Object firstKey = map.getKeyType().read(buff); System.out.println("pageLength=" + pageLength + ", firstKey=" + firstKey); pos += pageLength; buff.position(pos); } }
public void transferFrom(ReadableByteChannel src, long position, long count) throws IOException { ByteBuffer buff = ByteBuffer.allocateDirect((int) count); src.read(buff); buff.position((int) position); while (buff.remaining() > 0) { Object key = keyType.read(buff); boolean first = buff.get() == 1; PageKey pk = new PageKey(key, first); addLeafPage(pk, buff, true, true); // int pageLength = buff.getInt(); // pos += pageLength; // buff.position(pos); } }
@Override public Object read(ByteBuffer buff) { int vertion = DataUtils.readVarInt(buff); ValueArray value = (ValueArray) valueType.read(buff); return new VersionedValue(vertion, value); }
@SuppressWarnings("unchecked") public static <K> void redo(StorageMap<K, TransactionalValue> map, List<ByteBuffer> pendingKeyValues) { if (pendingKeyValues != null && !pendingKeyValues.isEmpty()) { StorageDataType kt = map.getKeyType(); StorageDataType vt = ((TransactionalValueType) map.getValueType()).valueType; for (ByteBuffer kv : pendingKeyValues) { K key = (K) kt.read(kv); if (kv.get() == 0) map.remove(key); else { Object value = vt.read(kv); map.put(key, TransactionalValue.createCommitted(value)); } } } } }
static BTreeLeafPage readLeafPage(BTreeMap<?, ?> map, ByteBuffer page) { List<String> replicationHostIds = readReplicationHostIds(page); boolean remote = page.get() == 1; BTreeLeafPage p; if (remote) { p = BTreeLeafPage.createEmpty(map); } else { StorageDataType kt = map.getKeyType(); StorageDataType vt = map.getValueType(); int length = page.getInt(); Object[] keys = new Object[length]; Object[] values = new Object[length]; for (int i = 0; i < length; i++) { keys[i] = kt.read(page); values[i] = vt.read(page); } p = BTreeLeafPage.create(map, keys, values, length, 0); } p.replicationHostIds = replicationHostIds; return p; }
@Override public Object executeGet(String mapName, ByteBuffer key) { StorageMap<Object, Object> map = session.getStorageMap(mapName); Object result = map.get(map.getKeyType().read(key)); return result; }
void readRowStorage(ByteBuffer buff, int chunkId, int offset, int maxLength, boolean disableCheck) { int start = buff.position(); int pageLength = buff.getInt(); checkPageLength(chunkId, pageLength, maxLength); buff.get(); // mode int oldLimit = buff.limit(); buff.limit(start + pageLength); readCheckValue(buff, chunkId, offset, pageLength, disableCheck); int keyLength = DataUtils.readVarInt(buff); keys = new Object[keyLength]; int type = buff.get(); ByteBuffer oldBuff = buff; buff = expandPage(buff, type, start, pageLength); map.getKeyType().read(buff, keys, keyLength); values = new Object[keyLength]; map.getValueType().read(buff, values, keyLength); totalCount = keyLength; replicationHostIds = readReplicationHostIds(buff); recalculateMemory(); oldBuff.limit(oldLimit); }
public static TransactionalValue read(ByteBuffer buff, StorageDataType valueType, StorageDataType oldValueType) { long tid = DataUtils.readVarLong(buff); if (tid == 0) { Object value = readValue(buff, valueType); return createCommitted(value); } else { return Uncommitted.read(tid, valueType, buff, oldValueType); } }
@Override public Object executePut(String replicationName, String mapName, ByteBuffer key, ByteBuffer value, boolean raw) { session.setReplicationName(replicationName); StorageMap<Object, Object> map = session.getStorageMap(mapName); if (raw) { map = map.getRawMap(); } Object result = map.put(map.getKeyType().read(key), map.getValueType().read(value)); if (result == null) return null; try (DataBuffer b = DataBuffer.create()) { ByteBuffer valueBuffer = b.write(map.getValueType(), result); return valueBuffer.array(); } }
@Override public Object replicationGet(Session session, Object key) { List<NetEndpoint> replicationEndpoints = getReplicationEndpoints(key); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); try (DataBuffer k = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); byte[] value = (byte[]) c.executeGet(getName(), keyBuffer); if (value == null) return null; return valueType.read(ByteBuffer.wrap(value)); } }
buff = expandPage(buff, type, start, pageLength); map.getKeyType().read(buff, keys, keyLength); setChildrenPageKeys(); recalculateMemory();
private Object putRemote(BTreePage p, Object key, Object value) { if (p.getLeafPageMovePlan().moverHostId.equals(getLocalHostId())) { int size = p.getLeafPageMovePlan().replicationEndpoints.size(); List<NetEndpoint> replicationEndpoints = new ArrayList<>(size); replicationEndpoints.addAll(p.getLeafPageMovePlan().replicationEndpoints); boolean containsLocalEndpoint = replicationEndpoints.remove(getLocalEndpoint()); Object returnValue = null; ReplicationSession rs = db.createReplicationSession(db.createInternalSession(), replicationEndpoints); try (DataBuffer k = DataBuffer.create(); DataBuffer v = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); ByteBuffer valueBuffer = v.write(valueType, value); byte[] oldValue = (byte[]) c.executePut(null, getName(), keyBuffer, valueBuffer, true); if (oldValue != null) { returnValue = valueType.read(ByteBuffer.wrap(oldValue)); } } // 如果新的复制节点中还包含本地节点,那么还需要put到本地节点中 if (containsLocalEndpoint) { return putLocal(p, key, value); } else { return returnValue; } } else { return null; // 不是由当前节点移动的,那么put操作就可以忽略了 } }
@Override public Object replicationPut(Session session, Object key, Object value, StorageDataType valueType) { List<NetEndpoint> replicationEndpoints = getReplicationEndpoints(key); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); try (DataBuffer k = DataBuffer.create(); DataBuffer v = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); ByteBuffer valueBuffer = v.write(valueType, value); byte[] oldValue = (byte[]) c.executePut(null, getName(), keyBuffer, valueBuffer, false); if (oldValue == null) return null; return valueType.read(ByteBuffer.wrap(oldValue)); } }
@Override public Object executeAppend(String replicationName, String mapName, ByteBuffer value, CommandUpdateResult commandUpdateResult) { session.setReplicationName(replicationName); StorageMap<Object, Object> map = session.getStorageMap(mapName); Object result = map.append(map.getValueType().read(value)); commandUpdateResult.addResult(this, ((ValueLong) result).getLong()); Transaction parentTransaction = session.getParentTransaction(); if (parentTransaction != null && !parentTransaction.isAutoCommit()) { parentTransaction.addLocalTransactionNames(session.getTransaction().getLocalTransactionNames()); } return result; }