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;
 
Dumps the contents of the log in XML format to System.out.

To print an environment log:

      DbPrintLog.main(argv);
 
 
 public class DbPrintLog {

    
Dump a JE log into human readable form.
 
     public void dump(File envHome,
                      String entryTypes,
                      String txnIds,
                      long startLsn,
                      long endLsn,
                      boolean verbose,
                      boolean stats,
                      boolean repEntriesOnly,
                      boolean csvFormat,
                      boolean forwards,
                      boolean vlsnDistribution,
                      String customDumpReaderClass)
         throws EnvironmentNotFoundException,
                EnvironmentLockedException {
 
         EnvironmentImpl env =
             CmdUtil.makeUtilityEnvironment(envHometrue);
         FileManager fileManager = env.getFileManager();
         fileManager.setIncludeDeletedFiles(true);
         int readBufferSize =
             env.getConfigManager().getInt
             (.);
 
         /* Configure the startLsn and endOfFileLsn if reading backwards. */
         long endOfFileLsn = .;
         if (startLsn == . && endLsn == . &&
             !forwards) {
             LastFileReader fileReader =
                 new LastFileReader(envreadBufferSize);
             while (fileReader.readNextEntry()) {
             }
             startLsn = fileReader.getLastValidLsn();
             endOfFileLsn = fileReader.getEndOfLog();
         }
 
         try {
 
             /* 
              * Make a reader. First see if a custom debug class is available,
              * else use the default versions. 
              */
             DumpFileReader reader = null;
             if (customDumpReaderClass != null) {
                 reader = getDebugReader(customDumpReaderClassenv,
                                         readBufferSizestartLsnendLsn,
                                         endOfFileLsnentryTypestxnIds
                                         verboserepEntriesOnlyforwards);
             } else {
                 if (stats) {
                     reader = new StatsFileReader(envreadBufferSizestartLsn,
                                                  endLsnendOfFileLsn
                                                  entryTypestxnIdsverbose,
                                                  repEntriesOnlyforwards);
                 } else if (vlsnDistribution) {
                     reader = new VLSNDistributionReader(envreadBufferSize,
                                                         startLsnendLsn,
                                                         endOfFileLsn,
                                                         verboseforwards);
                } else {
                    reader = new PrintFileReader(envreadBufferSizestartLsn
                                                 endLsnendOfFileLsn
                                                 entryTypestxnIdsverbose
                                                 repEntriesOnlyforwards);
                }
            }
            /* Enclose the output in a tag to keep proper XML syntax. */
            if (!csvFormat) {
                ..println("<DbPrintLog>");
            }
            while (reader.readNextEntry()) {
            }
            reader.summarize(csvFormat);
            if (!csvFormat) {
                ..println("</DbPrintLog>");
            }
        } finally {
            env.close();
        }
    }

    
The main used by the DbPrintLog utility.

Parameters:
argv An array of command line arguments to the DbPrintLog utility.
 usage: java { com.sleepycat.je.util.DbPrintLog | -jar
 je-<version>.jar DbPrintLog }
  -h <envHomeDir>
  -s  <start file number or LSN, in hex>
  -e  <end file number or LSN, in hex>
  -k  <binary|hex|text|obfuscate> (format for dumping the key/data)
  -tx <targeted txn ids, comma separated>
  -ty <targeted entry types, comma separated>
  -S  show summary of log entries
  -SC show summary of log entries in CSV format
  -r  only print replicated log entries
  -b  scan log backwards. The entire log must be scanned, cannot be used
      with -s or -e
  -q  if specified, concise version is printed,
      default is verbose version
  -c  <name of custom dump reader class> if specified, DbPrintLog
      will attempt to load a class of this name, which will be used to
      process log entries. Used to customize formatting and dumping when
      debugging files.
 

All arguments are optional. The current directory is used if -h is not specified.

    public static void main(String[] argv) {
        try {
            int whichArg = 0;
            String entryTypes = null;
            String txnIds = null;
            long startLsn = .;
            long endLsn = .;
            boolean verbose = true;
            boolean stats = false;
            boolean csvFormat = false;
            boolean repEntriesOnly = false;
            boolean forwards = true;
            String customDumpReaderClass = null;
            boolean vlsnDistribution = false;
            /* Default to looking in current directory. */
            File envHome = new File(".");
            . = .;
            while (whichArg < argv.length) {
                String nextArg = argv[whichArg];
                if (nextArg.equals("-h")) {
                    whichArg++;
                    envHome = new File(CmdUtil.getArg(argvwhichArg));
                } else if (nextArg.equals("-ty")) {
                    whichArg++;
                    entryTypes = CmdUtil.getArg(argvwhichArg);
                } else if (nextArg.equals("-tx")) {
                    whichArg++;
                    txnIds = CmdUtil.getArg(argvwhichArg);
                } else if (nextArg.equals("-s")) {
                    whichArg++;
                    startLsn = CmdUtil.readLsn(CmdUtil.getArg(argvwhichArg));
                } else if (nextArg.equals("-e")) {
                    whichArg++;
                    endLsn = CmdUtil.readLsn(CmdUtil.getArg(argvwhichArg));
                } else if (nextArg.equals("-k")) {
                    whichArg++;
                    String dumpType = CmdUtil.getArg(argvwhichArg);
                    if (dumpType.equalsIgnoreCase("text")) {
                        . = .;
                    } else if (dumpType.equalsIgnoreCase("hex")) {
                        . = .;
                    } else if (dumpType.equalsIgnoreCase("binary")) {
                        . = .;
                    } else if (dumpType.equalsIgnoreCase("obfuscate")) {
                        . = .;
                    } else {
                        ..println
                            (dumpType +
                             " is not a supported dump format type.");
                    }
                } else if (nextArg.equals("-q")) {
                    verbose = false;
                } else if (nextArg.equals("-b")) {
                    forwards = false;
                } else if (nextArg.equals("-S")) {
                    stats = true;
                } else if (nextArg.equals("-SC")) {
                    stats = true;
                    csvFormat = true;
                } else if (nextArg.equals("-r")) {
                    repEntriesOnly = true;
                } else if (nextArg.equals("-c")) {
                    whichArg++;
                    customDumpReaderClass = CmdUtil.getArg(argvwhichArg);
                } else if (nextArg.equals("-vd")) {
                    /* 
                     * An unadvertised option which displays vlsn distribution
                     * in a log, for debugging.
                     */
                    vlsnDistribution = true;
                } else {
                    ..println
                        (nextArg + " is not a supported option.");
                    usage();
                    System.exit(-1);
                }
                whichArg++;
            }
            /* Don't support scan backwards when -s or -e is enabled. */
            if ((startLsn != . || endLsn != .) &&
                !forwards) {
                throw new UnsupportedOperationException
                    ("Backwards scans are not supported when -s or -e are " +
                     "used. They can only be used against the entire log.");
            }
            DbPrintLog printer = new DbPrintLog();
            printer.dump(envHomeentryTypestxnIdsstartLsnendLsn
                         verbosestatsrepEntriesOnlycsvFormatforwards,
                         vlsnDistributioncustomDumpReaderClass);
        } catch (Throwable e) {
            e.printStackTrace();
            ..println(e.getMessage());
            usage();
            System.exit(1);
        }
    }
    private static void usage() {
        ..println("Usage: " +
                           CmdUtil.getJavaCommand(DbPrintLog.class));
        ..println(" -h  <envHomeDir>");
        ..println(" -s  <start file number or LSN, in hex>");
        ..println(" -e  <end file number or LSN, in hex>");
        ..println(" -k  <binary|text|hex|obfuscate> " +
                           "(format for dumping the key and data)");
        ..println(" -tx <targeted txn ids, comma separated>");
        ..println(" -ty <targeted entry types, comma separated>");
        ..println(" -S  show Summary of log entries");
        ..println(" -SC show Summary of log entries in CSV format");
        ..println(" -r  only print replicated log entries");
        ..println(" -b  scan all the log files backwards, don't ");
        ..println("     support scan between two log files");
        ..println(" -q  if specified, concise version is printed");
        ..println("     Default is verbose version.)");
        ..println(" -c  <custom dump reader class> if specified, ");
        ..println("     attempt to load this class to use for the ");
        ..println("     formatting of dumped log entries");
        ..println("All arguments are optional");
    }

    
If a custom dump reader class is specified, we'll use that for DbPrintLog instead of the regular DumpFileReader. The custom reader must have DumpFileReader as a superclass ancestor. Its constructor must have this signature: public class FooReader extends DumpFileReader { public FooReader(EnvironmentImpl env, Integer readBufferSize, Long startLsn, Long finishLsn, Long endOfFileLsn, String entryTypes, String txnIds, Boolean verbose, Boolean repEntriesOnly, Boolean forwards) super(env, readBufferSize, startLsn, finishLsn, endOfFileLsn, entryTypes, txnIds, verbose, repEntriesOnly, forwards); See com.sleepycat.je.util.TestDumper, on the test side, for an example.
    private DumpFileReader getDebugReader(String customDumpReaderClass,
                                          EnvironmentImpl env,
                                          int readBufferSize,
                                          long startLsn,
                                          long finishLsn,
                                          long endOfFileLsn,
                                          String entryTypes,
                                          String txnIds,
                                          boolean verbose,
                                          boolean repEntriesOnly,
                                          boolean forwards) {
        Class<?> debugClass = null;
        try {
            debugClass = Class.forName(customDumpReaderClass);
        } catch (Exception e) {
            throw new IllegalArgumentException
                ("-c was specified, but couldn't load " +
                 customDumpReaderClass + " "e);
        }
        Class<?> args[] = { EnvironmentImpl.class,
                            Integer.class,     // readBufferSize
                            Long.class,        // startLsn
                            Long.class,        // finishLsn
                            Long.class,        // endOfFileLsn
                            String.class,      // entryTypes
                            String.class,      // txnIds
                            Boolean.class,     // verbose
                            Boolean.class,     // repEntriesOnly
                            Boolean.class };   // forwards
        DumpFileReader debugReader = null;
        try {
            Constructor<?> con = 
                debugClass.getConstructor(args);
            debugReader = (DumpFileReadercon.newInstance(env,
                                                           readBufferSize,
                                                           startLsn,
                                                           finishLsn,
                                                           endOfFileLsn,
                                                           entryTypes,
                                                           txnIds,
                                                           verbose,
                                                           repEntriesOnly,
                                                           forwards);
        } catch (Exception e) {
            throw new IllegalStateException
                ("-c was specified, but couldn't instantiate " + 
                 customDumpReaderClass + " "e);
        }
        return debugReader;
    }
New to GrepCode? Check out our FAQ X