awdb

package
v0.0.0-...-2904112 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NotFound is returned by LookupOffset when a matched root record offset
	// cannot be found.
	NotFound = ^uintptr(0)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidDatabaseError

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

InvalidDatabaseError is returned when the database contains invalid data and cannot be parsed.

func (InvalidDatabaseError) Error

func (e InvalidDatabaseError) Error() string

type Metadata

type Metadata struct {
	BinaryFormatMajorVersion uint              `awdb:"binary_format_major_version"`
	BinaryFormatMinorVersion uint              `awdb:"binary_format_minor_version"`
	BuildEpoch               uint              `awdb:"build_epoch"`
	DatabaseType             string            `awdb:"database_type"`
	Description              map[string]string `awdb:"description"`
	IPVersion                uint              `awdb:"ip_version"`
	Languages                []string          `awdb:"languages"`
	NodeCount                uint              `awdb:"node_count"`
	RecordSize               uint              `awdb:"record_size"`
}

Metadata holds the metadata decoded from the aw DB file. In particular it has the format version, the build time as Unix epoch time, the database type and description, the IP version supported, and a slice of the natural languages included.

type Networks

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

Networks represents a set of subnets that we are iterating over.

func (*Networks) Err

func (n *Networks) Err() error

Err returns an error, if any, that was encountered during iteration.

func (*Networks) Network

func (n *Networks) Network(result interface{}) (*net.IPNet, error)

Network returns the current network or an error if there is a problem decoding the data for the network. It takes a pointer to a result value to decode the network's data into.

func (*Networks) Next

func (n *Networks) Next() bool

Next prepares the next network for reading with the Network method. It returns true if there is another network to be processed and false if there are no more networks or if there is an error.

type Reader

type Reader struct {
	Metadata Metadata
	// contains filtered or unexported fields
}

Reader holds the data corresponding to the aw DB file. Its only public field is Metadata, which contains the metadata from the aw DB file.

All of the methods on Reader are thread-safe. The struct may be safely shared across goroutines.

func FromBytes

func FromBytes(buffer []byte) (*Reader, error)

FromBytes takes a byte slice corresponding to a aw DB file and returns a Reader structure or an error.

func Open

func Open(file string) (*Reader, error)

Open takes a string path to a aw DB file and returns a Reader structure or an error. The database file is opened using a memory map, except on Google App Engine where mmap is not supported; there the database is loaded into memory. Use the Close method on the Reader object to return the resources to the system.

func (*Reader) Close

func (r *Reader) Close() error

Close unmaps the database file from virtual memory and returns the resources to the system. If called on a Reader opened using FromBytes or Open on Google App Engine, this method does nothing.

func (*Reader) Decode

func (r *Reader) Decode(offset uintptr, result interface{}) error

Decode the record at |offset| into |result|. The result value pointed to must be a data value that corresponds to a record in the database. This may include a struct representation of the data, a map capable of holding the data or an empty interface{} value.

If result is a pointer to a struct, the struct need not include a field for every value that may be in the database. If a field is not present in the structure, the decoder will not decode that field, reducing the time required to decode the record.

As a special case, a struct field of type uintptr will be used to capture the offset of the value. Decode may later be used to extract the stored value from the offset. aw DBs are highly normalized: for example in the City database, all records of the same country will reference a single representative record for that country. This uintptr behavior allows clients to leverage this normalization in their own sub-record caching.

func (*Reader) Lookup

func (r *Reader) Lookup(ip net.IP, result interface{}) error

Lookup retrieves the database record for ip and stores it in the value pointed to by result. If result is nil or not a pointer, an error is returned. If the data in the database record cannot be stored in result because of type differences, an UnmarshalTypeError is returned. If the database is invalid or otherwise cannot be read, an InvalidDatabaseError is returned.

func (*Reader) LookupNetwork

func (r *Reader) LookupNetwork(ip net.IP, result interface{}) (network *net.IPNet, ok bool, err error)

LookupNetwork retrieves the database record for ip and stores it in the value pointed to by result. The network returned is the network associated with the data record in the database. The ok return value indicates whether the database contained a record for the ip.

If result is nil or not a pointer, an error is returned. If the data in the database record cannot be stored in result because of type differences, an UnmarshalTypeError is returned. If the database is invalid or otherwise cannot be read, an InvalidDatabaseError is returned.

func (*Reader) LookupOffset

func (r *Reader) LookupOffset(ip net.IP) (uintptr, error)

LookupOffset maps an argument net.IP to a corresponding record offset in the database. NotFound is returned if no such record is found, and a record may otherwise be extracted by passing the returned offset to Decode. LookupOffset is an advanced API, which exists to provide clients with a means to cache previously-decoded records.

func (*Reader) Networks

func (r *Reader) Networks() *Networks

Networks returns an iterator that can be used to traverse all networks in the database.

Please note that a aw DB may map IPv4 networks into several locations in an IPv6 database. This iterator will iterate over all of these locations separately.

func (*Reader) NetworksWithin

func (r *Reader) NetworksWithin(network *net.IPNet) *Networks

NetworksWithin returns an iterator that can be used to traverse all networks in the database which are contained in a given network.

Please note that a aw DB may map IPv4 networks into several locations in an IPv6 database. This iterator will iterate over all of these locations separately.

If the provided network is contained within a network in the database, the iterator will iterate over exactly one network, the containing network.

func (*Reader) Verify

func (r *Reader) Verify() error

Verify checks that the database is valid. It validates the search tree, the data section, and the metadata section. This verifier is stricter than the specification and may return errors on databases that are readable.

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value string       // stringified copy of the database value that caused the error
	Type  reflect.Type // type of the value that could not be assign to
}

UnmarshalTypeError is returned when the value in the database cannot be assigned to the specified data type.

func (UnmarshalTypeError) Error

func (e UnmarshalTypeError) Error() string

Jump to

Keyboard shortcuts

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