Skip to content

Commit

Permalink
fix(list_buckets): add creation time and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanbk committed Jan 20, 2025
1 parent 157148c commit 3c37c5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
32 changes: 14 additions & 18 deletions src/cas/bucket_meta.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::{errors::FsError, fs::PTR_SIZE};
use chrono::{SecondsFormat, TimeZone, Utc};
use s3_server::dto::Bucket;
use std::convert::{TryFrom, TryInto};
use chrono::Utc;
use std::{
convert::{TryFrom, TryInto},
time::{SystemTime, UNIX_EPOCH},
};

#[derive(Debug)]
pub struct BucketMeta {
Expand All @@ -10,29 +12,23 @@ pub struct BucketMeta {
}

impl BucketMeta {
pub fn new(ctime: i64, name: String) -> Self {
Self { ctime, name }
pub fn new(name: String) -> Self {
Self {
ctime: Utc::now().timestamp(),
name,
}
}

pub fn ctime(&self) -> i64 {
self.ctime
pub fn ctime(&self) -> SystemTime {
UNIX_EPOCH + std::time::Duration::from_secs(self.ctime as u64)
}

pub fn name(&self) -> &str {
&self.name
}
}

impl From<BucketMeta> for Bucket {
fn from(bm: BucketMeta) -> Self {
Bucket {
creation_date: Some(
Utc.timestamp_opt(bm.ctime, 0)
.unwrap()
.to_rfc3339_opts(SecondsFormat::Secs, true),
),
name: Some(bm.name),
}
pub fn to_vec(&self) -> Vec<u8> {
self.into()
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/cas/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use super::{
object::Object,
};

use chrono::Utc;
use faster_hex::hex_string;
use futures::{
channel::mpsc::unbounded,
Expand All @@ -18,7 +17,7 @@ use futures::{
stream::{StreamExt, TryStreamExt},
};
use md5::{Digest, Md5};
use s3_server::dto::{Bucket, ByteStream};
use s3_server::dto::ByteStream;
use sled::{Db, Transactional};
use std::{
convert::{TryFrom, TryInto},
Expand Down Expand Up @@ -287,10 +286,7 @@ impl CasFS {
pub fn create_bucket(&self, bucket_name: String) -> Result<(), MetaError> {
let bucket_meta = self.get_tree(BUCKET_META_TREE)?;

let bm = Vec::from(&BucketMeta::new(
Utc::now().timestamp(),
bucket_name.clone(),
));
let bm = BucketMeta::new(bucket_name.clone()).to_vec();

match bucket_meta.insert(bucket_name, bm) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -349,7 +345,7 @@ impl CasFS {
}

/// Get a list of all buckets in the system.
pub fn get_buckets(&self) -> Result<Vec<Bucket>, MetaError> {
pub fn list_buckets(&self) -> Result<Vec<BucketMeta>, MetaError> {
let bucket_tree = match self.sled_bucket_meta_tree() {
Ok(t) => t,
Err(e) => return Err(MetaError::UnknownError(e.to_string())),
Expand All @@ -364,7 +360,7 @@ impl CasFS {
};
// unwrap here is fine as it means the db is corrupt
let bucket_meta = BucketMeta::try_from(&*value).expect("Corrupted bucket metadata");
Some(bucket_meta.into())
Some(bucket_meta)
})
.collect();
Ok(buckets)
Expand Down
6 changes: 3 additions & 3 deletions src/s3fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,12 @@ impl S3 for S3FS {
&self,
_: S3Request<ListBucketsInput>,
) -> S3Result<S3Response<ListBucketsOutput>> {
let csfs_buckets = try_!(self.casfs.get_buckets());
let csfs_buckets = try_!(self.casfs.list_buckets());
let mut buckets = Vec::with_capacity(csfs_buckets.len());
for bucket in csfs_buckets {
let bucket = Bucket {
creation_date: None, //creation_date: bucket.creation_date, TODO: fix it
name: bucket.name,
creation_date: Some(Timestamp::from(bucket.ctime())),
name: Some(bucket.name().into()),
};
buckets.push(bucket);
}
Expand Down

0 comments on commit 3c37c5c

Please sign in to comment.