Skip to content

Commit

Permalink
refactor: rename storer functions, rename protobuf lib to protobuf-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Aug 11, 2020
1 parent 45fe2de commit de12506
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 57 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ It's backed by [Coroutines](https://kotlinlang.org/docs/reference/coroutines-ove
- [x] `String` and `List<String>`
- [x] `Serializable`¹

¹ *Not supported by `satchel-serializer-protobuf`*
¹ *Not supported by `satchel-serializer-protobuf-lite`*

# Setup
1. Add the JitPack repository to your project level `build.gradle`:
Expand Down Expand Up @@ -62,7 +62,7 @@ dependencies {
implementation "com.github.adrielcafe.satchel:satchel-serializer-base64-jvm:$currentVersion"
implementation "com.github.adrielcafe.satchel:satchel-serializer-gzip:$currentVersion"
implementation "com.github.adrielcafe.satchel:satchel-serializer-kryo:$currentVersion"
implementation "com.github.adrielcafe.satchel:satchel-serializer-protobuf:$currentVersion"
implementation "com.github.adrielcafe.satchel:satchel-serializer-protobuf-lite:$currentVersion"
}
```
Current version: [![JitPack](https://img.shields.io/jitpack/v/github/adrielcafe/satchel.svg?style=flat-square)](https://jitpack.io/#adrielcafe/satchel)
Expand Down Expand Up @@ -184,11 +184,11 @@ Create a `class` or `object` that implements the `SatchelStorer` interface:
```kotlin
object MySatchelStorer : SatchelStorer {

suspend fun save(data: ByteArray) {
suspend fun store(data: ByteArray) {
// Save the ByteArray wherever you want
}

fun load(): ByteArray {
fun retrieve(): ByteArray {
// Load and return the stored ByteArray
}
}
Expand Down Expand Up @@ -283,10 +283,10 @@ val serializer = KryoSatchelSerializer
```
:warning: At the moment Kryo 5 only works on Android API 26 and later, [this issue](https://github.com/EsotericSoftware/kryo/issues/691) explains how to make it work in previous versions.

#### [ProtobufSatchelSerializer](https://github.com/adrielcafe/satchel/blob/master/satchel-serializer-protobuf/src/main/java/cafe/adriel/satchel/serializer/protobuf/ProtobufSatchelSerializer.kt)
#### [ProtobufLiteSatchelSerializer](https://github.com/adrielcafe/satchel/blob/master/satchel-serializer-protobuf-lite/src/main/java/cafe/adriel/satchel/serializer/protobuf/lite/ProtobufLiteSatchelSerializer.kt)
Uses the [Protocol Buffers](https://github.com/protocolbuffers/protobuf) library for serialization/deserialization.
```kotlin
val serializer = ProtobufSatchelSerializer
val serializer = ProtobufLiteSatchelSerializer
```
:warning: The current implementation doesn't supports [Serializable](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) objects.

Expand Down Expand Up @@ -350,4 +350,4 @@ Keep in mind that by using different modules you can get best or worse performan
| `Base64SatchelSerializer` (Android) | 683.231 | 1.029.077 |
| `Base64SatchelSerializer` (JVM) | 703.769 | 1.041.000 |
| `KryoSatchelSerializer` | 209.923 | 170.654 |
| `ProtobufSatchelSerializer` | 629.116 | 1.319.961 |
| `ProtobufLiteSatchelSerializer` | 629.116 | 1.319.961 |
2 changes: 1 addition & 1 deletion benchmark/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
androidTestImplementation project(':satchel-serializer-gzip')
androidTestImplementation project(':satchel-serializer-base64-jvm')
androidTestImplementation project(':satchel-serializer-base64-android')
androidTestImplementation project(':satchel-serializer-protobuf')
androidTestImplementation project(':satchel-serializer-protobuf-lite')
// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// androidTestImplementation project(':satchel-serializer-flatbuffers')
androidTestImplementation project(':satchel-serializer-kryo')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package cafe.adriel.satchel.benchmark.serializer
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import cafe.adriel.satchel.benchmark.sampleData
import cafe.adriel.satchel.ktx.serialize
import cafe.adriel.satchel.serializer.SatchelSerializer
import cafe.adriel.satchel.serializer.base64.android.Base64SatchelSerializer as AndroidBase64SatchelSerializer
import cafe.adriel.satchel.serializer.base64.jvm.Base64SatchelSerializer as JvmBase64SatchelSerializer
import cafe.adriel.satchel.serializer.gzip.GzipSatchelSerializer
import cafe.adriel.satchel.serializer.kryo.KryoSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.ProtobufSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.lite.ProtobufLiteSatchelSerializer
import cafe.adriel.satchel.serializer.raw.RawSatchelSerializer
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Rule
Expand All @@ -33,7 +32,7 @@ class SerializerReadBenchmark {
fun base64Android() = runBenchmark(AndroidBase64SatchelSerializer)

@Test
fun protobuf() = runBenchmark(ProtobufSatchelSerializer)
fun protobuf() = runBenchmark(ProtobufLiteSatchelSerializer)

// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// @Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package cafe.adriel.satchel.benchmark.serializer
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import cafe.adriel.satchel.benchmark.sampleData
import cafe.adriel.satchel.ktx.serialize
import cafe.adriel.satchel.serializer.SatchelSerializer
import cafe.adriel.satchel.serializer.base64.android.Base64SatchelSerializer as AndroidBase64SatchelSerializer
import cafe.adriel.satchel.serializer.base64.jvm.Base64SatchelSerializer as JvmBase64SatchelSerializer
import cafe.adriel.satchel.serializer.gzip.GzipSatchelSerializer
import cafe.adriel.satchel.serializer.kryo.KryoSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.ProtobufSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.lite.ProtobufLiteSatchelSerializer
import cafe.adriel.satchel.serializer.raw.RawSatchelSerializer
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Rule
Expand All @@ -33,7 +32,7 @@ class SerializerWriteBenchmark {
fun base64Android() = runBenchmark(AndroidBase64SatchelSerializer)

@Test
fun protobuf() = runBenchmark(ProtobufSatchelSerializer)
fun protobuf() = runBenchmark(ProtobufLiteSatchelSerializer)

// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// @Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class StorerReadBenchmark {
)

private fun runBenchmark(storer: SatchelStorer) = runBlockingTest {
storer.save(serializedSampleData)
storer.store(serializedSampleData)

benchmarkRule.measureRepeated {
storer.load()
storer.retrieve()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class StorerWriteBenchmark {
private fun runBenchmark(storer: SatchelStorer) {
benchmarkRule.measureRepeated {
runBlockingTest {
storer.save(serializedSampleData)
storer.store(serializedSampleData)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

implementation project(':satchel-serializer-base64-android')
implementation project(':satchel-serializer-gzip')
implementation project(':satchel-serializer-protobuf')
implementation project(':satchel-serializer-protobuf-lite')
// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// implementation project(':satchel-serializer-flatbuffers')
implementation project(':satchel-serializer-kryo')
Expand Down
4 changes: 2 additions & 2 deletions satchel-core/src/main/java/cafe/adriel/satchel/Satchel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Satchel private constructor(
}

private fun loadStorage(): Map<String, Any> =
storer.load()
storer.retrieve()
.let(encrypter::decrypt)
.let(serializer::deserialize)

Expand All @@ -118,7 +118,7 @@ class Satchel private constructor(
saveMutex.withLock {
serializer.serialize(storage)
.let { encrypter.encrypt(it) }
.let { storer.save(it) }
.let { storer.store(it) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cafe.adriel.satchel.storer

interface SatchelStorer {

suspend fun save(data: ByteArray)
suspend fun store(data: ByteArray)

fun load(): ByteArray
fun retrieve(): ByteArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import java.io.File

class FileSatchelStorer(private val file: File) : SatchelStorer {

override suspend fun save(data: ByteArray) =
override suspend fun store(data: ByteArray) =
file.writeBytes(data)

override fun load(): ByteArray =
override fun retrieve(): ByteArray =
when {
file.isEmpty -> ByteArray(0)
else -> file.readBytes()
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cafe.adriel.satchel.serializer.protobuf
package cafe.adriel.satchel.serializer.protobuf.lite

import cafe.adriel.satchel.serializer.protobuf.proto.SatchelProto
import cafe.adriel.satchel.serializer.protobuf.proto.SatchelProto.Value.Type.TypeCase
import cafe.adriel.satchel.serializer.protobuf.lite.proto.SatchelProto.Value
import cafe.adriel.satchel.serializer.protobuf.lite.proto.SatchelProto.Value.Type
import cafe.adriel.satchel.serializer.protobuf.lite.proto.SatchelProto.Value.Type.TypeCase

internal fun Any.toProtoValue(): SatchelProto.Value =
internal fun Any.toProtoValue(): Value =
if (this is List<*>) {
SatchelProto.Value
.newBuilder()
Value.newBuilder()
.also { builder ->
this.forEach { item ->
item
Expand All @@ -16,16 +16,13 @@ internal fun Any.toProtoValue(): SatchelProto.Value =
}
.build()
} else {
SatchelProto.Value
.newBuilder()
Value.newBuilder()
.setSingleValue(getProtoType(this))
.build()
}

private fun getProtoType(value: Any): SatchelProto.Value.Type =
SatchelProto.Value
.Type
.newBuilder()
private fun getProtoType(value: Any): Type =
Type.newBuilder()
.apply {
when (value) {
is Double -> doubleValue = value
Expand All @@ -39,14 +36,14 @@ private fun getProtoType(value: Any): SatchelProto.Value.Type =
}
.build()

internal fun SatchelProto.Value.toAnyValue(): Any =
internal fun Value.toAnyValue(): Any =
if (hasSingleValue()) {
singleValue.toSingleValue()
} else {
multiValueList.toListValue()
}

internal fun SatchelProto.Value.Type.toSingleValue(): Any =
internal fun Type.toSingleValue(): Any =
when (typeCase) {
TypeCase.DOUBLE_VALUE -> doubleValue
TypeCase.FLOAT_VALUE -> floatValue
Expand All @@ -57,5 +54,5 @@ internal fun SatchelProto.Value.Type.toSingleValue(): Any =
else -> throw TypeCastException("Protobuf type not supported: $typeCase")
}

internal fun List<SatchelProto.Value.Type>.toListValue(): List<Any> =
internal fun List<Type>.toListValue(): List<Any> =
mapNotNull { it.toSingleValue() }
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cafe.adriel.satchel.serializer.protobuf
package cafe.adriel.satchel.serializer.protobuf.lite

import cafe.adriel.satchel.serializer.SatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.proto.SatchelProto
import cafe.adriel.satchel.serializer.protobuf.lite.proto.SatchelProto

object ProtobufSatchelSerializer : SatchelSerializer {
object ProtobufLiteSatchelSerializer : SatchelSerializer {

override suspend fun serialize(data: Map<String, Any>): ByteArray =
SatchelProto.Satchel
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

option java_package = "cafe.adriel.satchel.serializer.protobuf.proto";
option java_package = "cafe.adriel.satchel.serializer.protobuf.lite.proto";
option java_outer_classname = "SatchelProto";
option java_multiple_files = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class EncryptedFileSatchelStorer private constructor(
)
}

override suspend fun save(data: ByteArray) {
override suspend fun store(data: ByteArray) {
file.delete()
encryptedFile.openFileOutput().use { stream ->
stream.write(data)
}
}

override fun load(): ByteArray =
override fun retrieve(): ByteArray =
when {
file.isEmpty -> ByteArray(0)
else -> encryptedFile.openFileInput().use { stream ->
Expand Down
Binary file modified satchel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include ':sample',
':satchel-serializer-gzip',
':satchel-serializer-base64-jvm',
':satchel-serializer-base64-android',
':satchel-serializer-protobuf',
':satchel-serializer-protobuf-lite',
// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// ':satchel-serializer-flatbuffers',
':satchel-serializer-kryo'
2 changes: 1 addition & 1 deletion test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

implementation project(':satchel-serializer-gzip')
implementation project(':satchel-serializer-base64-jvm')
implementation project(':satchel-serializer-protobuf')
implementation project(':satchel-serializer-protobuf-lite')
// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// implementation project(':satchel-serializer-flatbuffers')
implementation project(':satchel-serializer-kryo')
Expand Down
10 changes: 5 additions & 5 deletions test/src/test/java/cafe/adriel/satchel/SatchelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SatchelTest {
@Test
fun `when init Satchel then load stored data`() {
coVerifyOrder {
storer.load()
storer.retrieve()
encrypter.decrypt(any())
serializer.deserialize(any())
}
Expand All @@ -69,7 +69,7 @@ class SatchelTest {
coVerifyOrder {
serializer.serialize(any())
encrypter.encrypt(any())
storer.save(any())
storer.store(any())
}
}

Expand All @@ -81,7 +81,7 @@ class SatchelTest {
coVerify(exactly = 2) {
serializer.serialize(any())
encrypter.encrypt(any())
storer.save(any())
storer.store(any())
}
}

Expand All @@ -93,7 +93,7 @@ class SatchelTest {
coVerify(exactly = 1) {
serializer.serialize(any())
encrypter.encrypt(any())
storer.save(any())
storer.store(any())
}
}

Expand All @@ -104,7 +104,7 @@ class SatchelTest {
coVerifyOrder {
serializer.serialize(any())
encrypter.encrypt(any())
storer.save(any())
storer.store(any())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cafe.adriel.satchel.serializer
import cafe.adriel.satchel.serializer.base64.jvm.Base64SatchelSerializer
import cafe.adriel.satchel.serializer.gzip.GzipSatchelSerializer
import cafe.adriel.satchel.serializer.kryo.KryoSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.ProtobufSatchelSerializer
import cafe.adriel.satchel.serializer.protobuf.lite.ProtobufLiteSatchelSerializer
import cafe.adriel.satchel.serializer.raw.RawSatchelSerializer
import cafe.adriel.satchel.util.SampleData
import java.util.stream.Stream
Expand All @@ -19,7 +19,7 @@ class SatchelSerializerTest {
private val gzipSerializer = GzipSatchelSerializer
private val base64Serializer = Base64SatchelSerializer
private val kryoSerializer = KryoSatchelSerializer
private val protobufSerializer = ProtobufSatchelSerializer
private val protobufSerializer = ProtobufLiteSatchelSerializer
// TODO waiting for fix https://github.com/google/flatbuffers/issues/5944
// private val flatbuffersSerializer = FlatbuffersSatchelSerializer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class SatchelStorerTest {
).map { storer ->
dynamicTest(storer::class.simpleName) {
runBlockingTest {
storer.save(sampleByteArray)
val loaded = storer.load()
storer.store(sampleByteArray)
val loaded = storer.retrieve()

expectThat(loaded) contentEquals sampleByteArray
expectThat(loaded) propertiesAreEqualTo sampleByteArray
Expand Down

0 comments on commit de12506

Please sign in to comment.