Skip to content

Commit a69c073

Browse files
committed
[refact]: for scorpio
Signed-off-by: Han Xiaoyang <lux1an@qq.com>
1 parent 04d3242 commit a69c073

10 files changed

Lines changed: 355 additions & 332 deletions

File tree

mercury/src/hash.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::{fmt::Display, io};
77

8+
use bincode::{Encode, Decode};
89
use colored::Colorize;
910
use serde::{Deserialize, Serialize};
1011
use sha1::Digest;
@@ -28,6 +29,7 @@ use crate::internal::object::types::ObjectType;
2829
///
2930
#[derive(
3031
Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Deserialize, Serialize,
32+
Encode, Decode
3133
)]
3234
pub struct SHA1(pub [u8; 20]);
3335

mercury/src/internal/object/commit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::hash::SHA1;
1919
use crate::internal::object::signature::Signature;
2020
use crate::internal::object::ObjectTrait;
2121
use crate::internal::object::ObjectType;
22+
use bincode::{Decode, Encode};
2223
use bstr::ByteSlice;
2324
use callisto::git_commit;
2425
use callisto::mega_commit;
@@ -35,7 +36,7 @@ use serde::Serialize;
3536
/// history of a repository with a single commit object at its root.
3637
/// - The author and committer fields contain the name, email address, timestamp and timezone.
3738
/// - The message field contains the commit message, which maybe include signed or DCO.
38-
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
39+
#[derive(Eq, Debug, Clone, Serialize, Deserialize, Decode, Encode)]
3940
#[non_exhaustive]
4041
pub struct Commit {
4142
pub id: SHA1,

mercury/src/internal/object/signature.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//!
1212
use std::{fmt::Display, str::FromStr};
1313

14+
use bincode::{Decode, Encode};
1415
use bstr::ByteSlice;
1516
use chrono::Offset;
1617
use serde::{Deserialize, Serialize};
@@ -30,7 +31,7 @@ use crate::errors::GitError;
3031
/// ```
3132
///
3233
/// So, we design a `SignatureType` enum to indicate the signature type.
33-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
34+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Decode, Encode)]
3435
pub enum SignatureType {
3536
Author,
3637
Committer,
@@ -75,7 +76,7 @@ impl SignatureType {
7576
}
7677
}
7778

78-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
79+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize,Decode,Encode)]
7980
pub struct Signature {
8081
pub signature_type: SignatureType,
8182
pub name: String,

mercury/src/internal/object/tree.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::errors::GitError;
1818
use crate::hash::SHA1;
1919
use crate::internal::object::ObjectTrait;
2020
use crate::internal::object::ObjectType;
21+
use bincode::{Encode, Decode};
2122
use colored::Colorize;
2223
use encoding_rs::GBK;
2324
use serde::Deserialize;
@@ -28,7 +29,7 @@ use std::fmt::Display;
2829
/// that entry. The mode is a three-digit octal number that encodes both the permissions and the
2930
/// type of the object. The first digit specifies the object type, and the remaining two digits
3031
/// specify the file mode or permissions.
31-
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, Hash)]
32+
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, Hash, Encode, Decode)]
3233
pub enum TreeItemMode {
3334
Blob,
3435
BlobExecutable,
@@ -129,7 +130,7 @@ impl TreeItemMode {
129130
/// 100644 hello-world\0<blob object ID>
130131
/// 040000 data\0<tree object ID>
131132
/// ```
132-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Hash)]
133+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Hash, Encode, Decode)]
133134
pub struct TreeItem {
134135
pub mode: TreeItemMode,
135136
pub id: SHA1,
@@ -220,7 +221,7 @@ impl TreeItem {
220221

221222
/// A tree object is a Git object that represents a directory. It contains a list of entries, one
222223
/// for each file or directory in the tree.
223-
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
224+
#[derive(Eq, Debug, Clone, Serialize, Deserialize, Encode, Decode)]
224225
#[non_exhaustive]
225226
pub struct Tree {
226227
pub id: SHA1,

scorpio/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ once_cell = "1.19.0"
2828
arc-swap = "1.7.1"
2929
env_logger = "0.11.5"
3030
sled = "0.34.7"
31-
bincode = "2.0.1"
31+
bincode = { workspace = true , features = ["serde"] }
3232
async-recursion = "1.1.1"
3333
bytes = "1.7.2"
3434
futures = "0.3.31"

scorpio/src/dicfuse/tree_store.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::util::{config, GPath};
2+
use bincode::{Decode, Encode};
23
use rfuse3::raw::reply::ReplyEntry;
34
use rfuse3::FileType;
45
use serde::{Deserialize, Serialize};
@@ -14,7 +15,7 @@ pub struct TreeStorage {
1415
db: Db,
1516
}
1617

17-
#[derive(Serialize, Deserialize, Clone)]
18+
#[derive(Serialize, Deserialize, Clone,Encode,Decode)]
1819
pub struct StorageItem {
1920
inode: u64,
2021
parent: u64,
@@ -80,12 +81,12 @@ impl TreeStorage {
8081
children: Vec::new(),
8182
hash: item.hash,
8283
};
83-
84+
let config = bincode::config::standard();
8485
// Insert an item into db and update the parent item's children list.
8586
self.db
8687
.insert(
8788
inode.to_be_bytes(),
88-
bincode::serialize(&storage_item).map_err(Error::other)?,
89+
bincode::encode_to_vec(&storage_item,config).map_err(Error::other)?,
8990
)
9091
.map_err(Error::other)?;
9192

@@ -97,7 +98,7 @@ impl TreeStorage {
9798
self.db
9899
.insert(
99100
parent.to_be_bytes(),
100-
bincode::serialize(&parent_item).map_err(Error::other)?,
101+
bincode::encode_to_vec(&parent_item,config).map_err(Error::other)?,
101102
)
102103
.map_err(Error::other)?;
103104
}
@@ -122,10 +123,12 @@ impl TreeStorage {
122123
if storage_item.parent != 0 {
123124
let mut parent_item: StorageItem = self.get_storage_item(storage_item.parent)?;
124125
parent_item.children.retain(|&x| x != inode);
126+
let config = bincode::config::standard();
127+
125128
self.db
126129
.insert(
127130
storage_item.parent.to_be_bytes(),
128-
bincode::serialize(&parent_item).map_err(Error::other)?,
131+
bincode::encode_to_vec(&parent_item, config).map_err(Error::other)?,
129132
)
130133
.map_err(Error::other)?;
131134
}
@@ -140,10 +143,11 @@ impl TreeStorage {
140143
pub fn append_child(&self, parent: u64, inode: u64) -> io::Result<()> {
141144
let mut st = self.get_storage_item(parent)?;
142145
st.children.push(inode);
146+
let config = bincode::config::standard();
143147
self.db
144148
.insert(
145149
parent.to_be_bytes(),
146-
bincode::serialize(&st).map_err(Error::other)?,
150+
bincode::encode_to_vec(&st, config).map_err(Error::other)?,
147151
)
148152
.map_err(Error::other)?;
149153
Ok(())
@@ -163,7 +167,8 @@ impl TreeStorage {
163167
pub fn get_storage_item(&self, inode: u64) -> io::Result<StorageItem> {
164168
match self.db.get(inode.to_be_bytes())? {
165169
Some(value) => {
166-
let item: StorageItem = bincode::deserialize(&value).map_err(Error::other)?;
170+
let config = bincode::config::standard();
171+
let (item ,_) = bincode::decode_from_slice(&value,config).map_err(Error::other)?;
167172
Ok(item)
168173
}
169174
None => Err(Error::new(ErrorKind::NotFound, "Item not found")),
@@ -184,10 +189,11 @@ impl TreeStorage {
184189
pub fn update_item_hash(&self, inode: u64, hash: String) -> io::Result<()> {
185190
let mut item = self.get_storage_item(inode)?;
186191
item.hash = hash;
192+
let config = bincode::config::standard();
187193
self.db
188194
.insert(
189195
inode.to_be_bytes(),
190-
bincode::serialize(&item).map_err(Error::other)?,
196+
bincode::encode_to_vec(&item,config).map_err(Error::other)?,
191197
)
192198
.map_err(Error::other)?;
193199
Ok(())

scorpio/src/manager/commit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn build_new_tree_map(
108108
Err(_) => {
109109
#[cfg(debug_assertions)]
110110
color_info!("New Tree: \x1b[1;32m{}\x1b[0m", parent_path.display());
111-
Tree::from_tree_items(vec![])
111+
Tree::from_tree_items(vec![]).unwrap()
112112
}
113113
};
114114
// Update the new TreeItem
@@ -274,10 +274,11 @@ pub fn commit_core(
274274
// overwrite operation, we can update it
275275
// with confidence.
276276
let mut batch = sled::Batch::default();
277+
let config = bincode::config::standard();
277278
for (path, tree) in hashmap.iter() {
278279
batch.insert(
279280
path.to_string_lossy().into_owned().as_str(),
280-
bincode::serialize(tree).unwrap(),
281+
bincode::encode_to_vec(tree,config).unwrap(),
281282
);
282283
}
283284
new_tree_db.apply_batch(batch)?;

scorpio/src/manager/diff.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn diff(
9393
// just compute the NEW hash first.
9494
let content = std::fs::read(&node)?;
9595
item.id = SHA1::from_type_and_data(ObjectType::Blob, &content);
96-
blobs.push(Blob::from_content(content));
96+
blobs.push(Blob::from_content_bytes(content));
9797
}
9898
break;
9999
}
@@ -118,7 +118,7 @@ pub fn diff(
118118
} else {
119119
//is a file.
120120
let content = std::fs::read(&node)?;
121-
let b = Blob::from_content(content);
121+
let b = Blob::from_content_bytes(content);
122122
t.tree_items.push(TreeItem {
123123
mode: TreeItemMode::Blob,
124124
id: b.id,
@@ -147,10 +147,7 @@ pub fn change(
147147
} else {
148148
// there is a new dictionary.
149149
println!("new tree:{tree_path:?}");
150-
tree = Tree {
151-
id: SHA1::default(),
152-
tree_items: vec![],
153-
};
150+
tree = Tree::from_tree_items(vec![]).unwrap();
154151
}
155152

156153
let entries = std::fs::read_dir(real_path).unwrap();
@@ -185,7 +182,7 @@ pub fn change(
185182
println!("change: changed file {}", item.name);
186183
let content = std::fs::read(&path).unwrap();
187184

188-
let b = Blob::from_content(content);
185+
let b = Blob::from_content_bytes(content);
189186
item.id = b.id; // change file hash .
190187
blobs.push(b);
191188
}
@@ -211,7 +208,7 @@ pub fn change(
211208
} else {
212209
println!("change: new file {name}");
213210
let content = std::fs::read(&path).unwrap();
214-
let b = Blob::from_content(content);
211+
let b = Blob::from_content_bytes(content);
215212
tree.tree_items.push(TreeItem {
216213
mode: TreeItemMode::Blob,
217214
id: b.id,

scorpio/src/manager/fetch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::scolfs;
44
use crate::util::config;
55
use crate::util::GPath;
66
use async_recursion::async_recursion;
7-
use axum::async_trait;
87
use ceres::model::git::LatestCommitInfo;
98
use crossbeam::queue::SegQueue;
109
use futures::future::join_all;
@@ -351,8 +350,9 @@ pub async fn download_mr_files(
351350

352351
Ok(())
353352
}
354-
#[allow(unused)]
355-
#[async_trait]
353+
354+
355+
#[allow(async_fn_in_trait)]
356356
pub trait CheckHash {
357357
async fn check(&mut self);
358358

@@ -363,7 +363,7 @@ pub trait CheckHash {
363363
) -> WorkDir;
364364
}
365365

366-
#[async_trait]
366+
367367
impl CheckHash for ScorpioManager {
368368
async fn check(&mut self) {
369369
let mut handlers = Vec::new();

0 commit comments

Comments
 (0)