/// A key-value store, as an actor: node b runs it, any node calls it. /// Nothing here knows about distribution: a subject `dist.whereis` found /// on another node is called as a local one. import std/actor.{Continue} import std/dict.{type Dict} import std/process.{type Subject, type proc} /// What the store is asked. Every field can cross hosts (`Wire`): /// strings, and the subjects the answers go to, so `dist.name` accepts /// this type and `actor.call` works across nodes. pub type Request { /// Set `key` to `value`; the answer is its previous value. Put(key: String, value: String, reply: Subject>) , Get(key: String, reply: Subject>) /// How many keys the store holds. , Size(reply: Subject) } /// Start an empty store. pub fn start :: -> Subject / { actor.start (dict.empty ()) fn entries request { step entries request } } fn step :: entries: Dict request: Request -> actor.Next> / { match request { Put(key, value, reply) -> { process.send reply (dict.get entries key) Continue(dict.insert entries key value) } , Get(key, reply) -> { process.send reply (dict.get entries key) Continue(entries) } , Size(reply) -> { process.send reply (dict.size entries) Continue(entries) } } }