org.apache.zookeeper.server.persistence
Class Util

java.lang.Object
  extended by org.apache.zookeeper.server.persistence.Util

public class Util
extends Object

A collection of utility methods for dealing with file name parsing, low level I/O file operations and marshalling/unmarshalling.


Constructor Summary
Util()
           
 
Method Summary
static String getFormatConversionPolicy(Properties props)
          Extracts the value of the dbFormatConversion attribute.
static File getLogDir(Properties props)
          Extracts transaction log directory property value from the container.
static File getSnapDir(Properties props)
          Extracts snapshot directory property value from the container.
static long getZxidFromName(String name, String prefix)
          Extracts zxid from the file name.
static boolean isValidSnapshot(File f)
          Verifies that the file is a valid snapshot.
static URI makeFileLoggerURL(File dataDir, File dataLogDir)
          Given two directory files the method returns a well-formed logfile provider URI.
static URI makeFileLoggerURL(File dataDir, File dataLogDir, String convPolicy)
           
static String makeLogName(long zxid)
          Creates a valid transaction log file name.
static String makeSnapshotName(long zxid)
          Creates a snapshot file name.
static String makeURIString(String dataDir, String dataLogDir, String convPolicy)
           
static byte[] marshallTxnEntry(TxnHeader hdr, org.apache.jute.Record txn)
          Serializes transaction header and transaction data into a byte buffer.
static long padLogFile(FileOutputStream f, long currentSize, long preAllocSize)
          Grows the file to the specified number of bytes.
static byte[] readTxnBytes(org.apache.jute.InputArchive ia)
          Reads a transaction entry from the input archive.
static List<File> sortDataDir(File[] files, String prefix, boolean ascending)
          Sort the list of files.
static void writeTxnBytes(org.apache.jute.OutputArchive oa, byte[] bytes)
          Write the serialized transaction record to the output archive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

makeURIString

public static String makeURIString(String dataDir,
                                   String dataLogDir,
                                   String convPolicy)

makeFileLoggerURL

public static URI makeFileLoggerURL(File dataDir,
                                    File dataLogDir)
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.

Parameters:
dataDir - snapshot directory
dataLogDir - transaction log directory
Returns:
logfile provider URI

makeFileLoggerURL

public static URI makeFileLoggerURL(File dataDir,
                                    File dataLogDir,
                                    String convPolicy)

makeLogName

public static String makeLogName(long zxid)
Creates a valid transaction log file name.

Parameters:
zxid - used as a file name suffix (extention)
Returns:
file name

makeSnapshotName

public static String makeSnapshotName(long zxid)
Creates a snapshot file name.

Parameters:
zxid - used as a suffix
Returns:
file name

getSnapDir

public static File getSnapDir(Properties props)
Extracts snapshot directory property value from the container.

Parameters:
props - properties container
Returns:
file representing the snapshot directory

getLogDir

public static File getLogDir(Properties props)
Extracts transaction log directory property value from the container.

Parameters:
props - properties container
Returns:
file representing the txn log directory

getFormatConversionPolicy

public static String getFormatConversionPolicy(Properties props)
Extracts the value of the dbFormatConversion attribute.

Parameters:
props - properties container
Returns:
value of the dbFormatConversion attribute

getZxidFromName

public static long getZxidFromName(String name,
                                   String prefix)
Extracts zxid from the file name. The file name should have been created using one of the makeLogName or makeSnapshotName.

Parameters:
name - the file name to parse
prefix - the file name prefix (snapshot or log)
Returns:
zxid

isValidSnapshot

public static boolean isValidSnapshot(File f)
                               throws IOException
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.

Parameters:
f - file to verify
Returns:
true if the snapshot is valid
Throws:
IOException

padLogFile

public static long padLogFile(FileOutputStream f,
                              long currentSize,
                              long preAllocSize)
                       throws IOException
Grows the file to the specified number of bytes. This only happenes if the current file position is sufficiently close (less than 4K) to end of file.

Parameters:
f - output stream to pad
currentSize - application keeps track of the cuurent file size
preAllocSize - how many bytes to pad
Returns:
the new file size. It can be the same as currentSize if no padding was done.
Throws:
IOException

readTxnBytes

public static byte[] readTxnBytes(org.apache.jute.InputArchive ia)
                           throws IOException
Reads a transaction entry from the input archive.

Parameters:
ia - archive to read from
Returns:
null if the entry is corrupted or EOF has been reached; a buffer (possible empty) containing serialized transaction record.
Throws:
IOException

marshallTxnEntry

public static byte[] marshallTxnEntry(TxnHeader hdr,
                                      org.apache.jute.Record txn)
                               throws IOException
Serializes transaction header and transaction data into a byte buffer.

Parameters:
hdr - transaction header
txn - transaction data
Returns:
serialized transaction record
Throws:
IOException

writeTxnBytes

public static void writeTxnBytes(org.apache.jute.OutputArchive oa,
                                 byte[] bytes)
                          throws IOException
Write the serialized transaction record to the output archive.

Parameters:
oa - output archive
bytes - serialized trasnaction record
Throws:
IOException

sortDataDir

public static List<File> sortDataDir(File[] files,
                                     String prefix,
                                     boolean ascending)
Sort the list of files. Recency as determined by the version component of the file name.

Parameters:
files - array of files
prefix - files not matching this prefix are assumed to have a version = -1)
ascending - true sorted in ascending order, false results in descending order
Returns:
sorted input files


Copyright © 2011 The Apache Software Foundation