Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F20F4F63B for ; Tue, 26 Mar 2013 16:05:55 +0000 (UTC) Received: (qmail 88728 invoked by uid 500); 26 Mar 2013 16:05:55 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 88664 invoked by uid 500); 26 Mar 2013 16:05:55 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 88623 invoked by uid 99); 26 Mar 2013 16:05:54 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Mar 2013 16:05:54 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Mar 2013 16:05:51 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 139D723888FE; Tue, 26 Mar 2013 16:05:30 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Tue, 26 Mar 2013 16:05:29 -0000 To: commits@accumulo.apache.org From: kturner@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130326160530.139D723888FE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 formatter = new ThreadLocal() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat(DATE_FORMAT); + } + }; + + @Override + public void initialize(Iterable> 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> si; private boolean doTimestamps; + private static final ThreadLocal formatter = new ThreadLocal() { + @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> 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 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 tmpDate = new ThreadLocal() { + protected Date initialValue() { + return new Date(); + } + }; + + public static String formatEntry(Entry 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"); + } }