Package org.h2.mvstore
Class DataUtils
java.lang.Object
org.h2.mvstore.DataUtils
Utility methods
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The maximum integer that needs less space when using variable size encoding (only 3 bytes instead of 4).static final long
The maximum long that needs less space when using variable size encoding (only 7 bytes instead of 8).static final int
The block in the stream store was not found.static final int
The application was trying to read data from a chunk that is no longer available.static final int
The object is already closed.static final int
The file is corrupt or (for encrypted files) the encryption key is wrong.static final int
The file is locked.static final int
An internal error occurred.static final int
An error occurred while reading from the file.static final int
An error occurred when serializing or de-serializing.static final int
There are too many open transactions.static final int
The transaction store is corrupt.static final int
The transaction store is in an illegal state (for example, not yet initialized).static final int
An entry is still locked by another transaction.static final int
The transaction contains too many changes.static final int
Deadlock discovered and one of transactions involved chosen as victim and rolled back.static final int
The transaction store can not be initialized because data type is not found in type registry.static final int
The file format is not supported.static final int
An error occurred when trying to write to the file.static final int
The maximum length of a variable size int.static final int
The maximum length of a variable size long.static final String
The prefix for chunks ("chunk.").static final String
The prefix for maps ("map.").static final String
The prefix for names ("name.").static final String
The prefix for root positions of maps ("root.").static final int
The bit mask for compressed pages (compression level fast).static final int
The bit mask for compressed pages (compression level high).static final int
The bit mask for pages with page sequential number.static final int
The marker size of a very large page.static final int
The type for leaf page.static final int
The type for node page. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendMap
(StringBuilder buff, String key, int value) Append a key-value pair to the string builder.static void
appendMap
(StringBuilder buff, String key, long value) Append a key-value pair to the string builder.static void
appendMap
(StringBuilder buff, String key, String value) Append a key-value pair to the string builder.static StringBuilder
appendMap
(StringBuilder buff, HashMap<String, ?> map) Append a map to the string builder, sorted by key.private static StringBuilder
appendMapKey
(StringBuilder buff, String key) static void
checkArgument
(boolean test, String message, Object... arguments) Throw an IllegalArgumentException if the argument is invalid.static IOException
Convert an exception to an IO exception.static void
copyExcept
(Object src, Object dst, int oldSize, int removeIndex) Copy the elements of an array, and remove one element.static void
copyWithGap
(Object src, Object dst, int oldSize, int gapIndex) Copy the elements of an array, with a gap.static int
decodePageLength
(int code) Get the maximum length for the given code.static int
encodeLength
(int len) Convert the length to a length code 0..31.static String
formatMessage
(int errorCode, String message, Object... arguments) Format an error message.static short
getCheckValue
(int x) Calculate a check value for the given integer.(package private) static int
getConfigParam
(Map<String, ?> config, String key, int defaultValue) Get the configuration parameter value, or default.static int
getFletcher32
(byte[] bytes, int offset, int length) Calculate the Fletcher32 checksum.static String
getFromMap
(String s, String key) Parse a specified pair from key-value pair list.static String
getMapName
(String s) Parse a name from key-value pair list.static int
getPageChunkId
(long pos) Get the chunk id from the position.static int
getPageMapId
(long tocElement) Get the map id from the chunk's table of content element.static int
getPageMaxLength
(long pos) Get the maximum length for the given page position.static int
getPageOffset
(long tocElement) Get the offset from the position.static long
getPagePos
(int chunkId, int offset, int length, int type) Get the position of this page.static long
getPagePos
(int chunkId, long tocElement) Convert tocElement into pagePos by replacing mapId with chunkId.static int
getPageType
(long pos) Get the page type from the position.static long
getTocElement
(int mapId, int offset, int length, int type) Create table of content element.static int
getVarIntLen
(int x) Get the length of the variable size int.static int
getVarLongLen
(long x) Get the length of the variable size long.private static <T extends Exception>
Tstatic boolean
isLeafPosition
(long pos) Determines whether specified file position corresponds to a leaf page(package private) static boolean
isPageRemoved
(long pos) Find out if page was removed.static boolean
isPageSaved
(long pos) Find out if page was saved.static IllegalArgumentException
newIllegalArgumentException
(String message, Object... arguments) Create a new IllegalArgumentException.static MVStoreException
newMVStoreException
(int errorCode, String message, Object... arguments) Create a new MVStoreException.newUnsupportedOperationException
(String message) Create a new UnsupportedOperationException.parseChecksummedMap
(byte[] bytes) Parse a key-value pair list and checks its checksum.(package private) static byte[]
parseHexBytes
(Map<String, ?> map, String key) Parse the hex-encoded bytes of an entry in the map.static int
Parse an unsigned, hex long.static long
Parse an unsigned, hex long.Parse a key-value pair list.private static int
parseMapValue
(StringBuilder buff, String s, int i, int size) static void
readFully
(FileChannel file, long pos, ByteBuffer dst) Read from a file channel until the buffer is full.(package private) static int
readHexInt
(Map<String, ?> map, String key, int defaultValue) Read a hex int value from a map.static long
readHexLong
(Map<String, ?> map, String key, long defaultValue) Read a hex long value from a map.static String
readString
(ByteBuffer buff) Read a string.static String
readString
(ByteBuffer buff, int len) Read a string.static int
readVarInt
(ByteBuffer buff) Read a variable size int.private static int
readVarIntRest
(ByteBuffer buff, int b) static long
readVarLong
(ByteBuffer buff) Read a variable size long.static void
writeFully
(FileChannel file, long pos, ByteBuffer src) Write to a file channel.static void
writeStringData
(ByteBuffer buff, String s, int len) Write characters from a string (without the length).static void
writeVarInt
(OutputStream out, int x) Write a variable size int.static void
writeVarInt
(ByteBuffer buff, int x) Write a variable size int.static void
writeVarLong
(OutputStream out, long x) Write a variable size long.static void
writeVarLong
(ByteBuffer buff, long x) Write a variable size long.
-
Field Details
-
ERROR_READING_FAILED
public static final int ERROR_READING_FAILEDAn error occurred while reading from the file.- See Also:
-
ERROR_WRITING_FAILED
public static final int ERROR_WRITING_FAILEDAn error occurred when trying to write to the file.- See Also:
-
ERROR_INTERNAL
public static final int ERROR_INTERNALAn internal error occurred. This could be a bug, or a memory corruption (for example caused by out of memory).- See Also:
-
ERROR_CLOSED
public static final int ERROR_CLOSEDThe object is already closed.- See Also:
-
ERROR_UNSUPPORTED_FORMAT
public static final int ERROR_UNSUPPORTED_FORMATThe file format is not supported.- See Also:
-
ERROR_FILE_CORRUPT
public static final int ERROR_FILE_CORRUPTThe file is corrupt or (for encrypted files) the encryption key is wrong.- See Also:
-
ERROR_FILE_LOCKED
public static final int ERROR_FILE_LOCKEDThe file is locked.- See Also:
-
ERROR_SERIALIZATION
public static final int ERROR_SERIALIZATIONAn error occurred when serializing or de-serializing.- See Also:
-
ERROR_CHUNK_NOT_FOUND
public static final int ERROR_CHUNK_NOT_FOUNDThe application was trying to read data from a chunk that is no longer available.- See Also:
-
ERROR_BLOCK_NOT_FOUND
public static final int ERROR_BLOCK_NOT_FOUNDThe block in the stream store was not found.- See Also:
-
ERROR_TRANSACTION_CORRUPT
public static final int ERROR_TRANSACTION_CORRUPTThe transaction store is corrupt.- See Also:
-
ERROR_TRANSACTION_LOCKED
public static final int ERROR_TRANSACTION_LOCKEDAn entry is still locked by another transaction.- See Also:
-
ERROR_TOO_MANY_OPEN_TRANSACTIONS
public static final int ERROR_TOO_MANY_OPEN_TRANSACTIONSThere are too many open transactions.- See Also:
-
ERROR_TRANSACTION_ILLEGAL_STATE
public static final int ERROR_TRANSACTION_ILLEGAL_STATEThe transaction store is in an illegal state (for example, not yet initialized).- See Also:
-
ERROR_TRANSACTION_TOO_BIG
public static final int ERROR_TRANSACTION_TOO_BIGThe transaction contains too many changes.- See Also:
-
ERROR_TRANSACTIONS_DEADLOCK
public static final int ERROR_TRANSACTIONS_DEADLOCKDeadlock discovered and one of transactions involved chosen as victim and rolled back.- See Also:
-
ERROR_UNKNOWN_DATA_TYPE
public static final int ERROR_UNKNOWN_DATA_TYPEThe transaction store can not be initialized because data type is not found in type registry.- See Also:
-
PAGE_TYPE_LEAF
public static final int PAGE_TYPE_LEAFThe type for leaf page.- See Also:
-
PAGE_TYPE_NODE
public static final int PAGE_TYPE_NODEThe type for node page.- See Also:
-
PAGE_COMPRESSED
public static final int PAGE_COMPRESSEDThe bit mask for compressed pages (compression level fast).- See Also:
-
PAGE_COMPRESSED_HIGH
public static final int PAGE_COMPRESSED_HIGHThe bit mask for compressed pages (compression level high).- See Also:
-
PAGE_HAS_PAGE_NO
public static final int PAGE_HAS_PAGE_NOThe bit mask for pages with page sequential number.- See Also:
-
MAX_VAR_INT_LEN
public static final int MAX_VAR_INT_LENThe maximum length of a variable size int.- See Also:
-
MAX_VAR_LONG_LEN
public static final int MAX_VAR_LONG_LENThe maximum length of a variable size long.- See Also:
-
COMPRESSED_VAR_INT_MAX
public static final int COMPRESSED_VAR_INT_MAXThe maximum integer that needs less space when using variable size encoding (only 3 bytes instead of 4).- See Also:
-
COMPRESSED_VAR_LONG_MAX
public static final long COMPRESSED_VAR_LONG_MAXThe maximum long that needs less space when using variable size encoding (only 7 bytes instead of 8).- See Also:
-
PAGE_LARGE
public static final int PAGE_LARGEThe marker size of a very large page.- See Also:
-
META_CHUNK
The prefix for chunks ("chunk."). This, plus the chunk id (hex encoded) is the key, and the serialized chunk metadata is the value.- See Also:
-
META_ROOT
The prefix for root positions of maps ("root."). This, plus the map id (hex encoded) is the key, and the position (hex encoded) is the value.- See Also:
-
META_NAME
The prefix for names ("name."). This, plus the name of the map, is the key, and the map id (hex encoded) is the value.- See Also:
-
META_MAP
The prefix for maps ("map."). This, plus the map id (hex encoded) is the key, and the serialized in the map metadata is the value.- See Also:
-
-
Constructor Details
-
DataUtils
public DataUtils()
-
-
Method Details
-
getVarIntLen
public static int getVarIntLen(int x) Get the length of the variable size int.- Parameters:
x
- the value- Returns:
- the length in bytes
-
getVarLongLen
public static int getVarLongLen(long x) Get the length of the variable size long.- Parameters:
x
- the value- Returns:
- the length in bytes
-
readVarInt
Read a variable size int.- Parameters:
buff
- the source buffer- Returns:
- the value
-
readVarIntRest
-
readVarLong
Read a variable size long.- Parameters:
buff
- the source buffer- Returns:
- the value
-
writeVarInt
Write a variable size int.- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if some data could not be written
-
writeVarInt
Write a variable size int.- Parameters:
buff
- the source bufferx
- the value
-
writeStringData
Write characters from a string (without the length).- Parameters:
buff
- the target buffer (must be large enough)s
- the stringlen
- the number of characters
-
readString
Read a string.- Parameters:
buff
- the source buffer- Returns:
- the value
-
readString
Read a string.- Parameters:
buff
- the source bufferlen
- the number of characters- Returns:
- the value
-
writeVarLong
Write a variable size long.- Parameters:
buff
- the target bufferx
- the value
-
writeVarLong
Write a variable size long.- Parameters:
out
- the output streamx
- the value- Throws:
IOException
- if some data could not be written
-
copyWithGap
Copy the elements of an array, with a gap.- Parameters:
src
- the source arraydst
- the target arrayoldSize
- the size of the old arraygapIndex
- the index of the gap
-
copyExcept
Copy the elements of an array, and remove one element.- Parameters:
src
- the source arraydst
- the target arrayoldSize
- the size of the old arrayremoveIndex
- the index of the entry to remove
-
readFully
Read from a file channel until the buffer is full. The buffer is rewind after reading.- Parameters:
file
- the file channelpos
- the absolute position within the filedst
- the byte buffer- Throws:
MVStoreException
- if some data could not be read
-
writeFully
Write to a file channel.- Parameters:
file
- the file channelpos
- the absolute position within the filesrc
- the source buffer
-
encodeLength
public static int encodeLength(int len) Convert the length to a length code 0..31. 31 means more than 1 MB.- Parameters:
len
- the length- Returns:
- the length code
-
getPageChunkId
public static int getPageChunkId(long pos) Get the chunk id from the position.- Parameters:
pos
- the position- Returns:
- the chunk id
-
getPageMapId
public static int getPageMapId(long tocElement) Get the map id from the chunk's table of content element.- Parameters:
tocElement
- packed table of content element- Returns:
- the map id
-
getPageMaxLength
public static int getPageMaxLength(long pos) Get the maximum length for the given page position.- Parameters:
pos
- the position- Returns:
- the maximum length
-
decodePageLength
public static int decodePageLength(int code) Get the maximum length for the given code. For the code 31, PAGE_LARGE is returned.- Parameters:
code
- encoded page length- Returns:
- the maximum length
-
getPageOffset
public static int getPageOffset(long tocElement) Get the offset from the position.- Parameters:
tocElement
- packed table of content element- Returns:
- the offset
-
getPageType
public static int getPageType(long pos) Get the page type from the position.- Parameters:
pos
- the position- Returns:
- the page type (PAGE_TYPE_NODE or PAGE_TYPE_LEAF)
-
isLeafPosition
public static boolean isLeafPosition(long pos) Determines whether specified file position corresponds to a leaf page- Parameters:
pos
- the position- Returns:
- true if it is a leaf, false otherwise
-
isPageSaved
public static boolean isPageSaved(long pos) Find out if page was saved.- Parameters:
pos
- the position- Returns:
- true if page has been saved
-
isPageRemoved
static boolean isPageRemoved(long pos) Find out if page was removed.- Parameters:
pos
- the position- Returns:
- true if page has been removed (no longer accessible from the current root of the tree)
-
getPagePos
public static long getPagePos(int chunkId, int offset, int length, int type) Get the position of this page. The following information is encoded in the position: the chunk id, the page sequential number, the maximum length, and the type (node or leaf).- Parameters:
chunkId
- the chunk idoffset
- the offsetlength
- the lengthtype
- the page type (1 for node, 0 for leaf)- Returns:
- the position
-
getPagePos
public static long getPagePos(int chunkId, long tocElement) Convert tocElement into pagePos by replacing mapId with chunkId.- Parameters:
chunkId
- the chunk idtocElement
- the element- Returns:
- the page position
-
getTocElement
public static long getTocElement(int mapId, int offset, int length, int type) Create table of content element. The following information is encoded in it: the map id, the page offset, the maximum length, and the type (node or leaf).- Parameters:
mapId
- the chunk idoffset
- the offsetlength
- the lengthtype
- the page type (1 for node, 0 for leaf)- Returns:
- the position
-
getCheckValue
public static short getCheckValue(int x) Calculate a check value for the given integer. A check value is mean to verify the data is consistent with a high probability, but not meant to protect against media failure or deliberate changes.- Parameters:
x
- the value- Returns:
- the check value
-
appendMap
Append a map to the string builder, sorted by key.- Parameters:
buff
- the target buffermap
- the map- Returns:
- the string builder
-
appendMapKey
-
appendMap
Append a key-value pair to the string builder. Keys may not contain a colon. Values that contain a comma or a double quote are enclosed in double quotes, with special characters escaped using a backslash.- Parameters:
buff
- the target bufferkey
- the keyvalue
- the value
-
appendMap
Append a key-value pair to the string builder. Keys may not contain a colon.- Parameters:
buff
- the target bufferkey
- the keyvalue
- the value
-
appendMap
Append a key-value pair to the string builder. Keys may not contain a colon.- Parameters:
buff
- the target bufferkey
- the keyvalue
- the value
-
parseMapValue
- Parameters:
buff
- output buffer, should be emptys
- parsed stringi
- offset to parse fromsize
- stop offset (exclusive)- Returns:
- new offset
-
parseMap
Parse a key-value pair list.- Parameters:
s
- the list- Returns:
- the map
- Throws:
MVStoreException
- if parsing failed
-
parseChecksummedMap
Parse a key-value pair list and checks its checksum.- Parameters:
bytes
- encoded map- Returns:
- the map without mapping for
"fletcher"
, ornull
if checksum is wrong or parameter do not represent a properly formatted map serialization
-
getMapName
Parse a name from key-value pair list.- Parameters:
s
- the list- Returns:
- value of name item, or
null
- Throws:
MVStoreException
- if parsing failed
-
getFromMap
Parse a specified pair from key-value pair list.- Parameters:
s
- the listkey
- the name of the key- Returns:
- value of the specified item, or
null
- Throws:
MVStoreException
- if parsing failed
-
getFletcher32
public static int getFletcher32(byte[] bytes, int offset, int length) Calculate the Fletcher32 checksum.- Parameters:
bytes
- the bytesoffset
- initial offsetlength
- the message length (if odd, 0 is appended)- Returns:
- the checksum
-
checkArgument
Throw an IllegalArgumentException if the argument is invalid.- Parameters:
test
- true if the argument is validmessage
- the messagearguments
- the arguments- Throws:
IllegalArgumentException
- if the argument is invalid
-
newIllegalArgumentException
public static IllegalArgumentException newIllegalArgumentException(String message, Object... arguments) Create a new IllegalArgumentException.- Parameters:
message
- the messagearguments
- the arguments- Returns:
- the exception
-
newUnsupportedOperationException
Create a new UnsupportedOperationException.- Parameters:
message
- the message- Returns:
- the exception
-
newMVStoreException
public static MVStoreException newMVStoreException(int errorCode, String message, Object... arguments) Create a new MVStoreException.- Parameters:
errorCode
- the error codemessage
- the messagearguments
- the arguments- Returns:
- the exception
-
initCause
-
formatMessage
Format an error message.- Parameters:
errorCode
- the error codemessage
- the messagearguments
- the arguments- Returns:
- the formatted message
-
readHexLong
Read a hex long value from a map.- Parameters:
map
- the mapkey
- the keydefaultValue
- if the value is null- Returns:
- the parsed value
- Throws:
MVStoreException
- if parsing fails
-
parseHexLong
Parse an unsigned, hex long.- Parameters:
x
- the string- Returns:
- the parsed value
- Throws:
MVStoreException
- if parsing fails
-
parseHexInt
Parse an unsigned, hex long.- Parameters:
x
- the string- Returns:
- the parsed value
- Throws:
MVStoreException
- if parsing fails
-
readHexInt
Read a hex int value from a map.- Parameters:
map
- the mapkey
- the keydefaultValue
- if the value is null- Returns:
- the parsed value
- Throws:
MVStoreException
- if parsing fails
-
parseHexBytes
Parse the hex-encoded bytes of an entry in the map.- Parameters:
map
- the mapkey
- the key- Returns:
- the byte array, or null if not in the map
-
getConfigParam
Get the configuration parameter value, or default.- Parameters:
config
- the configurationkey
- the keydefaultValue
- the default- Returns:
- the configured value or default
-
convertToIOException
Convert an exception to an IO exception.- Parameters:
e
- the root cause- Returns:
- the IO exception
-