lpmtrie

package module
v0.0.0-...-3d81425 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 5 Imported by: 1

README

lpmtrie

LpmTrie is a trie data structure which implements Longest Prefix Match algorithm.

It's thread-safe and high performance with atomic operations.


It's inspired by: https://github.com/torvalds/linux/blob/master/kernel/bpf/lpm_trie.c

License

MIT License

Documentation

Index

Constants

View Source
const (
	MaxPrefixLenIPv4 = 32
	MaxPrefixLenIPv6 = 128
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Key

type Key struct {
	PrefixLen int
	Data      []byte
}

Key is the key of the trie. A key is a byte array with a prefix length. The prefix length is the number of bits for the key. The length of the byte array must be same with eighth of trie's max prefix length.

type LpmTrie

type LpmTrie interface {
	// Size returns the number of entries in the trie.
	Size() int64

	// Lookup lookups the value of the key by LPM algo.
	// The length of key's data must be same with eighth of trie's max prefix length,
	// or it will panic.
	Lookup(key Key) (interface{}, bool)

	// Update updates the value of the key by LPM algo.
	// If the key is not found, it inserts the key-value pair.
	// The length of key's data must be same with eighth of trie's max prefix length,
	// or it will panic.
	Update(key Key, val interface{}) (updated bool)

	// Delete deletes the key-value pair by LPM algo.
	// The length of key's data must be same with eighth of trie's max prefix length,
	// or it will panic.
	Delete(key Key) (deleted bool)

	// Range iterates over the key-value pairs in the trie by in-order.
	Range(fn func(key Key, val interface{}) bool)
}

LpmTrie is a trie data structure which implements Longest Prefix Match algorithm.

func New

func New(maxPrefixLen int) (LpmTrie, error)

Jump to

Keyboard shortcuts

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