You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2017/01/30 20:31:12 UTC

bookkeeper git commit: BOOKKEEPER-907: EntryFormatter should be configured

Repository: bookkeeper
Updated Branches:
  refs/heads/master 43971c3b9 -> ca7e5fd80


BOOKKEEPER-907: EntryFormatter should be configured

for ReadLedgerEntriesCmd, EntryFormatter should
be configurable and HexDumpEntryFormatter should
be one of them.

Author: Charan Reddy Guttapalem <cg...@salesforce.com>

Reviewers: Sijie Guo <si...@apache.org>

Closes #101 from reddycharan/entryformatter


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

Branch: refs/heads/master
Commit: ca7e5fd80662745e7f64ee8f70c66dc9734f6e3a
Parents: 43971c3
Author: Charan Reddy Guttapalem <cg...@salesforce.com>
Authored: Mon Jan 30 12:31:01 2017 -0800
Committer: Sijie Guo <si...@apache.org>
Committed: Mon Jan 30 12:31:01 2017 -0800

----------------------------------------------------------------------
 .../apache/bookkeeper/bookie/BookieShell.java   | 26 +++++++---
 .../bookkeeper/util/HexDumpEntryFormatter.java  | 53 ++++++++++++++++++++
 2 files changed, 73 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/ca7e5fd8/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index a83b3a8..1092001 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -465,22 +465,17 @@ public class BookieShell implements Tool {
             conf.addConfiguration(bkConf);
 
             BookKeeperAdmin bk = null;
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
             try {
                 bk = new BookKeeperAdmin(conf);
                 Iterator<LedgerEntry> entries = bk.readEntries(ledgerId, firstEntry, lastEntry).iterator();
                 while (entries.hasNext()) {
                     LedgerEntry entry = entries.next();
-                    HexDump.dump(entry.getEntry(), 0, out, 0);
-                    System.out.println(
-                            "Entry Id: " + entry.getEntryId() + ", Data: " + new String(out.toByteArray(), UTF_8));
-                    out.reset();
+                    formatEntry(entry, true);
                 }
             } catch (Exception e) {
                 LOG.error("Error reading entries from ledger {}", ledgerId, e.getCause());
                 return -1;
             } finally {
-                out.close();
                 if (bk != null) {
                     bk.close();
                 }
@@ -2193,6 +2188,25 @@ public class BookieShell implements Tool {
     }
 
     /**
+     * Format the entry into a readable format.
+     * 
+     * @param entry 
+     *          ledgerentry to print
+     * @param printMsg 
+     *          Whether printing the message body
+     */
+    private void formatEntry(LedgerEntry entry, boolean printMsg) {
+        long ledgerId = entry.getLedgerId();
+        long entryId = entry.getEntryId();
+        long entrySize = entry.getLength();
+        System.out
+                .println("--------- Lid=" + ledgerId + ", Eid=" + entryId + ", EntrySize=" + entrySize + " ---------");
+        if (printMsg) {
+            formatter.formatEntry(entry.getEntry());
+        }
+    }
+    
+    /**
      * Format the message into a readable format.
      *
      * @param pos

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/ca7e5fd8/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/HexDumpEntryFormatter.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/HexDumpEntryFormatter.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/HexDumpEntryFormatter.java
new file mode 100644
index 0000000..e51e2a8
--- /dev/null
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/HexDumpEntryFormatter.java
@@ -0,0 +1,53 @@
+/*
+ *
+ * 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.bookkeeper.util;
+
+import java.io.IOException;
+
+import org.apache.commons.io.HexDump;
+
+public class HexDumpEntryFormatter extends EntryFormatter {
+    @Override
+    public void formatEntry(byte[] data) {
+        try {
+            HexDump.dump(data, 0, System.out, 0);
+        } catch (ArrayIndexOutOfBoundsException e) {
+            System.out.println("Warn: Index is outside the data array's bounds : " + e.getMessage());
+        } catch (IllegalArgumentException e) {
+            System.out.println("Warn: The output stream is null : " + e.getMessage());
+        } catch (IOException e) {
+            System.out.println("Warn: Something has gone wrong writing the data to stream : " + e.getMessage());
+        }
+    }
+
+    @Override
+    public void formatEntry(java.io.InputStream input) {
+        try {
+            byte[] data = new byte[input.available()];
+            input.read(data, 0, data.length);
+            formatEntry(data);
+        } catch (IOException ie) {
+            System.out.println("Warn: Unreadable entry : " + ie.getMessage());
+        }
+    }
+
+};