finnbear / db_ip

An (unofficial) Rust library for querying db-ip.com data
https://crates.io/crates/db_ip
Other
5 stars 0 forks source link

I need some clarifications about `include_country_code_database!()` #2

Closed GyulyVGC closed 1 year ago

GyulyVGC commented 1 year ago

Hi! Thank you for having created the db_ip crate!

I would like to use it in my project and I wanted some clarifications.

Which database is the instruction let db = include_country_code_database!() loading? Do I need to serialize it? How can the serde feature be used? Is there any difference with the db I can download from dp-ip.com?

finnbear commented 1 year ago

Hi! Thank you for having created the db_ip crate!

:heart:

Which database is the instruction let db = include_country_code_database!() loading?

That loads the compressed (with respect to country codes) version of the IP to Country Lite database in CSV format. The downloading, caching, and compression of the most recent possible database are done in the build script.

Do I need to serialize it?

If you mean serialize as in serde, that is optional (see next answer).

If you mean serialize with respect to thread safety, no.

How can the serde feature be used?

The README hints at a potential use case: "The raw csv data takes a while to parse, even in release mode. You may use the serde feature to create and load a serialized version."

So if you want to load your own CSV data file at runtime (such as the non-"lite" version that the macros don't currently support), and that is too slow, you may instead opt to load it once, use the serde feature to serialize the loaded version (e.g. as JSON) to a file, and deserialize that file instead. You can expect the serde serialized version to be much smaller and take much less time to deserialize when compared to the CSV.

Is there any difference with the db I can download from dp-ip.com?

No, the build script downloads, caches, and compressses a CSV file directly from db-ip.com

GyulyVGC commented 1 year ago

In term of performance, how does the compressed db loaded with that function compares to the serde serialized version?

finnbear commented 1 year ago

In term of performance, how does the compressed db loaded with that function compares to the serde serialized version?

include_country_code_database!() actually uses serde, specifically Bincode. It would be just as fast as using Bincode yourself, but faster than almost any other format like JSON.

GyulyVGC commented 1 year ago

Got it!

Last question: is it really necessary for db.get to return an Option? Couldn't it return the unknown country code instead of the None value?

I'm asking this since the IP address should already be parsed by the parse method, so it can be assumed correct.

finnbear commented 1 year ago

Last question: is it really necessary for db.get to return an Option?

Yes, the None encodes a situation in which the loaded geolocation database (CSV, serde, ortherwise) doesn't contain any IP range containing the specified IP or the range didn't have that type data (this is impossible for CountryCode but possible in the general case; notice how the data to extract is a generic type parameter)

Couldn't it return the unknown country code instead of the None value?

I think Option is more idiomatic, and also generalizes well in the case of extracting other forms of data (like Region) that lack an "unknown" encoding.

I'm asking this since the IP address should already be parsed by the parse method, so it can be assumed correct.

Yes, but the database doesn't necessarily cover all correct IP's

GyulyVGC commented 1 year ago

Perfect, you have been really kind and helpful