zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an...@apache.org
Subject [29/51] [partial] zookeeper git commit: ZOOKEEPER-3032: MAVEN MIGRATION - branch-3.5 - move java server, client
Date Fri, 05 Oct 2018 13:36:57 GMT
http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
deleted file mode 100644
index f6f2ac5..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
+++ /dev/null
@@ -1,246 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.zip.Adler32;
-import java.util.zip.CheckedInputStream;
-import java.util.zip.CheckedOutputStream;
-
-import org.apache.jute.BinaryInputArchive;
-import org.apache.jute.BinaryOutputArchive;
-import org.apache.jute.InputArchive;
-import org.apache.jute.OutputArchive;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.zookeeper.server.DataTree;
-import org.apache.zookeeper.server.util.SerializeUtils;
-
-/**
- * This class implements the snapshot interface.
- * it is responsible for storing, serializing
- * and deserializing the right snapshot.
- * and provides access to the snapshots.
- */
-public class FileSnap implements SnapShot {
-    File snapDir;
-    private volatile boolean close = false;
-    private static final int VERSION = 2;
-    private static final long dbId = -1;
-    private static final Logger LOG = LoggerFactory.getLogger(FileSnap.class);
-    public final static int SNAP_MAGIC
-            = ByteBuffer.wrap("ZKSN".getBytes()).getInt();
-
-    public static final String SNAPSHOT_FILE_PREFIX = "snapshot";
-
-    public FileSnap(File snapDir) {
-        this.snapDir = snapDir;
-    }
-
-    /**
-     * deserialize a data tree from the most recent snapshot
-     * @return the zxid of the snapshot
-     */
-    public long deserialize(DataTree dt, Map<Long, Integer> sessions)
-            throws IOException {
-        // we run through 100 snapshots (not all of them)
-        // if we cannot get it running within 100 snapshots
-        // we should  give up
-        List<File> snapList = findNValidSnapshots(100);
-        if (snapList.size() == 0) {
-            return -1L;
-        }
-        File snap = null;
-        boolean foundValid = false;
-        for (int i = 0, snapListSize = snapList.size(); i < snapListSize; i++) {
-            snap = snapList.get(i);
-            LOG.info("Reading snapshot " + snap);
-            try (InputStream snapIS = new BufferedInputStream(new FileInputStream(snap));
-                 CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32())) {
-                InputArchive ia = BinaryInputArchive.getArchive(crcIn);
-                deserialize(dt, sessions, ia);
-                long checkSum = crcIn.getChecksum().getValue();
-                long val = ia.readLong("val");
-                if (val != checkSum) {
-                    throw new IOException("CRC corruption in snapshot :  " + snap);
-                }
-                foundValid = true;
-                break;
-            } catch (IOException e) {
-                LOG.warn("problem reading snap file " + snap, e);
-            }
-        }
-        if (!foundValid) {
-            throw new IOException("Not able to find valid snapshots in " + snapDir);
-        }
-        dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), SNAPSHOT_FILE_PREFIX);
-        return dt.lastProcessedZxid;
-    }
-
-    /**
-     * deserialize the datatree from an inputarchive
-     * @param dt the datatree to be serialized into
-     * @param sessions the sessions to be filled up
-     * @param ia the input archive to restore from
-     * @throws IOException
-     */
-    public void deserialize(DataTree dt, Map<Long, Integer> sessions,
-            InputArchive ia) throws IOException {
-        FileHeader header = new FileHeader();
-        header.deserialize(ia, "fileheader");
-        if (header.getMagic() != SNAP_MAGIC) {
-            throw new IOException("mismatching magic headers "
-                    + header.getMagic() +
-                    " !=  " + FileSnap.SNAP_MAGIC);
-        }
-        SerializeUtils.deserializeSnapshot(dt,ia,sessions);
-    }
-
-    /**
-     * find the most recent snapshot in the database.
-     * @return the file containing the most recent snapshot
-     */
-    public File findMostRecentSnapshot() throws IOException {
-        List<File> files = findNValidSnapshots(1);
-        if (files.size() == 0) {
-            return null;
-        }
-        return files.get(0);
-    }
-
-    /**
-     * find the last (maybe) valid n snapshots. this does some 
-     * minor checks on the validity of the snapshots. It just
-     * checks for / at the end of the snapshot. This does
-     * not mean that the snapshot is truly valid but is
-     * valid with a high probability. also, the most recent 
-     * will be first on the list. 
-     * @param n the number of most recent snapshots
-     * @return the last n snapshots (the number might be
-     * less than n in case enough snapshots are not available).
-     * @throws IOException
-     */
-    private List<File> findNValidSnapshots(int n) throws IOException {
-        List<File> files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false);
-        int count = 0;
-        List<File> list = new ArrayList<File>();
-        for (File f : files) {
-            // we should catch the exceptions
-            // from the valid snapshot and continue
-            // until we find a valid one
-            try {
-                if (Util.isValidSnapshot(f)) {
-                    list.add(f);
-                    count++;
-                    if (count == n) {
-                        break;
-                    }
-                }
-            } catch (IOException e) {
-                LOG.info("invalid snapshot " + f, e);
-            }
-        }
-        return list;
-    }
-
-    /**
-     * find the last n snapshots. this does not have
-     * any checks if the snapshot might be valid or not
-     * @param n the number of most recent snapshots
-     * @return the last n snapshots
-     * @throws IOException
-     */
-    public List<File> findNRecentSnapshots(int n) throws IOException {
-        List<File> files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false);
-        int count = 0;
-        List<File> list = new ArrayList<File>();
-        for (File f: files) {
-            if (count == n)
-                break;
-            if (Util.getZxidFromName(f.getName(), SNAPSHOT_FILE_PREFIX) != -1) {
-                count++;
-                list.add(f);
-            }
-        }
-        return list;
-    }
-
-    /**
-     * serialize the datatree and sessions
-     * @param dt the datatree to be serialized
-     * @param sessions the sessions to be serialized
-     * @param oa the output archive to serialize into
-     * @param header the header of this snapshot
-     * @throws IOException
-     */
-    protected void serialize(DataTree dt,Map<Long, Integer> sessions,
-            OutputArchive oa, FileHeader header) throws IOException {
-        // this is really a programmatic error and not something that can
-        // happen at runtime
-        if(header==null)
-            throw new IllegalStateException(
-                    "Snapshot's not open for writing: uninitialized header");
-        header.serialize(oa, "fileheader");
-        SerializeUtils.serializeSnapshot(dt,oa,sessions);
-    }
-
-    /**
-     * serialize the datatree and session into the file snapshot
-     * @param dt the datatree to be serialized
-     * @param sessions the sessions to be serialized
-     * @param snapShot the file to store snapshot into
-     */
-    public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot)
-            throws IOException {
-        if (!close) {
-            try (OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
-                 CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32())) {
-                //CheckedOutputStream cout = new CheckedOutputStream()
-                OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
-                FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
-                serialize(dt, sessions, oa, header);
-                long val = crcOut.getChecksum().getValue();
-                oa.writeLong(val, "val");
-                oa.writeString("/", "path");
-                sessOS.flush();
-            }
-        }
-    }
-
-    /**
-     * synchronized close just so that if serialize is in place
-     * the close operation will block and will wait till serialize
-     * is done and will set the close flag
-     */
-    @Override
-    public synchronized void close() throws IOException {
-        close = true;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
deleted file mode 100644
index 4f0ef2a..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
+++ /dev/null
@@ -1,744 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.EOFException;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.TimeUnit;
-import java.util.zip.Adler32;
-import java.util.zip.Checksum;
-
-import org.apache.jute.BinaryInputArchive;
-import org.apache.jute.BinaryOutputArchive;
-import org.apache.jute.InputArchive;
-import org.apache.jute.OutputArchive;
-import org.apache.jute.Record;
-import org.apache.zookeeper.server.ServerStats;
-import org.apache.zookeeper.server.util.SerializeUtils;
-import org.apache.zookeeper.txn.TxnHeader;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * This class implements the TxnLog interface. It provides api's
- * to access the txnlogs and add entries to it.
- * <p>
- * The format of a Transactional log is as follows:
- * <blockquote><pre>
- * LogFile:
- *     FileHeader TxnList ZeroPad
- *
- * FileHeader: {
- *     magic 4bytes (ZKLG)
- *     version 4bytes
- *     dbid 8bytes
- *   }
- *
- * TxnList:
- *     Txn || Txn TxnList
- *
- * Txn:
- *     checksum Txnlen TxnHeader Record 0x42
- *
- * checksum: 8bytes Adler32 is currently used
- *   calculated across payload -- Txnlen, TxnHeader, Record and 0x42
- *
- * Txnlen:
- *     len 4bytes
- *
- * TxnHeader: {
- *     sessionid 8bytes
- *     cxid 4bytes
- *     zxid 8bytes
- *     time 8bytes
- *     type 4bytes
- *   }
- *
- * Record:
- *     See Jute definition file for details on the various record types
- *
- * ZeroPad:
- *     0 padded to EOF (filled during preallocation stage)
- * </pre></blockquote>
- */
-public class FileTxnLog implements TxnLog {
-    private static final Logger LOG;
-
-    public final static int TXNLOG_MAGIC =
-        ByteBuffer.wrap("ZKLG".getBytes()).getInt();
-
-    public final static int VERSION = 2;
-
-    public static final String LOG_FILE_PREFIX = "log";
-
-    static final String FSYNC_WARNING_THRESHOLD_MS_PROPERTY = "fsync.warningthresholdms";
-    static final String ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY = "zookeeper." + FSYNC_WARNING_THRESHOLD_MS_PROPERTY;
-
-    /** Maximum time we allow for elapsed fsync before WARNing */
-    private final static long fsyncWarningThresholdMS;
-
-    static {
-        LOG = LoggerFactory.getLogger(FileTxnLog.class);
-
-        /** Local variable to read fsync.warningthresholdms into */
-        Long fsyncWarningThreshold;
-        if ((fsyncWarningThreshold = Long.getLong(ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY)) == null)
-            fsyncWarningThreshold = Long.getLong(FSYNC_WARNING_THRESHOLD_MS_PROPERTY, 1000);
-        fsyncWarningThresholdMS = fsyncWarningThreshold;
-    }
-
-    long lastZxidSeen;
-    volatile BufferedOutputStream logStream = null;
-    volatile OutputArchive oa;
-    volatile FileOutputStream fos = null;
-
-    File logDir;
-    private final boolean forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals("no");
-    long dbId;
-    private LinkedList<FileOutputStream> streamsToFlush =
-        new LinkedList<FileOutputStream>();
-    File logFileWrite = null;
-    private FilePadding filePadding = new FilePadding();
-
-    private ServerStats serverStats;
-
-    private volatile long syncElapsedMS = -1L;
-
-    /**
-     * constructor for FileTxnLog. Take the directory
-     * where the txnlogs are stored
-     * @param logDir the directory where the txnlogs are stored
-     */
-    public FileTxnLog(File logDir) {
-        this.logDir = logDir;
-    }
-
-    /**
-      * method to allow setting preallocate size
-      * of log file to pad the file.
-      * @param size the size to set to in bytes
-      */
-    public static void setPreallocSize(long size) {
-        FilePadding.setPreallocSize(size);
-    }
-
-    /**
-     * Setter for ServerStats to monitor fsync threshold exceed
-     * @param serverStats used to update fsyncThresholdExceedCount
-     */
-    @Override
-    public void setServerStats(ServerStats serverStats) {
-        this.serverStats = serverStats;
-    }
-
-    /**
-     * creates a checksum algorithm to be used
-     * @return the checksum used for this txnlog
-     */
-    protected Checksum makeChecksumAlgorithm(){
-        return new Adler32();
-    }
-
-    /**
-     * rollover the current log file to a new one.
-     * @throws IOException
-     */
-    public synchronized void rollLog() throws IOException {
-        if (logStream != null) {
-            this.logStream.flush();
-            this.logStream = null;
-            oa = null;
-        }
-    }
-
-    /**
-     * close all the open file handles
-     * @throws IOException
-      */
-    public synchronized void close() throws IOException {
-        if (logStream != null) {
-            logStream.close();
-        }
-        for (FileOutputStream log : streamsToFlush) {
-            log.close();
-        }
-    }
-
-    /**
-     * append an entry to the transaction log
-     * @param hdr the header of the transaction
-     * @param txn the transaction part of the entry
-     * returns true iff something appended, otw false
-     */
-    public synchronized boolean append(TxnHeader hdr, Record txn)
-        throws IOException
-    {
-        if (hdr == null) {
-            return false;
-        }
-        if (hdr.getZxid() <= lastZxidSeen) {
-            LOG.warn("Current zxid " + hdr.getZxid()
-                    + " is <= " + lastZxidSeen + " for "
-                    + hdr.getType());
-        } else {
-            lastZxidSeen = hdr.getZxid();
-        }
-        if (logStream==null) {
-           if(LOG.isInfoEnabled()){
-                LOG.info("Creating new log file: " + Util.makeLogName(hdr.getZxid()));
-           }
-
-           logFileWrite = new File(logDir, Util.makeLogName(hdr.getZxid()));
-           fos = new FileOutputStream(logFileWrite);
-           logStream=new BufferedOutputStream(fos);
-           oa = BinaryOutputArchive.getArchive(logStream);
-           FileHeader fhdr = new FileHeader(TXNLOG_MAGIC,VERSION, dbId);
-           fhdr.serialize(oa, "fileheader");
-           // Make sure that the magic number is written before padding.
-           logStream.flush();
-           filePadding.setCurrentSize(fos.getChannel().position());
-           streamsToFlush.add(fos);
-        }
-        filePadding.padFile(fos.getChannel());
-        byte[] buf = Util.marshallTxnEntry(hdr, txn);
-        if (buf == null || buf.length == 0) {
-            throw new IOException("Faulty serialization for header " +
-                    "and txn");
-        }
-        Checksum crc = makeChecksumAlgorithm();
-        crc.update(buf, 0, buf.length);
-        oa.writeLong(crc.getValue(), "txnEntryCRC");
-        Util.writeTxnBytes(oa, buf);
-
-        return true;
-    }
-
-    /**
-     * Find the log file that starts at, or just before, the snapshot. Return
-     * this and all subsequent logs. Results are ordered by zxid of file,
-     * ascending order.
-     * @param logDirList array of files
-     * @param snapshotZxid return files at, or before this zxid
-     * @return
-     */
-    public static File[] getLogFiles(File[] logDirList,long snapshotZxid) {
-        List<File> files = Util.sortDataDir(logDirList, LOG_FILE_PREFIX, true);
-        long logZxid = 0;
-        // Find the log file that starts before or at the same time as the
-        // zxid of the snapshot
-        for (File f : files) {
-            long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX);
-            if (fzxid > snapshotZxid) {
-                continue;
-            }
-            // the files
-            // are sorted with zxid's
-            if (fzxid > logZxid) {
-                logZxid = fzxid;
-            }
-        }
-        List<File> v=new ArrayList<File>(5);
-        for (File f : files) {
-            long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX);
-            if (fzxid < logZxid) {
-                continue;
-            }
-            v.add(f);
-        }
-        return v.toArray(new File[0]);
-
-    }
-
-    /**
-     * get the last zxid that was logged in the transaction logs
-     * @return the last zxid logged in the transaction logs
-     */
-    public long getLastLoggedZxid() {
-        File[] files = getLogFiles(logDir.listFiles(), 0);
-        long maxLog=files.length>0?
-                Util.getZxidFromName(files[files.length-1].getName(),LOG_FILE_PREFIX):-1;
-
-        // if a log file is more recent we must scan it to find
-        // the highest zxid
-        long zxid = maxLog;
-        TxnIterator itr = null;
-        try {
-            FileTxnLog txn = new FileTxnLog(logDir);
-            itr = txn.read(maxLog);
-            while (true) {
-                if(!itr.next())
-                    break;
-                TxnHeader hdr = itr.getHeader();
-                zxid = hdr.getZxid();
-            }
-        } catch (IOException e) {
-            LOG.warn("Unexpected exception", e);
-        } finally {
-            close(itr);
-        }
-        return zxid;
-    }
-
-    private void close(TxnIterator itr) {
-        if (itr != null) {
-            try {
-                itr.close();
-            } catch (IOException ioe) {
-                LOG.warn("Error closing file iterator", ioe);
-            }
-        }
-    }
-
-    /**
-     * commit the logs. make sure that everything hits the
-     * disk
-     */
-    public synchronized void commit() throws IOException {
-        if (logStream != null) {
-            logStream.flush();
-        }
-        for (FileOutputStream log : streamsToFlush) {
-            log.flush();
-            if (forceSync) {
-                long startSyncNS = System.nanoTime();
-
-                FileChannel channel = log.getChannel();
-                channel.force(false);
-
-                syncElapsedMS = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startSyncNS);
-                if (syncElapsedMS > fsyncWarningThresholdMS) {
-                    if(serverStats != null) {
-                        serverStats.incrementFsyncThresholdExceedCount();
-                    }
-                    LOG.warn("fsync-ing the write ahead log in "
-                            + Thread.currentThread().getName()
-                            + " took " + syncElapsedMS
-                            + "ms which will adversely effect operation latency. "
-                            + "File size is " + channel.size() + " bytes. "
-                            + "See the ZooKeeper troubleshooting guide");
-                }
-            }
-        }
-        while (streamsToFlush.size() > 1) {
-            streamsToFlush.removeFirst().close();
-        }
-    }
-
-    /**
-     *
-     * @return elapsed sync time of transaction log in milliseconds
-     */
-    public long getTxnLogSyncElapsedTime() {
-        return syncElapsedMS;
-    }
-
-    /**
-     * start reading all the transactions from the given zxid
-     * @param zxid the zxid to start reading transactions from
-     * @return returns an iterator to iterate through the transaction
-     * logs
-     */
-    public TxnIterator read(long zxid) throws IOException {
-        return read(zxid, true);
-    }
-
-    /**
-     * start reading all the transactions from the given zxid.
-     *
-     * @param zxid the zxid to start reading transactions from
-     * @param fastForward true if the iterator should be fast forwarded to point
-     *        to the txn of a given zxid, else the iterator will point to the
-     *        starting txn of a txnlog that may contain txn of a given zxid
-     * @return returns an iterator to iterate through the transaction logs
-     */
-    public TxnIterator read(long zxid, boolean fastForward) throws IOException {
-        return new FileTxnIterator(logDir, zxid, fastForward);
-    }
-
-    /**
-     * truncate the current transaction logs
-     * @param zxid the zxid to truncate the logs to
-     * @return true if successful false if not
-     */
-    public boolean truncate(long zxid) throws IOException {
-        FileTxnIterator itr = null;
-        try {
-            itr = new FileTxnIterator(this.logDir, zxid);
-            PositionInputStream input = itr.inputStream;
-            if(input == null) {
-                throw new IOException("No log files found to truncate! This could " +
-                        "happen if you still have snapshots from an old setup or " +
-                        "log files were deleted accidentally or dataLogDir was changed in zoo.cfg.");
-            }
-            long pos = input.getPosition();
-            // now, truncate at the current position
-            RandomAccessFile raf=new RandomAccessFile(itr.logFile,"rw");
-            raf.setLength(pos);
-            raf.close();
-            while(itr.goToNextLog()) {
-                if (!itr.logFile.delete()) {
-                    LOG.warn("Unable to truncate {}", itr.logFile);
-                }
-            }
-        } finally {
-            close(itr);
-        }
-        return true;
-    }
-
-    /**
-     * read the header of the transaction file
-     * @param file the transaction file to read
-     * @return header that was read from the file
-     * @throws IOException
-     */
-    private static FileHeader readHeader(File file) throws IOException {
-        InputStream is =null;
-        try {
-            is = new BufferedInputStream(new FileInputStream(file));
-            InputArchive ia=BinaryInputArchive.getArchive(is);
-            FileHeader hdr = new FileHeader();
-            hdr.deserialize(ia, "fileheader");
-            return hdr;
-         } finally {
-             try {
-                 if (is != null) is.close();
-             } catch (IOException e) {
-                 LOG.warn("Ignoring exception during close", e);
-             }
-         }
-    }
-
-    /**
-     * the dbid of this transaction database
-     * @return the dbid of this database
-     */
-    public long getDbId() throws IOException {
-        FileTxnIterator itr = new FileTxnIterator(logDir, 0);
-        FileHeader fh=readHeader(itr.logFile);
-        itr.close();
-        if(fh==null)
-            throw new IOException("Unsupported Format.");
-        return fh.getDbid();
-    }
-
-    /**
-     * the forceSync value. true if forceSync is enabled, false otherwise.
-     * @return the forceSync value
-     */
-    public boolean isForceSync() {
-        return forceSync;
-    }
-
-    /**
-     * a class that keeps track of the position
-     * in the input stream. The position points to offset
-     * that has been consumed by the applications. It can
-     * wrap buffered input streams to provide the right offset
-     * for the application.
-     */
-    static class PositionInputStream extends FilterInputStream {
-        long position;
-        protected PositionInputStream(InputStream in) {
-            super(in);
-            position = 0;
-        }
-
-        @Override
-        public int read() throws IOException {
-            int rc = super.read();
-            if (rc > -1) {
-                position++;
-            }
-            return rc;
-        }
-
-        public int read(byte[] b) throws IOException {
-            int rc = super.read(b);
-            if (rc > 0) {
-                position += rc;
-            }
-            return rc;
-        }
-
-        @Override
-        public int read(byte[] b, int off, int len) throws IOException {
-            int rc = super.read(b, off, len);
-            if (rc > 0) {
-                position += rc;
-            }
-            return rc;
-        }
-
-        @Override
-        public long skip(long n) throws IOException {
-            long rc = super.skip(n);
-            if (rc > 0) {
-                position += rc;
-            }
-            return rc;
-        }
-        public long getPosition() {
-            return position;
-        }
-
-        @Override
-        public boolean markSupported() {
-            return false;
-        }
-
-        @Override
-        public void mark(int readLimit) {
-            throw new UnsupportedOperationException("mark");
-        }
-
-        @Override
-        public void reset() {
-            throw new UnsupportedOperationException("reset");
-        }
-    }
-
-    /**
-     * this class implements the txnlog iterator interface
-     * which is used for reading the transaction logs
-     */
-    public static class FileTxnIterator implements TxnLog.TxnIterator {
-        File logDir;
-        long zxid;
-        TxnHeader hdr;
-        Record record;
-        File logFile;
-        InputArchive ia;
-        static final String CRC_ERROR="CRC check failed";
-
-        PositionInputStream inputStream=null;
-        //stored files is the list of files greater than
-        //the zxid we are looking for.
-        private ArrayList<File> storedFiles;
-
-        /**
-         * create an iterator over a transaction database directory
-         * @param logDir the transaction database directory
-         * @param zxid the zxid to start reading from
-         * @param fastForward   true if the iterator should be fast forwarded to
-         *        point to the txn of a given zxid, else the iterator will
-         *        point to the starting txn of a txnlog that may contain txn of
-         *        a given zxid
-         * @throws IOException
-         */
-        public FileTxnIterator(File logDir, long zxid, boolean fastForward)
-                throws IOException {
-            this.logDir = logDir;
-            this.zxid = zxid;
-            init();
-
-            if (fastForward && hdr != null) {
-                while (hdr.getZxid() < zxid) {
-                    if (!next())
-                        break;
-                }
-            }
-        }
-
-        /**
-         * create an iterator over a transaction database directory
-         * @param logDir the transaction database directory
-         * @param zxid the zxid to start reading from
-         * @throws IOException
-         */
-        public FileTxnIterator(File logDir, long zxid) throws IOException {
-            this(logDir, zxid, true);
-        }
-
-        /**
-         * initialize to the zxid specified
-         * this is inclusive of the zxid
-         * @throws IOException
-         */
-        void init() throws IOException {
-            storedFiles = new ArrayList<File>();
-            List<File> files = Util.sortDataDir(FileTxnLog.getLogFiles(logDir.listFiles(), 0), LOG_FILE_PREFIX, false);
-            for (File f: files) {
-                if (Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) >= zxid) {
-                    storedFiles.add(f);
-                }
-                // add the last logfile that is less than the zxid
-                else if (Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) < zxid) {
-                    storedFiles.add(f);
-                    break;
-                }
-            }
-            goToNextLog();
-            next();
-        }
-
-        /**
-         * Return total storage size of txnlog that will return by this iterator.
-         */
-        public long getStorageSize() {
-            long sum = 0;
-            for (File f : storedFiles) {
-                sum += f.length();
-            }
-            return sum;
-        }
-
-        /**
-         * go to the next logfile
-         * @return true if there is one and false if there is no
-         * new file to be read
-         * @throws IOException
-         */
-        private boolean goToNextLog() throws IOException {
-            if (storedFiles.size() > 0) {
-                this.logFile = storedFiles.remove(storedFiles.size()-1);
-                ia = createInputArchive(this.logFile);
-                return true;
-            }
-            return false;
-        }
-
-        /**
-         * read the header from the inputarchive
-         * @param ia the inputarchive to be read from
-         * @param is the inputstream
-         * @throws IOException
-         */
-        protected void inStreamCreated(InputArchive ia, InputStream is)
-            throws IOException{
-            FileHeader header= new FileHeader();
-            header.deserialize(ia, "fileheader");
-            if (header.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
-                throw new IOException("Transaction log: " + this.logFile + " has invalid magic number "
-                        + header.getMagic()
-                        + " != " + FileTxnLog.TXNLOG_MAGIC);
-            }
-        }
-
-        /**
-         * Invoked to indicate that the input stream has been created.
-         * @param ia input archive
-         * @param is file input stream associated with the input archive.
-         * @throws IOException
-         **/
-        protected InputArchive createInputArchive(File logFile) throws IOException {
-            if(inputStream==null){
-                inputStream= new PositionInputStream(new BufferedInputStream(new FileInputStream(logFile)));
-                LOG.debug("Created new input stream " + logFile);
-                ia  = BinaryInputArchive.getArchive(inputStream);
-                inStreamCreated(ia,inputStream);
-                LOG.debug("Created new input archive " + logFile);
-            }
-            return ia;
-        }
-
-        /**
-         * create a checksum algorithm
-         * @return the checksum algorithm
-         */
-        protected Checksum makeChecksumAlgorithm(){
-            return new Adler32();
-        }
-
-        /**
-         * the iterator that moves to the next transaction
-         * @return true if there is more transactions to be read
-         * false if not.
-         */
-        public boolean next() throws IOException {
-            if (ia == null) {
-                return false;
-            }
-            try {
-                long crcValue = ia.readLong("crcvalue");
-                byte[] bytes = Util.readTxnBytes(ia);
-                // Since we preallocate, we define EOF to be an
-                if (bytes == null || bytes.length==0) {
-                    throw new EOFException("Failed to read " + logFile);
-                }
-                // EOF or corrupted record
-                // validate CRC
-                Checksum crc = makeChecksumAlgorithm();
-                crc.update(bytes, 0, bytes.length);
-                if (crcValue != crc.getValue())
-                    throw new IOException(CRC_ERROR);
-                hdr = new TxnHeader();
-                record = SerializeUtils.deserializeTxn(bytes, hdr);
-            } catch (EOFException e) {
-                LOG.debug("EOF exception " + e);
-                inputStream.close();
-                inputStream = null;
-                ia = null;
-                hdr = null;
-                // this means that the file has ended
-                // we should go to the next file
-                if (!goToNextLog()) {
-                    return false;
-                }
-                // if we went to the next log file, we should call next() again
-                return next();
-            } catch (IOException e) {
-                inputStream.close();
-                throw e;
-            }
-            return true;
-        }
-
-        /**
-         * return the current header
-         * @return the current header that
-         * is read
-         */
-        public TxnHeader getHeader() {
-            return hdr;
-        }
-
-        /**
-         * return the current transaction
-         * @return the current transaction
-         * that is read
-         */
-        public Record getTxn() {
-            return record;
-        }
-
-        /**
-         * close the iterator
-         * and release the resources.
-         */
-        public void close() throws IOException {
-            if (inputStream != null) {
-                inputStream.close();
-            }
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
deleted file mode 100644
index dbd5279..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
+++ /dev/null
@@ -1,507 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-
-import java.io.File;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.apache.jute.Record;
-import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.KeeperException.Code;
-import org.apache.zookeeper.ZooDefs.OpCode;
-import org.apache.zookeeper.server.DataTree;
-import org.apache.zookeeper.server.DataTree.ProcessTxnResult;
-import org.apache.zookeeper.server.Request;
-import org.apache.zookeeper.server.ServerStats;
-import org.apache.zookeeper.server.ZooTrace;
-import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator;
-import org.apache.zookeeper.txn.CreateSessionTxn;
-import org.apache.zookeeper.txn.TxnHeader;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * This is a helper class
- * above the implementations
- * of txnlog and snapshot
- * classes
- */
-public class FileTxnSnapLog {
-    //the direcotry containing the
-    //the transaction logs
-    private final File dataDir;
-    //the directory containing the
-    //the snapshot directory
-    private final File snapDir;
-    private TxnLog txnLog;
-    private SnapShot snapLog;
-    public final static int VERSION = 2;
-    public final static String version = "version-";
-
-    private static final Logger LOG = LoggerFactory.getLogger(FileTxnSnapLog.class);
-
-    public static final String ZOOKEEPER_DATADIR_AUTOCREATE =
-            "zookeeper.datadir.autocreate";
-
-    public static final String ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT = "true";
-
-    /**
-     * This listener helps
-     * the external apis calling
-     * restore to gather information
-     * while the data is being
-     * restored.
-     */
-    public interface PlayBackListener {
-        void onTxnLoaded(TxnHeader hdr, Record rec);
-    }
-
-    /**
-     * the constructor which takes the datadir and
-     * snapdir.
-     * @param dataDir the transaction directory
-     * @param snapDir the snapshot directory
-     */
-    public FileTxnSnapLog(File dataDir, File snapDir) throws IOException {
-        LOG.debug("Opening datadir:{} snapDir:{}", dataDir, snapDir);
-
-        this.dataDir = new File(dataDir, version + VERSION);
-        this.snapDir = new File(snapDir, version + VERSION);
-
-        // by default create snap/log dirs, but otherwise complain instead
-        // See ZOOKEEPER-1161 for more details
-        boolean enableAutocreate = Boolean.valueOf(
-                System.getProperty(ZOOKEEPER_DATADIR_AUTOCREATE,
-                        ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT));
-
-        if (!this.dataDir.exists()) {
-            if (!enableAutocreate) {
-                throw new DatadirException("Missing data directory "
-                        + this.dataDir
-                        + ", automatic data directory creation is disabled ("
-                        + ZOOKEEPER_DATADIR_AUTOCREATE
-                        + " is false). Please create this directory manually.");
-            }
-
-            if (!this.dataDir.mkdirs()) {
-                throw new DatadirException("Unable to create data directory "
-                        + this.dataDir);
-            }
-        }
-        if (!this.dataDir.canWrite()) {
-            throw new DatadirException("Cannot write to data directory " + this.dataDir);
-        }
-
-        if (!this.snapDir.exists()) {
-            // by default create this directory, but otherwise complain instead
-            // See ZOOKEEPER-1161 for more details
-            if (!enableAutocreate) {
-                throw new DatadirException("Missing snap directory "
-                        + this.snapDir
-                        + ", automatic data directory creation is disabled ("
-                        + ZOOKEEPER_DATADIR_AUTOCREATE
-                        + " is false). Please create this directory manually.");
-            }
-
-            if (!this.snapDir.mkdirs()) {
-                throw new DatadirException("Unable to create snap directory "
-                        + this.snapDir);
-            }
-        }
-        if (!this.snapDir.canWrite()) {
-            throw new DatadirException("Cannot write to snap directory " + this.snapDir);
-        }
-
-        // check content of transaction log and snapshot dirs if they are two different directories
-        // See ZOOKEEPER-2967 for more details
-        if(!this.dataDir.getPath().equals(this.snapDir.getPath())){
-            checkLogDir();
-            checkSnapDir();
-        }
-
-        txnLog = new FileTxnLog(this.dataDir);
-        snapLog = new FileSnap(this.snapDir);
-    }
-
-    public void setServerStats(ServerStats serverStats) {
-        txnLog.setServerStats(serverStats);
-    }
-
-    private void checkLogDir() throws LogDirContentCheckException {
-        File[] files = this.dataDir.listFiles(new FilenameFilter() {
-            @Override
-            public boolean accept(File dir, String name) {
-                return Util.isSnapshotFileName(name);
-            }
-        });
-        if (files != null && files.length > 0) {
-            throw new LogDirContentCheckException("Log directory has snapshot files. Check if dataLogDir and dataDir configuration is correct.");
-        }
-    }
-
-    private void checkSnapDir() throws SnapDirContentCheckException {
-        File[] files = this.snapDir.listFiles(new FilenameFilter() {
-            @Override
-            public boolean accept(File dir, String name) {
-                return Util.isLogFileName(name);
-            }
-        });
-        if (files != null && files.length > 0) {
-            throw new SnapDirContentCheckException("Snapshot directory has log files. Check if dataLogDir and dataDir configuration is correct.");
-        }
-    }
-
-    /**
-     * get the datadir used by this filetxn
-     * snap log
-     * @return the data dir
-     */
-    public File getDataDir() {
-        return this.dataDir;
-    }
-
-    /**
-     * get the snap dir used by this
-     * filetxn snap log
-     * @return the snap dir
-     */
-    public File getSnapDir() {
-        return this.snapDir;
-    }
-
-    /**
-     * this function restores the server
-     * database after reading from the
-     * snapshots and transaction logs
-     * @param dt the datatree to be restored
-     * @param sessions the sessions to be restored
-     * @param listener the playback listener to run on the
-     * database restoration
-     * @return the highest zxid restored
-     * @throws IOException
-     */
-    public long restore(DataTree dt, Map<Long, Integer> sessions,
-                        PlayBackListener listener) throws IOException {
-        long deserializeResult = snapLog.deserialize(dt, sessions);
-        FileTxnLog txnLog = new FileTxnLog(dataDir);
-        if (-1L == deserializeResult) {
-            /* this means that we couldn't find any snapshot, so we need to
-             * initialize an empty database (reported in ZOOKEEPER-2325) */
-            if (txnLog.getLastLoggedZxid() != -1) {
-                throw new IOException(
-                        "No snapshot found, but there are log entries. " +
-                        "Something is broken!");
-            }
-            /* TODO: (br33d) we should either put a ConcurrentHashMap on restore()
-             *       or use Map on save() */
-            save(dt, (ConcurrentHashMap<Long, Integer>)sessions);
-            /* return a zxid of zero, since we the database is empty */
-            return 0;
-        }
-        return fastForwardFromEdits(dt, sessions, listener);
-    }
-
-    /**
-     * This function will fast forward the server database to have the latest
-     * transactions in it.  This is the same as restore, but only reads from
-     * the transaction logs and not restores from a snapshot.
-     * @param dt the datatree to write transactions to.
-     * @param sessions the sessions to be restored.
-     * @param listener the playback listener to run on the
-     * database transactions.
-     * @return the highest zxid restored.
-     * @throws IOException
-     */
-    public long fastForwardFromEdits(DataTree dt, Map<Long, Integer> sessions,
-                                     PlayBackListener listener) throws IOException {
-        TxnIterator itr = txnLog.read(dt.lastProcessedZxid+1);
-        long highestZxid = dt.lastProcessedZxid;
-        TxnHeader hdr;
-        try {
-            while (true) {
-                // iterator points to
-                // the first valid txn when initialized
-                hdr = itr.getHeader();
-                if (hdr == null) {
-                    //empty logs
-                    return dt.lastProcessedZxid;
-                }
-                if (hdr.getZxid() < highestZxid && highestZxid != 0) {
-                    LOG.error("{}(highestZxid) > {}(next log) for type {}",
-                            highestZxid, hdr.getZxid(), hdr.getType());
-                } else {
-                    highestZxid = hdr.getZxid();
-                }
-                try {
-                    processTransaction(hdr,dt,sessions, itr.getTxn());
-                } catch(KeeperException.NoNodeException e) {
-                   throw new IOException("Failed to process transaction type: " +
-                         hdr.getType() + " error: " + e.getMessage(), e);
-                }
-                listener.onTxnLoaded(hdr, itr.getTxn());
-                if (!itr.next())
-                    break;
-            }
-        } finally {
-            if (itr != null) {
-                itr.close();
-            }
-        }
-        return highestZxid;
-    }
-
-    /**
-     * Get TxnIterator for iterating through txnlog starting at a given zxid
-     *
-     * @param zxid starting zxid
-     * @return TxnIterator
-     * @throws IOException
-     */
-    public TxnIterator readTxnLog(long zxid) throws IOException {
-        return readTxnLog(zxid, true);
-    }
-
-    /**
-     * Get TxnIterator for iterating through txnlog starting at a given zxid
-     *
-     * @param zxid starting zxid
-     * @param fastForward true if the iterator should be fast forwarded to point
-     *        to the txn of a given zxid, else the iterator will point to the
-     *        starting txn of a txnlog that may contain txn of a given zxid
-     * @return TxnIterator
-     * @throws IOException
-     */
-    public TxnIterator readTxnLog(long zxid, boolean fastForward)
-            throws IOException {
-        FileTxnLog txnLog = new FileTxnLog(dataDir);
-        return txnLog.read(zxid, fastForward);
-    }
-    
-    /**
-     * process the transaction on the datatree
-     * @param hdr the hdr of the transaction
-     * @param dt the datatree to apply transaction to
-     * @param sessions the sessions to be restored
-     * @param txn the transaction to be applied
-     */
-    public void processTransaction(TxnHeader hdr,DataTree dt,
-            Map<Long, Integer> sessions, Record txn)
-        throws KeeperException.NoNodeException {
-        ProcessTxnResult rc;
-        switch (hdr.getType()) {
-        case OpCode.createSession:
-            sessions.put(hdr.getClientId(),
-                    ((CreateSessionTxn) txn).getTimeOut());
-            if (LOG.isTraceEnabled()) {
-                ZooTrace.logTraceMessage(LOG,ZooTrace.SESSION_TRACE_MASK,
-                        "playLog --- create session in log: 0x"
-                                + Long.toHexString(hdr.getClientId())
-                                + " with timeout: "
-                                + ((CreateSessionTxn) txn).getTimeOut());
-            }
-            // give dataTree a chance to sync its lastProcessedZxid
-            rc = dt.processTxn(hdr, txn);
-            break;
-        case OpCode.closeSession:
-            sessions.remove(hdr.getClientId());
-            if (LOG.isTraceEnabled()) {
-                ZooTrace.logTraceMessage(LOG,ZooTrace.SESSION_TRACE_MASK,
-                        "playLog --- close session in log: 0x"
-                                + Long.toHexString(hdr.getClientId()));
-            }
-            rc = dt.processTxn(hdr, txn);
-            break;
-        default:
-            rc = dt.processTxn(hdr, txn);
-        }
-
-        /**
-         * Snapshots are lazily created. So when a snapshot is in progress,
-         * there is a chance for later transactions to make into the
-         * snapshot. Then when the snapshot is restored, NONODE/NODEEXISTS
-         * errors could occur. It should be safe to ignore these.
-         */
-        if (rc.err != Code.OK.intValue()) {
-            LOG.debug(
-                    "Ignoring processTxn failure hdr: {}, error: {}, path: {}",
-                    hdr.getType(), rc.err, rc.path);
-        }
-    }
-
-    /**
-     * the last logged zxid on the transaction logs
-     * @return the last logged zxid
-     */
-    public long getLastLoggedZxid() {
-        FileTxnLog txnLog = new FileTxnLog(dataDir);
-        return txnLog.getLastLoggedZxid();
-    }
-
-    /**
-     * save the datatree and the sessions into a snapshot
-     * @param dataTree the datatree to be serialized onto disk
-     * @param sessionsWithTimeouts the session timeouts to be
-     * serialized onto disk
-     * @throws IOException
-     */
-    public void save(DataTree dataTree,
-            ConcurrentHashMap<Long, Integer> sessionsWithTimeouts)
-        throws IOException {
-        long lastZxid = dataTree.lastProcessedZxid;
-        File snapshotFile = new File(snapDir, Util.makeSnapshotName(lastZxid));
-        LOG.info("Snapshotting: 0x{} to {}", Long.toHexString(lastZxid),
-                snapshotFile);
-        snapLog.serialize(dataTree, sessionsWithTimeouts, snapshotFile);
-
-    }
-
-    /**
-     * truncate the transaction logs the zxid
-     * specified
-     * @param zxid the zxid to truncate the logs to
-     * @return true if able to truncate the log, false if not
-     * @throws IOException
-     */
-    public boolean truncateLog(long zxid) throws IOException {
-        // close the existing txnLog and snapLog
-        close();
-
-        // truncate it
-        FileTxnLog truncLog = new FileTxnLog(dataDir);
-        boolean truncated = truncLog.truncate(zxid);
-        truncLog.close();
-
-        // re-open the txnLog and snapLog
-        // I'd rather just close/reopen this object itself, however that 
-        // would have a big impact outside ZKDatabase as there are other
-        // objects holding a reference to this object.
-        txnLog = new FileTxnLog(dataDir);
-        snapLog = new FileSnap(snapDir);
-
-        return truncated;
-    }
-
-    /**
-     * the most recent snapshot in the snapshot
-     * directory
-     * @return the file that contains the most
-     * recent snapshot
-     * @throws IOException
-     */
-    public File findMostRecentSnapshot() throws IOException {
-        FileSnap snaplog = new FileSnap(snapDir);
-        return snaplog.findMostRecentSnapshot();
-    }
-
-    /**
-     * the n most recent snapshots
-     * @param n the number of recent snapshots
-     * @return the list of n most recent snapshots, with
-     * the most recent in front
-     * @throws IOException
-     */
-    public List<File> findNRecentSnapshots(int n) throws IOException {
-        FileSnap snaplog = new FileSnap(snapDir);
-        return snaplog.findNRecentSnapshots(n);
-    }
-
-    /**
-     * get the snapshot logs which may contain transactions newer than the given zxid.
-     * This includes logs with starting zxid greater than given zxid, as well as the
-     * newest transaction log with starting zxid less than given zxid.  The latter log
-     * file may contain transactions beyond given zxid.
-     * @param zxid the zxid that contains logs greater than
-     * zxid
-     * @return
-     */
-    public File[] getSnapshotLogs(long zxid) {
-        return FileTxnLog.getLogFiles(dataDir.listFiles(), zxid);
-    }
-
-    /**
-     * append the request to the transaction logs
-     * @param si the request to be appended
-     * returns true iff something appended, otw false
-     * @throws IOException
-     */
-    public boolean append(Request si) throws IOException {
-        return txnLog.append(si.getHdr(), si.getTxn());
-    }
-
-    /**
-     * commit the transaction of logs
-     * @throws IOException
-     */
-    public void commit() throws IOException {
-        txnLog.commit();
-    }
-
-    /**
-     *
-     * @return elapsed sync time of transaction log commit in milliseconds
-     */
-    public long getTxnLogElapsedSyncTime() {
-        return txnLog.getTxnLogSyncElapsedTime();
-    }
-
-    /**
-     * roll the transaction logs
-     * @throws IOException
-     */
-    public void rollLog() throws IOException {
-        txnLog.rollLog();
-    }
-
-    /**
-     * close the transaction log files
-     * @throws IOException
-     */
-    public void close() throws IOException {
-        txnLog.close();
-        snapLog.close();
-    }
-
-    @SuppressWarnings("serial")
-    public static class DatadirException extends IOException {
-        public DatadirException(String msg) {
-            super(msg);
-        }
-        public DatadirException(String msg, Exception e) {
-            super(msg, e);
-        }
-    }
-
-    @SuppressWarnings("serial")
-    public static class LogDirContentCheckException extends DatadirException {
-        public LogDirContentCheckException(String msg) {
-            super(msg);
-        }
-    }
-
-    @SuppressWarnings("serial")
-    public static class SnapDirContentCheckException extends DatadirException {
-        public SnapDirContentCheckException(String msg) {
-            super(msg);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java b/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java
deleted file mode 100644
index c964afc..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/SnapShot.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-import java.io.File;
-import java.io.IOException;
-import java.util.Map;
-
-import org.apache.zookeeper.server.DataTree;
-
-/**
- * snapshot interface for the persistence layer.
- * implement this interface for implementing 
- * snapshots.
- */
-public interface SnapShot {
-    
-    /**
-     * deserialize a data tree from the last valid snapshot and 
-     * return the last zxid that was deserialized
-     * @param dt the datatree to be deserialized into
-     * @param sessions the sessions to be deserialized into
-     * @return the last zxid that was deserialized from the snapshot
-     * @throws IOException
-     */
-    long deserialize(DataTree dt, Map<Long, Integer> sessions) 
-        throws IOException;
-    
-    /**
-     * persist the datatree and the sessions into a persistence storage
-     * @param dt the datatree to be serialized
-     * @param sessions 
-     * @throws IOException
-     */
-    void serialize(DataTree dt, Map<Long, Integer> sessions, 
-            File name) 
-        throws IOException;
-    
-    /**
-     * find the most recent snapshot file
-     * @return the most recent snapshot file
-     * @throws IOException
-     */
-    File findMostRecentSnapshot() throws IOException;
-    
-    /**
-     * free resources from this snapshot immediately
-     * @throws IOException
-     */
-    void close() throws IOException;
-} 

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
deleted file mode 100644
index 9781f45..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
+++ /dev/null
@@ -1,141 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-
-import java.io.IOException;
-
-import org.apache.jute.Record;
-import org.apache.zookeeper.server.ServerStats;
-import org.apache.zookeeper.txn.TxnHeader;
-
-/**
- * Interface for reading transaction logs.
- *
- */
-public interface TxnLog {
-
-    /**
-     * Setter for ServerStats to monitor fsync threshold exceed
-     * @param serverStats used to update fsyncThresholdExceedCount
-     */
-    void setServerStats(ServerStats serverStats);
-    
-    /**
-     * roll the current
-     * log being appended to
-     * @throws IOException 
-     */
-    void rollLog() throws IOException;
-    /**
-     * Append a request to the transaction log
-     * @param hdr the transaction header
-     * @param r the transaction itself
-     * returns true iff something appended, otw false 
-     * @throws IOException
-     */
-    boolean append(TxnHeader hdr, Record r) throws IOException;
-
-    /**
-     * Start reading the transaction logs
-     * from a given zxid
-     * @param zxid
-     * @return returns an iterator to read the 
-     * next transaction in the logs.
-     * @throws IOException
-     */
-    TxnIterator read(long zxid) throws IOException;
-    
-    /**
-     * the last zxid of the logged transactions.
-     * @return the last zxid of the logged transactions.
-     * @throws IOException
-     */
-    long getLastLoggedZxid() throws IOException;
-    
-    /**
-     * truncate the log to get in sync with the 
-     * leader.
-     * @param zxid the zxid to truncate at.
-     * @throws IOException 
-     */
-    boolean truncate(long zxid) throws IOException;
-    
-    /**
-     * the dbid for this transaction log. 
-     * @return the dbid for this transaction log.
-     * @throws IOException
-     */
-    long getDbId() throws IOException;
-    
-    /**
-     * commit the transaction and make sure
-     * they are persisted
-     * @throws IOException
-     */
-    void commit() throws IOException;
-
-    /**
-     *
-     * @return transaction log's elapsed sync time in milliseconds
-     */
-    long getTxnLogSyncElapsedTime();
-   
-    /** 
-     * close the transactions logs
-     */
-    void close() throws IOException;
-    /**
-     * an iterating interface for reading 
-     * transaction logs. 
-     */
-    public interface TxnIterator {
-        /**
-         * return the transaction header.
-         * @return return the transaction header.
-         */
-        TxnHeader getHeader();
-        
-        /**
-         * return the transaction record.
-         * @return return the transaction record.
-         */
-        Record getTxn();
-     
-        /**
-         * go to the next transaction record.
-         * @throws IOException
-         */
-        boolean next() throws IOException;
-        
-        /**
-         * close files and release the 
-         * resources
-         * @throws IOException
-         */
-        void close() throws IOException;
-        
-        /**
-         * Get an estimated storage space used to store transaction records
-         * that will return by this iterator
-         * @throws IOException
-         */
-        long getStorageSize() throws IOException;
-    }
-}
-

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java
deleted file mode 100644
index e47d55b..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java
+++ /dev/null
@@ -1,355 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-
-import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
-import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.Option;
-import org.apache.commons.cli.Options;
-import org.apache.commons.cli.ParseException;
-import org.apache.commons.cli.PosixParser;
-import org.apache.jute.BinaryInputArchive;
-import org.apache.jute.BinaryOutputArchive;
-import org.apache.jute.Record;
-import org.apache.zookeeper.server.TraceFormatter;
-import org.apache.zookeeper.server.util.SerializeUtils;
-import org.apache.zookeeper.txn.CreateContainerTxn;
-import org.apache.zookeeper.txn.CreateTTLTxn;
-import org.apache.zookeeper.txn.CreateTxn;
-import org.apache.zookeeper.txn.SetDataTxn;
-import org.apache.zookeeper.txn.TxnHeader;
-
-import java.io.Closeable;
-import java.io.EOFException;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.text.DateFormat;
-import java.util.Date;
-import java.util.Scanner;
-import java.util.zip.Adler32;
-import java.util.zip.Checksum;
-
-import static org.apache.zookeeper.server.persistence.FileTxnLog.TXNLOG_MAGIC;
-
-public class TxnLogToolkit implements Closeable {
-
-    static class TxnLogToolkitException extends Exception {
-        private static final long serialVersionUID = 1L;
-        private int exitCode;
-
-        TxnLogToolkitException(int exitCode, String message, Object... params) {
-            super(String.format(message, params));
-            this.exitCode = exitCode;
-        }
-
-        int getExitCode() {
-            return exitCode;
-        }
-    }
-
-    static class TxnLogToolkitParseException extends TxnLogToolkitException {
-        private static final long serialVersionUID = 1L;
-        private Options options;
-
-        TxnLogToolkitParseException(Options options, int exitCode, String message, Object... params) {
-            super(exitCode, message, params);
-            this.options = options;
-        }
-
-        Options getOptions() {
-            return options;
-        }
-    }
-
-    private File txnLogFile;
-    private boolean recoveryMode = false;
-    private boolean verbose = false;
-    private FileInputStream txnFis;
-    private BinaryInputArchive logStream;
-
-    // Recovery mode
-    private int crcFixed = 0;
-    private FileOutputStream recoveryFos;
-    private BinaryOutputArchive recoveryOa;
-    private File recoveryLogFile;
-    private FilePadding filePadding = new FilePadding();
-    private boolean force = false;
-
-    /**
-     * @param args Command line arguments
-     */
-    public static void main(String[] args) throws Exception {
-        try (final TxnLogToolkit lt = parseCommandLine(args)) {
-            lt.dump(new Scanner(System.in));
-            lt.printStat();
-        } catch (TxnLogToolkitParseException e) {
-            System.err.println(e.getMessage() + "\n");
-            printHelpAndExit(e.getExitCode(), e.getOptions());
-        } catch (TxnLogToolkitException e) {
-            System.err.println(e.getMessage());
-            System.exit(e.getExitCode());
-        }
-    }
-
-    public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileName, boolean force)
-            throws FileNotFoundException, TxnLogToolkitException {
-        this.recoveryMode = recoveryMode;
-        this.verbose = verbose;
-        this.force = force;
-        txnLogFile = new File(txnLogFileName);
-        if (!txnLogFile.exists() || !txnLogFile.canRead()) {
-            throw new TxnLogToolkitException(1, "File doesn't exist or not readable: %s", txnLogFile);
-        }
-        if (recoveryMode) {
-            recoveryLogFile = new File(txnLogFile.toString() + ".fixed");
-            if (recoveryLogFile.exists()) {
-                throw new TxnLogToolkitException(1, "Recovery file %s already exists or not writable", recoveryLogFile);
-            }
-        }
-
-        openTxnLogFile();
-        if (recoveryMode) {
-            openRecoveryFile();
-        }
-    }
-
-    public void dump(Scanner scanner) throws Exception {
-        crcFixed = 0;
-
-        FileHeader fhdr = new FileHeader();
-        fhdr.deserialize(logStream, "fileheader");
-        if (fhdr.getMagic() != TXNLOG_MAGIC) {
-            throw new TxnLogToolkitException(2, "Invalid magic number for %s", txnLogFile.getName());
-        }
-        System.out.println("ZooKeeper Transactional Log File with dbid "
-                + fhdr.getDbid() + " txnlog format version "
-                + fhdr.getVersion());
-
-        if (recoveryMode) {
-            fhdr.serialize(recoveryOa, "fileheader");
-            recoveryFos.flush();
-            filePadding.setCurrentSize(recoveryFos.getChannel().position());
-        }
-
-        int count = 0;
-        while (true) {
-            long crcValue;
-            byte[] bytes;
-            try {
-                crcValue = logStream.readLong("crcvalue");
-                bytes = logStream.readBuffer("txnEntry");
-            } catch (EOFException e) {
-                System.out.println("EOF reached after " + count + " txns.");
-                return;
-            }
-            if (bytes.length == 0) {
-                // Since we preallocate, we define EOF to be an
-                // empty transaction
-                System.out.println("EOF reached after " + count + " txns.");
-                return;
-            }
-            Checksum crc = new Adler32();
-            crc.update(bytes, 0, bytes.length);
-            if (crcValue != crc.getValue()) {
-                if (recoveryMode) {
-                    if (!force) {
-                        printTxn(bytes, "CRC ERROR");
-                        if (askForFix(scanner)) {
-                            crcValue = crc.getValue();
-                            ++crcFixed;
-                        }
-                    } else {
-                        crcValue = crc.getValue();
-                        printTxn(bytes, "CRC FIXED");
-                        ++crcFixed;
-                    }
-                } else {
-                    printTxn(bytes, "CRC ERROR");
-                }
-            }
-            if (!recoveryMode || verbose) {
-                printTxn(bytes);
-            }
-            if (logStream.readByte("EOR") != 'B') {
-                throw new TxnLogToolkitException(1, "Last transaction was partial.");
-            }
-            if (recoveryMode) {
-                filePadding.padFile(recoveryFos.getChannel());
-                recoveryOa.writeLong(crcValue, "crcvalue");
-                recoveryOa.writeBuffer(bytes, "txnEntry");
-                recoveryOa.writeByte((byte)'B', "EOR");
-            }
-            count++;
-        }
-    }
-
-    private boolean askForFix(Scanner scanner) throws TxnLogToolkitException {
-        while (true) {
-            System.out.print("Would you like to fix it (Yes/No/Abort) ? ");
-            char answer = Character.toUpperCase(scanner.next().charAt(0));
-            switch (answer) {
-                case 'Y':
-                    return true;
-                case 'N':
-                    return false;
-                case 'A':
-                    throw new TxnLogToolkitException(0, "Recovery aborted.");
-            }
-        }
-    }
-
-    private void printTxn(byte[] bytes) throws IOException {
-        printTxn(bytes, "");
-    }
-
-    private void printTxn(byte[] bytes, String prefix) throws IOException {
-        TxnHeader hdr = new TxnHeader();
-        Record txn = SerializeUtils.deserializeTxn(bytes, hdr);
-        String txnStr = getDataStrFromTxn(txn);
-        String txns = String.format("%s session 0x%s cxid 0x%s zxid 0x%s %s %s",
-                DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())),
-                Long.toHexString(hdr.getClientId()),
-                Long.toHexString(hdr.getCxid()),
-                Long.toHexString(hdr.getZxid()),
-                TraceFormatter.op2String(hdr.getType()),
-                txnStr);
-        if (prefix != null && !"".equals(prefix.trim())) {
-            System.out.print(prefix + " - ");
-        }
-        if (txns.endsWith("\n")) {
-            System.out.print(txns);
-        } else {
-            System.out.println(txns);
-        }
-    }
-
-    /**
-     * get transaction log data string with node's data as a string
-     * @param txn
-     * @return
-     */
-    private static String getDataStrFromTxn(Record txn) {
-        StringBuilder txnData = new StringBuilder();
-        if (txn == null) {
-            return txnData.toString();
-        }
-        if (txn instanceof CreateTxn) {
-            CreateTxn createTxn = ((CreateTxn) txn);
-            txnData.append(createTxn.getPath() + "," + new String(createTxn.getData()))
-                   .append("," + createTxn.getAcl() + "," + createTxn.getEphemeral())
-                   .append("," + createTxn.getParentCVersion());
-        } else if (txn instanceof SetDataTxn) {
-            SetDataTxn setDataTxn = ((SetDataTxn) txn);
-            txnData.append(setDataTxn.getPath() + "," + new String(setDataTxn.getData()))
-                   .append("," + setDataTxn.getVersion());
-        } else if (txn instanceof CreateContainerTxn) {
-            CreateContainerTxn createContainerTxn = ((CreateContainerTxn) txn);
-            txnData.append(createContainerTxn.getPath() + "," + new String(createContainerTxn.getData()))
-                   .append("," + createContainerTxn.getAcl() + "," + createContainerTxn.getParentCVersion());
-        } else if (txn instanceof CreateTTLTxn) {
-            CreateTTLTxn createTTLTxn = ((CreateTTLTxn) txn);
-            txnData.append(createTTLTxn.getPath() + "," + new String(createTTLTxn.getData()))
-                   .append("," + createTTLTxn.getAcl() + "," + createTTLTxn.getParentCVersion())
-                   .append("," + createTTLTxn.getTtl());
-        } else {
-            txnData.append(txn.toString());
-        }
-
-        return txnData.toString();
-    }
-    
-    private void openTxnLogFile() throws FileNotFoundException {
-        txnFis = new FileInputStream(txnLogFile);
-        logStream = BinaryInputArchive.getArchive(txnFis);
-    }
-
-    private void closeTxnLogFile() throws IOException {
-        if (txnFis != null) {
-            txnFis.close();
-        }
-    }
-
-    private void openRecoveryFile() throws FileNotFoundException {
-        recoveryFos = new FileOutputStream(recoveryLogFile);
-        recoveryOa = BinaryOutputArchive.getArchive(recoveryFos);
-    }
-
-    private void closeRecoveryFile() throws IOException {
-        if (recoveryFos != null) {
-            recoveryFos.close();
-        }
-    }
-
-    private static TxnLogToolkit parseCommandLine(String[] args) throws TxnLogToolkitException, FileNotFoundException {
-        CommandLineParser parser = new PosixParser();
-        Options options = new Options();
-
-        Option helpOpt = new Option("h", "help", false, "Print help message");
-        options.addOption(helpOpt);
-
-        Option recoverOpt = new Option("r", "recover", false, "Recovery mode. Re-calculate CRC for broken entries.");
-        options.addOption(recoverOpt);
-
-        Option quietOpt = new Option("v", "verbose", false, "Be verbose in recovery mode: print all entries, not just fixed ones.");
-        options.addOption(quietOpt);
-
-        Option dumpOpt = new Option("d", "dump", false, "Dump mode. Dump all entries of the log file with printing the content of a nodepath (default)");
-        options.addOption(dumpOpt);
-
-        Option forceOpt = new Option("y", "yes", false, "Non-interactive mode: repair all CRC errors without asking");
-        options.addOption(forceOpt);
-
-        try {
-            CommandLine cli = parser.parse(options, args);
-            if (cli.hasOption("help")) {
-                printHelpAndExit(0, options);
-            }
-            if (cli.getArgs().length < 1) {
-                printHelpAndExit(1, options);
-            }
-            return new TxnLogToolkit(cli.hasOption("recover"), cli.hasOption("verbose"), cli.getArgs()[0], cli.hasOption("yes"));
-        } catch (ParseException e) {
-            throw new TxnLogToolkitParseException(options, 1, e.getMessage());
-        }
-    }
-
-    private static void printHelpAndExit(int exitCode, Options options) {
-        HelpFormatter help = new HelpFormatter();
-        help.printHelp(120,"TxnLogToolkit [-dhrv] <txn_log_file_name>", "", options, "");
-        System.exit(exitCode);
-    }
-
-    private void printStat() {
-        if (recoveryMode) {
-            System.out.printf("Recovery file %s has been written with %d fixed CRC error(s)%n", recoveryLogFile, crcFixed);
-        }
-    }
-
-    @Override
-    public void close() throws IOException {
-        if (recoveryMode) {
-            closeRecoveryFile();
-        }
-        closeTxnLogFile();
-    }
-}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/3ce1b18b/src/java/main/org/apache/zookeeper/server/persistence/Util.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/Util.java b/src/java/main/org/apache/zookeeper/server/persistence/Util.java
deleted file mode 100644
index 8efc772..0000000
--- a/src/java/main/org/apache/zookeeper/server/persistence/Util.java
+++ /dev/null
@@ -1,318 +0,0 @@
-/**
- * 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.zookeeper.server.persistence;
-
-import java.io.ByteArrayOutputStream;
-import java.io.EOFException;
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.io.Serializable;
-import java.net.URI;
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Properties;
-
-import org.apache.jute.BinaryOutputArchive;
-import org.apache.jute.InputArchive;
-import org.apache.jute.OutputArchive;
-import org.apache.jute.Record;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.zookeeper.txn.TxnHeader;
-
-/**
- * A collection of utility methods for dealing with file name parsing, 
- * low level I/O file operations and marshalling/unmarshalling.
- */
-public class Util {
-    private static final Logger LOG = LoggerFactory.getLogger(Util.class);
-    private static final String SNAP_DIR="snapDir";
-    private static final String LOG_DIR="logDir";
-    private static final String DB_FORMAT_CONV="dbFormatConversion";
-    
-    public static String makeURIString(String dataDir, String dataLogDir, 
-            String convPolicy){
-        String uri="file:"+SNAP_DIR+"="+dataDir+";"+LOG_DIR+"="+dataLogDir;
-        if(convPolicy!=null)
-            uri+=";"+DB_FORMAT_CONV+"="+convPolicy;
-        return uri.replace('\\', '/');
-    }
-    /**
-     * Given two directory files the method returns a well-formed 
-     * logfile provider URI. This method is for backward compatibility with the
-     * existing code that only supports logfile persistence and expects these two
-     * parameters passed either on the command-line or in the configuration file.
-     * 
-     * @param dataDir snapshot directory
-     * @param dataLogDir transaction log directory
-     * @return logfile provider URI
-     */
-    public static URI makeFileLoggerURL(File dataDir, File dataLogDir){
-        return URI.create(makeURIString(dataDir.getPath(),dataLogDir.getPath(),null));
-    }
-    
-    public static URI makeFileLoggerURL(File dataDir, File dataLogDir,String convPolicy){
-        return URI.create(makeURIString(dataDir.getPath(),dataLogDir.getPath(),convPolicy));
-    }
-
-    /**
-     * Creates a valid transaction log file name. 
-     * 
-     * @param zxid used as a file name suffix (extension)
-     * @return file name
-     */
-    public static String makeLogName(long zxid) {
-        return FileTxnLog.LOG_FILE_PREFIX + "." + Long.toHexString(zxid);
-    }
-
-    /**
-     * Creates a snapshot file name.
-     * 
-     * @param zxid used as a suffix
-     * @return file name
-     */
-    public static String makeSnapshotName(long zxid) {
-        return FileSnap.SNAPSHOT_FILE_PREFIX + "." + Long.toHexString(zxid);
-    }
-    
-    /**
-     * Extracts snapshot directory property value from the container.
-     * 
-     * @param props properties container
-     * @return file representing the snapshot directory
-     */
-    public static File getSnapDir(Properties props){
-        return new File(props.getProperty(SNAP_DIR));
-    }
-
-    /**
-     * Extracts transaction log directory property value from the container.
-     * 
-     * @param props properties container
-     * @return file representing the txn log directory
-     */
-    public static File getLogDir(Properties props){
-        return new File(props.getProperty(LOG_DIR));
-    }
-    
-    /**
-     * Extracts the value of the dbFormatConversion attribute.
-     * 
-     * @param props properties container
-     * @return value of the dbFormatConversion attribute
-     */
-    public static String getFormatConversionPolicy(Properties props){
-        return props.getProperty(DB_FORMAT_CONV);
-    }
-   
-    /**
-     * Extracts zxid from the file name. The file name should have been created
-     * using one of the {@link #makeLogName(long)} or {@link #makeSnapshotName(long)}.
-     * 
-     * @param name the file name to parse
-     * @param prefix the file name prefix (snapshot or log)
-     * @return zxid
-     */
-    public static long getZxidFromName(String name, String prefix) {
-        long zxid = -1;
-        String nameParts[] = name.split("\\.");
-        if (nameParts.length == 2 && nameParts[0].equals(prefix)) {
-            try {
-                zxid = Long.parseLong(nameParts[1], 16);
-            } catch (NumberFormatException e) {
-            }
-        }
-        return zxid;
-    }
-
-    /**
-     * Verifies that the file is a valid snapshot. Snapshot may be invalid if 
-     * it's incomplete as in a situation when the server dies while in the process
-     * of storing a snapshot. Any file that is not a snapshot is also 
-     * an invalid snapshot. 
-     * 
-     * @param f file to verify
-     * @return true if the snapshot is valid
-     * @throws IOException
-     */
-    public static boolean isValidSnapshot(File f) throws IOException {
-        if (f==null || Util.getZxidFromName(f.getName(), FileSnap.SNAPSHOT_FILE_PREFIX) == -1)
-            return false;
-
-        // Check for a valid snapshot
-        try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
-            // including the header and the last / bytes
-            // the snapshot should be at least 10 bytes
-            if (raf.length() < 10) {
-                return false;
-            }
-            raf.seek(raf.length() - 5);
-            byte bytes[] = new byte[5];
-            int readlen = 0;
-            int l;
-            while (readlen < 5 &&
-                    (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
-                readlen += l;
-            }
-            if (readlen != bytes.length) {
-                LOG.info("Invalid snapshot " + f
-                        + " too short, len = " + readlen);
-                return false;
-            }
-            ByteBuffer bb = ByteBuffer.wrap(bytes);
-            int len = bb.getInt();
-            byte b = bb.get();
-            if (len != 1 || b != '/') {
-                LOG.info("Invalid snapshot " + f + " len = " + len
-                        + " byte = " + (b & 0xff));
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    /**
-     * Reads a transaction entry from the input archive.
-     * @param ia archive to read from
-     * @return null if the entry is corrupted or EOF has been reached; a buffer
-     * (possible empty) containing serialized transaction record.
-     * @throws IOException
-     */
-    public static byte[] readTxnBytes(InputArchive ia) throws IOException {
-        try{
-            byte[] bytes = ia.readBuffer("txtEntry");
-            // Since we preallocate, we define EOF to be an
-            // empty transaction
-            if (bytes.length == 0)
-                return bytes;
-            if (ia.readByte("EOF") != 'B') {
-                LOG.error("Last transaction was partial.");
-                return null;
-            }
-            return bytes;
-        }catch(EOFException e){}
-        return null;
-    }
-    
-
-    /**
-     * Serializes transaction header and transaction data into a byte buffer.
-     *  
-     * @param hdr transaction header
-     * @param txn transaction data
-     * @return serialized transaction record
-     * @throws IOException
-     */
-    public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn)
-            throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        OutputArchive boa = BinaryOutputArchive.getArchive(baos);
-
-        hdr.serialize(boa, "hdr");
-        if (txn != null) {
-            txn.serialize(boa, "txn");
-        }
-        return baos.toByteArray();
-    }
-
-    /**
-     * Write the serialized transaction record to the output archive.
-     *  
-     * @param oa output archive
-     * @param bytes serialized transaction record
-     * @throws IOException
-     */
-    public static void writeTxnBytes(OutputArchive oa, byte[] bytes)
-            throws IOException {
-        oa.writeBuffer(bytes, "txnEntry");
-        oa.writeByte((byte) 0x42, "EOR"); // 'B'
-    }
-    
-    
-    /**
-     * Compare file file names of form "prefix.version". Sort order result
-     * returned in order of version.
-     */
-    private static class DataDirFileComparator
-        implements Comparator<File>, Serializable
-    {
-        private static final long serialVersionUID = -2648639884525140318L;
-
-        private String prefix;
-        private boolean ascending;
-        public DataDirFileComparator(String prefix, boolean ascending) {
-            this.prefix = prefix;
-            this.ascending = ascending;
-        }
-
-        public int compare(File o1, File o2) {
-            long z1 = Util.getZxidFromName(o1.getName(), prefix);
-            long z2 = Util.getZxidFromName(o2.getName(), prefix);
-            int result = z1 < z2 ? -1 : (z1 > z2 ? 1 : 0);
-            return ascending ? result : -result;
-        }
-    }
-    
-    /**
-     * Sort the list of files. Recency as determined by the version component
-     * of the file name.
-     *
-     * @param files array of files
-     * @param prefix files not matching this prefix are assumed to have a
-     * version = -1)
-     * @param ascending true sorted in ascending order, false results in
-     * descending order
-     * @return sorted input files
-     */
-    public static List<File> sortDataDir(File[] files, String prefix, boolean ascending)
-    {
-        if(files==null)
-            return new ArrayList<File>(0);
-        List<File> filelist = Arrays.asList(files);
-        Collections.sort(filelist, new DataDirFileComparator(prefix, ascending));
-        return filelist;
-    }
-
-    /**
-     * Returns true if fileName is a log file name.
-     *
-     * @param fileName
-     * @return
-     */
-    public static boolean isLogFileName(String fileName) {
-        return fileName.startsWith(FileTxnLog.LOG_FILE_PREFIX + ".");
-    }
-
-    /**
-     * Returns true if fileName is a snapshot file name.
-     *
-     * @param fileName
-     * @return
-     */
-    public static boolean isSnapshotFileName(String fileName) {
-        return fileName.startsWith(FileSnap.SNAPSHOT_FILE_PREFIX + ".");
-    }
-    
-}


Mime
View raw message