This commit is contained in:
Nathan Rashleigh committed 2025-09-24 16:33:01 +10:00
1 parent d35b51d4c3
commit 13ee0dcf01
2 files changed
+43 -43

No files matched your search

+38 -42
View File
@@ -57,30 +57,26 @@ pub struct Database {
const PAGE_SIZE: u16 = 4096;
impl Database {
pub fn export(self, path: &Path) -> Result<(), PageError> {
let pages = Table::from_rows(PageType::Tracks, 1, PAGE_SIZE, self.tracks.values().cloned().map(Row::Track))?;
println!("{:?}", pages.len());
println!("{:?}", pages.last());
Ok(())
}
// pub fn export(self, path: &Path) -> std::io::Result<()> {
// // 1) Reserve header page
// let mut f = File::create(path)?;
// f.write_all(&vec![0u8; PAGE_SIZE as usize])?;
// pub fn export(self, path: &Path) -> Result<(), PageError> {
// let table = Table::from_rows(PageType::Tracks, 1, PAGE_SIZE, self.tracks.values().cloned().map(Row::Track))?;
// println!("{:?}", table);
// Ok(())
// }
// // 2) Emit tables linearly, collect table metadata
// let mut next_page_index: u32 = 1;
pub fn export(self, path: &Path) -> std::io::Result<()> {
// 1) Reserve header page
let mut f = File::create(path)?;
f.write_all(&vec![0u8; PAGE_SIZE as usize])?;
// // ok now we're getting somewhere
// // smash rows into row_groups of 16
// // try to write those into a page until it is full
// // yield pages until all the rows are in
// // set the indices
// // 420 blaze it
let (table, pages) = Table::from_rows(PageType::Tracks, 1, PAGE_SIZE, self.tracks.values().cloned().take(1).map(Row::Track)).unwrap();
// let (tracks_first, tracks_last) =
// Self::write_table(&mut f, PageType::Tracks, next_page_index, self.tracks.values().cloned().map(Row::Track))?;
// next_page_index = tracks_last + 1;
println!("{:?}", table);
println!("{:?}", pages);
table.write_le(&mut f).unwrap();
pages.write_le_args(&mut f, (PAGE_SIZE.into(),)).unwrap();
// page is somehow getting zeroed when ... writing? reading??
// validate it by writing and reading it back
// let (tree_first, tree_last) =
// Self::write_table(&mut f, PageType::PlaylistTree, next_page_index, self.nodes.values().cloned().map(Row::PlaylistTreeNode))?;
@@ -100,30 +96,30 @@ impl Database {
// Self::write_table(&mut f, PageType::Artwork, next_page_index, self.artwork.values().cloned().map(Row::Artwork))?;
// next_page_index = art_last + 1;
// // 3) Build header and back-patch at page 0
// let header = Header {
// // fill other required fields in your crate
// sequence: 1, // or previous+1
// next_unused_page: PageIndex(next_page_index),
// tables: vec![
// Table { page_type: PageType::Tracks, first_page: PageIndex(tracks_first), last_page: PageIndex(tracks_last), empty_candidate: 0, /* … */ },
// 3) Build header and back-patch at page 0
let header = Header {
// fill other required fields in your crate
sequence: 0, // or previous+1
next_unused_page: PageIndex(pages.len() as u32 + 1),
tables: vec![
table,
// Table { page_type: PageType::PlaylistTree, first_page: PageIndex(tree_first), last_page: PageIndex(tree_last), empty_candidate: 0, /* … */ },
// Table { page_type: PageType::PlaylistEntries, first_page: PageIndex(entries_first), last_page: PageIndex(entries_last), empty_candidate: 0, /* … */ },
// Table { page_type: PageType::Artwork, first_page: PageIndex(art_first), last_page: PageIndex(art_last), empty_candidate: 0, /* … */ },
// ],
// ..Default::default()
// };
],
..Default::default()
};
// // Write header at offset 0 and pad to one page
// f.seek(SeekFrom::Start(0))?;
// header.write(&mut f);
// // pad residual of page 0 if BinWrite didn’t fill it:
// let pos = f.stream_position()?;
// if pos < PAGE_SIZE as u64 {
// f.write_all(&vec![0u8; (PAGE_SIZE as u64 - pos) as usize])?;
// }
// Ok(())
// }
// Write header at offset 0 and pad to one page
f.seek(SeekFrom::Start(0))?;
header.write_le(&mut f).unwrap();
// pad residual of page 0 if BinWrite didn’t fill it:
let pos = f.stream_position()?;
if pos < PAGE_SIZE as u64 {
f.write_all(&vec![0u8; (PAGE_SIZE as u64 - pos) as usize])?;
}
Ok(())
}
// fn write_table<I>(
// f: &mut File,
@@ -198,7 +194,7 @@ impl Database {
let mut entries: BTreeMap<PlaylistTreeNodeId, Vec<PlaylistEntry>> = BTreeMap::new();
for table in &header.tables {
let pages = header.read_pages(&mut f, Endian::NATIVE, (&table.first_page, &table.last_page))?;
let pages = header.read_pages(&mut f, Endian::Little, (&table.first_page, &table.last_page))?;
for page in pages {
for rg in page.row_groups {
+5 -1
View File
@@ -26,7 +26,11 @@ fn main() -> Result<(), PageError> {
println!("tables: {:?}", db.header);
println!("tables: {:?}", db.header.tables);
db.export(&Path::new("./data/output.pdb"))?;
let output = Path::new("./data/output.pdb");
db.export(&output).unwrap();
// let db = Database::load_from_pdb("./data/output.pdb").unwrap();
// println!("{:?}", db);
// for n in db.nodes {
// println!("{:?}", n);