Skip to content

Commit a93be37

Browse files
committed
enabled extensions package
1 parent dcf237a commit a93be37

25 files changed

Lines changed: 3972 additions & 3981 deletions

File tree

api-stress/src/test/scala/swaydb/weather/WeatherDataSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
145145
key < (startFrom + 100)
146146
}
147147

148-
took.materialize.right.value should have size 100
148+
took.materialize.runRandomIO.right.value should have size 100
149149
took.headOption.get.get._1 shouldBe startFrom
150150
took.lastOption.get.get._1 shouldBe (startFrom + 99)
151151
}
@@ -178,7 +178,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
178178
if (key % 10000 == 0)
179179
println(s"mapRight: key = $key")
180180
key
181-
}.materialize.right.value
181+
}.materialize.runRandomIO.right.value
182182

183183
val expected = (0 until 100) map (startFrom - _)
184184
took should have size 100
@@ -193,7 +193,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
193193
if (key % 10000 == 0)
194194
println(s"take: key = $key")
195195
key
196-
}.materialize.right.value shouldBe (1 to 100)
196+
}.materialize.runRandomIO.right.value shouldBe (1 to 100)
197197

198198
db
199199
.fromOrAfter(0)
@@ -203,7 +203,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
203203
if (key % 10000 == 0)
204204
println(s"take: key = $key")
205205
key
206-
}.materialize.right.value shouldBe (1 to 100)
206+
}.materialize.runRandomIO.right.value shouldBe (1 to 100)
207207
}
208208

209209
def doDrop =
@@ -215,7 +215,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
215215
if (key % 10000 == 0)
216216
println(s"take: key = $key")
217217
key
218-
}.materialize.right.value shouldBe (keyValueCount - 100 to keyValueCount)
218+
}.materialize.runRandomIO.right.value shouldBe (keyValueCount - 100 to keyValueCount)
219219

220220
def doTakeRight =
221221
db
@@ -227,7 +227,7 @@ sealed trait WeatherDataSpec extends TestBase with LazyLogging with BeforeAndAft
227227
if (key % 10000 == 0)
228228
println(s"take: key = $key")
229229
key
230-
}.materialize.right.value shouldBe (keyValueCount - 99 to keyValueCount).reverse
230+
}.materialize.runRandomIO.right.value shouldBe (keyValueCount - 99 to keyValueCount).reverse
231231

232232
def doCount =
233233
db.size.get should be >= keyValueCount
Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,64 @@
1-
///*
2-
// * Copyright (c) 2019 Simer Plaha (@simerplaha)
3-
// *
4-
// * This file is a part of SwayDB.
5-
// *
6-
// * SwayDB is free software: you can redistribute it and/or modify
7-
// * it under the terms of the GNU Affero General Public License as
8-
// * published by the Free Software Foundation, either version 3 of the
9-
// * License, or (at your option) any later version.
10-
// *
11-
// * SwayDB is distributed in the hope that it will be useful,
12-
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
// * GNU Affero General Public License for more details.
15-
// *
16-
// * You should have received a copy of the GNU Affero General Public License
17-
// * along with SwayDB. If not, see <https://www.gnu.org/licenses/>.
18-
// */
19-
//
20-
//package swaydb.extensions
21-
//
22-
//import swaydb.Tag.Tag.CoreIO
23-
//import swaydb.{IO, Prepare}
24-
//import swaydb.data.order.KeyOrder
25-
//import swaydb.data.slice.Slice
26-
//import swaydb.serializers.Serializer
27-
//
28-
//private[extensions] object Extend {
29-
//
30-
// /**
31-
// * Wraps the input [[swaydb.Map]] instance and returns a new [[Map]] instance
32-
// * which contains extended APIs to create nested Maps.
33-
// */
34-
// def apply[K, V](map: swaydb.Map[Key[K], Option[V], Tag.CoreIO])(implicit keySerializer: Serializer[K],
35-
// optionValueSerializer: Serializer[Option[V]],
36-
// keyOrder: KeyOrder[Slice[Byte]]): IO[IO.OK, Map[K, V]] = {
37-
// implicit val mapKeySerializer = Key.serializer(keySerializer)
38-
//
39-
// implicit val valueSerializer = new Serializer[V] {
40-
// override def write(data: V): Slice[Byte] =
41-
// optionValueSerializer.write(Some(data))
42-
//
43-
// override def read(data: Slice[Byte]): V =
44-
// optionValueSerializer.read(data) getOrElse {
45-
// throw new Exception("optionValueSerializer returned None for value bytes.")
46-
// }
47-
// }
48-
// val rootMapKey = Seq.empty[K]
49-
//
50-
// map.commit(
51-
// Prepare.Put(Key.MapStart(rootMapKey), None),
52-
// Prepare.Put(Key.MapEntriesStart(rootMapKey), None),
53-
// Prepare.Put(Key.MapEntriesEnd(rootMapKey), None),
54-
// Prepare.Put(Key.SubMapsStart(rootMapKey), None),
55-
// Prepare.Put(Key.SubMapsEnd(rootMapKey), None),
56-
// Prepare.Put(Key.MapEnd(rootMapKey), None)
57-
// ) map {
58-
// _ =>
59-
//// Map[K, V](
60-
//// map = map,
61-
//// mapKey = rootMapKey
62-
//// )
63-
// ???
64-
// }
65-
// }
66-
//}
1+
/*
2+
* Copyright (c) 2019 Simer Plaha (@simerplaha)
3+
*
4+
* This file is a part of SwayDB.
5+
*
6+
* SwayDB is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* SwayDB is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with SwayDB. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package swaydb.extensions
21+
22+
import swaydb.data.order.KeyOrder
23+
import swaydb.data.slice.Slice
24+
import swaydb.serializers.Serializer
25+
import swaydb.{IO, Prepare}
26+
27+
private[extensions] object Extend {
28+
29+
/**
30+
* Wraps the input [[swaydb.Map]] instance and returns a new [[Map]] instance
31+
* which contains extended APIs to create nested Maps.
32+
*/
33+
def apply[K, V](map: swaydb.Map[Key[K], Option[V], IO.ApiIO])(implicit keySerializer: Serializer[K],
34+
optionValueSerializer: Serializer[Option[V]],
35+
keyOrder: KeyOrder[Slice[Byte]]): IO.ApiIO[Map[K, V]] = {
36+
implicit val mapKeySerializer = Key.serializer(keySerializer)
37+
38+
implicit val valueSerializer = new Serializer[V] {
39+
override def write(data: V): Slice[Byte] =
40+
optionValueSerializer.write(Some(data))
41+
42+
override def read(data: Slice[Byte]): V =
43+
optionValueSerializer.read(data) getOrElse {
44+
throw new Exception("optionValueSerializer returned None for value bytes.")
45+
}
46+
}
47+
val rootMapKey = Seq.empty[K]
48+
49+
map.commit(
50+
Prepare.Put(Key.MapStart(rootMapKey), None),
51+
Prepare.Put(Key.MapEntriesStart(rootMapKey), None),
52+
Prepare.Put(Key.MapEntriesEnd(rootMapKey), None),
53+
Prepare.Put(Key.SubMapsStart(rootMapKey), None),
54+
Prepare.Put(Key.SubMapsEnd(rootMapKey), None),
55+
Prepare.Put(Key.MapEnd(rootMapKey), None)
56+
) map {
57+
_ =>
58+
Map[K, V](
59+
map = map,
60+
mapKey = rootMapKey
61+
)
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)