Skip to content

Commit

Permalink
Merge pull request #2 from Wetty-Technology/master
Browse files Browse the repository at this point in the history
sort & missing
  • Loading branch information
Codetector1374 authored Jul 27, 2024
2 parents f7b9b90 + 695c4ab commit 0ba5513
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ circular-buffer = "0.1.7"
clap = { version = "4.5.9", features = ["derive"] }
crc = "3.2.1"
indicatif = "0.17.8"
memmap2 = "0.9.4"
num_enum = "0.7.2"
pretty-hex = "0.4.1"
simple_endian = "0.3.2"
Expand Down
54 changes: 52 additions & 2 deletions src/bin/tablespace_sort.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
fn main() {
todo!()
use std::fs::File;
use std::path::PathBuf;

use clap::Parser;
use memmap2::MmapMut;

#[derive(Parser, Debug)]
struct Arguments {
file: PathBuf,
}

const PAGE_SIZE: usize = 16 * 1024; // 16K块大小

struct Page {
data: [u8; PAGE_SIZE],
}

impl Page {
fn offset(&self) -> u32 {
let num: [u8; 4] = self.data[4..8].try_into().unwrap();
return u32::from_be_bytes(num);
}
}

fn main() -> std::io::Result<()> {
let args = Arguments::parse();


let file = File::options().read(true).write(true).open(args.file)?;

let mmap = unsafe { MmapMut::map_mut(&file)? };

// 不知道怎么更科学的转类型,GPT 写的,
let pages: &mut [Page] = unsafe {
std::slice::from_raw_parts_mut(mmap.as_ptr() as *mut Page, mmap.len() / PAGE_SIZE)
};

pages.sort_by_key(|f| f.offset());

let mut missing = 0;
let mut expected = pages[0].offset();
for page in pages {
missing += page.offset() - expected;
expected = page.offset() + 1;
}

println!("max: {}, missing: {}", expected - 1, missing);

// 确保所有更改被刷到磁盘
mmap.flush()?;

Ok(())
}

0 comments on commit 0ba5513

Please sign in to comment.