helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject git commit: HELIX-129: ZKDumper should use byte[] instead of String to read/write file/zk
Date Tue, 11 Jun 2013 00:13:18 GMT
Updated Branches:
  refs/heads/master 430bef24c -> b5cff39e5


HELIX-129: ZKDumper should use byte[] instead of String to read/write file/zk


Project: http://git-wip-us.apache.org/repos/asf/incubator-helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-helix/commit/b5cff39e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-helix/tree/b5cff39e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-helix/diff/b5cff39e

Branch: refs/heads/master
Commit: b5cff39e578e6a8853d9221c95b22c01f582aabc
Parents: 430bef2
Author: zzhang <zzhang5@uci.edu>
Authored: Mon Jun 10 17:13:10 2013 -0700
Committer: zzhang <zzhang5@uci.edu>
Committed: Mon Jun 10 17:13:10 2013 -0700

----------------------------------------------------------------------
 .../java/org/apache/helix/tools/ZKDumper.java   | 195 +++++++------------
 1 file changed, 74 insertions(+), 121 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/b5cff39e/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java b/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
index 9588b74..3d6c52f 100644
--- a/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
+++ b/helix-core/src/main/java/org/apache/helix/tools/ZKDumper.java
@@ -19,15 +19,15 @@ package org.apache.helix.tools;
  * under the License.
  */
 
-import java.io.BufferedReader;
+import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FilenameFilter;
-import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.List;
 
-import org.I0Itec.zkclient.exception.ZkMarshallingError;
 import org.I0Itec.zkclient.serialize.ZkSerializer;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
@@ -37,49 +37,43 @@ import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.OptionGroup;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
+import org.apache.helix.manager.zk.ByteArraySerializer;
 import org.apache.helix.manager.zk.ZkClient;
 
-
 /**
  * Dumps the Zookeeper file structure on to Disk
  * 
  * 
  */
 @SuppressWarnings("static-access")
-public class ZKDumper
-{
+public class ZKDumper {
   private ZkClient client;
   private FilenameFilter filter;
   static Options options;
   private String suffix = "";
-  //enable by default
-  private boolean removeSuffix=false;
+  // enable by default
+  private boolean removeSuffix = false;
 
-  public String getSuffix()
-  {
+  public String getSuffix() {
     return suffix;
   }
 
-  public void setSuffix(String suffix)
-  {
+  public void setSuffix(String suffix) {
     this.suffix = suffix;
   }
 
-  public boolean isRemoveSuffix()
-  {
+  public boolean isRemoveSuffix() {
     return removeSuffix;
   }
 
-  public void setRemoveSuffix(boolean removeSuffix)
-  {
+  public void setRemoveSuffix(boolean removeSuffix) {
     this.removeSuffix = removeSuffix;
   }
 
-  static
-  {
+  static {
     options = new Options();
     OptionGroup optionGroup = new OptionGroup();
-    
+
     Option d = OptionBuilder.withLongOpt("download")
         .withDescription("Download from ZK to File System").create();
     d.setArgs(0);
@@ -87,17 +81,17 @@ public class ZKDumper
         .withDescription("add suffix to every file downloaded from ZK").create();
     dSuffix.setArgs(1);
     dSuffix.setRequired(false);
-    
-    Option u = OptionBuilder.withLongOpt("upload")
-        .withDescription("Upload from File System to ZK").create();
+
+    Option u = OptionBuilder.withLongOpt("upload").withDescription("Upload from File System
to ZK")
+        .create();
     u.setArgs(0);
     Option uSuffix = OptionBuilder.withLongOpt("removeSuffix")
         .withDescription("remove suffix from every file uploaded to ZK").create();
     uSuffix.setArgs(0);
     uSuffix.setRequired(false);
-    
-    Option del = OptionBuilder.withLongOpt("delete")
-        .withDescription("Delete given path from ZK").create();
+
+    Option del = OptionBuilder.withLongOpt("delete").withDescription("Delete given path from
ZK")
+        .create();
 
     optionGroup.setRequired(true);
     optionGroup.addOption(del);
@@ -113,40 +107,22 @@ public class ZKDumper
     options.addOption(uSuffix);
   }
 
-  public ZKDumper(String zkAddress)
-  {
+  public ZKDumper(String zkAddress) {
     client = new ZkClient(zkAddress, ZkClient.DEFAULT_CONNECTION_TIMEOUT);
-    ZkSerializer zkSerializer = new ZkSerializer()
-    {
-
-      @Override
-      public byte[] serialize(Object arg0) throws ZkMarshallingError
-      {
-        return arg0.toString().getBytes();
-      }
 
-      @Override
-      public Object deserialize(byte[] arg0) throws ZkMarshallingError
-      {
-        return new String(arg0);
-      }
-    };
+    ZkSerializer zkSerializer = new ByteArraySerializer();
     client.setZkSerializer(zkSerializer);
-    filter = new FilenameFilter()
-    {
+    filter = new FilenameFilter() {
 
       @Override
-      public boolean accept(File dir, String name)
-      {
+      public boolean accept(File dir, String name) {
         return !name.startsWith(".");
       }
     };
   }
 
-  public static void main(String[] args) throws Exception
-  {
-    if (args == null || args.length == 0)
-    {
+  public static void main(String[] args) throws Exception {
+    if (args == null || args.length == 0) {
       HelpFormatter helpFormatter = new HelpFormatter();
       helpFormatter.printHelp("java " + ZKDumper.class.getName(), options);
       System.exit(1);
@@ -162,116 +138,93 @@ public class ZKDumper
     String fsPath = cmd.getOptionValue("fspath");
 
     ZKDumper zkDump = new ZKDumper(zkAddress);
-    if (download)
-    {
-      if (cmd.hasOption("addSuffix"))
-      {
+    if (download) {
+      if (cmd.hasOption("addSuffix")) {
         zkDump.suffix = cmd.getOptionValue("addSuffix");
       }
       zkDump.download(zkPath, fsPath + zkPath);
     }
-    if (upload)
-    {
-      if (cmd.hasOption("removeSuffix"))
-      {
+    if (upload) {
+      if (cmd.hasOption("removeSuffix")) {
         zkDump.removeSuffix = true;
       }
       zkDump.upload(zkPath, fsPath);
     }
-    if (del)
-    {
+    if (del) {
       zkDump.delete(zkPath);
     }
   }
 
-  private void delete(String zkPath)
-  {
+  private void delete(String zkPath) {
     client.deleteRecursive(zkPath);
 
   }
 
-  public void upload(String zkPath, String fsPath) throws Exception
-  {
+  public void upload(String zkPath, String fsPath) throws Exception {
     File file = new File(fsPath);
-    System.out
-        .println("Uploading " + file.getCanonicalPath() + " to " + zkPath);
+    System.out.println("Uploading " + file.getCanonicalPath() + " to " + zkPath);
     zkPath = zkPath.replaceAll("[/]+", "/");
     int index = -1;
-    if (removeSuffix && (index = file.getName().indexOf(".")) > -1)
-    {
+    if (removeSuffix && (index = file.getName().indexOf(".")) > -1) {
       zkPath = zkPath.replaceAll(file.getName().substring(index), "");
     }
-    if (file.isDirectory())
-    {
+    if (file.isDirectory()) {
       File[] children = file.listFiles(filter);
       client.createPersistent(zkPath, true);
-      if (children != null && children.length > 0)
-      {
+      if (children != null && children.length > 0) {
 
-        for (File child : children)
-        {
+        for (File child : children) {
           upload(zkPath + "/" + child.getName(), fsPath + "/" + child.getName());
         }
-      } else
-      {
+      } else {
 
       }
-    } else
-    {
-      BufferedReader bfr = null;
-      try
-      {
-        bfr = new BufferedReader(new FileReader(file));
-        StringBuilder sb = new StringBuilder();
-        String line;
-        String recordDelimiter = "";
-        while ((line = bfr.readLine()) != null)
-        {
-          sb.append(recordDelimiter).append(line);
-          recordDelimiter = "\n";
-        }
-        client.createPersistent(zkPath, sb.toString());
-      } catch (Exception e)
-      {
-        throw e;
-      } finally
-      {
-        if (bfr != null)
-        {
-          try
-          {
-            bfr.close();
-          } catch (IOException e)
-          {
+    } else {
+      byte[] result = new byte[(int) file.length()];
+      InputStream input = null;
+      try {
+        int totalBytesRead = 0;
+        input = new BufferedInputStream(new FileInputStream(file));
+        while (totalBytesRead < result.length) {
+          int bytesRemaining = result.length - totalBytesRead;
+          // input.read() returns -1, 0, or more :
+          int bytesRead = input.read(result, totalBytesRead, bytesRemaining);
+          if (bytesRead > 0) {
+            totalBytesRead = totalBytesRead + bytesRead;
           }
         }
+        /*
+         * the above style is a bit tricky: it places bytes into the 'result'
+         * array; 'result' is an output parameter; the while loop usually has a
+         * single iteration only.
+         */
+
+        client.createPersistent(zkPath, result);
+      } finally {
+        input.close();
       }
+
     }
   }
 
-  public void download(String zkPath, String fsPath) throws Exception
-  {
-    
+  public void download(String zkPath, String fsPath) throws Exception {
+
     List<String> children = client.getChildren(zkPath);
-    if (children != null && children.size() > 0)
-    {
+    if (children != null && children.size() > 0) {
       new File(fsPath).mkdirs();
-      for (String child : children)
-      {
-        String childPath = zkPath.equals("/")? "/" + child : zkPath + "/" + child;
+      for (String child : children) {
+        String childPath = zkPath.equals("/") ? "/" + child : zkPath + "/" + child;
         download(childPath, fsPath + "/" + child);
       }
-    } else
-    {
-      System.out.println("Saving " + zkPath + " to "
-          + new File(fsPath + suffix).getCanonicalPath());
-      FileWriter fileWriter = new FileWriter(fsPath + suffix);
+    } else {
+      System.out
+          .println("Saving " + zkPath + " to " + new File(fsPath + suffix).getCanonicalPath());
+      OutputStream out = new FileOutputStream(fsPath + suffix);
       Object readData = client.readData(zkPath);
-      if (readData != null)
-      {
-        fileWriter.write((String) readData);
+      if (readData != null) {
+        out.write((byte[]) readData);
       }
-      fileWriter.close();
+      out.close();
     }
   }
 }


Mime
View raw message