From 42a6d37c43361f7947c29e3621fae405e61e5b53 Mon Sep 17 00:00:00 2001 From: biluohc Date: Mon, 2 Jul 2018 23:04:46 +0800 Subject: [PATCH] add a example --- Cargo.toml | 2 +- binding/rust/example/Cargo.toml | 13 +++++++ binding/rust/example/src/main.rs | 58 ++++++++++++++++++++++++++++++++ binding/rust/readme.md | 25 ++++++++++---- binding/rust/src/lib.rs | 7 ++-- 5 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 binding/rust/example/Cargo.toml create mode 100644 binding/rust/example/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index a0427e3..31c0e9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["binding/rust"] +members = ["binding/rust", "binding/rust/example"] diff --git a/binding/rust/example/Cargo.toml b/binding/rust/example/Cargo.toml new file mode 100644 index 0000000..eb9f146 --- /dev/null +++ b/binding/rust/example/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example" +version = "0.1.0" +authors = ["biluohc "] + +[dependencies] + +[dependencies.ip2region] +path = "../" +# git = "https://github.com/lionsoul2014/ip2region" +# git = "https://github.com/biluohc/ip2region" +version = "*" +features = ["lazy"] diff --git a/binding/rust/example/src/main.rs b/binding/rust/example/src/main.rs new file mode 100644 index 0000000..6e0fe61 --- /dev/null +++ b/binding/rust/example/src/main.rs @@ -0,0 +1,58 @@ +extern crate ip2region; + +use ip2region::*; + +use std::env; +use std::time::Instant; + +// cargo run --release ../../../data/ip2region.db + +static IPS: &'static [&'_ str] = &[ + "117.136.105.202", + "47.95.47.253", + "127.0.0.1", + "10.0.0.1", + "1.1.1.1", +]; + +fn main() { + lazy(); + + overview(); +} + +fn lazy() { + for ip in IPS { + let start = Instant::now(); + let res = memory_search(ip); + let end = start.elapsed().subsec_micros(); + println!("lazy__ {:06} microseconds: {:?}", end, res); + } +} + +fn overview() { + let args = env::args().skip(1).collect::>(); + let db_path = &args[0]; + + let mut ip2 = Ip2Region::new(db_path).unwrap(); + let ip2o = ip2.to_owned().unwrap(); + + for ip in IPS { + let start = Instant::now(); + let res = ip2o.memory_search(ip); + let end = start.elapsed().subsec_micros(); + println!("memory {:06} microseconds: {:?}", end, res); + + let start = Instant::now(); + let res = ip2.binary_search(ip); + let end = start.elapsed().subsec_micros(); + println!("binary {:06} microseconds: {:?}", end, res); + + let start = Instant::now(); + let res = ip2.btree_search(ip); + let end = start.elapsed().subsec_micros(); + println!("btree {:06} microseconds: {:?}", end, res); + + println!(); + } +} diff --git a/binding/rust/readme.md b/binding/rust/readme.md index 4b6d8dd..5e13a2b 100644 --- a/binding/rust/readme.md +++ b/binding/rust/readme.md @@ -2,22 +2,33 @@ ## 用法 -基本都在 `src/main.rs` 的 `overview` 函数里, +都在 `src/example` 里 - 另外 `cargo doc --features lazy`可以看到所有`API`。 +另外 `cargo doc --features lazy` 可以看到所有 `API`。 -## `lazy` feature 直接把DB打包到二进制 +### 添加依赖 -复制下面的 toml 添加依赖即可使用,其 api 是 `memory_search`。 - -只是目前 DB 足有3.2M,还是有些感人的。 +注意 `#` 是 toml格式的注释,文件前三行的 `path` 和 `git` 是引用包的方式,`path` 是按路径引用包,`git` 是按git项目地址,按实际情况选一个。 ```toml [dependencies.ip2region] git = "https://github.com/lionsoul2014/ip2region" # git = "https://github.com/biluohc/ip2region" +# path = "../" version = "*" -features = ["lazy"] +# features = ["lazy"] ``` +### 代码 +查看 `src/example/src/main.rs` + +### `lazy` feature 把 DB 直接打包进二进制 + +取消上面 toml 的 `# features = ["lazy"]` 行的注释即可使用,其 api 是 `memory_search`。 + +只是目前 DB 足有3.2M,还是有些感人的。 + +关键的一行是 `features = ["lazy"]` ,不需要则可以注释或者删掉。 + + diff --git a/binding/rust/src/lib.rs b/binding/rust/src/lib.rs index 7d198bb..efcde97 100644 --- a/binding/rust/src/lib.rs +++ b/binding/rust/src/lib.rs @@ -11,12 +11,15 @@ mod db; mod error; pub use error::{Error, Result}; mod owned; + +#[doc(hidden)] +pub use db::DB_PATH; +pub use owned::{OwnedIp2Region, OwnedIpInfo}; + #[cfg(feature = "lazy")] use db::DB_BYTES; -pub use db::DB_PATH; #[cfg(feature = "lazy")] pub use owned::memory_search; -pub use owned::{OwnedIp2Region, OwnedIpInfo}; const INDEX_BLOCK_LENGTH: u32 = 12; const TOTAL_HEADER_LENGTH: usize = 8192;