Skip to content

Commit 413ce4a

Browse files
committed
Add some comments
1 parent b98171f commit 413ce4a

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

src/kvstorage/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub(crate) trait KVStorageTrait {
4343
hash: &str,
4444
) -> Result<(), Box<dyn Error>> {
4545
let cnt = self.get_ref_count(bucket, hash).await?;
46+
if cnt == 0 {
47+
return Ok(());
48+
}
4649
self.set_ref_count(bucket, hash, cnt - 1).await
4750
}
4851

@@ -87,13 +90,20 @@ impl KVStorage {
8790
}
8891
}
8992

93+
/**
94+
* Setup the KV storage.
95+
*/
9096
pub async fn setup(&mut self) -> Result<(), Box<dyn Error>> {
9197
match self {
9298
KVStorage::Postgres(storage) => storage.setup().await,
9399
KVStorage::SQLite(storage) => storage.setup().await,
94100
}
95101
}
96102

103+
/**
104+
* Get the reference count for a hash.
105+
* If the hash does not exist, return 0.
106+
*/
97107
pub async fn get_ref_count(&mut self, bucket: &str, hash: &str) -> Result<i32, Box<dyn Error>> {
98108
debug!("Getting ref count for bucket: {}, hash: {}", bucket, hash);
99109
match self {
@@ -102,6 +112,9 @@ impl KVStorage {
102112
}
103113
}
104114

115+
/**
116+
* Set the reference count for a hash.
117+
*/
105118
pub async fn set_ref_count(
106119
&mut self,
107120
bucket: &str,
@@ -118,6 +131,9 @@ impl KVStorage {
118131
}
119132
}
120133

134+
/**
135+
* Increment the reference count for a hash.
136+
*/
121137
pub async fn increment_ref_count(
122138
&mut self,
123139
bucket: &str,
@@ -130,6 +146,10 @@ impl KVStorage {
130146
}
131147
}
132148

149+
/**
150+
* Decrement the reference count for a hash.
151+
* If the reference count is already 0, do nothing.
152+
*/
133153
pub async fn decrement_ref_count(
134154
&mut self,
135155
bucket: &str,
@@ -142,6 +162,10 @@ impl KVStorage {
142162
}
143163
}
144164

165+
/**
166+
* Get the modified time for a path.
167+
* If the path does not exist, return 0.
168+
*/
145169
pub async fn get_modified(&mut self, bucket: &str, path: &str) -> Result<i64, Box<dyn Error>> {
146170
debug!("Getting modified time for bucket: {}, path: {}", bucket, path);
147171
match self {
@@ -150,6 +174,9 @@ impl KVStorage {
150174
}
151175
}
152176

177+
/**
178+
* Set the modified time for a path.
179+
*/
153180
pub async fn set_modified(
154181
&mut self,
155182
bucket: &str,
@@ -166,6 +193,9 @@ impl KVStorage {
166193
}
167194
}
168195

196+
/**
197+
* Delete the modified time for a path.
198+
*/
169199
pub async fn delete_modified(
170200
&mut self,
171201
bucket: &str,
@@ -178,6 +208,10 @@ impl KVStorage {
178208
}
179209
}
180210

211+
/**
212+
* Get the reference file for a path.
213+
* If the path does not exist, return an empty string.
214+
*/
181215
pub async fn get_ref_file(
182216
&mut self,
183217
bucket: &str,
@@ -190,6 +224,9 @@ impl KVStorage {
190224
}
191225
}
192226

227+
/**
228+
* Set the reference file for a path.
229+
*/
193230
pub async fn set_ref_file(
194231
&mut self,
195232
bucket: &str,
@@ -206,6 +243,9 @@ impl KVStorage {
206243
}
207244
}
208245

246+
/**
247+
* Delete the reference file for a path.
248+
*/
209249
pub async fn delete_ref_file(
210250
&mut self,
211251
bucket: &str,

src/locks/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ use tracing::{debug, info};
33

44
pub mod memory;
55

6+
/**
7+
* Get key for lock on file
8+
*/
69
pub(crate) fn file_lock(bucket: &str, path: &str) -> String {
710
format!("file:{}:{}", bucket, path)
811
}
912

13+
/**
14+
* Get key for lock on hash
15+
*/
1016
fn hash_lock(bucket: &str, hash: &str) -> String {
1117
format!("hash:{}:{}", bucket, hash)
1218
}
@@ -42,6 +48,9 @@ impl LocksStorage {
4248
}
4349
}
4450

51+
/**
52+
* Acquire shared lock for key
53+
*/
4554
pub fn acquire_shared(&mut self, key: &str) {
4655
debug!("Acquiring shared lock for key: {}", key);
4756
match self {
@@ -51,6 +60,9 @@ impl LocksStorage {
5160
}
5261
}
5362

63+
/**
64+
* Acquire exclusive lock for key
65+
*/
5466
pub fn acquire_exclusive(&mut self, key: &str) {
5567
debug!("Acquiring exclusive lock for key: {}", key);
5668
match self {
@@ -60,6 +72,9 @@ impl LocksStorage {
6072
}
6173
}
6274

75+
/**
76+
* Release lock for key
77+
*/
6378
pub fn release(&mut self, key: &str) -> bool {
6479
debug!("Releasing lock for key: {}", key);
6580
match self {

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn main() {
5555
let app = Router::new()
5656
.route("/ft/version", get(ft_version))
5757
.route("/ft/files/{path}", put(ft_put_file))
58-
.layer(
58+
.layer( // Logging middleware
5959
TraceLayer::new_for_http()
6060
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
6161
.on_response(DefaultOnResponse::new().level(Level::INFO)),

src/routes/ft/put_file.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub async fn ft_put_file(
1515
) -> impl IntoResponse {
1616
debug!("timestamp: {}", query.last_modified);
1717
let timestamp = utils::conv_rfc2822_to_unix_timestamp(&query.last_modified);
18+
1819
if let Err(e) = timestamp {
1920
error!("Failed to parse last_modified: {}", e);
2021
return Response::builder()
@@ -37,6 +38,7 @@ pub async fn ft_put_file(
3738
}
3839
let current_modified = current_modified.unwrap();
3940

41+
// If the uploaded file is younger than the current one, return 200 OK
4042
if current_modified >= timestamp {
4143
state.locks.release(&*locks::file_lock(&state.bucket_name, &path));
4244
return Response::builder()

0 commit comments

Comments
 (0)