-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_value.gleam
58 lines (50 loc) 路 1.41 KB
/
key_value.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
//// An actor that provides a simple key-value store.
import gleam/dict.{type Dict}
import gleam/erlang/process.{type Subject}
import gleam/otp/actor
import gleam/result
/// An actor that serves as an in-memory key-value store.
pub opaque type KeyValueActor(a) {
KeyValueActor(subject: Subject(Message(a)))
}
/// Starts a new `KeyValueActor`.
pub fn start() -> Result(KeyValueActor(a), actor.StartError) {
actor.start(dict.new(), handle_message)
|> result.map(KeyValueActor)
}
/// Sets the value associated with the given key.
///
/// If value already exists for the given key it will be overwritten.
pub fn set(actor: KeyValueActor(a), key: String, value: a) {
process.send(actor.subject, Set(key, value))
}
/// Gets the value for the given key.
pub fn get(actor: KeyValueActor(a), key: String) -> Result(a, Nil) {
process.try_call(actor.subject, Get(key, _), 10)
|> result.map_error(fn(_) { Nil })
|> result.flatten
}
type Message(a) {
Set(key: String, value: a)
Get(name: String, reply_with: Subject(Result(a, Nil)))
}
fn handle_message(
message: Message(a),
state: Dict(String, a),
) -> actor.Next(Message(a), Dict(String, a)) {
case message {
Set(key, value) -> {
actor.continue(
state
|> dict.insert(key, value),
)
}
Get(key, client) -> {
let value =
state
|> dict.get(key)
process.send(client, value)
actor.continue(state)
}
}
}