Class Util
- java.lang.Object
-
- org.apache.zookeeper.server.persistence.Util
-
public class Util extends java.lang.ObjectA collection of utility methods for dealing with file name parsing, low level I/O file operations and marshalling/unmarshalling.
-
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringgetFormatConversionPolicy(java.util.Properties props)Extracts the value of the dbFormatConversion attribute.static java.io.FilegetLogDir(java.util.Properties props)Extracts transaction log directory property value from the container.static java.io.FilegetSnapDir(java.util.Properties props)Extracts snapshot directory property value from the container.static longgetZxidFromName(java.lang.String name, java.lang.String prefix)Extracts zxid from the file name.static booleanisLogFileName(java.lang.String fileName)Returns true if fileName is a log file name.static booleanisSnapshotFileName(java.lang.String fileName)Returns true if fileName is a snapshot file name.static booleanisValidSnapshot(java.io.File f)Verifies that the file is a valid snapshot.static java.net.URImakeFileLoggerURL(java.io.File dataDir, java.io.File dataLogDir)Given two directory files the method returns a well-formed logfile provider URI.static java.net.URImakeFileLoggerURL(java.io.File dataDir, java.io.File dataLogDir, java.lang.String convPolicy)static java.lang.StringmakeLogName(long zxid)Creates a valid transaction log file name.static java.lang.StringmakeSnapshotName(long zxid)Creates a snapshot file name.static java.lang.StringmakeURIString(java.lang.String dataDir, java.lang.String dataLogDir, java.lang.String convPolicy)static byte[]marshallTxnEntry(TxnHeader hdr, org.apache.jute.Record txn)Serializes transaction header and transaction data into a byte buffer.static byte[]readTxnBytes(org.apache.jute.InputArchive ia)Reads a transaction entry from the input archive.static java.util.List<java.io.File>sortDataDir(java.io.File[] files, java.lang.String prefix, boolean ascending)Sort the list of files.static voidwriteTxnBytes(org.apache.jute.OutputArchive oa, byte[] bytes)Write the serialized transaction record to the output archive.
-
-
-
Method Detail
-
makeURIString
public static java.lang.String makeURIString(java.lang.String dataDir, java.lang.String dataLogDir, java.lang.String convPolicy)
-
makeFileLoggerURL
public static java.net.URI makeFileLoggerURL(java.io.File dataDir, java.io.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 directorydataLogDir- transaction log directory- Returns:
- logfile provider URI
-
makeFileLoggerURL
public static java.net.URI makeFileLoggerURL(java.io.File dataDir, java.io.File dataLogDir, java.lang.String convPolicy)
-
makeLogName
public static java.lang.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 java.lang.String makeSnapshotName(long zxid)
Creates a snapshot file name.- Parameters:
zxid- used as a suffix- Returns:
- file name
-
getSnapDir
public static java.io.File getSnapDir(java.util.Properties props)
Extracts snapshot directory property value from the container.- Parameters:
props- properties container- Returns:
- file representing the snapshot directory
-
getLogDir
public static java.io.File getLogDir(java.util.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 java.lang.String getFormatConversionPolicy(java.util.Properties props)
Extracts the value of the dbFormatConversion attribute.- Parameters:
props- properties container- Returns:
- value of the dbFormatConversion attribute
-
getZxidFromName
public static long getZxidFromName(java.lang.String name, java.lang.String prefix)Extracts zxid from the file name. The file name should have been created using one of themakeLogName(long)ormakeSnapshotName(long).- Parameters:
name- the file name to parseprefix- the file name prefix (snapshot or log)- Returns:
- zxid
-
isValidSnapshot
public static boolean isValidSnapshot(java.io.File f) throws java.io.IOExceptionVerifies 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:
java.io.IOException
-
readTxnBytes
public static byte[] readTxnBytes(org.apache.jute.InputArchive ia) throws java.io.IOExceptionReads 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:
java.io.IOException
-
marshallTxnEntry
public static byte[] marshallTxnEntry(TxnHeader hdr, org.apache.jute.Record txn) throws java.io.IOException
Serializes transaction header and transaction data into a byte buffer.- Parameters:
hdr- transaction headertxn- transaction data- Returns:
- serialized transaction record
- Throws:
java.io.IOException
-
writeTxnBytes
public static void writeTxnBytes(org.apache.jute.OutputArchive oa, byte[] bytes) throws java.io.IOExceptionWrite the serialized transaction record to the output archive.- Parameters:
oa- output archivebytes- serialized trasnaction record- Throws:
java.io.IOException
-
sortDataDir
public static java.util.List<java.io.File> sortDataDir(java.io.File[] files, java.lang.String prefix, boolean ascending)Sort the list of files. Recency as determined by the version component of the file name.- Parameters:
files- array of filesprefix- 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
-
isLogFileName
public static boolean isLogFileName(java.lang.String fileName)
Returns true if fileName is a log file name.- Parameters:
fileName-- Returns:
-
isSnapshotFileName
public static boolean isSnapshotFileName(java.lang.String fileName)
Returns true if fileName is a snapshot file name.- Parameters:
fileName-- Returns:
-
-