joerl 0.7.1

An Erlang-inspired actor model library for Rust
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Named process registry for Erlang-style process registration.
//!
//! This module provides a thread-safe registry for associating names with Pids,
//! enabling location-transparent addressing similar to Erlang's `register/2`,
//! `whereis/1`, and `unregister/1`.
//!
//! # Examples
//!
//! ```rust
//! use joerl::{ActorSystem, Actor, ActorContext, Message};
//! use async_trait::async_trait;
//!
//! struct Worker;
//!
//! #[async_trait]
//! impl Actor for Worker {
//!     async fn handle_message(&mut self, _msg: Message, _ctx: &mut ActorContext) {}
//! }
//!
//! # tokio_test::block_on(async {
//! let system = ActorSystem::new();
//! let worker = system.spawn(Worker);
//!
//! // Register the worker with a name
//! system.register("worker1", worker.pid()).unwrap();
//!
//! // Look up the pid by name
//! let pid = system.whereis("worker1").unwrap();
//! assert_eq!(pid, worker.pid());
//!
//! // Unregister the name
//! system.unregister("worker1").unwrap();
//! # });
//! ```

use crate::Pid;
use crate::error::{ActorError, Result};
use dashmap::DashMap;
use std::sync::Arc;

#[cfg(feature = "telemetry")]
use crate::telemetry::RegistryMetrics;

/// A thread-safe registry for mapping names to process IDs.
///
/// The Registry maintains bidirectional mappings between names and Pids,
/// allowing both forward lookup (name → Pid) and reverse lookup (Pid → name).
/// When a process terminates, its name registration is automatically cleaned up.
#[derive(Default)]
pub struct Registry {
    /// Forward mapping: name → Pid
    names: Arc<DashMap<String, Pid>>,
    /// Reverse mapping: Pid → name
    pids: Arc<DashMap<Pid, String>>,
}

impl Registry {
    /// Creates a new empty registry.
    pub fn new() -> Self {
        Self {
            names: Arc::new(DashMap::new()),
            pids: Arc::new(DashMap::new()),
        }
    }

    /// Registers a process with the given name.
    ///
    /// If the name is already registered to another process, returns
    /// `ActorError::NameAlreadyRegistered`. If the Pid is already registered
    /// under a different name, the old name is automatically unregistered first.
    ///
    /// In Erlang: `register(Name, Pid)`
    ///
    /// # Arguments
    ///
    /// * `name` - The name to register (must be non-empty)
    /// * `pid` - The process ID to associate with the name
    ///
    /// # Errors
    ///
    /// Returns `ActorError::NameAlreadyRegistered` if the name is already taken.
    /// Returns `ActorError::InvalidName` if the name is empty.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// let pid = Pid::new();
    ///
    /// registry.register("my_process", pid).unwrap();
    ///
    /// // Trying to register the same name again fails
    /// let pid2 = Pid::new();
    /// assert!(registry.register("my_process", pid2).is_err());
    /// ```
    pub fn register(&self, name: impl Into<String>, pid: Pid) -> Result<()> {
        let name = name.into();

        // Validate name
        if name.is_empty() {
            return Err(ActorError::InvalidName("Name cannot be empty".to_string()));
        }

        // Check if this Pid is already registered under a different name
        if let Some(old_name) = self.pids.get(&pid)
            && old_name.value() != &name
        {
            // Unregister the old name first
            self.names.remove(old_name.value());
        }

        // Try to insert the name
        match self.names.entry(name.clone()) {
            dashmap::mapref::entry::Entry::Occupied(_) => {
                #[cfg(feature = "telemetry")]
                RegistryMetrics::registration_conflict();
                Err(ActorError::NameAlreadyRegistered(name))
            }
            dashmap::mapref::entry::Entry::Vacant(entry) => {
                entry.insert(pid);
                self.pids.insert(pid, name.clone());
                #[cfg(feature = "telemetry")]
                RegistryMetrics::process_registered();
                Ok(())
            }
        }
    }

    /// Unregisters a name from the registry.
    ///
    /// Returns the Pid that was associated with the name.
    ///
    /// In Erlang: `unregister(Name)`
    ///
    /// # Arguments
    ///
    /// * `name` - The name to unregister
    ///
    /// # Errors
    ///
    /// Returns `ActorError::NameNotRegistered` if the name is not registered.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// let pid = Pid::new();
    ///
    /// registry.register("my_process", pid).unwrap();
    /// let unregistered_pid = registry.unregister("my_process").unwrap();
    /// assert_eq!(unregistered_pid, pid);
    ///
    /// // Trying to unregister again fails
    /// assert!(registry.unregister("my_process").is_err());
    /// ```
    pub fn unregister(&self, name: &str) -> Result<Pid> {
        if let Some((_, pid)) = self.names.remove(name) {
            self.pids.remove(&pid);
            #[cfg(feature = "telemetry")]
            RegistryMetrics::process_unregistered();
            Ok(pid)
        } else {
            Err(ActorError::NameNotRegistered(name.to_string()))
        }
    }

    /// Looks up a process ID by name.
    ///
    /// Returns `Some(Pid)` if the name is registered, or `None` if not found.
    ///
    /// In Erlang: `whereis(Name)`
    ///
    /// # Arguments
    ///
    /// * `name` - The name to look up
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// let pid = Pid::new();
    ///
    /// registry.register("my_process", pid).unwrap();
    /// assert_eq!(registry.whereis("my_process"), Some(pid));
    /// assert_eq!(registry.whereis("unknown"), None);
    /// ```
    pub fn whereis(&self, name: &str) -> Option<Pid> {
        #[cfg(feature = "telemetry")]
        RegistryMetrics::lookup_performed();

        self.names.get(name).map(|entry| *entry.value())
    }

    /// Returns a list of all registered names.
    ///
    /// In Erlang: `registered()`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// registry.register("process1", Pid::new()).unwrap();
    /// registry.register("process2", Pid::new()).unwrap();
    ///
    /// let names = registry.registered();
    /// assert_eq!(names.len(), 2);
    /// assert!(names.contains(&"process1".to_string()));
    /// assert!(names.contains(&"process2".to_string()));
    /// ```
    pub fn registered(&self) -> Vec<String> {
        self.names.iter().map(|entry| entry.key().clone()).collect()
    }

    /// Looks up the registered name for a process ID.
    ///
    /// Returns `Some(String)` if the Pid is registered, or `None` if not found.
    /// This is the reverse operation of `whereis`.
    ///
    /// # Arguments
    ///
    /// * `pid` - The process ID to look up
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// let pid = Pid::new();
    ///
    /// registry.register("my_process", pid).unwrap();
    /// assert_eq!(registry.name_of(pid), Some("my_process".to_string()));
    /// ```
    pub fn name_of(&self, pid: Pid) -> Option<String> {
        self.pids.get(&pid).map(|entry| entry.value().clone())
    }

    /// Returns the number of registered names.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use joerl::registry::Registry;
    /// use joerl::Pid;
    ///
    /// let registry = Registry::new();
    /// assert_eq!(registry.len(), 0);
    ///
    /// registry.register("process1", Pid::new()).unwrap();
    /// assert_eq!(registry.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.names.len()
    }

    /// Returns true if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.names.is_empty()
    }

    /// Cleans up any name registrations for the given Pid.
    ///
    /// This is called internally when an actor terminates to ensure
    /// that dead processes don't remain in the registry.
    ///
    /// # Arguments
    ///
    /// * `pid` - The process ID to clean up
    pub(crate) fn cleanup_pid(&self, pid: Pid) {
        if let Some((_, name)) = self.pids.remove(&pid) {
            self.names.remove(&name);
            #[cfg(feature = "telemetry")]
            RegistryMetrics::process_unregistered();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_register_and_whereis() {
        let registry = Registry::new();
        let pid = Pid::new();

        registry.register("test", pid).unwrap();
        assert_eq!(registry.whereis("test"), Some(pid));
    }

    #[test]
    fn test_register_duplicate_name() {
        let registry = Registry::new();
        let pid1 = Pid::new();
        let pid2 = Pid::new();

        registry.register("test", pid1).unwrap();
        let result = registry.register("test", pid2);
        assert!(matches!(result, Err(ActorError::NameAlreadyRegistered(_))));
    }

    #[test]
    fn test_register_same_pid_different_name() {
        let registry = Registry::new();
        let pid = Pid::new();

        registry.register("name1", pid).unwrap();
        registry.register("name2", pid).unwrap();

        // The pid should now be registered as "name2"
        assert_eq!(registry.whereis("name1"), None);
        assert_eq!(registry.whereis("name2"), Some(pid));
        assert_eq!(registry.name_of(pid), Some("name2".to_string()));
    }

    #[test]
    fn test_unregister() {
        let registry = Registry::new();
        let pid = Pid::new();

        registry.register("test", pid).unwrap();
        let unregistered_pid = registry.unregister("test").unwrap();
        assert_eq!(unregistered_pid, pid);
        assert_eq!(registry.whereis("test"), None);
    }

    #[test]
    fn test_unregister_not_registered() {
        let registry = Registry::new();
        let result = registry.unregister("nonexistent");
        assert!(matches!(result, Err(ActorError::NameNotRegistered(_))));
    }

    #[test]
    fn test_registered() {
        let registry = Registry::new();
        let pid1 = Pid::new();
        let pid2 = Pid::new();

        registry.register("process1", pid1).unwrap();
        registry.register("process2", pid2).unwrap();

        let names = registry.registered();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"process1".to_string()));
        assert!(names.contains(&"process2".to_string()));
    }

    #[test]
    fn test_name_of() {
        let registry = Registry::new();
        let pid = Pid::new();

        registry.register("my_process", pid).unwrap();
        assert_eq!(registry.name_of(pid), Some("my_process".to_string()));
    }

    #[test]
    fn test_cleanup_pid() {
        let registry = Registry::new();
        let pid = Pid::new();

        registry.register("test", pid).unwrap();
        registry.cleanup_pid(pid);

        assert_eq!(registry.whereis("test"), None);
        assert_eq!(registry.name_of(pid), None);
    }

    #[test]
    fn test_empty_name() {
        let registry = Registry::new();
        let pid = Pid::new();

        let result = registry.register("", pid);
        assert!(matches!(result, Err(ActorError::InvalidName(_))));
    }

    #[test]
    fn test_len_and_is_empty() {
        let registry = Registry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);

        let pid = Pid::new();
        registry.register("test", pid).unwrap();
        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);

        registry.unregister("test").unwrap();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }
}