smengcl commented on code in PR #4315:
URL: https://github.com/apache/ozone/pull/4315#discussion_r1129956985
##########
hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/db/managed/PipeInputStream.java:
##########
@@ -0,0 +1,81 @@
+package org.apache.hadoop.hdds.utils.db.managed;
+
+import org.apache.hadoop.hdds.utils.NativeLibraryLoader;
+import org.apache.hadoop.hdds.utils.NativeLibraryNotLoadedException;
+import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static
org.apache.hadoop.hdds.utils.NativeConstants.ROCKS_TOOLS_NATIVE_LIBRARY_NAME;
+
+/**
+ * JNI for reading data from pipe.
+ */
+public class PipeInputStream extends InputStream {
+
+ static {
+ NativeLibraryLoader.getInstance()
+ .loadLibrary(ROCKS_TOOLS_NATIVE_LIBRARY_NAME);
+ }
+ private byte[] byteBuffer;
+ private long nativeHandle;
+ private int numberOfBytesLeftToRead;
+ private int index = 0;
+ private int capacity;
+
+ private AtomicBoolean cleanup;
+
+ PipeInputStream(int capacity) throws NativeLibraryNotLoadedException {
+ if (!NativeLibraryLoader.getInstance()
+ .isLibraryLoaded(ROCKS_TOOLS_NATIVE_LIBRARY_NAME)) {
+ throw new NativeLibraryNotLoadedException(
+ ROCKS_TOOLS_NATIVE_LIBRARY_NAME);
+ }
+
+ this.byteBuffer = new byte[capacity];
+ this.numberOfBytesLeftToRead = 0;
+ this.capacity = capacity;
+ this.nativeHandle = newPipe();
+ this.cleanup = new AtomicBoolean(false);
+ }
+
+ long getNativeHandle() {
+ return nativeHandle;
+ }
+
+ @Override
+ public int read() {
+ if (numberOfBytesLeftToRead < 0) {
+ this.close();
+ return -1;
+ }
+ if (numberOfBytesLeftToRead == 0) {
+ numberOfBytesLeftToRead = readInternal(byteBuffer, capacity,
+ nativeHandle);
+ index = 0;
+ return read();
+ }
+ numberOfBytesLeftToRead--;
+ int ret = byteBuffer[index] & 0xFF;
+ index += 1;
+ return ret;
Review Comment:
Got it. I wonder if there a way to read from it more efficiently while
maintaining the correctness? `int` is at least 4 bytes but we are only
utilizing 1 byte.
--
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]