未验证 提交 fdd3b2c7 编写于 作者: G gongzhengyang 提交者: GitHub

Merge pull request #2 from gongzhengyang/feature-2xdb

feat: change the ip2region2 -> xdb
[workspace]
members = ["example", "ip2region2"]
members = ["example", "xdb"]
......@@ -23,7 +23,7 @@
```toml
[dependencies]
ip2region2 = { git = "https://github.com/lionsoul2014/ip2region.git", branch = "master" }
xdb = { git = "https://github.com/lionsoul2014/ip2region.git", branch = "master" }
# 用于生成随机数
rand = "0.8"
# 用于初始化日志打印
......@@ -42,7 +42,7 @@ use std::net::Ipv4Addr;
use std::thread;
use std::time::{Duration, Instant};
use ip2region2::{search_by_ip, searcher_init};
use xdb::{search_by_ip, searcher_init};
fn main() {
// 配置输出日志信息
......@@ -103,7 +103,7 @@ fn main() {
Compiling ip-test v0.1.0 (/home/gong/rust-work/ip-test)
Finished release [optimized] target(s) in 0.27s
Running `target/release/ip-test`
2022-12-24T03:31:22.921958Z DEBUG ip2region2::searcher: load xdb searcher file at ./ip2region.xdb
2022-12-24T03:31:22.921958Z DEBUG xdb::searcher: load xdb searcher file at ./ip2region.xdb
测试多类型查询
0|0|0|内网IP|内网IP
......@@ -131,7 +131,7 @@ use std::time::Instant;
use tokio::sync::mpsc;
use ip2region2::{search_by_ip, searcher_init};
use xdb::{search_by_ip, searcher_init};
#[tokio::main]
async fn main() {
......@@ -171,7 +171,7 @@ $ RUST_LOG=debug cargo run -r
Compiling ip-test v0.1.0 (/home/gong/rust-work/ip-test)
Finished release [optimized] target(s) in 0.51s
Running `target/release/ip-test`
2022-12-24T04:05:32.876664Z DEBUG ip2region2::searcher: load xdb searcher file at ./ip2region.xdb
2022-12-24T04:05:32.876664Z DEBUG xdb::searcher: load xdb searcher file at ./ip2region.xdb
tokio spawn 4 over cost: 1.133448675s, ave: 113ns
tokio spawn 2 over cost: 1.133938619s, ave: 113ns
tokio spawn 1 over cost: 1.136872027s, ave: 113ns
......@@ -183,7 +183,7 @@ tokio spawn 3 over cost: 1.26446099s, ave: 126ns
# `binding/rust`路径下面的结构说明
`ip2region2`
`xdb`
- 封装了`ip``region`的函数
- 里面包含单元测试和`benchmark`测试
......@@ -311,7 +311,7 @@ $ cargo test
需要保证查询速度不会有大幅降低,希望有朝一日,远方的朋友可以再深度优化一下,实现几十纳秒级别的查询速度
下面是`ip2region2`库的第一版`benchmark`结果
下面是`rust/xdb`库的第一版`benchmark`结果
重点关注如下
......
......@@ -10,7 +10,7 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ip2region2 = { path = "../ip2region2" }
xdb = { path = "../xdb" }
clap = { version = "4.0" }
tracing = "0.1"
tracing-subscriber = "0.3.14"
......@@ -9,7 +9,7 @@ use std::time::Instant;
use clap::ArgMatches;
use ip2region2::{search_by_ip, searcher_init};
use xdb::{search_by_ip, searcher_init};
mod cmd;
......
[package]
name = "ip2region2"
name = "xdb"
version = "0.1.0"
edition = "2021"
rust-version = "1.66.0"
......
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand;
use ip2region2::searcher::{
use xdb::searcher::{
get_block_by_size, get_full_cache, get_vector_index_cache, search_by_ip, searcher_init,
};
......
......@@ -24,7 +24,13 @@ where
T: ToUIntIP + Display,
{
let ip = ip.to_u32_ip()?;
let (start_ptr, end_ptr) = get_start_end_ptr(ip);
let il0 = ((ip >> 24) & 0xFF) as usize;
let il1 = ((ip >> 16) & 0xFF) as usize;
let idx = VECTOR_INDEX_SIZE * (il0 * VECTOR_INDEX_COLS + il1);
let start_point = idx;
let vector_cache = get_vector_index_cache();
let start_ptr = get_block_by_size(vector_cache, start_point, 4);
let end_ptr = get_block_by_size(vector_cache, start_point + 4, 4);
let mut left: usize = 0;
let mut right: usize = (end_ptr - start_ptr) / SEGMENT_INDEX_SIZE;
......@@ -49,17 +55,6 @@ where
Err("not matched".into())
}
pub fn get_start_end_ptr(ip: u32) -> (usize, usize) {
let il0 = ((ip >> 24) & 0xFF) as usize;
let il1 = ((ip >> 16) & 0xFF) as usize;
let idx = VECTOR_INDEX_SIZE * (il0 * VECTOR_INDEX_COLS + il1);
let start_point = idx;
let vector_cache = get_vector_index_cache();
let start_ptr = get_block_by_size(vector_cache, start_point, 4);
let end_ptr = get_block_by_size(vector_cache, start_point + 4, 4);
(start_ptr, end_ptr)
}
/// it will check ../data/ip2region.xdb, ../../data/ip2region.xdb, ../../../data/ip2region.xdb
fn default_detect_xdb_file() -> Result<String, Box<dyn Error>> {
let prefix = "../".to_owned();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册