| 1 |
// Copyright 2019-2020 Georges Racinet <[email protected]> |
| 2 |
// |
| 3 |
// This software may be used and distributed according to the terms of the |
| 4 |
// GNU General Public License version 2 or any later version. |
| 5 |
|
| 6 |
use std::fs::File; |
| 7 |
use std::io; |
| 8 |
use std::io::Write; |
| 9 |
use std::path::Path; |
| 10 |
use std::path::PathBuf; |
| 11 |
use std::time::Instant; |
| 12 |
|
| 13 |
use hg::revlog::node::*; |
| 14 |
use hg::revlog::nodemap::*; |
| 15 |
use hg::revlog::*; |
| 16 |
use memmap2::MmapOptions; |
| 17 |
use rand::Rng; |
| 18 |
|
| 19 |
mod index; |
| 20 |
use index::Index; |
| 21 |
|
| 22 |
fn mmap_index(repo_path: &Path) -> Index { |
| 23 |
let mut path = PathBuf::from(repo_path); |
| 24 |
path.extend([".hg", "store", "00changelog.i"].iter()); |
| 25 |
Index::load_mmap(path) |
| 26 |
} |
| 27 |
|
| 28 |
fn mmap_nodemap(path: &Path) -> NodeTree { |
| 29 |
let file = File::open(path).unwrap(); |
| 30 |
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() }; |
| 31 |
let len = mmap.len(); |
| 32 |
NodeTree::load_bytes(Box::new(mmap), len) |
| 33 |
} |
| 34 |
|
| 35 |
/// Scan the whole index and create the corresponding nodemap file at `path` |
| 36 |
fn create(index: &Index, path: &Path) -> io::Result<()> { |
| 37 |
let mut file = File::create(path)?; |
| 38 |
let start = Instant::now(); |
| 39 |
let mut nm = NodeTree::default(); |
| 40 |
for rev in 0..index.len() { |
| 41 |
let rev = Revision(rev as BaseRevision); |
| 42 |
nm.insert(index, index.node(rev), rev).unwrap(); |
| 43 |
} |
| 44 |
eprintln!("Nodemap constructed in RAM in {:?}", start.elapsed()); |
| 45 |
file.write_all(&nm.into_readonly_and_added_bytes().1)?; |
| 46 |
eprintln!("Nodemap written to disk"); |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
|
| 50 |
fn query(index: &Index, nm: &NodeTree, prefix: &str) { |
| 51 |
let start = Instant::now(); |
| 52 |
let res = NodePrefix::from_hex(prefix).map(|p| nm.find_bin(index, p)); |
| 53 |
println!("Result found in {:?}: {:?}", start.elapsed(), res); |
| 54 |
} |
| 55 |
|
| 56 |
fn bench(index: &Index, nm: &NodeTree, queries: usize) { |
| 57 |
let len = index.len() as u32; |
| 58 |
let mut rng = rand::rng(); |
| 59 |
let nodes: Vec<Node> = (0..queries) |
| 60 |
.map(|_| { |
| 61 |
*index.node(Revision((rng.random::<u32>() % len) as BaseRevision)) |
| 62 |
}) |
| 63 |
.collect(); |
| 64 |
if queries < 10 { |
| 65 |
let nodes_hex: Vec<String> = |
| 66 |
nodes.iter().map(|n| format!("{:x}", n)).collect(); |
| 67 |
println!("Nodes: {:?}", nodes_hex); |
| 68 |
} |
| 69 |
let mut last: Option<Revision> = None; |
| 70 |
let start = Instant::now(); |
| 71 |
for node in nodes.iter() { |
| 72 |
last = nm.find_bin(index, node.into()).unwrap(); |
| 73 |
} |
| 74 |
let elapsed = start.elapsed(); |
| 75 |
println!( |
| 76 |
"Did {} queries in {:?} (mean {:?}), last was {:x} with result {:?}", |
| 77 |
queries, |
| 78 |
elapsed, |
| 79 |
elapsed / (queries as u32), |
| 80 |
nodes.last().unwrap(), |
| 81 |
last |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
fn main() { |
| 86 |
use clap::Parser; |
| 87 |
use clap::Subcommand; |
| 88 |
|
| 89 |
#[derive(Parser)] |
| 90 |
#[command()] |
| 91 |
/// Nodemap pure Rust example |
| 92 |
struct App { |
| 93 |
// Path to the repository, always necessary for its index |
| 94 |
#[arg(short, long)] |
| 95 |
repository: PathBuf, |
| 96 |
// Path to the nodemap file, independent of REPOSITORY |
| 97 |
#[arg(short, long)] |
| 98 |
nodemap_file: PathBuf, |
| 99 |
#[command(subcommand)] |
| 100 |
command: Command, |
| 101 |
} |
| 102 |
|
| 103 |
#[derive(Subcommand)] |
| 104 |
enum Command { |
| 105 |
/// Create `NODEMAP_FILE` by scanning repository index |
| 106 |
Create, |
| 107 |
/// Query `NODEMAP_FILE` for `prefix` |
| 108 |
Query { prefix: String }, |
| 109 |
/// Perform #`QUERIES` random successful queries on `NODEMAP_FILE` |
| 110 |
Bench { queries: usize }, |
| 111 |
} |
| 112 |
|
| 113 |
let app = App::parse(); |
| 114 |
|
| 115 |
let repo = &app.repository; |
| 116 |
let nm_path = &app.nodemap_file; |
| 117 |
|
| 118 |
let index = mmap_index(repo); |
| 119 |
let nm = mmap_nodemap(nm_path); |
| 120 |
|
| 121 |
match &app.command { |
| 122 |
Command::Create => { |
| 123 |
println!( |
| 124 |
"Creating nodemap file {} for repository {}", |
| 125 |
nm_path.display(), |
| 126 |
repo.display() |
| 127 |
); |
| 128 |
create(&index, Path::new(nm_path)).unwrap(); |
| 129 |
} |
| 130 |
Command::Bench { queries } => { |
| 131 |
println!( |
| 132 |
"Doing {} random queries in nodemap file {} of repository {}", |
| 133 |
queries, |
| 134 |
nm_path.display(), |
| 135 |
repo.display() |
| 136 |
); |
| 137 |
bench(&index, &nm, *queries); |
| 138 |
} |
| 139 |
Command::Query { prefix } => { |
| 140 |
println!( |
| 141 |
"Querying {} in nodemap file {} of repository {}", |
| 142 |
prefix, |
| 143 |
nm_path.display(), |
| 144 |
repo.display() |
| 145 |
); |
| 146 |
query(&index, &nm, prefix); |
| 147 |
} |
| 148 |
} |
| 149 |
} |