Add functions to get files from Store by certain predicated (id, match, tags)
This commit is contained in:
parent
bbd11086d8
commit
e84986680d
1 changed files with 82 additions and 34 deletions
|
@ -1,7 +1,12 @@
|
||||||
use std::fmt::{Debug, Display, Formatter};
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::ops::Deref;
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
use runtime::Runtime;
|
use runtime::Runtime;
|
||||||
use module::Module;
|
use module::Module;
|
||||||
|
@ -9,12 +14,16 @@ use module::Module;
|
||||||
use storage::Store;
|
use storage::Store;
|
||||||
use storage::file::hash::FileHash;
|
use storage::file::hash::FileHash;
|
||||||
use storage::file::id::FileID;
|
use storage::file::id::FileID;
|
||||||
|
use storage::file::File;
|
||||||
use storage::parser::FileHeaderParser;
|
use storage::parser::FileHeaderParser;
|
||||||
use storage::parser::Parser;
|
use storage::parser::Parser;
|
||||||
use storage::json::parser::JsonHeaderParser;
|
use storage::json::parser::JsonHeaderParser;
|
||||||
|
|
||||||
mod header;
|
mod header;
|
||||||
|
|
||||||
|
use self::header::get_url_from_header;
|
||||||
|
use self::header::get_tags_from_header;
|
||||||
|
|
||||||
pub struct BM<'a> {
|
pub struct BM<'a> {
|
||||||
rt: &'a Runtime<'a>,
|
rt: &'a Runtime<'a>,
|
||||||
}
|
}
|
||||||
|
@ -65,8 +74,6 @@ impl<'a> BM<'a> {
|
||||||
fn validate_url<HP>(&self, url: &String, parser: &Parser<HP>) -> bool
|
fn validate_url<HP>(&self, url: &String, parser: &Parser<HP>) -> bool
|
||||||
where HP: FileHeaderParser
|
where HP: FileHeaderParser
|
||||||
{
|
{
|
||||||
use self::header::get_url_from_header;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use util::is_url;
|
use util::is_url;
|
||||||
|
|
||||||
if !is_url(url) {
|
if !is_url(url) {
|
||||||
|
@ -95,8 +102,6 @@ impl<'a> BM<'a> {
|
||||||
|
|
||||||
fn command_list(&self, matches: &ArgMatches) -> bool {
|
fn command_list(&self, matches: &ArgMatches) -> bool {
|
||||||
use ui::file::{FilePrinter, TablePrinter};
|
use ui::file::{FilePrinter, TablePrinter};
|
||||||
use self::header::get_url_from_header;
|
|
||||||
use self::header::get_tags_from_header;
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
let parser = Parser::new(JsonHeaderParser::new(None));
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
|
@ -154,48 +159,79 @@ impl<'a> BM<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_by_hash(&self, hash: FileHash) -> bool {
|
fn remove_by_hash(&self, hash: FileHash) -> bool {
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
debug!("Removing for hash = '{:?}'", hash);
|
debug!("Removing for hash = '{:?}'", hash);
|
||||||
let parser = Parser::new(JsonHeaderParser::new(None));
|
|
||||||
|
|
||||||
let file = self.rt.store().load_by_hash(self, &parser, hash);
|
self.get_files_by_id(hash)
|
||||||
debug!("file = {:?}", file);
|
.iter()
|
||||||
file.map(|file| {
|
.map(|file| {
|
||||||
debug!("File loaded, can remove now: {:?}", file);
|
debug!("File loaded, can remove now: {:?}", file);
|
||||||
let f = file.deref().borrow();
|
let f = file.deref().borrow();
|
||||||
self.rt.store().remove(f.id().clone())
|
self.rt.store().remove(f.id().clone())
|
||||||
}).unwrap_or(false)
|
})
|
||||||
|
.all(|x| x)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_by_tags(&self, tags: Vec<String>) -> bool {
|
fn remove_by_tags(&self, tags: Vec<String>) -> bool {
|
||||||
use std::fs::remove_file;
|
use std::fs::remove_file;
|
||||||
use std::ops::Deref;
|
|
||||||
use self::header::get_tags_from_header;
|
|
||||||
|
|
||||||
let parser = Parser::new(JsonHeaderParser::new(None));
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
self.rt
|
self.get_files_by_tags(tags)
|
||||||
.store()
|
|
||||||
.load_for_module(self, &parser)
|
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|file| {
|
.map(|file| {
|
||||||
let f = file.deref().borrow();
|
|
||||||
get_tags_from_header(f.header()).iter().any(|tag| {
|
|
||||||
tags.iter().any(|remtag| remtag == tag)
|
|
||||||
})
|
|
||||||
}).map(|file| {
|
|
||||||
let f = file.deref().borrow();
|
let f = file.deref().borrow();
|
||||||
self.rt.store().remove(f.id().clone())
|
self.rt.store().remove(f.id().clone())
|
||||||
}).all(|x| x)
|
}).all(|x| x)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_by_match(&self, matcher: String) -> bool {
|
fn remove_by_match(&self, matcher: String) -> bool {
|
||||||
use self::header::get_url_from_header;
|
|
||||||
use std::fs::remove_file;
|
use std::fs::remove_file;
|
||||||
use std::ops::Deref;
|
|
||||||
use std::process::exit;
|
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
|
self.get_files_by_match(matcher)
|
||||||
|
.iter()
|
||||||
|
.map(|file| {
|
||||||
|
let f = file.deref().borrow();
|
||||||
|
self.rt.store().remove(f.id().clone())
|
||||||
|
}).all(|x| x)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_files(&self,
|
||||||
|
matches: &ArgMatches,
|
||||||
|
id_key: &'static str,
|
||||||
|
match_key: &'static str,
|
||||||
|
tag_key: &'static str)
|
||||||
|
-> Vec<Rc<RefCell<File>>>
|
||||||
|
{
|
||||||
|
if matches.is_present(id_key) {
|
||||||
|
let hash = FileHash::from(matches.value_of(id_key).unwrap());
|
||||||
|
self.get_files_by_id(hash)
|
||||||
|
} else if matches.is_present(match_key) {
|
||||||
|
let matcher = String::from(matches.value_of(match_key).unwrap());
|
||||||
|
self.get_files_by_match(matcher)
|
||||||
|
} else if matches.is_present(tag_key) {
|
||||||
|
let tags = matches.value_of(tag_key)
|
||||||
|
.unwrap()
|
||||||
|
.split(",")
|
||||||
|
.map(String::from)
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
self.get_files_by_tags(tags)
|
||||||
|
} else {
|
||||||
|
// get all files
|
||||||
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
|
self.rt.store().load_for_module(self, &parser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_files_by_id(&self, hash: FileHash) -> Vec<Rc<RefCell<File>>> {
|
||||||
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
|
self.rt
|
||||||
|
.store()
|
||||||
|
.load_by_hash(self, &parser, hash)
|
||||||
|
.map(|f| vec![f])
|
||||||
|
.unwrap_or(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_files_by_match(&self, matcher: String) -> Vec<Rc<RefCell<File>>> {
|
||||||
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
let re = Regex::new(&matcher[..]).unwrap_or_else(|e| {
|
let re = Regex::new(&matcher[..]).unwrap_or_else(|e| {
|
||||||
error!("Cannot build regex out of '{}'", matcher);
|
error!("Cannot build regex out of '{}'", matcher);
|
||||||
error!("{}", e);
|
error!("{}", e);
|
||||||
|
@ -204,11 +240,10 @@ impl<'a> BM<'a> {
|
||||||
|
|
||||||
debug!("Compiled '{}' to regex: '{:?}'", matcher, re);
|
debug!("Compiled '{}' to regex: '{:?}'", matcher, re);
|
||||||
|
|
||||||
let parser = Parser::new(JsonHeaderParser::new(None));
|
|
||||||
self.rt
|
self.rt
|
||||||
.store()
|
.store()
|
||||||
.load_for_module(self, &parser)
|
.load_for_module(self, &parser)
|
||||||
.iter()
|
.into_iter()
|
||||||
.filter(|file| {
|
.filter(|file| {
|
||||||
let f = file.deref().borrow();
|
let f = file.deref().borrow();
|
||||||
let url = get_url_from_header(f.header());
|
let url = get_url_from_header(f.header());
|
||||||
|
@ -217,10 +252,23 @@ impl<'a> BM<'a> {
|
||||||
debug!("Matching '{}' ~= '{}'", re.as_str(), u);
|
debug!("Matching '{}' ~= '{}'", re.as_str(), u);
|
||||||
re.is_match(&u[..])
|
re.is_match(&u[..])
|
||||||
}).unwrap_or(false)
|
}).unwrap_or(false)
|
||||||
}).map(|file| {
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_files_by_tags(&self, tags: Vec<String>) -> Vec<Rc<RefCell<File>>> {
|
||||||
|
let parser = Parser::new(JsonHeaderParser::new(None));
|
||||||
|
self.rt
|
||||||
|
.store()
|
||||||
|
.load_for_module(self, &parser)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|file| {
|
||||||
let f = file.deref().borrow();
|
let f = file.deref().borrow();
|
||||||
self.rt.store().remove(f.id().clone())
|
get_tags_from_header(f.header()).iter().any(|tag| {
|
||||||
}).all(|x| x)
|
tags.iter().any(|remtag| remtag == tag)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue