mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-07-04 18:26:05 +08:00
为Blob增加压缩支持,IO优化放置到Store中
This commit is contained in:
@@ -9,6 +9,9 @@ pub struct Store {
|
||||
store_path: PathBuf,
|
||||
}
|
||||
|
||||
/**Store负责管理objects
|
||||
* 每一个object文件名与内容的hash值相同
|
||||
*/
|
||||
impl Store {
|
||||
pub fn new() -> Store {
|
||||
util::check_repo_exist();
|
||||
@@ -26,13 +29,6 @@ impl Store {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, hash: &String) -> bool {
|
||||
let mut path = self.store_path.clone();
|
||||
path.push("objects");
|
||||
path.push(hash);
|
||||
path.exists()
|
||||
}
|
||||
|
||||
/// 将hash对应的文件内容(主要是blob)还原到file
|
||||
pub fn restore_to_file(&self, hash: &Hash, file: &PathBuf) {
|
||||
let content = self.load(hash);
|
||||
@@ -69,6 +65,7 @@ impl Store {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn save(&self, content: &String) -> String {
|
||||
/* 保存文件内容 */
|
||||
let hash = util::calc_hash(content);
|
||||
@@ -76,6 +73,10 @@ impl Store {
|
||||
path.push("objects");
|
||||
path.push(&hash);
|
||||
// println!("Saved to: [{}]", path.display());
|
||||
if path.exists() {
|
||||
// IO优化,文件已存在,不再写入
|
||||
return hash;
|
||||
}
|
||||
match std::fs::write(path, content) {
|
||||
Ok(_) => hash,
|
||||
Err(_) => panic!("储存库疑似损坏,无法写入文件"),
|
||||
|
||||
Reference in New Issue
Block a user