-
Notifications
You must be signed in to change notification settings - Fork 5
added compression feature for circe-akka-serializer #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b7f5f3b
added compression feature for circe-akka-serializer
LukaszKontowski cb9b6e6
Added newline character for checks
LukaszKontowski b3b5152
removed trailing space from README
LukaszKontowski be30f02
few minor PR fixes
LukaszKontowski 5a67a19
replace real logs with random strings
LukaszKontowski 9694e2d
fix structure in circe-akka-serializer reference.conf
LukaszKontowski f15b0da
PR fixes and improvements
LukaszKontowski 053c115
added useful comment on check
LukaszKontowski 718ff20
added more comments
LukaszKontowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,5 +1,19 @@ | ||
# Default configuration | ||
|
||
org.virtuslab.ash { | ||
verbose-debug-logging = off | ||
circe { | ||
verbose-debug-logging = off | ||
|
||
# settings for compression of the payload | ||
compression { | ||
# Compression algorithm. | ||
# - off : no compression | ||
# - gzip : using common java gzip | ||
algorithm = off | ||
|
||
# If compression is enabled with the `algorithm` setting, | ||
# the payload will be compressed if its size is bigger than this value. | ||
compress-larger-than = 32 KiB | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
circe-akka-serializer/src/main/scala/org/virtuslab/ash/circe/Compression.scala
This file contains hidden or 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,74 @@ | ||
package org.virtuslab.ash.circe | ||
|
||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.util.zip.GZIPInputStream | ||
import java.util.zip.GZIPOutputStream | ||
|
||
import scala.annotation.tailrec | ||
|
||
object Compression { | ||
sealed trait Algorithm | ||
case object Off extends Algorithm | ||
case class GZip(greaterThan: Long) extends Algorithm | ||
// TODO (#159): add support for LZ4 java compression | ||
// case class LZ4(greaterThan: Long) extends Algorithm | ||
|
||
private[circe] def compressIfNeeded( | ||
bytes: Array[Byte], | ||
bufferSize: Int, | ||
compressionAlgorithm: Algorithm): Array[Byte] = | ||
compressionAlgorithm match { | ||
case Compression.Off => bytes | ||
case Compression.GZip(largerThan) => | ||
if (bytes.length <= largerThan) { | ||
bytes | ||
} else { | ||
val byteArrayOutputStream = new ByteArrayOutputStream(bufferSize) | ||
val outputStream = new GZIPOutputStream(byteArrayOutputStream) | ||
try outputStream.write(bytes) | ||
finally outputStream.close() | ||
byteArrayOutputStream.toByteArray | ||
} | ||
} | ||
|
||
private[circe] def decompressIfNeeded(bytes: Array[Byte], bufferSize: Int): Array[Byte] = | ||
if (isCompressedWithGzip(bytes)) { | ||
val inputStream = new GZIPInputStream(new ByteArrayInputStream(bytes)) | ||
val outputStream = new ByteArrayOutputStream() | ||
val buffer = new Array[Byte](bufferSize) | ||
|
||
@tailrec def readChunk(): Unit = | ||
inputStream.read(buffer) match { | ||
case -1 => () | ||
case n => | ||
outputStream.write(buffer, 0, n) | ||
readChunk() | ||
} | ||
|
||
try readChunk() | ||
finally inputStream.close() | ||
outputStream.toByteArray | ||
} else { | ||
bytes | ||
} | ||
|
||
/* | ||
Since we are encoding JSON for Ser <: AnyRef types - they will start with '{' char for Json objects or with '[' char for Arrays. | ||
LukaszKontowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Thus, the first element of the `bytes` array is either the 123 Byte number - which is the decimal representation of the { character, | ||
or - the 91 Byte number - which is the decimal representation of the [ character. | ||
|
||
So, below quick comment on why isCompressedWithGzip will not return false positives (for not compressed JSON data): | ||
|
||
bytes(0) == GZIPInputStream.GZIP_MAGIC.toByte | ||
gets evaluated to: | ||
bytes(0) == 35615.toByte | ||
which gets evaluated to: | ||
bytes(0) == 31 // where 31 is of type Byte | ||
And since bytes(0) holds a Byte with value equal to 123 or 91 - this will never be true. | ||
*/ | ||
private[circe] def isCompressedWithGzip(bytes: Array[Byte]): Boolean = | ||
(bytes != null) && (bytes.length >= 2) && | ||
(bytes(0) == GZIPInputStream.GZIP_MAGIC.toByte) && | ||
LukaszKontowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(bytes(1) == (GZIPInputStream.GZIP_MAGIC >> 8).toByte) | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.