diff --git a/squid-db/Cargo.toml b/squid-db/Cargo.toml index c79ce7a..8141177 100644 --- a/squid-db/Cargo.toml +++ b/squid-db/Cargo.toml @@ -12,5 +12,4 @@ lz4 = { version = "1.24", optional = true } serde = { version = "1", features = ["derive"] } [features] -default = ["compress"] compress = ["lz4"] diff --git a/squid-db/src/lib.rs b/squid-db/src/lib.rs index 9a46c0b..cbdbe59 100644 --- a/squid-db/src/lib.rs +++ b/squid-db/src/lib.rs @@ -14,7 +14,7 @@ use std::{ error::Error, fmt, fs::{File, OpenOptions}, - io::Read, + io::{BufRead, BufReader, Write}, }; const SOURCE_FILE: &str = "./data.bin"; @@ -59,7 +59,8 @@ where /// Structure representing one instance of the database. #[derive(Debug)] pub struct Instance { - // file: File, + /// File. + file: File, /// The data in the file represented in the memory. pub data: World, } @@ -87,7 +88,7 @@ where let loaded_file = load()?; Ok(Self { - // file: loaded_file.0, + file: loaded_file.0, data: loaded_file.1, }) } @@ -118,8 +119,6 @@ where /// }); /// ``` pub fn set(&mut self, data: T) -> Result<(), DbError> { - self.data.0.insert(0, data); - #[cfg(feature = "compress")] let encoded = compress::compress( &bincode::serialize(&self.data.0) @@ -128,24 +127,42 @@ where .map_err(|_| DbError::FailedCompression)?; #[cfg(not(feature = "compress"))] - let encoded = bincode::serialize(&self.data.0) + /*let encoded = bincode::serialize(&self.data.0) + .map_err(|_| DbError::FailedSerialization)?;*/ + let encoded = bincode::serialize(&data) .map_err(|_| DbError::FailedSerialization)?; + // Add new data on cache. + self.data.0.insert(0, data); + + //self.save_all(&encoded)?; self.save(&encoded)?; Ok(()) } - /// Save data in a file. + /// Save all data in a file. + /// If any data is omitted during registration, it is deleted. + #[inline(always)] + #[allow(unused)] + fn save_all(&mut self, buf: &[u8]) -> Result<(), DbError> { + std::fs::write(SOURCE_FILE, buf).map_err(|_| DbError::Unspecified)?; + Ok(()) + } + + /// Append one data to the file. #[inline(always)] + #[allow(unused)] fn save(&mut self, buf: &[u8]) -> Result<(), DbError> { - // It seems that data is saved both times here. - // I mean, new datas are saved while old kept also, so there are two datas. - // And again, and again... - /*self.file.write_all(buf).map_err(|_| DbError::Unspecified)?; - self.file.flush().map_err(|_| DbError::Unspecified)?;*/ + let mut buffer: Vec = vec![]; + + buffer.extend_from_slice(buf); + buffer.extend_from_slice(b"\n"); + + self.file + .write_all(&buffer) + .map_err(|_| DbError::Unspecified)?; - std::fs::write(SOURCE_FILE, buf).map_err(|_| DbError::Unspecified)?; Ok(()) } } @@ -156,20 +173,26 @@ fn load() -> Result<(File, World), DbError> where T: serde::de::DeserializeOwned + serde::Serialize, { - let mut file = OpenOptions::new() + let file = OpenOptions::new() .read(true) - .write(true) + .append(true) .create(true) .open(SOURCE_FILE) .map_err(|_| DbError::Unspecified)?; - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer) - .map_err(|_| DbError::FailedReading)?; + let reader = BufReader::new(&file); + let mut world: World = World(Vec::new()); - if buffer.is_empty() { - Ok((file, World(vec![]))) - } else { + for line in reader.lines() { + world.0.push( + bincode::deserialize( + line.map_err(|_| DbError::FailedReading)?.as_bytes(), + ) + .map_err(|_| DbError::FailedDeserialization)?, + ); + } + + /* #[cfg(feature = "compress")] let result = bincode::deserialize( &compress::decompress(&buffer[..]) @@ -182,5 +205,7 @@ where .map_err(|_| DbError::FailedDeserialization)?; Ok((file, result)) - } + */ + + Ok((file, world)) } diff --git a/squid/Cargo.toml b/squid/Cargo.toml index 93cfd1e..0c9c385 100644 --- a/squid/Cargo.toml +++ b/squid/Cargo.toml @@ -18,8 +18,6 @@ serde = { version = "1", features = ["derive"] } serde_yaml = "0.9" anyhow = "1" -smallvec = { version = "1", features = ["serde"] } - fern = "0.6" log = "0.4"