-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[greyhound] parallel consumer - compression and encoding for gaps lim…
…it (#35423) GitOrigin-RevId: 10b761cb8fcd926178aebf586e99ed1a8829c734
- Loading branch information
1 parent
59fad3a
commit b67f67e
Showing
7 changed files
with
101 additions
and
14 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
core/src/main/scala/com/wixpress/dst/greyhound/core/compression/BUILD.bazel
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,14 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
# visibility is extended to allow packaging a jar to deploy to maven central | ||
sources(["//core:__subpackages__"]) | ||
|
||
scala_library( | ||
name = "compression", | ||
srcs = [ | ||
":sources", | ||
], | ||
deps = [ | ||
"@org_apache_commons_commons_compress", | ||
], | ||
) |
39 changes: 39 additions & 0 deletions
39
core/src/main/scala/com/wixpress/dst/greyhound/core/compression/GzipCompression.scala
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,39 @@ | ||
package com.wixpress.dst.greyhound.core.compression | ||
|
||
import org.apache.commons.compress.utils.IOUtils | ||
|
||
import java.io.{ByteArrayInputStream, ByteArrayOutputStream} | ||
import java.util.zip.{GZIPInputStream, GZIPOutputStream} | ||
import scala.util.Try | ||
|
||
object GzipCompression { | ||
def compress(input: Array[Byte]): Array[Byte] = { | ||
val bos = new ByteArrayOutputStream(input.length) | ||
val gzip = new GZIPOutputStream(bos) | ||
gzip.write(input) | ||
gzip.close() | ||
val compressed = bos.toByteArray | ||
bos.close() | ||
compressed | ||
} | ||
|
||
def decompress(compressed: Array[Byte]): Option[Array[Byte]] = { | ||
val byteStream = new ByteArrayInputStream(compressed) | ||
Try(new GZIPInputStream(byteStream)) | ||
.flatMap(gzipStream => | ||
Try { | ||
val result = IOUtils.toByteArray(gzipStream) | ||
gzipStream.close() | ||
byteStream.close() | ||
result | ||
}.recover { | ||
case e: Throwable => | ||
Try(gzipStream.close()) | ||
Try(byteStream.close()) | ||
e.printStackTrace() | ||
throw e | ||
} | ||
) | ||
.toOption | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
core/src/test/scala/com/wixpress/dst/greyhound/core/compression/BUILD.bazel
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,15 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
sources() | ||
|
||
specs2_unit_test( | ||
name = "compression", | ||
srcs = [ | ||
":sources", | ||
], | ||
deps = [ | ||
"//core/src/main/scala/com/wixpress/dst/greyhound/core", | ||
"//core/src/main/scala/com/wixpress/dst/greyhound/core/compression", | ||
"//core/src/test/scala/com/wixpress/dst/greyhound/core/testkit", | ||
], | ||
) |
11 changes: 11 additions & 0 deletions
11
core/src/test/scala/com/wixpress/dst/greyhound/core/compression/GzipCompressionTest.scala
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,11 @@ | ||
package com.wixpress.dst.greyhound.core.compression | ||
|
||
import com.wixpress.dst.greyhound.core.testkit.BaseTestNoEnv | ||
|
||
class GzipCompressionTest extends BaseTestNoEnv { | ||
"GZIPCompressor" should { | ||
"return None for bad input" in { | ||
GzipCompression.decompress("not a gzip".toCharArray.map(_.toByte)) must beNone | ||
} | ||
} | ||
} |
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