Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort & missing #2

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}