szetszwo commented on code in PR #4813:
URL: https://github.com/apache/ozone/pull/4813#discussion_r1214442944
##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/TypedTable.java:
##########
@@ -311,66 +314,86 @@ private void increaseBufferSize(int required) {
}
}
- private VALUE getFromTableCodecBuffer(KEY key) throws IOException {
- try (CodecBuffer inKey = keyCodec.toDirectCodecBuffer(key)) {
- for (; ;) {
- final int allocated = bufferSize.get();
- try (CodecBuffer outValue = CodecBuffer.allocateDirect(allocated)) {
- final Integer required = outValue.putFromSource(
- buffer -> rawTable.get(inKey.asReadOnlyByteBuffer(), buffer));
- if (required == null) {
- // key not found
- return null;
- } else if (required <= allocated) {
- // buffer size is big enough
- return valueCodec.fromCodecBuffer(outValue);
- }
- // buffer size too small, retry
- increaseBufferSize(required);
- }
- }
- }
+ /**
+ * Use {@link RDBTable#get(ByteBuffer, ByteBuffer)}
+ * to get a value mapped to the given key.
+ *
+ * @param key the buffer containing the key.
+ * @param outValue the buffer to write the output value.
+ * When the buffer capacity is smaller than the value size,
+ * partial value may be written.
+ * @return null if the key is not found;
+ * otherwise, return the size of the value.
+ * @throws IOException in case is an error reading from the db.
+ */
+ private Integer getFromTable(CodecBuffer key, CodecBuffer outValue)
+ throws IOException {
+ return outValue.putFromSource(
+ buffer -> rawTable.get(key.asReadOnlyByteBuffer(), buffer));
}
private VALUE getFromTable(KEY key) throws IOException {
if (supportCodecBuffer) {
- return getFromTableCodecBuffer(key);
+ return getFromTable(key, this::getFromTable);
} else {
final byte[] keyBytes = encodeKey(key);
byte[] valueBytes = rawTable.get(keyBytes);
return decodeValue(valueBytes);
}
}
- private Integer getFromTableIfExist(CodecBuffer key,
- CodecBuffer outValue) throws IOException {
+ /**
+ * Similar to {@link #getFromTable(CodecBuffer, CodecBuffer)} except that
+ * this method use {@link RDBTable#getIfExist(ByteBuffer, ByteBuffer)}.
+ */
+ private Integer getFromTableIfExist(CodecBuffer key, CodecBuffer outValue)
+ throws IOException {
return outValue.putFromSource(
buffer -> rawTable.getIfExist(key.asReadOnlyByteBuffer(), buffer));
}
- private VALUE getFromTableIfExistCodecBuffer(KEY key) throws IOException {
+ private VALUE getFromTable(KEY key,
+ CheckedBiFunction<CodecBuffer, CodecBuffer, Integer, IOException> get)
+ throws IOException {
try (CodecBuffer inKey = keyCodec.toDirectCodecBuffer(key)) {
for (; ;) {
- final int allocated = bufferSize.get();
- try (CodecBuffer outValue = CodecBuffer.allocateDirect(allocated)) {
- final Integer required = getFromTableIfExist(inKey, outValue);
+ final Integer required;
+ final int initial = -bufferSize.get();
+ try (CodecBuffer outValue = CodecBuffer.allocateDirect(initial)) {
+ required = get.apply(inKey, outValue);
if (required == null) {
// key not found
return null;
- } else if (required >= 0 && required <= allocated) {
- // buffer size is big enough
- return valueCodec.fromCodecBuffer(outValue);
+ } else if (required < 0) {
+ throw new IllegalStateException("required = " + required + " < 0");
+ }
+
+ for (; ;) {
+ if (required == outValue.readableBytes()) {
+ // buffer size is big enough
+ return valueCodec.fromCodecBuffer(outValue);
+ }
+ // buffer size too small, try increasing the capacity.
+ if (!outValue.setCapacity(required)) {
+ break;
+ }
+
+ // retry with the new capacity
+ outValue.clear();
+ final int retried = getFromTableIfExist(inKey, outValue);
+ Preconditions.assertSame(required.intValue(), retried, "required");
Review Comment:
Your are right that it should be `get.apply`. Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]