Feat: improve rust binding example read file

This commit is contained in:
gongzhengyang 2025-09-23 10:15:57 +08:00 committed by 龚正阳
parent b16fe21506
commit b5b00aeb87
2 changed files with 18 additions and 7 deletions

View File

@ -2,10 +2,19 @@ use clap::{Parser, Subcommand, ValueEnum};
/// Rust binding example for ip2region
///
/// `cargo run -- --xdb=../../../data/ip2region_v4.xdb bench ../../../data/ip.test.txt`
/// e.g
///
/// `cargo run -- --xdb=../../../data/ip2region_v4.xdb query`
/// ```
///
/// export XDB='../../../data/ip2region_v4.xdb'
///
/// export CHECK='../../../data/ipv4_source.txt'
///
/// cargo run -- --xdb=$XDB bench $CHECK
///
/// cargo run -- --xdb=$XDB query
///
/// ```
#[derive(Parser)]
pub struct Command {
/// xdb filepath, e.g. `../../../data/ip2region_v4.xdb`

View File

@ -1,7 +1,7 @@
extern crate core;
use std::fs::File;
use std::io::Read;
use std::io::{BufRead, BufReader};
use std::io::Write;
use std::net::Ipv4Addr;
use std::str::FromStr;
@ -14,13 +14,15 @@ use crate::cmd::{Action, CmdCachePolicy, Command};
mod cmd;
fn bench(searcher: &Searcher, check_filepath: &str) {
let file = File::open(check_filepath).unwrap();
let reader = BufReader::new(file);
let lines = reader.lines().take(100_000).collect::<Vec<_>>();
let now = Instant::now();
let mut count = 0;
let mut file = File::open(check_filepath).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
for line in contents.split('\n') {
for line in lines {
let line = line.unwrap();
if !line.contains('|') {
continue;
}