-
Notifications
You must be signed in to change notification settings - Fork 6
/
Cryo.scala
125 lines (118 loc) · 3.93 KB
/
Cryo.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2021 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fabric
import java.nio.ByteBuffer
import scala.collection.immutable.VectorMap
object Cryo {
private object identifiers {
val Obj: Byte = 1
val Str: Byte = 2
val NumInt: Byte = 3
val NumDec: Byte = 4
val Bool: Byte = 5
val Arr: Byte = 6
val Null: Byte = -1
}
private object bytes {
val Byte: Int = 1
val Integer: Int = 4
val Long: Int = 8
}
def bytes(json: Json): Int = json match {
case Obj(map) => bytes.Byte + bytes.Integer + map.foldLeft(0)((sum, t) =>
t match {
case (key, value) => sum + bytes(Str(key)) + bytes(value)
}
)
case Str(s, _) => bytes.Byte + bytes.Integer + s.length
case NumInt(_, _) => bytes.Byte + bytes.Long
case NumDec(bd, _) => bytes.Byte + bytes(Str(bd.toString()))
case Bool(_, _) => bytes.Byte + bytes.Byte
case Arr(v, _) => bytes.Byte + bytes.Integer + v.foldLeft(0)((sum, json) => sum + bytes(json))
case Null => bytes.Byte
}
def freeze(json: Json, allocateDirect: Boolean): ByteBuffer = {
val size = bytes(json)
val bb =
if (allocateDirect) ByteBuffer.allocateDirect(size)
else ByteBuffer.allocate(size)
freeze(json, bb)
bb
}
def freeze(json: Json, bb: ByteBuffer): Unit = json match {
case Obj(map) =>
bb.put(identifiers.Obj)
bb.putInt(map.size)
map.foreach { case (key, value) =>
freeze(Str(key), bb)
freeze(value, bb)
}
case Str(s, _) =>
bb.put(identifiers.Str)
bb.putInt(s.length)
bb.put(s.getBytes("UTF-8"))
()
case NumInt(l, _) =>
bb.put(identifiers.NumInt)
bb.putLong(l)
()
case NumDec(bd, _) =>
bb.put(identifiers.NumDec)
freeze(Str(bd.toString()), bb)
case Bool(b, _) =>
bb.put(identifiers.Bool)
bb.put(if (b) 1.toByte else 0.toByte)
()
case Arr(v, _) =>
bb.put(identifiers.Arr)
bb.putInt(v.length)
v.foreach(json => freeze(json, bb))
case Null =>
bb.put(identifiers.Null)
()
}
def thaw(bb: ByteBuffer): Json = bb.get() match {
case identifiers.Obj =>
val size = bb.getInt
val map = VectorMap((0 until size).map { _ =>
val key = thaw(bb).asString
val value = thaw(bb)
key -> value
}: _*)
Obj(map)
case identifiers.Str =>
val size = bb.getInt
val array = new Array[Byte](size)
bb.get(array)
Str(new String(array, "UTF-8"))
case identifiers.NumInt => NumInt(bb.getLong)
case identifiers.NumDec =>
val s = thaw(bb).asString
NumDec(BigDecimal(s))
case identifiers.Bool => Bool(bb.get() == 1.toByte)
case identifiers.Arr =>
val size = bb.getInt
val v = (0 until size).toVector.map(_ => thaw(bb))
Arr(v)
case identifiers.Null => Null
case v => throw new UnsupportedOperationException(s"Unsupported: $v")
}
}