Skip to content

Commit 3c37c5c

Browse files
committed
fix(list_buckets): add creation time and refactor the code
1 parent 157148c commit 3c37c5c

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

src/cas/bucket_meta.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::{errors::FsError, fs::PTR_SIZE};
2-
use chrono::{SecondsFormat, TimeZone, Utc};
3-
use s3_server::dto::Bucket;
4-
use std::convert::{TryFrom, TryInto};
2+
use chrono::Utc;
3+
use std::{
4+
convert::{TryFrom, TryInto},
5+
time::{SystemTime, UNIX_EPOCH},
6+
};
57

68
#[derive(Debug)]
79
pub struct BucketMeta {
@@ -10,29 +12,23 @@ pub struct BucketMeta {
1012
}
1113

1214
impl BucketMeta {
13-
pub fn new(ctime: i64, name: String) -> Self {
14-
Self { ctime, name }
15+
pub fn new(name: String) -> Self {
16+
Self {
17+
ctime: Utc::now().timestamp(),
18+
name,
19+
}
1520
}
1621

17-
pub fn ctime(&self) -> i64 {
18-
self.ctime
22+
pub fn ctime(&self) -> SystemTime {
23+
UNIX_EPOCH + std::time::Duration::from_secs(self.ctime as u64)
1924
}
2025

2126
pub fn name(&self) -> &str {
2227
&self.name
2328
}
24-
}
2529

26-
impl From<BucketMeta> for Bucket {
27-
fn from(bm: BucketMeta) -> Self {
28-
Bucket {
29-
creation_date: Some(
30-
Utc.timestamp_opt(bm.ctime, 0)
31-
.unwrap()
32-
.to_rfc3339_opts(SecondsFormat::Secs, true),
33-
),
34-
name: Some(bm.name),
35-
}
30+
pub fn to_vec(&self) -> Vec<u8> {
31+
self.into()
3632
}
3733
}
3834

src/cas/fs.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use super::{
99
object::Object,
1010
};
1111

12-
use chrono::Utc;
1312
use faster_hex::hex_string;
1413
use futures::{
1514
channel::mpsc::unbounded,
@@ -18,7 +17,7 @@ use futures::{
1817
stream::{StreamExt, TryStreamExt},
1918
};
2019
use md5::{Digest, Md5};
21-
use s3_server::dto::{Bucket, ByteStream};
20+
use s3_server::dto::ByteStream;
2221
use sled::{Db, Transactional};
2322
use std::{
2423
convert::{TryFrom, TryInto},
@@ -287,10 +286,7 @@ impl CasFS {
287286
pub fn create_bucket(&self, bucket_name: String) -> Result<(), MetaError> {
288287
let bucket_meta = self.get_tree(BUCKET_META_TREE)?;
289288

290-
let bm = Vec::from(&BucketMeta::new(
291-
Utc::now().timestamp(),
292-
bucket_name.clone(),
293-
));
289+
let bm = BucketMeta::new(bucket_name.clone()).to_vec();
294290

295291
match bucket_meta.insert(bucket_name, bm) {
296292
Ok(_) => Ok(()),
@@ -349,7 +345,7 @@ impl CasFS {
349345
}
350346

351347
/// Get a list of all buckets in the system.
352-
pub fn get_buckets(&self) -> Result<Vec<Bucket>, MetaError> {
348+
pub fn list_buckets(&self) -> Result<Vec<BucketMeta>, MetaError> {
353349
let bucket_tree = match self.sled_bucket_meta_tree() {
354350
Ok(t) => t,
355351
Err(e) => return Err(MetaError::UnknownError(e.to_string())),
@@ -364,7 +360,7 @@ impl CasFS {
364360
};
365361
// unwrap here is fine as it means the db is corrupt
366362
let bucket_meta = BucketMeta::try_from(&*value).expect("Corrupted bucket metadata");
367-
Some(bucket_meta.into())
363+
Some(bucket_meta)
368364
})
369365
.collect();
370366
Ok(buckets)

src/s3fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,12 @@ impl S3 for S3FS {
406406
&self,
407407
_: S3Request<ListBucketsInput>,
408408
) -> S3Result<S3Response<ListBucketsOutput>> {
409-
let csfs_buckets = try_!(self.casfs.get_buckets());
409+
let csfs_buckets = try_!(self.casfs.list_buckets());
410410
let mut buckets = Vec::with_capacity(csfs_buckets.len());
411411
for bucket in csfs_buckets {
412412
let bucket = Bucket {
413-
creation_date: None, //creation_date: bucket.creation_date, TODO: fix it
414-
name: bucket.name,
413+
creation_date: Some(Timestamp::from(bucket.ctime())),
414+
name: Some(bucket.name().into()),
415415
};
416416
buckets.push(bucket);
417417
}

0 commit comments

Comments
 (0)