Struct MmdbReader
pub struct MmdbReader { /* private fields */ }net only.Expand description
A zero-copy reader over a MaxMind DB.
The database buffer is shared, so cloning a reader is cheap and a single
database can be queried concurrently from many tasks without locks. The
bytes are either held in memory (Self::from_bytes / Self::open) or,
with the mmap feature, memory-mapped from disk via open_mmap.
Implementations§
§impl MmdbReader
impl MmdbReader
pub fn from_bytes(bytes: impl Into<Bytes>) -> Result<MmdbReader, GeoIpError>
pub fn from_bytes(bytes: impl Into<Bytes>) -> Result<MmdbReader, GeoIpError>
Build a reader from raw database bytes already held in memory.
§Errors
Returns GeoIpError if the bytes are not a valid MaxMind DB or use
an unsupported record size / format version.
pub fn open(path: impl AsRef<Path>) -> Result<MmdbReader, GeoIpError>
Available on crate feature std only.
pub fn open(path: impl AsRef<Path>) -> Result<MmdbReader, GeoIpError>
std only.Read a MaxMind DB from disk into memory.
The whole file is read into a shared buffer. With the mmap feature,
the unsafe open_mmap maps the file instead for a smaller resident
set on very large databases.
§Errors
Returns GeoIpError::Io if the file cannot be read, or another
GeoIpError if it is not a valid database.
pub unsafe fn open_mmap(
path: impl AsRef<Path>,
) -> Result<MmdbReader, GeoIpError>
Available on crate features mmap and std only.
pub unsafe fn open_mmap( path: impl AsRef<Path>, ) -> Result<MmdbReader, GeoIpError>
mmap and std only.Memory-map a MaxMind DB from disk instead of reading it into memory.
Lookups fault in only the pages they touch, so a large database costs little resident memory. The mapping is held until the returned reader and every clone of it have been dropped.
§Safety
The caller must ensure that the file at path is not written to,
truncated or otherwise modified, by this process or any other, from
this call until the returned reader and every clone of it have been
dropped. Lookups read the mapping through &[u8], so a modification
is undefined behaviour (a truncation can also raise SIGBUS). Use
Self::open when this cannot be guaranteed.
§Errors
Returns GeoIpError::Io if the file cannot be opened or mapped, or
another GeoIpError if it is not a valid database.
pub fn with_language(self, lang: impl Into<Box<str>>) -> MmdbReader
pub fn with_language(self, lang: impl Into<Box<str>>) -> MmdbReader
Set the preferred language code used to resolve localised names
(default "en"). This is the lookup key into the database’s localised
names maps (e.g. "en", "de", "pt-BR").
pub fn without_jump_table(self) -> MmdbReader
pub fn without_jump_table(self) -> MmdbReader
Drop the 16-bit jump table built at open.
The table maps the first 16 address bits straight to the node at that depth, which skips 16 node reads per lookup. It costs 256 KiB, or 512 KiB for an IPv6 database (one table for IPv6 and one for the IPv4 subtree), shared by all clones of the reader. Dropping it frees that memory; opening still walks the top 16 levels of the tree, since that walk also validates the records it reaches.
pub fn has_jump_table(&self) -> bool
pub fn has_jump_table(&self) -> bool
Whether lookups use the 16-bit jump table.
pub fn lookup(&self, ip: IpAddr) -> Option<GeoLocationRef<'_>>
pub fn lookup(&self, ip: IpAddr) -> Option<GeoLocationRef<'_>>
Look up ip, returning a zero-copy view of its record if present.
IPv4-mapped IPv6 inputs are canonicalised to IPv4 first. Looking up an
IPv4 address in an IPv6 database traverses the ::/96 range.
This is best-effort: a structurally corrupt database encountered mid
lookup yields None, indistinguishable from a legitimate miss. Use
Self::try_lookup when you need to observe corruption.
pub fn try_lookup(
&self,
ip: IpAddr,
) -> Result<Option<GeoLocationRef<'_>>, GeoIpError>
pub fn try_lookup( &self, ip: IpAddr, ) -> Result<Option<GeoLocationRef<'_>>, GeoIpError>
Like Self::lookup, but reports database corruption encountered
during traversal as a GeoIpError instead of collapsing it to None.
§Errors
Returns GeoIpError::Corrupt if the search tree is structurally
invalid for ip.
Trait Implementations§
§impl Clone for MmdbReader
impl Clone for MmdbReader
§fn clone(&self) -> MmdbReader
fn clone(&self) -> MmdbReader
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for MmdbReader
impl Debug for MmdbReader
§impl From<MmdbReader> for GeoReader
impl From<MmdbReader> for GeoReader
§fn from(reader: MmdbReader) -> GeoReader
fn from(reader: MmdbReader) -> GeoReader
Auto Trait Implementations§
impl !Freeze for MmdbReader
impl Forget for MmdbReader
impl RefUnwindSafe for MmdbReader
impl Send for MmdbReader
impl Sync for MmdbReader
impl Unpin for MmdbReader
impl UnsafeUnpin for MmdbReader
impl UnwindSafe for MmdbReader
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more