fix: Wrap Write and Read functions with results
This commit is contained in:
parent
c68cdb3b60
commit
fd0439aa44
@ -50,19 +50,21 @@ impl Rustysearch {
|
|||||||
/// If the stats do not exist, it makes returns data with the current
|
/// If the stats do not exist, it makes returns data with the current
|
||||||
/// version of ``rustysearch`` & zero docs (used in scoring).
|
/// version of ``rustysearch`` & zero docs (used in scoring).
|
||||||
///
|
///
|
||||||
pub fn read_stats(&self) -> Stats {
|
pub fn read_stats(&self) -> std::io::Result<Stats> {
|
||||||
|
let mut stats: Stats;
|
||||||
|
|
||||||
if !Path::new(&self.stats_path).exists() {
|
if !Path::new(&self.stats_path).exists() {
|
||||||
let stats = Stats {
|
stats = Stats {
|
||||||
version: String::from("0.1.0"),
|
version: String::from("0.1.0"),
|
||||||
total_docs: 0,
|
total_docs: 0,
|
||||||
};
|
};
|
||||||
return stats;
|
} else {
|
||||||
|
// Read the stats file
|
||||||
|
let stats_json = fs::read_to_string(&self.stats_path).expect("Unable to read stats");
|
||||||
|
stats = serde_json::from_str(&stats_json).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the stats file
|
Ok(stats)
|
||||||
let stats_json = fs::read_to_string(&self.stats_path).expect("Unable to read stats");
|
|
||||||
let stats: Stats = serde_json::from_str(&stats_json).unwrap();
|
|
||||||
return stats;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Writes the index-wide stats**
|
/// **Writes the index-wide stats**
|
||||||
@ -75,10 +77,11 @@ impl Rustysearch {
|
|||||||
/// 'total_docs': 25,
|
/// 'total_docs': 25,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
pub fn write_stats(&self, new_stats: Stats) {
|
pub fn write_stats(&self, new_stats: Stats) -> std::io::Result<()> {
|
||||||
// Write new_stats as json to stats_path
|
// Write new_stats as json to stats_path
|
||||||
let new_stats_json = serde_json::to_string(&new_stats).unwrap();
|
let new_stats_json = serde_json::to_string(&new_stats).unwrap();
|
||||||
fs::write(&self.stats_path, new_stats_json).expect("Unable to write stats");
|
fs::write(&self.stats_path, new_stats_json)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Increments the total number of documents the index is aware of**
|
/// **Increments the total number of documents the index is aware of**
|
||||||
@ -87,15 +90,15 @@ impl Rustysearch {
|
|||||||
/// of the indexing process.
|
/// of the indexing process.
|
||||||
///
|
///
|
||||||
pub fn increment_total_docs(&self) {
|
pub fn increment_total_docs(&self) {
|
||||||
let mut current_stats = self.read_stats();
|
let mut current_stats = self.read_stats().unwrap();
|
||||||
current_stats.total_docs += 1;
|
current_stats.total_docs += 1;
|
||||||
self.write_stats(current_stats);
|
self.write_stats(current_stats).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the total number of documents the index is aware of
|
/// Returns the total number of documents the index is aware of
|
||||||
///
|
///
|
||||||
pub fn get_total_docs(&self) -> i32 {
|
pub fn get_total_docs(&self) -> i32 {
|
||||||
let stats = self.read_stats();
|
let stats = self.read_stats().unwrap();
|
||||||
return stats.total_docs;
|
return stats.total_docs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::{thread::{self}, time::{self, Duration}};
|
||||||
use rustysearch::{types::Stats, search::Rustysearch};
|
use rustysearch::{types::Stats, search::Rustysearch};
|
||||||
|
|
||||||
|
const TEN_MILLIS: Duration = time::Duration::from_millis(100);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_write_new_stats(){
|
fn test_write_new_stats(){
|
||||||
let stats = Stats{
|
let stats = Stats{
|
||||||
@ -15,7 +18,8 @@ mod tests {
|
|||||||
let search = Rustysearch::new("/tmp/rustysearch");
|
let search = Rustysearch::new("/tmp/rustysearch");
|
||||||
search.setup();
|
search.setup();
|
||||||
|
|
||||||
search.write_stats(stats);
|
search.write_stats(stats).unwrap();
|
||||||
|
thread::sleep(TEN_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -25,9 +29,10 @@ mod tests {
|
|||||||
|
|
||||||
clean_stats();
|
clean_stats();
|
||||||
|
|
||||||
let stats = search.read_stats();
|
let stats = search.read_stats().unwrap();
|
||||||
assert_eq!(stats.version, "0.1.0");
|
assert_eq!(stats.version, "0.1.0");
|
||||||
assert_eq!(stats.total_docs, 0);
|
assert_eq!(stats.total_docs, 0);
|
||||||
|
thread::sleep(TEN_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -37,12 +42,13 @@ mod tests {
|
|||||||
|
|
||||||
clean_stats();
|
clean_stats();
|
||||||
|
|
||||||
let stats = search.read_stats();
|
let stats = search.read_stats().unwrap();
|
||||||
assert_eq!(stats.total_docs, 0);
|
assert_eq!(stats.total_docs, 0);
|
||||||
|
|
||||||
search.increment_total_docs();
|
search.increment_total_docs();
|
||||||
let stats = search.read_stats();
|
let stats = search.read_stats().unwrap();
|
||||||
assert_eq!(stats.total_docs, 1);
|
assert_eq!(stats.total_docs, 1);
|
||||||
|
thread::sleep(TEN_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -52,15 +58,16 @@ mod tests {
|
|||||||
|
|
||||||
clean_stats();
|
clean_stats();
|
||||||
|
|
||||||
let stats = search.read_stats();
|
let stats = search.read_stats().unwrap();
|
||||||
assert_eq!(stats.total_docs, 0);
|
assert_eq!(stats.total_docs, 0);
|
||||||
|
|
||||||
search.increment_total_docs();
|
search.increment_total_docs();
|
||||||
let stats = search.read_stats();
|
let stats = search.read_stats().unwrap();
|
||||||
assert_eq!(stats.total_docs, 1);
|
assert_eq!(stats.total_docs, 1);
|
||||||
|
|
||||||
let total_docs = search.get_total_docs();
|
let total_docs = search.get_total_docs();
|
||||||
assert_eq!(total_docs, 1);
|
assert_eq!(total_docs, 1);
|
||||||
|
thread::sleep(TEN_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clean_stats(){
|
fn clean_stats(){
|
||||||
@ -71,6 +78,7 @@ mod tests {
|
|||||||
version: String::from("0.1.0"),
|
version: String::from("0.1.0"),
|
||||||
total_docs: 0,
|
total_docs: 0,
|
||||||
};
|
};
|
||||||
search.write_stats(new_stats);
|
search.write_stats(new_stats).unwrap();
|
||||||
|
thread::sleep(TEN_MILLIS);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user