You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2020/05/08 22:49:40 UTC
[incubator-datasketches-characterization] 01/02: add to Files and
DumpSketchSegment
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-characterization.git
commit 6e0ed2b36473440cef628dd7398a6b522cae04e2
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Mon May 4 18:59:31 2020 -0700
add to Files and DumpSketchSegment
---
.../org/apache/datasketches/DumpSketchSegment.java | 80 ++++++++++++++++++++++
src/main/java/org/apache/datasketches/Files.java | 36 ++++++++++
2 files changed, 116 insertions(+)
diff --git a/src/main/java/org/apache/datasketches/DumpSketchSegment.java b/src/main/java/org/apache/datasketches/DumpSketchSegment.java
new file mode 100644
index 0000000..05b3d57
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/DumpSketchSegment.java
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ */
+
+package org.apache.datasketches;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.Base64;
+
+import org.apache.datasketches.hll.HllSketch;
+import org.testng.annotations.Test;
+
+/**
+ * @author Lee Rhodes
+ */
+public class DumpSketchSegment {
+ final static Base64.Decoder decoder = Base64.getDecoder();
+ final String fname = "/Users/lrhodes/dev/git/Apache/druid-0.18.0/Dump/hll_segment.txt";
+ final String tgtDir = "/Users/lrhodes/dev/git/Apache/druid-0.18.0/Dump/Sketches/";
+ final ProcessSketch processSketch = new ProcessSketch();
+
+ @Test
+ public void analyzeSegment() {
+ final LineReader lineReader = new LineReader(fname);
+ lineReader.read(0, processSketch);
+ }
+
+ class ProcessSketch implements ProcessLine {
+
+ @Override
+ public void process(final String line, final int lineNo) {
+ final String[] splits = line.split(":");
+ if (splits.length != 2) { throw new IllegalArgumentException("Splits != 2, Line#: " + lineNo); }
+ final String s1 = splits[1];
+ final String substr = s1.substring(1, s1.length() - 2);
+ final byte[] byteArray = decoder.decode(substr);
+ //writeByteArrayToFile(byteArray, tgtDir + "HllSketch" + lineNo);
+ final HllSketch sk = HllSketch.heapify(byteArray);
+ println("HllSketch " + lineNo);
+ println(HllSketch.toString(byteArray));
+ println(sk.toString(true, false, false, false));
+ println("*****************");
+ }
+
+ }
+
+ private static void writeByteArrayToFile(final byte[] arr, final String fullFileName) {
+ Files.checkFileName(fullFileName); //checks for null, empty
+ final File file = new File(fullFileName);
+ if (file.exists() && file.isFile()) {
+ file.delete();
+ }
+ try (FileOutputStream fos = new FileOutputStream(file)) {
+ fos.write(arr);
+ fos.flush();
+ fos.close();
+ } catch (final IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ static void println(final Object obj) { System.out.println(obj.toString()); }
+}
diff --git a/src/main/java/org/apache/datasketches/Files.java b/src/main/java/org/apache/datasketches/Files.java
index 3f3620d..f512d5d 100644
--- a/src/main/java/org/apache/datasketches/Files.java
+++ b/src/main/java/org/apache/datasketches/Files.java
@@ -1040,4 +1040,40 @@ public final class Files {
throw new RuntimeException(e);
}
}
+
+ public static byte[] readByteArrayFromFileName(final String fullFileName) {
+ Files.checkFileName(fullFileName); //checks for null, empty
+ final File file = new File(fullFileName);
+ if (!file.exists() || !file.isFile()) {
+ throw new IllegalArgumentException(fullFileName + " does not exist.");
+ }
+ return readByteArrayFromFile(file);
+ }
+
+ public static byte[] readByteArrayFromFile(final File file) {
+ try (FileInputStream fis = new FileInputStream(file)) {
+ final int len = fis.available();
+ final byte[] out = new byte[len];
+ fis.read(out);
+ fis.close();
+ return out;
+ } catch (final IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static void writeByteArrayToFile(final byte[] arr, final String fullFileName) {
+ Files.checkFileName(fullFileName); //checks for null, empty
+ final File file = new File(fullFileName);
+ if (file.exists() && file.isFile()) {
+ file.delete();
+ }
+ try (FileOutputStream fos = new FileOutputStream(file)) {
+ fos.write(arr);
+ fos.flush();
+ fos.close();
+ } catch (final IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org