forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORDA-973 Compression support for serialization (corda#2473)
* Serialization magic is now 7 bytes * Introduce encoding property and whitelist
- Loading branch information
Showing
26 changed files
with
436 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/OrdinalIO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.corda.nodeapi.internal.serialization | ||
|
||
import java.io.EOFException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.nio.ByteBuffer | ||
|
||
class OrdinalBits(private val ordinal: Int) { | ||
interface OrdinalWriter { | ||
val bits: OrdinalBits | ||
val encodedSize get() = 1 | ||
fun writeTo(stream: OutputStream) = stream.write(bits.ordinal) | ||
fun putTo(buffer: ByteBuffer) = buffer.put(bits.ordinal.toByte())!! | ||
} | ||
|
||
init { | ||
require(ordinal >= 0) { "The ordinal must be non-negative." } | ||
require(ordinal < 128) { "Consider implementing a varint encoding." } | ||
} | ||
} | ||
|
||
class OrdinalReader<out E : Any>(private val values: Array<E>) { | ||
private val enumName = values[0].javaClass.simpleName | ||
private val range = 0 until values.size | ||
fun readFrom(stream: InputStream): E { | ||
val ordinal = stream.read() | ||
if (ordinal == -1) throw EOFException("Expected a $enumName ordinal.") | ||
if (ordinal !in range) throw NoSuchElementException("No $enumName with ordinal: $ordinal") | ||
return values[ordinal] | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/SerializationFormat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,58 @@ | ||
package net.corda.nodeapi.internal.serialization | ||
|
||
import net.corda.core.internal.VisibleForTesting | ||
import net.corda.core.serialization.SerializationEncoding | ||
import net.corda.core.utilities.ByteSequence | ||
import net.corda.core.utilities.OpaqueBytes | ||
import net.corda.nodeapi.internal.serialization.OrdinalBits.OrdinalWriter | ||
import org.iq80.snappy.SnappyFramedInputStream | ||
import org.iq80.snappy.SnappyFramedOutputStream | ||
import java.io.OutputStream | ||
import java.io.InputStream | ||
import java.nio.ByteBuffer | ||
import java.util.zip.DeflaterOutputStream | ||
import java.util.zip.InflaterInputStream | ||
|
||
class CordaSerializationMagic(bytes: ByteArray) : OpaqueBytes(bytes) { | ||
private val bufferView = slice() | ||
fun consume(data: ByteSequence): ByteBuffer? { | ||
return if (data.slice(end = size) == bufferView) data.slice(size) else null | ||
} | ||
} | ||
|
||
enum class SectionId : OrdinalWriter { | ||
/** Serialization data follows, and then discard the rest of the stream (if any) as legacy data may have trailing garbage. */ | ||
DATA_AND_STOP, | ||
/** Identical behaviour to [DATA_AND_STOP], historically used for Kryo. Do not use in new code. */ | ||
ALT_DATA_AND_STOP, | ||
/** The ordinal of a [CordaSerializationEncoding] follows, which should be used to decode the remainder of the stream. */ | ||
ENCODING; | ||
|
||
companion object { | ||
val reader = OrdinalReader(values()) | ||
} | ||
|
||
override val bits = OrdinalBits(ordinal) | ||
} | ||
|
||
enum class CordaSerializationEncoding : SerializationEncoding, OrdinalWriter { | ||
DEFLATE { | ||
override fun wrap(stream: OutputStream) = DeflaterOutputStream(stream) | ||
override fun wrap(stream: InputStream) = InflaterInputStream(stream) | ||
}, | ||
SNAPPY { | ||
override fun wrap(stream: OutputStream) = SnappyFramedOutputStream(stream) | ||
override fun wrap(stream: InputStream) = SnappyFramedInputStream(stream, false) | ||
}; | ||
|
||
companion object { | ||
val reader = OrdinalReader(values()) | ||
} | ||
|
||
override val bits = OrdinalBits(ordinal) | ||
abstract fun wrap(stream: OutputStream): OutputStream | ||
abstract fun wrap(stream: InputStream): InputStream | ||
} | ||
|
||
@VisibleForTesting | ||
internal val encodingNotPermittedFormat = "Encoding not permitted: %s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
node-api/src/main/kotlin/net/corda/nodeapi/internal/serialization/amqp/AMQPStreams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.corda.nodeapi.internal.serialization.amqp | ||
|
||
import com.esotericsoftware.kryo.io.ByteBufferInputStream | ||
import net.corda.nodeapi.internal.serialization.kryo.ByteBufferOutputStream | ||
import net.corda.nodeapi.internal.serialization.kryo.serializeOutputStreamPool | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.nio.ByteBuffer | ||
|
||
fun InputStream.asByteBuffer(): ByteBuffer { | ||
return if (this is ByteBufferInputStream) { | ||
byteBuffer // BBIS has no other state, so this is perfectly safe. | ||
} else { | ||
ByteBuffer.wrap(serializeOutputStreamPool.run { | ||
copyTo(it) | ||
it.toByteArray() | ||
}) | ||
} | ||
} | ||
|
||
fun <T> OutputStream.alsoAsByteBuffer(remaining: Int, task: (ByteBuffer) -> T): T { | ||
return if (this is ByteBufferOutputStream) { | ||
alsoAsByteBuffer(remaining, task) | ||
} else { | ||
serializeOutputStreamPool.run { | ||
val result = it.alsoAsByteBuffer(remaining, task) | ||
it.copyTo(this) | ||
result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.