You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by kt...@apache.org on 2013/03/26 17:05:29 UTC

svn commit: r1461198 - in /accumulo/trunk/core/src: main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java test/java/org/apache/accumulo/core/util/shell/ShellTest.java

Author: kturner
Date: Tue Mar 26 16:05:28 2013
New Revision: 1461198

URL: http://svn.apache.org/r1461198
Log:
ACCUMULO-1203 applied patch from Kevin Faro that adds human readable date formatter

Added:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java
Modified:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/ShellTest.java

Added: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java?rev=1461198&view=auto
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java (added)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DateStringFormatter.java Tue Mar 26 16:05:28 2013
@@ -0,0 +1,61 @@
+/*
+ * 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.accumulo.core.util.format;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Map.Entry;
+
+import org.apache.accumulo.core.data.Key;
+import org.apache.accumulo.core.data.Value;
+
+public class DateStringFormatter implements Formatter {
+  boolean printTimestamps = false;
+  DefaultFormatter defaultFormatter = new DefaultFormatter();
+  public static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SSS";
+  // SimpleDataFormat is not thread safe
+  private static final ThreadLocal<DateFormat> formatter = new ThreadLocal<DateFormat>() {
+    @Override
+    protected SimpleDateFormat initialValue() {
+      return new SimpleDateFormat(DATE_FORMAT);
+    }
+  };
+  
+  @Override
+  public void initialize(Iterable<Entry<Key,Value>> scanner, boolean printTimestamps) {
+    this.printTimestamps = printTimestamps;
+    defaultFormatter.initialize(scanner, printTimestamps);
+  }
+  @Override
+  public boolean hasNext() {
+    return defaultFormatter.hasNext();
+  }
+  @Override
+  public String next() {
+    DateFormat timestampformat = null;
+    
+    if(printTimestamps) {
+      timestampformat = formatter.get();
+    }
+    
+    return defaultFormatter.next(timestampformat);
+  }
+  @Override
+  public void remove() {
+    defaultFormatter.remove();
+  }
+}

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java?rev=1461198&r1=1461197&r2=1461198&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java (original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/format/DefaultFormatter.java Tue Mar 26 16:05:28 2013
@@ -16,6 +16,10 @@
  */
 package org.apache.accumulo.core.util.format;
 
+import java.text.DateFormat;
+import java.text.FieldPosition;
+import java.text.ParsePosition;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.Map.Entry;
 
@@ -27,6 +31,28 @@ import org.apache.hadoop.io.Text;
 public class DefaultFormatter implements Formatter {
   private Iterator<Entry<Key,Value>> si;
   private boolean doTimestamps;
+  private static final ThreadLocal<DateFormat> formatter = new ThreadLocal<DateFormat>() {
+    @Override
+    protected DateFormat initialValue() {
+      return new DefaultDateFormat();
+    }
+    
+    class DefaultDateFormat extends DateFormat {
+      private static final long serialVersionUID = 1L;
+
+      @Override
+      public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
+        toAppendTo.append(Long.toString(date.getTime()));
+        return toAppendTo;
+      }
+
+      @Override
+      public Date parse(String source, ParsePosition pos) {
+        return new Date(Long.parseLong(source));
+      }
+      
+    }
+  };
   
   @Override
   public void initialize(Iterable<Entry<Key,Value>> scanner, boolean printTimestamps) {
@@ -41,8 +67,18 @@ public class DefaultFormatter implements
   }
   
   public String next() {
+    DateFormat timestampFormat = null;
+    
+    if(doTimestamps) {
+      timestampFormat = formatter.get();
+    }
+    
+    return next(timestampFormat);
+  }
+  
+  protected String next(DateFormat timestampFormat) {
     checkState(si, true);
-    return formatEntry(si.next(), doTimestamps);
+    return formatEntry(si.next(), timestampFormat);
   }
   
   public void remove() {
@@ -59,6 +95,23 @@ public class DefaultFormatter implements
   
   // this should be replaced with something like Record.toString();
   public static String formatEntry(Entry<Key,Value> entry, boolean showTimestamps) {
+    DateFormat timestampFormat = null;
+    
+    if(showTimestamps) {
+      timestampFormat = formatter.get();
+    }
+    
+    return formatEntry(entry, timestampFormat);
+  }
+  
+  /* so a new date object doesn't get created for every record in the scan result */
+  private static ThreadLocal<Date> tmpDate = new ThreadLocal<Date>() {
+    protected Date initialValue() { 
+      return new Date();
+    }
+  };
+  
+  public static String formatEntry(Entry<Key,Value> entry, DateFormat timestampFormat) {
     StringBuilder sb = new StringBuilder();
     
     // append row
@@ -74,9 +127,10 @@ public class DefaultFormatter implements
     sb.append(new ColumnVisibility(entry.getKey().getColumnVisibility()));
     
     // append timestamp
-    if (showTimestamps)
-      sb.append(" ").append(entry.getKey().getTimestamp());
-    
+    if (timestampFormat != null) {
+      tmpDate.get().setTime(entry.getKey().getTimestamp());
+      sb.append(" ").append(timestampFormat.format(tmpDate.get()));
+    }
     // append value
     if (entry.getValue() != null && entry.getValue().getSize() > 0) {
       sb.append("\t");

Modified: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/ShellTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/ShellTest.java?rev=1461198&r1=1461197&r2=1461198&view=diff
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/ShellTest.java (original)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/util/shell/ShellTest.java Tue Mar 26 16:05:28 2013
@@ -24,9 +24,13 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import jline.ConsoleReader;
 
+import org.apache.accumulo.core.util.format.DateStringFormatter;
 import org.apache.log4j.Level;
 import org.junit.Before;
 import org.junit.Test;
@@ -190,4 +194,15 @@ public class ShellTest {
     exec("deletetable t -f", true, "Table: [t] has been deleted");
     exec("deletetable tt -f", true, "Table: [tt] has been deleted");
   }
+  
+  @Test
+  public void scanDateStringFormatterTest() throws IOException {
+    Shell.log.debug("Starting scan dateStringFormatter test --------------------------");
+    exec("createtable t", true);
+    exec("insert r f q v -ts 0", true);
+    DateFormat dateFormat = new SimpleDateFormat(DateStringFormatter.DATE_FORMAT);
+    String expected = String.format("r f:q [] %s    v", dateFormat.format(new Date(0)));
+    exec("scan -fm org.apache.accumulo.core.util.format.DateStringFormatter -st", true, expected);
+    exec("deletetable t -f", true, "Table: [t] has been deleted");
+  }
 }