Package io.permazen.kv.util
Class KeyListEncoder
java.lang.Object
io.permazen.kv.util.KeyListEncoder
Serializes a sequence of
byte[]
arrays, or byte[]
key/value pairs, using a simple
key prefix compression scheme.
Keys are encoded/decoded by read()
and write()
in one of two forms:
total-length bytes...
-prefix-length suffix-length suffix-bytes ...
total-length
is encoded using LongEncoder
as a positive number
and is followed by that many bytes consituting the key. In the second form, the length of the key prefix
that matches the previous key is encoded as a negative value using LongEncoder
, followed by the
number of suffix bytes encoded via UnsignedIntEncoder
, followed by that many suffix bytes.
Support for encoding and decoding an entire iteration of key/value pairs is supported via
readPairs()
and writePairs()
.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ByteData
read
(InputStream input, ByteData prev) Read the next key.readPairs
(InputStream input) Decode an iteration of key/value pairs previously encoded bywritePairs()
.static void
write
(OutputStream out, ByteData key, ByteData prev) Write the next key, compressing its common prefix with the previous key (if any).static int
writeLength
(ByteData key, ByteData 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
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.Each key/value pair is encoded by encoding the key using prefix compression from the previous key, followed by the value with no prefix compression. A final
0xff
byte terminates the sequence.- 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 nullIllegalArgumentException
- if the result is larger thanLong.MAX_VALUE
-
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
.Decoding stops after reading the end of stream or a terminating
0xff
byte.- Parameters:
input
- encoded input- Returns:
- iteration of key/value pairs
- Throws:
IllegalArgumentException
- ifinput
is null
-