iprangetree

package module
v0.0.0-...-901dda1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: MIT Imports: 6 Imported by: 0

README

iprangetree collection

@License MIT

The simple IP range-tree collection which provides fast access to inMemory storage of searching by IP range. Usually can be used in GeoIP services to fast IP lookup in a big amount of IP addresses. The project used in the real high-load project with DB of >13,000,000 records and average time of response ~10µs

Example

tree := iprangetree.New(2)

err := tree.AddRangeByString("86.100.32.0-86.100.32.255", []int{10})
if err != nil {
  log.Fatal(err)
}

fmt.Println(tree.LookupByString("86.100.32.10"))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidItem        = errors.New("[iprangetree] invalid IP range item")
	ErrItemReplaced       = errors.New("[iprangetree] item was replaced")
	ErrInvalidRangeFormat = errors.New("[iprangetree] invalid range format")
	ErrInvalidRangeValues = errors.New("[iprangetree] start IP is less then end IP")
)

Errors list

Functions

This section is empty.

Types

type IPItemAccessor

type IPItemAccessor interface {
	GetData() interface{}
	GetStartIP() net.IP
	GetEndIP() net.IP
}

IPItemAccessor data accessor interface

type IPItemFix

type IPItemFix struct {
	StartIP ipFix
	EndIP   ipFix
	Data    interface{}
}

IPItemFix IP range

func (*IPItemFix) Compare

func (i *IPItemFix) Compare(it interface{}) int

Compare with the second item

func (*IPItemFix) GetData

func (i *IPItemFix) GetData() interface{}

GetData object

func (*IPItemFix) GetEndIP

func (i *IPItemFix) GetEndIP() net.IP

GetEndIP net object

func (*IPItemFix) GetStartIP

func (i *IPItemFix) GetStartIP() net.IP

GetStartIP net object

func (*IPItemFix) Has

func (i *IPItemFix) Has(ip net.IP) bool

Has IP in range

func (*IPItemFix) Less

func (i *IPItemFix) Less(then btree.Item) bool

Less camparing for btree

type IPItemV4

type IPItemV4 struct {
	StartIP ipV4
	EndIP   ipV4
	Data    interface{}
}

IPItemV4 IP range

func (*IPItemV4) Compare

func (i *IPItemV4) Compare(it interface{}) int

Compare with the second item

func (*IPItemV4) GetData

func (i *IPItemV4) GetData() interface{}

GetData object

func (*IPItemV4) GetEndIP

func (i *IPItemV4) GetEndIP() net.IP

GetEndIP net object

func (*IPItemV4) GetStartIP

func (i *IPItemV4) GetStartIP() net.IP

GetStartIP net object

func (*IPItemV4) Has

func (i *IPItemV4) Has(ip net.IP) bool

Has IP in range

func (*IPItemV4) Less

func (i *IPItemV4) Less(then btree.Item) bool

Less camparing for btree

type IPTree

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

IPTree base

func New

func New(degree int) *IPTree

New tree object

func (*IPTree) AddRange

func (t *IPTree) AddRange(ip1, ip2 net.IP, val interface{}) (err error)

AddRange IPs with value

func (*IPTree) AddRangeByString

func (t *IPTree) AddRangeByString(ipRange string, val interface{}) (err error)

AddRangeByString data by IP range string or single IP Example:

db.AddRangeByString("127.0.0.1", data)
db.AddRangeByString("127.0.0.1-127.0.0.2", data)

func (*IPTree) Lookup

func (t *IPTree) Lookup(ip net.IP) (response IPItemAccessor)

Lookup to search the IP value in the IP tree

func (*IPTree) LookupByString

func (t *IPTree) LookupByString(ip string) IPItemAccessor

LookupByString parse the IP value and search data in the IP tree

Jump to

Keyboard shortcuts

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