-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from rodrigo-molina/support-snappy
Support snappy
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 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
114 changes: 114 additions & 0 deletions
114
snappy/src/main/scala/de/lhns/fs2/compress/SnappyCompressor.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,114 @@ | ||
package de.lhns.fs2.compress | ||
|
||
import cats.effect.Async | ||
import fs2.Pipe | ||
import fs2.io._ | ||
import org.xerial.snappy.{ | ||
SnappyFramedInputStream, | ||
SnappyFramedOutputStream, | ||
SnappyHadoopCompatibleOutputStream, | ||
SnappyInputStream, | ||
SnappyOutputStream | ||
} | ||
|
||
import java.io.{BufferedInputStream, InputStream, OutputStream} | ||
|
||
class SnappyCompressor[F[_]: Async] private (chunkSize: Int, mode: SnappyCompressor.WriteMode) extends Compressor[F] { | ||
override def compress: Pipe[F, Byte, Byte] = { stream => | ||
readOutputStream[F](chunkSize) { outputStream => | ||
stream | ||
.through(writeOutputStream(Async[F].blocking[OutputStream] { | ||
mode.fromOutputStream(outputStream) | ||
})) | ||
.compile | ||
.drain | ||
} | ||
} | ||
} | ||
|
||
object SnappyCompressor { | ||
sealed trait WriteMode | ||
object WriteMode { | ||
// https://github.com/xerial/snappy-java/blob/ec23d7c611563bedce536ca4d02ebdb9a690ea91/src/main/java/org/xerial/snappy/SnappyOutputStream.java#L64 | ||
private val DefaultBasicBlockSize = 32 * 1024 | ||
private val DefaultHadoopBlockSize = DefaultBasicBlockSize | ||
|
||
/** See | ||
* [[https://github.com/xerial/snappy-java/blob/ec23d7c611563bedce536ca4d02ebdb9a690ea91/src/main/java/org/xerial/snappy/SnappyOutputStream.java#L59]] | ||
*/ | ||
final case class Basic(blockSize: Int = DefaultBasicBlockSize) extends WriteMode | ||
|
||
/** See | ||
* [[https://github.com/xerial/snappy-java/blob/ec23d7c611563bedce536ca4d02ebdb9a690ea91/src/main/java/org/xerial/snappy/SnappyFramedOutputStream.java#L34]] | ||
*/ | ||
final case class Framed( | ||
blockSize: Int = SnappyFramedOutputStream.DEFAULT_BLOCK_SIZE, | ||
minCompressionRatio: Double = SnappyFramedOutputStream.DEFAULT_MIN_COMPRESSION_RATIO | ||
) extends WriteMode | ||
|
||
/** Compression for use with Hadoop libraries: it does not emit a file header but write out the current block size | ||
* as a preamble to each block | ||
*/ | ||
final case class HadoopCompatible(blockSize: Int = DefaultHadoopBlockSize) extends WriteMode | ||
} | ||
|
||
private implicit class WriteModeOps(mode: WriteMode) { | ||
def fromOutputStream(o: OutputStream): OutputStream = mode match { | ||
case r: WriteMode.Basic => new SnappyOutputStream(o, r.blockSize) | ||
case f: WriteMode.Framed => new SnappyFramedOutputStream(o, f.blockSize, f.minCompressionRatio) | ||
case h: WriteMode.HadoopCompatible => new SnappyHadoopCompatibleOutputStream(o, h.blockSize) | ||
} | ||
} | ||
|
||
def apply[F[_]](implicit instance: SnappyCompressor[F]): SnappyCompressor[F] = instance | ||
|
||
def make[F[_]: Async]( | ||
chunkSize: Int = Defaults.defaultChunkSize, | ||
mode: WriteMode | ||
): SnappyCompressor[F] = new SnappyCompressor(chunkSize, mode) | ||
} | ||
|
||
class SnappyDecompressor[F[_]: Async] private (chunkSize: Int, decompressionType: SnappyDecompressor.ReadMode) | ||
extends Decompressor[F] { | ||
override def decompress: Pipe[F, Byte, Byte] = { stream => | ||
stream | ||
.through(toInputStream[F]) | ||
.map(new BufferedInputStream(_, chunkSize)) | ||
.flatMap { inputStream => | ||
readInputStream( | ||
Async[F].blocking( | ||
decompressionType.fromInputStream(inputStream) | ||
), | ||
chunkSize | ||
) | ||
} | ||
} | ||
} | ||
|
||
object SnappyDecompressor { | ||
sealed trait ReadMode | ||
object ReadMode { | ||
|
||
/** See | ||
* [[https://github.com/xerial/snappy-java/blob/ec23d7c611563bedce536ca4d02ebdb9a690ea91/src/main/java/org/xerial/snappy/SnappyInputStream.java#L36]] | ||
*/ | ||
final case class Basic(maxChunkSize: Int = SnappyInputStream.MAX_CHUNK_SIZE) extends ReadMode | ||
|
||
/** See | ||
* [[https://github.com/xerial/snappy-java/blob/ec23d7c611563bedce536ca4d02ebdb9a690ea91/src/main/java/org/xerial/snappy/SnappyFramedInputStream.java#L39]] | ||
*/ | ||
final case class Framed(verifyChecksums: Boolean = true) extends ReadMode | ||
} | ||
|
||
private implicit class ReadModeOps(mode: ReadMode) { | ||
def fromInputStream(i: InputStream): InputStream = mode match { | ||
case r: ReadMode.Basic => new SnappyInputStream(i, r.maxChunkSize) | ||
case f: ReadMode.Framed => new SnappyFramedInputStream(i, f.verifyChecksums) | ||
} | ||
} | ||
|
||
def apply[F[_]](implicit instance: SnappyDecompressor[F]): SnappyDecompressor[F] = instance | ||
|
||
def make[F[_]: Async](chunkSize: Int = Defaults.defaultChunkSize, mode: ReadMode): SnappyDecompressor[F] = | ||
new SnappyDecompressor(chunkSize, mode) | ||
} |
43 changes: 43 additions & 0 deletions
43
snappy/src/test/scala/de/lhns/fs2/compress/SnappyRoundTripSuite.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,43 @@ | ||
package de.lhns.fs2.compress | ||
|
||
import cats.effect.IO | ||
import cats.effect.std.Random | ||
import fs2.{Chunk, Stream} | ||
import munit.CatsEffectSuite | ||
|
||
import java.util | ||
|
||
class SnappyRoundTripSuite extends CatsEffectSuite { | ||
|
||
test("snappy unframed round trip") { | ||
val readMode = SnappyDecompressor.ReadMode.Basic() | ||
val writeMode = SnappyCompressor.WriteMode.Basic() | ||
|
||
testRoundTrip(readMode, writeMode) | ||
} | ||
|
||
test("snappy framed round trip") { | ||
val readMode = SnappyDecompressor.ReadMode.Framed() | ||
val writeMode = SnappyCompressor.WriteMode.Framed() | ||
|
||
testRoundTrip(readMode, writeMode) | ||
} | ||
|
||
private def testRoundTrip(readMode: SnappyDecompressor.ReadMode, writeMode: SnappyCompressor.WriteMode): IO[Unit] = { | ||
implicit val snappyCompressor: SnappyCompressor[IO] = SnappyCompressor.make(mode = writeMode) | ||
implicit val snappyDecompressor: SnappyDecompressor[IO] = SnappyDecompressor.make(mode = readMode) | ||
for { | ||
random <- Random.scalaUtilRandom[IO] | ||
expected <- random.nextBytes(1024 * 1024) | ||
obtained <- Stream | ||
.chunk(Chunk.array(expected)) | ||
.through(SnappyCompressor[IO].compress) | ||
.through(SnappyDecompressor[IO].decompress) | ||
.chunkAll | ||
.compile | ||
.lastOrError | ||
.map(_.toArray) | ||
_ = assert(util.Arrays.equals(expected, obtained)) | ||
} yield () | ||
} | ||
} |