/*
 * 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.file.rfile;

import java.util.ArrayList;

import org.apache.accumulo.core.conf.AccumuloConfiguration;
import org.apache.accumulo.core.data.ByteSequence;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.file.FileUtil;
import org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile;
import org.apache.accumulo.core.file.rfile.RFile.Reader;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class PrintInfo {
  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    @SuppressWarnings("deprecation")
    //Not for client use
    FileSystem fs = FileUtil.getFileSystem(conf, AccumuloConfiguration.getSiteConfiguration());
    
    Options opts = new Options();
    Option dumpKeys = new Option("d", "dump", false, "dump the key/value pairs");
    opts.addOption(dumpKeys);
    
    CommandLine commandLine = new BasicParser().parse(opts, args);
    
    for (String arg : commandLine.getArgs()) {
      
      Path path = new Path(arg);
      CachableBlockFile.Reader _rdr = new CachableBlockFile.Reader(fs, path, conf, null, null);
      Reader iter = new RFile.Reader(_rdr);
      
      iter.printInfo();
      System.out.println();
      org.apache.accumulo.core.file.rfile.bcfile.PrintInfo.main(new String[] {arg});
      
      if (commandLine.hasOption(dumpKeys.getOpt())) {
        iter.seek(new Range((Key) null, (Key) null), new ArrayList<ByteSequence>(), false);
        while (iter.hasTop()) {
          Key key = iter.getTopKey();
          Value value = iter.getTopValue();
          System.out.println(key + " -> " + value);
          iter.next();
        }
      }
      
      iter.close();
    }
  }
}

@@ -2017,80 +2017,484 @@
     
-%0A    CommandLine commandLine = new BasicParser().parse(opts, args);%0A    
+Option histogramOption = new Option(%22h%22, %22histogram%22, false, %22print a histogram of the key-value sizes%22);%0A    opts.addOption(histogramOption);%0A    %0A    CommandLine commandLine = new BasicParser().parse(opts, args);%0A    %0A    boolean dump = commandLine.hasOption(dumpKeys.getOpt());%0A    boolean doHistogram = commandLine.hasOption(histogramOption.getOpt());%0A    long countBuckets%5B%5D = new long%5B11%5D;%0A    long sizeBuckets%5B%5D = new long%5BcountBuckets.length%5D;%0A    long totalSize = 0;%0A
 %0A   
@@ -2882,48 +2882,27 @@
 if (
-commandLine.hasOption(dumpKeys.getOpt())
+doHistogram %7C%7C dump
 ) %7B%0A
@@ -3103,24 +3103,46 @@
 TopValue();%0A
+          if (dump)%0A  
           Sy
@@ -3188,24 +3188,279 @@
 %0A          i
+f (doHistogram) %7B%0A            long size = key.getSize() + value.getSize();%0A            int bucket = (int) Math.log10(size);%0A            countBuckets%5Bbucket%5D++;%0A            sizeBuckets%5Bbucket%5D += size;%0A            totalSize += size;%0A          %7D%0A          i
 ter.next();%0A
@@ -3461,24 +3461,16 @@
 xt();%0A  
-      %7D%0A
       %7D%0A
@@ -3471,24 +3471,25 @@
     %7D%0A      
+%7D
 %0A      iter.
@@ -3493,24 +3493,326 @@
 er.close();%0A
+      if (doHistogram) %7B%0A        System.out.println(%22Up to size      count      %25-age%22);%0A        for (int i = 1; i %3C countBuckets.length; i++) %7B%0A          System.out.println(String.format(%22%2511.0f : %2510d %256.2f%25%25%22, Math.pow(10, i), countBuckets%5Bi%5D, sizeBuckets%5Bi%5D * 100. / totalSize));%0A        %7D%0A      %7D%0A
     %7D%0A  %7D%0A%7D%0A

merge to trunk