macula_routing_dht (macula v0.20.5)
View SourceCore DHT algorithms for Kademlia routing. Implements iterative lookup, store, and find operations. Pure functions - no GenServer, designed to be called by macula_routing_server.
Summary
Functions
Delete a key from the DHT. Delegates to macula_routing_server if running.
Find a value in the DHT. Delegates to macula_routing_server if running.
Find value in DHT. Returns {ok, Value} if found, {nodes, [NodeInfo]} if not found.
Iterative lookup to find k closest nodes to target. Uses alpha concurrent queries (default: 3).
Notify subscribers about a DHT delete event. Called by macula_routing_server when a key is deleted.
Notify subscribers about a DHT store event. Called by macula_routing_server when a key is stored.
Select up to alpha unqueried nodes from closest set.
Store a key-value pair in the DHT. Delegates to macula_routing_server if running.
Store value at k closest nodes to key.
Subscribe to DHT events matching a key prefix. Subscribes the given Pid to receive events when keys with the given prefix are stored or deleted. Uses gproc property-based subscriptions.
Update closest set with new nodes, maintaining k closest and removing duplicates.
Types
-type query_fn() :: fun((macula_routing_bucket:node_info(), binary()) -> {ok, [macula_routing_bucket:node_info()]} | {value, term()} | {nodes, [macula_routing_bucket:node_info()]} | {error, term()}).
-type store_fn() :: fun((macula_routing_bucket:node_info(), binary(), term()) -> ok | {error, term()}).
Functions
Delete a key from the DHT. Delegates to macula_routing_server if running.
Find a value in the DHT. Delegates to macula_routing_server if running.
-spec find_value(macula_routing_table:routing_table(), binary(), pos_integer(), query_fn()) -> {ok, term()} | {nodes, [macula_routing_bucket:node_info()]}.
Find value in DHT. Returns {ok, Value} if found, {nodes, [NodeInfo]} if not found.
-spec iterative_find_node(macula_routing_table:routing_table(), binary(), pos_integer(), query_fn()) -> {ok, [macula_routing_bucket:node_info()]}.
Iterative lookup to find k closest nodes to target. Uses alpha concurrent queries (default: 3).
-spec notify_delete(binary()) -> ok.
Notify subscribers about a DHT delete event. Called by macula_routing_server when a key is deleted.
Notify subscribers about a DHT store event. Called by macula_routing_server when a key is stored.
-spec select_alpha([macula_routing_bucket:node_info()], [binary()], pos_integer()) -> [macula_routing_bucket:node_info()].
Select up to alpha unqueried nodes from closest set.
Store a key-value pair in the DHT. Delegates to macula_routing_server if running.
-spec store_value(macula_routing_table:routing_table(), binary(), term(), pos_integer(), query_fn(), store_fn()) -> ok.
Store value at k closest nodes to key.
Subscribe to DHT events matching a key prefix. Subscribes the given Pid to receive events when keys with the given prefix are stored or deleted. Uses gproc property-based subscriptions.
Events sent to the subscriber: {dht_stored, Key, Value} - When a key is stored {dht_deleted, Key} - When a key is deleted
To unsubscribe, the subscriber process should call: gproc:unreg({p, l, {dht_prefix_subscription, Prefix}})
-spec update_closest([macula_routing_bucket:node_info()], [macula_routing_bucket:node_info()], binary(), pos_integer()) -> [macula_routing_bucket:node_info()].
Update closest set with new nodes, maintaining k closest and removing duplicates.