GeoIP2
view release on metacpan or search on metacpan
maxmind-db/MaxMind-DB-spec.md view on Meta::CPAN
The codes may include additional information such as script or country
identifiers, like "zh-TW" or "mn-Cyrl-MN". The additional identifiers will be
separated by a dash character ("-").
This key is optional. However, creators of databases are strongly
encouraged to include a description in at least one language.
### Calculating the Search Tree Section Size
The formula for calculating the search tree section size *in bytes* is as
follows:
( ( $record_size * 2 ) / 8 ) * $number_of_nodes
The end of the search tree marks the beginning of the data section.
## Binary Search Tree Section
The database file starts with a binary search tree. The number of nodes in the
tree is dependent on how many unique netblocks are needed for the particular
maxmind-db/MaxMind-DB-spec.md view on Meta::CPAN
If the record value is equal to the number of nodes, that means that we do not
have any data for the IP address, and the search ends here.
If the record value is *greater* than the number of nodes in the search tree,
then it is an actual pointer value pointing into the data section. The value
of the pointer is relative to the start of the data section, *not* the
start of the file.
In order to determine where in the data section we should start looking, we use
the following formula:
$data_section_offset = ( $record_value - $node_count ) - 16
The 16 is the size of the data section separator. We subtract it because we
want to permit pointing to the first byte of the data section. Recall that
the record value cannot equal the node count as that means there is no
data. Instead, we choose to start values that go to the data section at
`$node_count + 16`. (This has the side effect that record values
`$node_count + 1` through `$node_count + 15` inclusive are not valid).
maxmind-db/MaxMind-DB-spec.md view on Meta::CPAN
Let's assume we have a 24-bit tree with 1,000 nodes. Each node contains 48
bits, or 6 bytes. The size of the tree is 6,000 bytes.
When a record in the tree contains a number that is less than 1,000, this
is a *node number*, and we look up that node. If a record contains a value
greater than or equal to 1,016, we know that it is a data section value. We
subtract the node count (1,000) and then subtract 16 for the data section
separator, giving us the number 0, the first byte of the data section.
If a record contained the value 6,000, this formula would give us an offset of
4,984 into the data section.
In order to determine where in the file this offset really points to, we also
need to know where the data section starts. This can be calculated by
determining the size of the search tree in bytes and then adding an additional
16 bytes for the data section separator:
$offset_in_file = $data_section_offset
+ $search_tree_size_in_bytes
+ 16
Since we subtract and then add 16, the final formula to determine the
offset in the file can be simplified to:
$offset_in_file = ( $record_value - $node_count )
+ $search_tree_size_in_bytes
### IPv4 addresses in an IPv6 tree
When storing IPv4 addresses in an IPv6 tree, they are stored as-is, so they
occupy the first 32-bits of the address space (from 0 to 2**32 - 1).
( run in 0.222 second using v1.01-cache-2.11-cpan-26ccb49234f )