
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | /// A key-value store, as an actor: node b runs it, any node calls it. |
| 2 | /// Nothing here knows about distribution: a subject `dist.whereis` found |
| 3 | /// on another node is called as a local one. |
| 4 | |
| 5 | import std/actor.{Continue} |
| 6 | import std/dict.{type Dict} |
| 7 | import std/process.{type Subject, type proc} |
| 8 | |
| 9 | /// What the store is asked. Every field can cross hosts (`Wire`): |
| 10 | /// strings, and the subjects the answers go to, so `dist.name` accepts |
| 11 | /// this type and `actor.call` works across nodes. |
| 12 | pub type Request |
| 13 | { /// Set `key` to `value`; the answer is its previous value. |
| 14 | Put(key: String, value: String, reply: Subject<Option<String>>) |
| 15 | , Get(key: String, reply: Subject<Option<String>>) |
| 16 | /// How many keys the store holds. |
| 17 | , Size(reply: Subject<Int>) |
| 18 | } |
| 19 | |
| 20 | /// Start an empty store. |
| 21 | pub fn start :: -> Subject<Request> / <proc> |
| 22 | { |
| 23 | actor.start (dict.empty ()) fn entries request { step entries request } |
| 24 | } |
| 25 | |
| 26 | fn step :: |
| 27 | entries: Dict<String, String> |
| 28 | request: Request |
| 29 | -> actor.Next<Dict<String, String>> / <proc> |
| 30 | { |
| 31 | match request |
| 32 | { Put(key, value, reply) -> |
| 33 | { |
| 34 | process.send reply (dict.get entries key) |
| 35 | Continue(dict.insert entries key value) |
| 36 | } |
| 37 | , Get(key, reply) -> |
| 38 | { |
| 39 | process.send reply (dict.get entries key) |
| 40 | Continue(entries) |
| 41 | } |
| 42 | , Size(reply) -> |
| 43 | { |
| 44 | process.send reply (dict.size entries) |
| 45 | Continue(entries) |
| 46 | } |
| 47 | } |
| 48 | } |