swamirishi commented on code in PR #4315:
URL: https://github.com/apache/ozone/pull/4315#discussion_r1132153666


##########
hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/db/managed/ManagedSSTDumpIterator.java:
##########
@@ -0,0 +1,221 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership.  The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+
+package org.apache.hadoop.hdds.utils.db.managed;
+
+import org.apache.hadoop.hdds.utils.NativeLibraryNotLoadedException;
+import org.eclipse.jetty.io.RuntimeIOException;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Iterator to Parse output of RocksDBSSTDumpTool.
+ */
+public class ManagedSSTDumpIterator implements
+        Iterator<ManagedSSTDumpIterator.KeyValue>, AutoCloseable {
+  private static final String SST_DUMP_TOOL_CLASS =
+          "org.apache.hadoop.hdds.utils.db.managed.ManagedSSTDumpTool";
+  private static final String PATTERN_REGEX =
+          "'([^=>]+)' seq:([0-9]+), type:([0-9]+) => ";
+
+  public static final int PATTERN_KEY_GROUP_NUMBER = 1;
+  public static final int PATTERN_SEQ_GROUP_NUMBER = 2;
+  public static final int PATTERN_TYPE_GROUP_NUMBER = 3;
+  private static final Pattern PATTERN_MATCHER =
+          Pattern.compile(PATTERN_REGEX);
+  private BufferedReader processOutput;
+  private StringBuilder stdoutString;
+
+  private Matcher currentMatcher;
+  private int prevMatchEndIndex;
+  private KeyValue currentKey;
+  private char[] charBuffer;
+  private KeyValue nextKey;
+
+  private ManagedSSTDumpTool.SSTDumpToolTask sstDumpToolTask;
+  private AtomicBoolean open;
+
+
+  public ManagedSSTDumpIterator(ManagedSSTDumpTool sstDumpTool,
+                                String sstFilePath,
+                                ManagedOptions options) throws IOException,
+          NativeLibraryNotLoadedException {
+    File sstFile = new File(sstFilePath);
+    if (!sstFile.exists() || !sstFile.isFile()) {
+      throw new IOException(String.format("Invalid SST File Path : %s",
+              sstFile.getAbsolutePath()));
+    }
+    init(sstDumpTool, sstFile, options);
+  }
+
+  private void init(ManagedSSTDumpTool sstDumpTool, File sstFile,
+                    ManagedOptions options)
+          throws NativeLibraryNotLoadedException {
+    String[] args = {"--file=" + sstFile.getAbsolutePath(),
+                     "--command=scan"};
+    this.sstDumpToolTask = sstDumpTool.run(args, options);
+    processOutput = new BufferedReader(new InputStreamReader(
+            sstDumpToolTask.getPipedOutput(), StandardCharsets.UTF_8));
+    stdoutString = new StringBuilder();
+    currentMatcher = PATTERN_MATCHER.matcher(stdoutString);
+    charBuffer = new char[8192];
+    open = new AtomicBoolean(true);
+    next();
+  }
+
+  /**
+   * Throws Runtime exception in the case iterator is closed or
+   * the native Dumptool exited with non zero exit value.
+   */
+  private void checkSanityOfProcess() {
+    if (!this.open.get()) {
+      throw new RuntimeException("Iterator has been closed");
+    }
+    if (sstDumpToolTask.getFuture().isDone()
+            && sstDumpToolTask.exitValue() != 0) {
+      throw new RuntimeException("Process Terminated with non zero " +
+              String.format("exit value %d", sstDumpToolTask.exitValue()));
+    }
+  }
+
+  /**
+   * Checks the status of the process & sees if there is another record.
+   * @return True if next exists & false otherwise
+   * Throws Runtime Exception in case of SST File read failure
+   */
+
+  @Override
+  public boolean hasNext() {
+    checkSanityOfProcess();
+    return nextKey != null;
+  }
+
+  /**
+   * Returns the next record from SSTDumpTool.
+   * @return next Key
+   * Throws Runtime Exception incase of failure.
+   */
+  @Override
+  public KeyValue next() {
+    checkSanityOfProcess();
+    currentKey = nextKey;
+    nextKey = null;
+    while (!currentMatcher.find()) {
+      try {
+        if (prevMatchEndIndex != 0) {
+          stdoutString = new StringBuilder(stdoutString.substring(
+                  prevMatchEndIndex, stdoutString.length()));
+          prevMatchEndIndex = 0;
+          currentMatcher = PATTERN_MATCHER.matcher(stdoutString);
+        }
+        int numberOfCharsRead = processOutput.read(charBuffer);
+        if (numberOfCharsRead < 0) {
+          if (currentKey != null) {
+            currentKey.setValue(stdoutString.substring(0,
+                    Math.max(stdoutString.length() - 1, 0)));
+          }
+          return currentKey;
+        }
+        stdoutString.append(charBuffer, 0, numberOfCharsRead);
+        currentMatcher.reset();
+      } catch (IOException e) {
+        throw new RuntimeIOException(e);
+      }
+    }
+    if (currentKey != null) {
+      currentKey.setValue(stdoutString.substring(prevMatchEndIndex,
+              currentMatcher.start() - 1));
+    }
+    prevMatchEndIndex = currentMatcher.end();
+    nextKey =  new KeyValue(

Review Comment:
   done



##########
hadoop-hdds/rocks-native/src/main/native/SSTDumpTool.cpp:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "org_apache_hadoop_hdds_utils_db_managed_ManagedSSTDumpTool.h"
+#include "rocksdb/options.h"
+#include "rocksdb/sst_dump_tool.h"
+#include <string>
+#include "cplusplus_to_java_convert.h"
+#include "Pipe.h"
+#include <iostream>
+
+jint 
Java_org_apache_hadoop_hdds_utils_db_managed_ManagedSSTDumpTool_runInternal(JNIEnv
 *env, jobject obj,
+ jobjectArray argsArray, jlong optionsHandle, jlong pipeHandle) {

Review Comment:
   done



-- 
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]

Reply via email to