为Blob增加压缩支持,IO优化放置到Store中

This commit is contained in:
HouXiaoxuan
2023-12-28 22:02:02 +08:00
parent cdc289b3bb
commit b3aea1d0d1
4 changed files with 51 additions and 69 deletions

View File

@@ -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!("储存库疑似损坏,无法写入文件"),