|
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