- Notifications
You must be signed in to change notification settings - Fork3
Rust B-Tree map for pub/sub services
License
alttch/submap
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
B-tree map for pub/sub services.
use submap::SubMap;typeClient =String;letmut smap:SubMap<Client> =SubMap::new();
where "Client" is a pub/sub client type, which is usually either a channel or astructure which contains a channel or locked socket or anything else, requiredto work with the client.
The client type MUST provide traits Ord, Eq and Clone.
All clients must be registered in the map, before they cansubscribe/unsubscribe. Use "register_client" function for this.
When "unregister_client" is called, it also automatically unsubscribes theclient from all the subscribed topics.
[SubMap
] supports the following masks:
- this/is/a/topic - single topic subscription
- this/?/a/topic - all topics which match the pattern (2nd chunk - any value)
- this/is/* - all subtopics of "this/is"
- * - all topics
Service symbols can be changed. E.g. let us create a subscription map withMQTT-style wildcards (+ for ? and # for *) but with the dot as the subtopicseparator:
use submap::SubMap;typeClient =String;letmut smap:SubMap<Client> =SubMap::new().separator('.').match_any("+").wildcard("#");
Note that "/topic/x", "topic/x" and "topic//x" are 3 different topics. Ifany kind of normalization is required, it should be done manually, beforecalling [SubMap
] functions.
[SubMap
] supports formulas, which are used both to subscribe to a topic byformula or to get a list of clients which match one.
Formulas are non-standard pub/sub functionality and are useful when a clientwant to subscribe to topics which have got e.g. some importance level. Insteadof subscribing to all level topics, a client can subscribe to one topic with aformula:
use submap::SubMap;typeClient =String;letmut smap:SubMap<Client> =SubMap::new().separator('/').match_any("+").wildcard("#").formula_prefix("!");let client1 ="client1".to_owned();smap.register_client(&client1);smap.subscribe("some/!ge(2)/topic",&client1);assert_eq!(smap.get_subscribers("some/1/topic").len(),0);assert_eq!(smap.get_subscribers("some/2/topic").len(),1);assert_eq!(smap.get_subscribers("some/3/topic").len(),1);
See more: [mkmf::Formula
].
[SubMap
] supports regular expressions in subtopic names.
Regular expressions are non-standard pub/sub functionality, are pretty slow(especially for subscribe/unsubscribe operations) and should be used withcaution. A regular expression can not contain the separator symbol.
use submap::SubMap;typeClient =String;letmut smap:SubMap<Client> =SubMap::new().separator('/').match_any("+").wildcard("#").regex_prefix("~");let client1 ="client1".to_owned();smap.register_client(&client1);smap.subscribe("some/~subtopic[0-9]+/topic",&client1);assert_eq!(smap.get_subscribers("some/subtopic1/topic").len(),1);assert_eq!(smap.get_subscribers("some/subtopic2/topic").len(),1);assert_eq!(smap.get_subscribers("some/subtopic333/topic").len(),1);assert_eq!(smap.get_subscribers("some/subtopicx/topic").len(),0);
use submap::BroadcastMap;typeClient =String;letmut bmap:BroadcastMap<Client> =BroadcastMap::new();
Does the opposite job - clients are registered with regular names, while"get_clients_by_mask" function returns clients, which match the mask.
Note: the default separator is dot.
letmut acl_map = submap::AclMap::new();
SubMap-based high-speed access control lists checker. Uses SubMap algorithmwith a single unit "client" to verify various access control lists.
- indexmap switches the engine toindexmap (the default is based onstd::collections::BTreeMap/BTreeSet), requires Hash trait implemented for mapclients.
The current engine can be obtained from
use submap::types::ENGINE;dbg!(ENGINE);// std-btree or indexmap
1.81.0