Skip to content

Commit f742686

Browse files
authored
Release 0.0.2 (#9)
1 parent 8fa0913 commit f742686

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1277
-1820
lines changed

README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ libraryDependencies ++= Seq(
2222
)
2323
```
2424

25-
The only dependencies are [zio](https://github.com/zio/zio) and [scodec](https://github.com/scodec/scodec).
26-
2725
## Getting started
2826
```scala
2927
import zio._
@@ -127,33 +125,28 @@ But you always can create your own `TupleEncoder[_]`:
127125
final case class TestTuple(f1: String, f2: Int, f3: Long)
128126

129127
implicit val tupleEncoder: TupleEncoder[TestTuple] = new TupleEncoder[TestTuple] {
130-
override def encode(v: TestTuple): Attempt[MpArray] =
131-
for {
132-
f1Mp <- Encoder[String].encode(v.f1)
133-
f2Mp <- Encoder[Int].encode(v.f2)
134-
f3Mp <- Encoder[Long].encode(v.f3)
135-
136-
tupleMp <- Encoder[Vector[MessagePack]].encode(Vector(f1Mp, f2Mp, f3Mp))
137-
} yield tupleMp.asInstanceOf[MpArray]
138-
139-
override def decode(v: MpArray, idx: Int): Attempt[TestTuple] = {
140-
val vector = v.value
141-
142-
val f1Mp = Encoder[String].decode(vector(idx)) // decode the first field
143-
val f2Mp = Encoder[Int].decode(vector(idx + 1)) // second
144-
val f3Mp = Encoder[Long].decode(vector(idx + 2)) // third
145-
146-
for {
147-
f1 <- f1Mp
148-
f2 <- f2Mp
149-
f3 <- f3Mp
150-
} yield TestTuple(f1, f2, f3)
128+
129+
override def decode(v: ArrayValue, idx: Int): TestTuple = {
130+
val f1Mp = Encoder[String].decode(v.get(idx))
131+
val f2Mp = Encoder[Int].decode(v.get(idx + 1))
132+
val f3Mp = Encoder[Long].decode(v.get(idx + 2))
133+
TestTuple(f1Mp, f2Mp, f3Mp)
134+
}
135+
136+
override def encode(v: TestTuple): Vector[Value] = {
137+
val f1Mp = Encoder[String].encode(v.f1)
138+
val f2Mp = Encoder[Int].encode(v.f2)
139+
val f3Mp = Encoder[Long].encode(v.f3)
140+
141+
Vector(f1Mp, f2Mp, f3Mp)
151142
}
152143
}
153144
```
154145

155146
## Update operations
156-
There is also an implicit update operation builder for any case class:
147+
**EXPERIMENTAL**
148+
149+
Instead of manual construction of update operation you can generate it from special implicit builder:
157150

158151
```scala
159152
import zio.tarantool.codec.auto._
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package zio.tarantool
2+
3+
import zio.{BootstrapRuntime, ZIO, ZLayer}
4+
import zio.tarantool.TarantoolClient.TarantoolClient
5+
import zio.clock.Clock
6+
import zio.internal.Platform
7+
8+
trait BenchmarkBase extends BootstrapRuntime {
9+
10+
override val platform: Platform = Platform.benchmark
11+
12+
def zioUnsafeRun[A](fa: ZIO[TarantoolClient, TarantoolError, A]): A = unsafeRun(
13+
fa.provideLayer(BenchmarkBase.layer)
14+
)
15+
}
16+
17+
object BenchmarkBase {
18+
private final val layer: ZLayer[Any, TarantoolError, TarantoolClient] =
19+
(Clock.live ++ ZLayer.succeed(
20+
TarantoolConfig(
21+
connectionConfig = ConnectionConfig(host = "localhost", port = 3301),
22+
clientConfig = ClientConfig(useSchemaMetaCache = false),
23+
authInfo = None
24+
)
25+
)) >>> TarantoolClient.live
26+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package zio.tarantool
2+
3+
import java.util.concurrent.TimeUnit
4+
5+
import org.msgpack.value._
6+
import org.msgpack.value.impl._
7+
import org.openjdk.jmh.annotations._
8+
import zio.ZIO
9+
import zio.tarantool.TarantoolClient.TarantoolClient
10+
import zio.tarantool.TarantoolClientBenchmark.A
11+
12+
@Fork(1)
13+
@State(Scope.Thread)
14+
@Warmup(iterations = 5)
15+
@Measurement(iterations = 5)
16+
@OutputTimeUnit(TimeUnit.SECONDS)
17+
@BenchmarkMode(Array(Mode.Throughput))
18+
class TarantoolClientBenchmark extends BenchmarkBase {
19+
@Param(Array("1000"))
20+
var count: Int = _
21+
22+
private var msgpackValues: List[Value] = _
23+
24+
private var notEncodedValues: List[A] = _
25+
26+
private var spaceId: Int = _
27+
28+
@Setup(Level.Trial)
29+
def setup(): Unit = {
30+
val init: ZIO[TarantoolClient, TarantoolError, Int] = for {
31+
space <- TarantoolClient.eval("box.schema.create_space('A', {if_not_exists = true})")
32+
ids <- TarantoolClient.eval("box.schema.sequence.create('ids', {if_not_exists = true})")
33+
index <- TarantoolClient.eval(
34+
"box.space.A:create_index('primary', {if_not_exists = true, sequence='ids'})"
35+
)
36+
37+
spaceIdReq <- TarantoolClient.eval("return box.space.A.id")
38+
spaceId <- spaceIdReq.await.flatMap(_.head[Int])
39+
_ <- space.await
40+
_ <- ids.await
41+
_ <- index.await
42+
} yield spaceId
43+
44+
spaceId = zioUnsafeRun(init)
45+
46+
msgpackValues = (0 to count).map { i =>
47+
new ImmutableArrayValueImpl(
48+
Array(
49+
ImmutableNilValueImpl.get(),
50+
new ImmutableLongValueImpl(i.toLong),
51+
new ImmutableStringValueImpl(s"value: $i")
52+
)
53+
)
54+
}.toList
55+
56+
notEncodedValues = (0 to count).map(i => A(None, i.toLong, s"value: $i")).toList
57+
}
58+
59+
@TearDown
60+
def tearDown(): Unit =
61+
zioUnsafeRun(
62+
TarantoolClient.eval("box.space.A:truncate()").flatMap(_.await.unit)
63+
)
64+
65+
@Benchmark
66+
def insertMsgpackValues(): Unit =
67+
zioUnsafeRun(
68+
ZIO
69+
.foreach(msgpackValues)(v => TarantoolClient.insert(spaceId, v))
70+
.flatMap(promises => ZIO.foreach_(promises)(_.await))
71+
)
72+
73+
@Benchmark
74+
def insertNotEncodedValues(): Unit = {
75+
import zio.tarantool.codec.auto._
76+
77+
zioUnsafeRun(
78+
ZIO
79+
.foreach(notEncodedValues)(v => TarantoolClient.insert(spaceId, v))
80+
.flatMap(promises => ZIO.foreach_(promises)(_.await))
81+
)
82+
}
83+
}
84+
85+
object TarantoolClientBenchmark {
86+
final case class A(id: Option[Long], f1: Long, f3: String)
87+
}

build.sbt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
val zioVersion = "1.0.3"
2-
val scodecVersion = "1.11.7"
2+
val shapelessVersion = "2.3.7"
3+
val msgpackVersion = "0.9.0"
34
val testContainersVersion = "0.39.5"
45
val logbackVersion = "1.2.3"
56

@@ -111,7 +112,8 @@ lazy val core = project
111112
.settings(
112113
moduleName := "zio-tarantool-core",
113114
libraryDependencies ++= Seq(
114-
"org.scodec" %% "scodec-core" % scodecVersion,
115+
"org.msgpack" % "msgpack-core" % msgpackVersion,
116+
"com.chuusai" %% "shapeless" % shapelessVersion,
115117
"dev.zio" %% "zio-streams" % zioVersion,
116118
"dev.zio" %% "zio-test" % zioVersion % Test,
117119
"dev.zio" %% "zio-test-sbt" % zioVersion % Test,
@@ -123,3 +125,11 @@ lazy val core = project
123125

124126
lazy val examples =
125127
project.in(file("examples")).settings(allSettings).settings(noPublish).dependsOn(core)
128+
129+
lazy val benchmarks =
130+
project
131+
.in(file("benchmarks"))
132+
.enablePlugins(JmhPlugin)
133+
.settings(allSettings)
134+
.settings(noPublish)
135+
.dependsOn(core)

0 commit comments

Comments
 (0)