Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.util;
  
 import java.io.File;
 import java.util.Map;
 
DbSpace displays the disk space utilization for an environment.
 usage: java { com.sleepycat.je.util.DbSpace |
               -jar je-<version>.jar DbSpace }
          -h <dir># environment home directory
         [-q]     # quiet, print grand totals only
         [-u]     # sort by utilization
         [-d]     # dump file summary details
         [-r]     # recalculate utilization (expensive)
         [-s]     # start file number or LSN, in hex
         [-e]     # end file number or LSN, in hex
         [-V]     # print JE version number
 
 
 public class DbSpace {
 
     private static final String USAGE =
         "usage: " + CmdUtil.getJavaCommand(DbSpace.class) + "\n" +
         "       -h <dir> # environment home directory\n" +
         "       [-q]     # quiet, print grand totals only\n" +
         "       [-u]     # sort by utilization\n" +
         "       [-d]     # dump file summary details\n" +
         "       [-r]     # recalculate utilization (expensive)\n" +
         "       [-s]     # start file number or LSN, in hex\n" +
         "       [-e]     # end file number or LSN, in hex\n" +
         "       [-V]     # print JE version number";
 
     public static void main(String argv[])
         throws Exception {
 
         DbSpace space = new DbSpace();
         space.parseArgs(argv);
 
         EnvironmentConfig envConfig = new EnvironmentConfig();
         envConfig.setReadOnly(true);
         Environment env = new Environment(space.envHomeenvConfig);
         space.initEnv(DbInternal.getEnvironmentImpl(env));
 
         try {
             space.print(.);
             System.exit(0);
         } catch (Throwable e) {
             e.printStackTrace(.);
             System.exit(1);
         } finally {
             try {
                 env.close();
             } catch (Throwable e) {
                 e.printStackTrace(.);
                 System.exit(1);
             }
         }
     }
 
     private File envHome = null;
     private EnvironmentImpl envImpl;
     private UtilizationCalculator calculator;
     private boolean quiet = false;
     private boolean sorted = false;
     private boolean details = false;
     private boolean recalc = false;
     private long startLsn = .;
     private long finishLsn = .;
 
     private DbSpace() {
     }

    
Creates a DbSpace object for calculating utilization using an open Environment.
    public DbSpace(Environment env,
                   boolean quiet,
                   boolean details,
                   boolean sorted) {
        this(DbInternal.getEnvironmentImpl(env), quietdetailssorted);
    }

    
For internal use only.

Hidden:
    public DbSpace(EnvironmentImpl envImpl,
                   boolean quiet,
                   boolean details,
                   boolean sorted) {
        initEnv(envImpl);
        this. = quiet;
        this. = details;
        this. = sorted;
    }
    private void initEnv(EnvironmentImpl envImpl) {
        this. = envImpl;
         = envImpl.getCleaner().getUtilizationCalculator();
    }
    private void printUsage(String msg) {
        if (msg != null) {
            ..println(msg);
        }
        ..println();
        System.exit(-1);
    }
    private void parseArgs(String argv[]) {
        int argc = 0;
        int nArgs = argv.length;
        if (nArgs == 0) {
            printUsage(null);
            System.exit(0);
        }
        while (argc < nArgs) {
            String thisArg = argv[argc++];
            if (thisArg.equals("-q")) {
                 = true;
            } else if (thisArg.equals("-u")) {
                 = true;
            } else if (thisArg.equals("-d")) {
                 = true;
            } else if (thisArg.equals("-r")) {
                 = true;
            } else if (thisArg.equals("-V")) {
                ..println(.);
                System.exit(0);
            } else if (thisArg.equals("-h")) {
                if (argc < nArgs) {
                     = new File(argv[argc++]);
                } else {
                    printUsage("-h requires an argument");
                }
            } else if (thisArg.equals("-s")) {
                if (argc < nArgs) {
                     = CmdUtil.readLsn(argv[argc++]);
                } else {
                    printUsage("-s requires an argument");
                }
            } else if (thisArg.equals("-e")) {
                if (argc < nArgs) {
                     = CmdUtil.readLsn(argv[argc++]);
                } else {
                    printUsage("-e requires an argument");
                }
            }
        }
        if ( == null) {
            printUsage("-h is a required argument");
        }
    }

    
Sets the recalculation property, which if true causes a more expensive recalculation of utilization to be performed for debugging purposes. This property is false by default.
    public void setRecalculate(boolean recalc) {
        this. = recalc;
    }

    
Sets the start file number, which is a lower bound on the range of files for which utilization is reported and (optionally) recalculated. By default there is no lower bound.
    public void setStartFile(long startFile) {
        this. = startFile;
    }

    
Sets the ending file number, which is an upper bound on the range of files for which utilization is reported and (optionally) recalculated. By default there is no upper bound.
    public void setEndFile(long endFile) {
        this. = endFile;
    }

    
Calculates utilization and prints a report to the given output stream.
    public void print(PrintStream out)
        throws DatabaseException {
        long startFile = ( != .) ?
            DbLsn.getFileNumber() :
            0;
        long finishFile = ( != .) ?
            DbLsn.getFileNumber() :
            .;
        SortedMap<Long,FileSummarymap =
            .getUtilizationProfile().getFileSummaryMap(true).
            subMap(startFilefinishFile);
        Map<LongFileSummaryrecalcMap =
             ?
            UtilizationFileReader.calcFileSummaryMap(,
                                                     ) :
            null;
        int fileIndex = 0;
        Summary totals = new Summary();
        Summary[] summaries = null;
        if (!) {
            summaries = new Summary[map.size()];
        }
        Iterator<Map.Entry<Long,FileSummary>> iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<Long,FileSummaryentry = iter.next();
            Long fileNum = entry.getKey();
            FileSummary fs = entry.getValue();
            FileSummary recalcFs = null;
            if (recalcMap != null) {
                 recalcFs = recalcMap.get(fileNum);
            }
            Summary summary = new Summary(fileNumfsrecalcFs);
            if (summaries != null) {
                summaries[fileIndex] = summary;
            }
            if () {
                out.println
                    ("File 0x" + Long.toHexString(fileNum.longValue()) +
                     ": " + fs);
                if (recalcMap != null) {
                    out.println
                        ("Recalculated File 0x" +
                         Long.toHexString(fileNum.longValue()) +
                         ": " + recalcFs);
                }
            }
            totals.add(summary);
            fileIndex += 1;
        }
        if () {
            out.println();
        }
        out.println( ? . : .);
        if (summaries != null) {
            if () {
                Arrays.sort(summariesnew Comparator<Summary>() {
                    public int compare(Summary s1Summary s2) {
                        return s1.utilization() - s2.utilization();
                    }
                });
            }
            for (int i = 0; i < summaries.lengthi += 1) {
                summaries[i].print(out);
            }
        }
        totals.print(out);
        out.println("(LN size correction factor: " +
                    .getLNSizeCorrectionFactor() + ')');
    }
    private class Summary {
        static final String HEADER = "  File    Size (KB)  % Used\n" +
                                     "--------  ---------  ------";
                                   // 12345678  123456789     123
                                   //         12         12345
                                   // TOTALS:
        static final String RECALC_HEADER =
                   "  File    Size (KB)  % Used  % Used (recalculated)\n" +
                   "--------  ---------  ------  ------";
                 // 12345678  123456789     123     123
                 //         12         12345   12345
                 // TOTALS:
        Long fileNum;
        long totalSize;
        long obsoleteSize;
        long recalcObsoleteSize;
        Summary() {}
        Summary(Long fileNumFileSummary summaryFileSummary recalcSummary) {
            this. = fileNum;
             = summary.totalSize;
             = summary.getObsoleteSize
                (.getLNSizeCorrectionFactor());
            if (recalcSummary != null) {
                 = recalcSummary.getObsoleteSize();
            }
        }
        void add(Summary o) {
             += o.totalSize;
             += o.obsoleteSize;
             += o.recalcObsoleteSize;
        }
        void print(PrintStream outboolean recalc) {
            if ( != null) {
                pad(out, Long.toHexString(.longValue()), 8, '0');
            } else {
                out.print(" TOTALS ");
            }
            int kb = (int) ( / 1024);
            out.print("  ");
            pad(out, Integer.toString(kb), 9, ' ');
            out.print("     ");
            pad(out, Integer.toString(utilization()), 3, ' ');
            if (recalc) {
                out.print("     ");
                pad(out, Integer.toString(recalcUtilization()), 3, ' ');
            }
            out.println();
        }
        int utilization() {
            return FileSummary.utilization();
        }
        int recalcUtilization() {
            return FileSummary.utilization();
        }
        private void pad(PrintStream outString valint digits,
                           char padChar) {
            int padSize = digits - val.length();
            for (int i = 0; i < padSizei += 1) {
                out.print(padChar);
            }
            out.print(val);
        }
    }
New to GrepCode? Check out our FAQ X