-
Notifications
You must be signed in to change notification settings - Fork 0
/
gxid.gleam
205 lines (187 loc) · 4.95 KB
/
gxid.gleam
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gleam/int
import gleam/list
import gleam/bit_string
import gleam/string
import gleam/bitwise
import gleam/erlang.{Millisecond}
import gleam/otp/actor.{Continue, Next, StartResult}
import gleam/erlang/process.{Subject}
/// A library implementing XID generation
/// Message handled by the actor
pub opaque type Message {
Generate(Subject(String))
}
/// Actor's internal state
pub opaque type State {
State(random_number: Int, pid: Int, machine_id: Int)
}
/// Starts XID generator
pub fn start() -> StartResult(Message) {
actor.start(
State(int.random(0, 16_777_215), get_int_pid(), get_machine_id()),
handle,
)
}
/// Generates a XID
///
/// ### Usage
/// ```gleam
/// import gxid
///
/// assert Ok(channel) = gxid.start()
///
/// let id: String = gxid.generate(channel)
/// ```
/// See: https:///hexdocs.pm/gleam_otp/0.1.1/gleam/otp/actor/#call
pub fn generate(channel: Subject(Message)) -> String {
actor.call(channel, Generate, 1000)
}
/// Handles generation logic (encoding)
fn handle(msg: Message, state: State) -> Next(State) {
case msg {
Generate(reply) -> {
let id =
format_id([
format_time(erlang.system_time(Millisecond)),
format_machine_id(state.machine_id),
format_pid(state.pid),
format_random_number(state.random_number),
])
actor.send(reply, id)
Continue(State(..state, random_number: state.random_number + 1))
}
}
}
/// 4-byte (32 bits) representation
fn format_time(time: Int) -> BitString {
<<time:big-32>>
}
/// 3-byte (24 bits) representation
fn format_machine_id(mid: Int) -> BitString {
<<mid:big-24>>
}
/// 2-byte (16 bits) representation
fn format_pid(pid: Int) -> BitString {
<<pid:big-16>>
}
/// 3-byte (24 bits) representation
fn format_random_number(random_number: Int) -> BitString {
<<bitwise.shift_left(random_number, 8):24>>
}
fn format_id(id_data: List(BitString)) -> String {
encode(
id_data
|> bit_string.concat(),
)
}
fn get_int_pid() -> Int {
assert Ok(pid) =
os_getpid()
|> char_list_to_string()
|> int.parse()
pid
}
fn get_machine_id() -> Int {
let localhost = net_adm_localhost()
localhost
|> list.fold(from: 0, with: fn(char, acc) { char + acc })
}
fn encode(bit_xid: BitString) -> String {
let <<b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11>> = <<
bit_xid:bit_string,
>>
let res = [
encode_hex(bitwise.shift_right(b0, 3)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b1, 6), 0x1F),
bitwise.and(bitwise.shift_left(b0, 2), 0x1F),
)),
encode_hex(bitwise.and(bitwise.shift_right(b1, 1), 0x1F)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b2, 4), 0x1F),
bitwise.and(bitwise.shift_left(b1, 4), 0x1F),
)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b3, 7), 0x1F),
bitwise.and(bitwise.shift_left(b2, 1), 0x1F),
)),
encode_hex(bitwise.and(bitwise.shift_right(b3, 2), 0x1F)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b4, 5), 0x1F),
bitwise.and(bitwise.shift_left(b3, 3), 0x1F),
)),
encode_hex(bitwise.and(b4, 0x1F)),
encode_hex(bitwise.shift_right(b5, 3)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b6, 6), 0x1F),
bitwise.and(bitwise.shift_left(b5, 2), 0x1F),
)),
encode_hex(bitwise.and(bitwise.shift_right(b6, 1), 0x1F)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b7, 4), 0x1F),
bitwise.and(bitwise.shift_left(b6, 4), 0x1F),
)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b8, 7), 0x1F),
bitwise.and(bitwise.shift_left(b7, 1), 0x1F),
)),
encode_hex(bitwise.and(bitwise.shift_right(b8, 2), 0x1F)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b9, 5), 0x1F),
bitwise.and(bitwise.shift_left(b8, 3), 0x1F),
)),
encode_hex(bitwise.and(b9, 0x1F)),
encode_hex(bitwise.shift_right(b10, 3)),
encode_hex(bitwise.or(
bitwise.and(bitwise.shift_right(b11, 6), 0x1F),
bitwise.and(bitwise.shift_left(b10, 2), 0x1F),
)),
encode_hex(bitwise.and(bitwise.shift_right(b11, 1), 0x1F)),
encode_hex(bitwise.and(bitwise.shift_left(b11, 4), 0x1F)),
]
string.join(res, "")
}
/// 0123456789abcdefghijklmnopqrstuv - Used for encoding
fn encode_hex(i: Int) -> String {
case i {
0 -> "0"
1 -> "1"
2 -> "2"
3 -> "3"
4 -> "4"
5 -> "5"
6 -> "6"
7 -> "7"
8 -> "8"
9 -> "9"
10 -> "a"
11 -> "b"
12 -> "c"
13 -> "d"
14 -> "e"
15 -> "f"
16 -> "g"
17 -> "h"
18 -> "i"
19 -> "j"
20 -> "k"
21 -> "l"
22 -> "m"
23 -> "n"
24 -> "o"
25 -> "p"
26 -> "q"
27 -> "r"
28 -> "s"
29 -> "t"
30 -> "u"
31 -> "v"
}
}
external type CharList
external fn os_getpid() -> CharList =
"os" "getpid"
external fn char_list_to_string(CharList) -> String =
"erlang" "list_to_binary"
external fn net_adm_localhost() -> List(Int) =
"net_adm" "localhost"