Package io.permazen.kv.util
Class KeyListEncoder
java.lang.Object
io.permazen.kv.util.KeyListEncoder
Serializes a sequence of
byte[]
arrays, compressing consecutive common prefixes.
Keys are encoded/decoded by read()
and write()
in one of two forms:
total-length bytes...
-prefix-length suffix-length suffix-bytes ...
total-length
or negative prefix-length
) is encoded using LongEncoder
.
The suffix-length
, if present, is encoded using UnsignedIntEncoder
.
Support for encoding and decoding an entire iteration of key/value pairs is supported via
readPairs()
and writePairs()
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
read
(InputStream input, byte[] prev) Read the next key.readPairs
(InputStream input) Decode an iteration of key/value pairs previously encoded bywritePairs()
.static void
write
(OutputStream out, byte[] key, byte[] prev) Write the next key, compressing its common prefix with the previous key (if any).static int
writeLength
(byte[] key, byte[] prev) Calculate the number of bytes that would be required to write the next key viawrite()
.static void
writePairs
(Iterator<KVPair> kvpairs, OutputStream output) Encode an iteration of key/value pairs.static long
writePairsLength
(Iterator<KVPair> kvpairs) Determine the number of bytes that would be written bywritePairs()
.
-
Method Details
-
write
Write the next key, compressing its common prefix with the previous key (if any).- Parameters:
out
- output streamkey
- key to writeprev
- previous key, or null for none- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- ifout
orkey
is null
-
writeLength
public static int writeLength(byte[] key, byte[] prev) Calculate the number of bytes that would be required to write the next key viawrite()
.- Parameters:
key
- key to writeprev
- previous key, or null for none- Returns:
- number of bytes to be written by
write(out, key, prev)
- Throws:
IllegalArgumentException
- ifkey
is null
-
read
Read the next key.- Parameters:
input
- input streamprev
- previous key, or null for none- Returns:
- next key
- Throws:
IOException
- if an I/O error occursEOFException
- if an unexpected EOF is encounteredIllegalArgumentException
- ifinput
is nullIllegalArgumentException
- ifinput
contains invalid data
-
writePairs
Encode an iteration of key/value pairs.- Parameters:
kvpairs
- key/value pair iterationoutput
- encoded output- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- if either parameter is null
-
writePairsLength
Determine the number of bytes that would be written bywritePairs()
.- Parameters:
kvpairs
- key/value pair iteration- Returns:
- encoded length of this instance
- Throws:
IllegalArgumentException
- ifkvpairs
is null
-
readPairs
Decode an iteration of key/value pairs previously encoded bywritePairs()
.If an
IOException
occurs during iteration, the returnedIterator
wraps it in aRuntimeException
.If invalid input is encountered during iteration, the returned
Iterator
will throw anIllegalArgumentException
.- Parameters:
input
- encoded input- Returns:
- iteration of key/value pairs
- Throws:
IllegalArgumentException
- ifinput
is null
-