macula_routing_bucket (macula v0.20.3)

View Source

K-bucket for Kademlia routing table. Stores up to k nodes with LRU eviction policy.

Summary

Functions

Add a node to the bucket. If node exists, move to tail (most recent). If bucket full, return {error, bucket_full}.

Get bucket capacity.

Find n closest nodes to target (sorted by XOR distance).

Find a node by ID.

Get all nodes in the bucket (ordered: oldest first).

Check if bucket contains node.

Create a new bucket with capacity k.

Remove a node from the bucket.

Get number of nodes in bucket.

Update node's last_seen timestamp (moves to tail).

Types

bucket/0

-type bucket() :: #{capacity := pos_integer(), nodes := [node_info()]}.

node_info/0

-type node_info() ::
          #{node_id := binary(),
            address := {inet:ip_address(), inet:port_number()},
            last_seen => integer()}.

Functions

add_node(Bucket, NodeInfo)

-spec add_node(bucket(), node_info()) -> bucket() | {error, bucket_full}.

Add a node to the bucket. If node exists, move to tail (most recent). If bucket full, return {error, bucket_full}.

capacity(_)

-spec capacity(bucket()) -> pos_integer().

Get bucket capacity.

find_closest(_, Target, N)

-spec find_closest(bucket(), binary(), pos_integer()) -> [node_info()].

Find n closest nodes to target (sorted by XOR distance).

find_node(_, NodeId)

-spec find_node(bucket(), binary()) -> {ok, node_info()} | not_found.

Find a node by ID.

get_nodes(_)

-spec get_nodes(bucket()) -> [node_info()].

Get all nodes in the bucket (ordered: oldest first).

has_node(_, NodeId)

-spec has_node(bucket(), binary()) -> boolean().

Check if bucket contains node.

new(Capacity)

-spec new(pos_integer()) -> bucket().

Create a new bucket with capacity k.

remove_node(Bucket, NodeId)

-spec remove_node(bucket(), binary()) -> bucket().

Remove a node from the bucket.

size(_)

-spec size(bucket()) -> non_neg_integer().

Get number of nodes in bucket.

update_timestamp(Bucket, NodeId)

-spec update_timestamp(bucket(), binary()) -> bucket().

Update node's last_seen timestamp (moves to tail).