1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

more efficient handling of tags

This commit is contained in:
rabite 2020-02-15 16:01:41 +01:00
parent cf2d04c04b
commit 4b7d83f91a

View File

@ -104,9 +104,9 @@ pub fn load_tags() -> HResult<()> {
let tags = std::fs::read_to_string(tag_path)?; let tags = std::fs::read_to_string(tag_path)?;
let mut tags = tags.lines() let mut tags = tags.lines()
.map(|f| .map(PathBuf::from)
PathBuf::from(f))
.collect::<Vec<PathBuf>>(); .collect::<Vec<PathBuf>>();
tags.sort();
let mut tag_lock = TAGS.write()?; let mut tag_lock = TAGS.write()?;
tag_lock.0 = true; tag_lock.0 = true;
tag_lock.1.append(&mut tags); tag_lock.1.append(&mut tags);
@ -128,7 +128,9 @@ pub fn import_tags() -> HResult<()> {
pub fn check_tag(path: &PathBuf) -> HResult<bool> { pub fn check_tag(path: &PathBuf) -> HResult<bool> {
tags_loaded()?; tags_loaded()?;
let tagged = TAGS.read()?.1.contains(path); let tagged = TAGS.read()?.1.binary_search(path)
.map_or_else(|_| false,
|_| true);
Ok(tagged) Ok(tagged)
} }
@ -1261,23 +1263,45 @@ impl File {
}; };
self.tag = Some(new_state); self.tag = Some(new_state);
match new_state {
true => TAGS.write()?.1.push(self.path.clone()),
false => { TAGS.write()?.1.remove_item(&self.path); },
}
self.save_tags()?; self.save_tags()?;
Ok(()) Ok(())
} }
pub fn save_tags(&self) -> HResult<()> { pub fn save_tags(&self) -> HResult<()> {
std::thread::spawn(|| -> HResult<()> { if self.tag.is_none() { return Ok(()); }
let path = self.path.clone();
let state = self.tag.unwrap();
std::thread::spawn(move || -> HResult<()> {
use std::os::unix::ffi::OsStrExt;
let tagfile_path = crate::paths::tagfile_path()?; let tagfile_path = crate::paths::tagfile_path()?;
let tags = TAGS.read()?.clone(); let mut tags = TAGS.write()?;
let tags_str = tags.1.iter().map(|p| {
let path = p.to_string_lossy().to_string(); match state {
format!("{}\n", path) true => {
}).collect::<String>(); match tags.1.binary_search(&path) {
std::fs::write(tagfile_path, tags_str)?; Ok(_) => {},
Err(inspos) => tags.1.insert(inspos, path)
};
},
false => {
match tags.1.binary_search(&path) {
Ok(delpos) => { tags.1.remove(delpos); },
Err(_) => {}
};
}
}
let tagstr = tags.1.iter()
.fold(std::ffi::OsString::new(), |mut s, f| {
s.push(f);
s.push("\n");
s
});
std::fs::write(tagfile_path, tagstr.as_bytes())?;
Ok(()) Ok(())
}); });
Ok(()) Ok(())