-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c2e4ed
commit a053bf3
Showing
7 changed files
with
126 additions
and
2 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
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
53 changes: 53 additions & 0 deletions
53
zstd/src/main/scala/zeroformatter/zstd/ZeroFormatter.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,53 @@ | ||
package zeroformatter | ||
package zstd | ||
|
||
import com.github.luben.zstd.Zstd | ||
|
||
object ZeroFormatter { | ||
|
||
def serialize[T](value: T, level: Int = 3)(implicit F: Formatter[T]): Array[Byte] = { | ||
var encoder = unsafe.UnsafeEncoder(new Array[Byte](F.length.getOrElse(0))) | ||
val decompressedLength = F.serialize(encoder, 0, value) | ||
val binary = encoder.toByteArray | ||
if(decompressedLength <= 64) { | ||
encoder = unsafe.UnsafeEncoder(new Array[Byte](decompressedLength + 4)) | ||
encoder.writeIntUnsafe(0, decompressedLength) | ||
encoder.writeByteArrayUnsafe(4, binary, 0, decompressedLength) | ||
encoder.toByteArray | ||
} | ||
else { | ||
//val maxCompressedLength = Zstd.compressBound(decompressedLength).toInt | ||
//val compressed = new Array[Byte](maxCompressedLength + 4) | ||
//val size = Zstd.compressFastDict(compressed, 4, binary, 0, decompressedLength, null) | ||
//if(Zstd.isError(size)) throw new Exception(Zstd.getErrorName(size)) | ||
//else { | ||
// encoder = unsafe.UnsafeEncoder(compressed) | ||
// encoder.writeIntUnsafe(0, decompressedLength) | ||
// encoder.resize(size.toInt) | ||
// encoder.toByteArray | ||
//} | ||
encoder.resize(decompressedLength) | ||
val compressed = Zstd.compress(encoder.toByteArray, level) | ||
encoder = unsafe.UnsafeEncoder(new Array[Byte](compressed.length + 4)) | ||
encoder.writeIntUnsafe(0, decompressedLength) | ||
encoder.writeByteArrayUnsafe(4, compressed) | ||
encoder.toByteArray | ||
} | ||
} | ||
|
||
def deserialize[T](bytes: Array[Byte])(implicit F: Formatter[T]): T = { | ||
val decoder = unsafe.UnsafeDecoder(bytes, 0) | ||
val decompressedLength = decoder.readInt() | ||
if(decompressedLength < 0) throw FormatException(0, s"Invalid lz4 decompressed length($decompressedLength).") | ||
else if(decompressedLength <= 64) F.deserialize(decoder) | ||
else { | ||
//val restored = new Array[Byte](decompressedLength) | ||
//val size = Zstd.decompressFastDict(restored, 0, bytes, 4, bytes.length - 4, null) | ||
//if(Zstd.isError(size)) throw new FormatException(4, Zstd.getErrorName(size)) | ||
//else unsafe.ZeroFormatter.deserialize[T](restored) | ||
val encoder = unsafe.UnsafeEncoder(new Array[Byte](bytes.length - 4)) | ||
encoder.writeByteArrayUnsafe(0, bytes, 4, bytes.length - 4) | ||
unsafe.ZeroFormatter.deserialize[T](Zstd.decompress(encoder.toByteArray, decompressedLength)) | ||
} | ||
} | ||
} |
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 zeroformatter | ||
|
||
import dog._ | ||
import scalaz.Equal | ||
import scalaz.std.anyVal._ | ||
import scalaz.std.string._ | ||
|
||
object ZstdTest extends Base { | ||
|
||
val `serialize Array[Int]` = TestCase { | ||
val values = (0 to 14).toArray | ||
assert.eq(values, zstd.ZeroFormatter.deserialize[Array[Int]](zstd.ZeroFormatter.serialize(values))) | ||
} | ||
|
||
@ZeroFormattable | ||
case class TestElement( | ||
@Index(0) a: Int, | ||
@Index(1) b: String, | ||
@Index(2) c: Short | ||
) | ||
|
||
implicit val testElementEqual: Equal[TestElement] = new Equal[TestElement] { | ||
override def equal(a1: TestElement, a2: TestElement) = | ||
Equal[Int].equal(a1.a, a2.a) && Equal[String].equal(a1.b, a2.b) && Equal[Short].equal(a1.c, a2.c) | ||
} | ||
|
||
val `serialize Array[TestElement]` = TestCase { | ||
val values = Array(TestElement(2, "01234", 3), TestElement(4, "567890", 5)) | ||
assert.eq(values, zstd.ZeroFormatter.deserialize[Array[TestElement]](zstd.ZeroFormatter.serialize(values))) | ||
} | ||
} |