nradix

package module
v1.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 7 Imported by: 1

README

nradix

Translated to Golang implementation of nginx's radix tree (allowing to store and lookup IP information)
- Updated to support netip.Addr for improved performance

This project is licensed under the terms of the MIT license.
Read LICENSE file for information for all notices and permissions.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNodeBusy = errors.New("Node Busy")
	ErrNotFound = errors.New("No Such Node")
	ErrBadIP    = errors.New("Bad IP address or mask")
)

Functions

This section is empty.

Types

type Node added in v1.0.7

type Node struct {
	// contains filtered or unexported fields
}

func (*Node) GetAllParents added in v1.0.7

func (n *Node) GetAllParents() []*Node

func (*Node) GetLeft added in v1.0.7

func (n *Node) GetLeft() *Node

func (*Node) GetParent added in v1.0.7

func (n *Node) GetParent() *Node

GetParent returns the parent node of the current node. This is the first node that has a value above the current node.

func (*Node) GetPrefix added in v1.0.7

func (n *Node) GetPrefix() netip.Prefix

func (*Node) GetRight added in v1.0.7

func (n *Node) GetRight() *Node

func (*Node) GetTreeParent added in v1.0.7

func (n *Node) GetTreeParent() *Node

GetTreeParent returns the parent node of the current node. This is the node that is used to traverse the tree.

func (*Node) GetValue added in v1.0.7

func (n *Node) GetValue() interface{}

func (*Node) SetValue added in v1.0.7

func (n *Node) SetValue(value interface{})

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree implements a radix tree for working with IP/mask. Thread safety is not guaranteed, you should choose your own style of protecting safety of operations.

func NewTree

func NewTree(preallocate int) *Tree

NewTree initializes a Tree and preallocates a specified number of nodes ready to store data. It creates a new Tree structure, sets up the root node, and optionally preallocates nodes based on the number of bits specified. This is useful for optimizing the tree for a certain number of entries.

func (*Tree) DeleteCIDRNetIP added in v1.0.2

func (tree *Tree) DeleteCIDRNetIP(ip net.IP, mask net.IPMask) error

DeleteCIDRNetIP removes a value associated with a net.IP and net.IPMask from the tree. It locks the tree for writing, determines if the IP is IPv4 or IPv6, and deletes the specific entry from the tree. For IPv4 addresses, it converts the IP and mask to uint32 format before deletion.

func (*Tree) DeleteCIDRNetIPAddr added in v1.0.2

func (tree *Tree) DeleteCIDRNetIPAddr(ip netip.Addr, mask netip.Prefix) error

DeleteCIDRNetIPAddr removes a value associated with a netip.Addr and netip.Prefix from the tree. It locks the tree for writing, determines if the IP is IPv4 or IPv6, and deletes the specific entry from the tree. For IPv4 addresses, it uses pre-computed masks from a cache for better performance.

func (*Tree) DeleteCIDRString

func (tree *Tree) DeleteCIDRString(cidr string) error

DeleteCIDRString removes a value associated with an IP/mask from the tree. It locks the tree for writing, converts the CIDR string to bytes, and calls DeleteCIDRb.

func (*Tree) DeleteCIDRb

func (tree *Tree) DeleteCIDRb(cidr []byte) error

DeleteCIDRb removes a value associated with an IP/mask from the tree using byte slices. It determines if the CIDR is IPv4 or IPv6, parses it, and deletes the specific entry from the tree.

func (*Tree) DeleteWholeRangeCIDR

func (tree *Tree) DeleteWholeRangeCIDR(cidr string) error

DeleteWholeRangeCIDR removes all values associated with IPs in the entire subnet specified by the CIDR. It locks the tree for writing, converts the CIDR string to bytes, and calls DeleteWholeRangeCIDRb.

func (*Tree) DeleteWholeRangeCIDRb

func (tree *Tree) DeleteWholeRangeCIDRb(cidr []byte) error

DeleteWholeRangeCIDRb removes all values associated with IPs in the entire subnet specified by the CIDR using byte slices. It determines if the CIDR is IPv4 or IPv6, parses it, and deletes the entire range from the tree.

func (*Tree) FindCIDRIPNet

func (tree *Tree) FindCIDRIPNet(ipm net.IPNet) (interface{}, error)

FindCIDRIPNet finds the value associated with a given net.IPNet. It locks the tree for reading and determines if the IP is IPv4 or IPv6, then finds the corresponding entry in the tree.

func (*Tree) FindCIDRNetIP

func (tree *Tree) FindCIDRNetIP(ip net.IP) (interface{}, error)

FindCIDRNetIP finds the value associated with a given net.IP. It locks the tree for reading and determines if the IP is IPv4 or IPv6, then finds the corresponding entry in the tree.

func (*Tree) FindCIDRNetIPAddr

func (tree *Tree) FindCIDRNetIPAddr(nip netip.Addr) (interface{}, error)

func (*Tree) FindCIDRNetIPAddrV2 added in v1.0.7

func (tree *Tree) FindCIDRNetIPAddrV2(nip netip.Addr) (node *Node, value interface{}, err error)

func (*Tree) FindCIDRNetIPAddrWithNode added in v1.0.7

func (tree *Tree) FindCIDRNetIPAddrWithNode(nip netip.Addr) (node *Node, value interface{}, err error)

func (*Tree) FindCIDRString

func (tree *Tree) FindCIDRString(cidr string) (interface{}, error)

FindCIDRString traverses the tree to the proper node and returns previously saved information in the longest covered IP. It locks the tree for reading, converts the CIDR string to bytes, and calls FindCIDRb.

func (*Tree) FindCIDRb

func (tree *Tree) FindCIDRb(cidr []byte) (interface{}, error)

FindCIDRb traverses the tree to the proper node and returns previously saved information in the longest covered IP using byte slices. It determines if the CIDR is IPv4 or IPv6, parses it, and finds the corresponding entry in the tree.

func (*Tree) SetCIDRNetIP added in v1.0.1

func (tree *Tree) SetCIDRNetIP(ip net.IP, mask net.IPMask, val interface{}, overwrite bool) error

SetCIDRNetIP sets a value associated with a net.IP and net.IPMask in the tree, overwriting any existing value. It locks the tree for writing, determines if the IP is IPv4 or IPv6, and inserts it into the tree with overwrite enabled.

func (*Tree) SetCIDRNetIPAddr added in v1.0.1

func (tree *Tree) SetCIDRNetIPAddr(ip netip.Addr, mask netip.Prefix, val interface{}, overwrite bool) error

SetCIDRNetIPAddr sets a value associated with a netip.Addr IP and netip.Prefix mask in the tree, overwriting any existing value. It locks the tree for writing, determines if the IP is IPv4 or IPv6, and inserts it into the tree with overwrite enabled.

func (*Tree) SetCIDRNetIPPrefix added in v1.0.8

func (tree *Tree) SetCIDRNetIPPrefix(prefix netip.Prefix, val interface{}, overwrite bool) error

SetCIDRNetIPPrefix sets a value associated with a netip.Addr IP and netip.Prefix mask in the tree, overwriting any existing value. It locks the tree for writing, determines if the IP is IPv4 or IPv6, and inserts it into the tree with overwrite enabled.

func (*Tree) SetCIDRString

func (tree *Tree) SetCIDRString(cidr string, val interface{}, overwrite bool) error

SetCIDRString sets a value associated with an IP/mask in the tree, overwriting any existing value. It locks the tree for writing, converts the CIDR string to bytes, and calls SetCIDRb.

func (*Tree) SetCIDRb

func (tree *Tree) SetCIDRb(cidr []byte, val interface{}, overwrite bool) error

SetCIDRb adds a value associated with an IP/mask to the tree using byte slices. It determines if the CIDR is IPv4 or IPv6, parses it, and inserts it into the tree.

func (*Tree) WalkV4 added in v1.0.4

func (tree *Tree) WalkV4(walkFn WalkFunc) error

Walk traverses the tree in-order, calling walkFn for each node that contains a value. The walk function receives the CIDR prefix as a string and the value stored at that node.

func (*Tree) WalkV6 added in v1.0.4

func (tree *Tree) WalkV6(walkFn WalkFunc) error

type WalkFunc added in v1.0.3

type WalkFunc func(prefix netip.Prefix, value interface{}) error

WalkFunc is the type of the function called for each node visited by Walk. The path argument contains the prefix leading to this node. If the function returns an error, walking stops and the error is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL