Skip to content

Commit

Permalink
wip: improve database
Browse files Browse the repository at this point in the history
  • Loading branch information
markxoe committed Jun 4, 2024
1 parent 37e1657 commit 49b0df8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/commands/derive_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use log::info;

use crate::{
data::{
database,
database::Database,
maps::{link_map::LinkMap, page_map::PageMap},
parsers::{links, pages, redirects},
},
Expand Down Expand Up @@ -112,7 +112,8 @@ fn derive_db_command(args: DeriveDbArgs) {
.with_finish_message("Serialized and written to file")
.build();
spinner.enable_background();
database::serialize(output.as_str(), &links, &lookup);
Database::new(links, lookup).to_file(output.as_str());

spinner.finish();
}
}
4 changes: 2 additions & 2 deletions src/commands/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Args;
use crate::{
data::{
algorithm::bfs,
database,
database::Database,
maps::page_map::{PageMap, PageMapResult},
},
indication::ProgressBuilder,
Expand Down Expand Up @@ -31,7 +31,7 @@ fn interactive_cmd(args: &InteractiveArgs) {
.with_message("📝 Deserializing DB")
.build();
spinner.enable_background();
let data = database::deserialize(&db);
let data = Database::from_file(&db);
spinner.finish();

println!(
Expand Down
32 changes: 15 additions & 17 deletions src/data/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@ use serde::{Deserialize, Serialize};

use crate::data::maps::{link_map::LinkMap, page_map::PageMap};

#[derive(Serialize)]
struct SerializerDatabase<'a> {
pub links: &'a LinkMap,
pub pages: &'a PageMap,
}

#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct Database {
pub links: LinkMap,
pub pages: PageMap,
}

pub fn serialize(outfile: &str, links: &LinkMap, pages: &PageMap) {
let db = SerializerDatabase { links, pages };
impl Database {
pub fn new(links: LinkMap, pages: PageMap) -> Self {
Self { links, pages }
}

let file = std::fs::File::create(outfile).unwrap();
let writer = std::io::BufWriter::new(file);
pub fn to_file(&self, outfile: &str) {
let file = std::fs::File::create(outfile).unwrap();
let writer = std::io::BufWriter::new(file);

ciborium::into_writer(&db, writer).expect("Error writing db");
}
ciborium::into_writer(self, writer).expect("Error writing db");
}

pub fn deserialize(infile: &str) -> Database {
let file = std::fs::File::open(infile).unwrap();
let reader = std::io::BufReader::new(file);
pub fn from_file(infile: &str) -> Database {
let file = std::fs::File::open(infile).unwrap();
let reader = std::io::BufReader::new(file);

ciborium::from_reader(reader).expect("Error reading db")
ciborium::from_reader(reader).expect("Error reading db")
}
}

0 comments on commit 49b0df8

Please sign in to comment.