pub struct Writer { /* private fields */ }Expand description
Builds a MaxMind DB file from inserted networks.
The writer accumulates encoded data records, deduplicating equal payloads,
and serializes a search tree plus metadata when Writer::write_to is
called.
§Example
use maxminddb::writer::{Writer, WriterOptions};
let opts = WriterOptions {
database_type: "Test".to_string(),
ip_version: 4,
record_size: 24,
..Default::default()
};
let mut writer = Writer::new(opts).unwrap();
writer.insert("10.0.0.0/8".parse().unwrap(), "example").unwrap();
let mut buf = Vec::new();
writer.write_to(&mut buf).unwrap();
let reader = maxminddb::Reader::from_source(buf).unwrap();
assert_eq!(reader.lookup::<String>("10.0.0.1".parse().unwrap()).unwrap(), "example");Implementations§
Source§impl Writer
impl Writer
Sourcepub fn new(opts: WriterOptions) -> WriterResult<Self>
pub fn new(opts: WriterOptions) -> WriterResult<Self>
Create a writer.
§Errors
Returns an error if ip_version is not 4 or 6, or if record_size
is not 24, 28, or 32.
Sourcepub fn insert_value(
&mut self,
network: IpNetwork,
value: Value,
) -> WriterResult<()>
pub fn insert_value( &mut self, network: IpNetwork, value: Value, ) -> WriterResult<()>
Insert a fully encoded Value for network.
Equal values are stored once in the data section and referenced by every network that uses them.
§Errors
Returns an error if an IPv6 network is inserted into an IPv4 database or if the value cannot be encoded.
§Overwriting
Inserting a network replaces the subtree at that prefix. Inserting a less specific network after a more specific one therefore discards the more specific entries, so insert least specific networks first.
Sourcepub fn insert<T: Serialize>(
&mut self,
network: IpNetwork,
value: T,
) -> WriterResult<()>
pub fn insert<T: Serialize>( &mut self, network: IpNetwork, value: T, ) -> WriterResult<()>
Insert a Serialize value for network.
This is the same as Writer::insert_value but encodes value for you.
§Errors
Returns an error if value cannot be encoded, if an IPv6 network is
inserted into an IPv4 database, or if the value is out of range for the
MaxMind DB format (for example a negative integer below i32::MIN).
§Overwriting
See Writer::insert_value: insert least specific networks first.
Sourcepub fn write_to<W: Write>(&mut self, writer: &mut W) -> WriterResult<u64>
pub fn write_to<W: Write>(&mut self, writer: &mut W) -> WriterResult<u64>
Serialize the database into writer, returning the number of bytes
written.
This finalizes node numbering, so call it once, after all inserts.
§Errors
Returns an error if a record value does not fit the configured record
size, if the node count exceeds u32, or if writing to writer fails.