Skip to content

Commit

Permalink
Add metadata and headers to Object Store
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <[email protected]>
  • Loading branch information
Jarema committed Nov 27, 2024
1 parent 546f2f0 commit 23bb978
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
20 changes: 19 additions & 1 deletion async-nats/src/jetstream/object_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// limitations under the License.

//! Object Store module
use std::collections::VecDeque;
use std::collections::{HashMap, VecDeque};
use std::fmt::Display;
use std::{cmp, str::FromStr, task::Poll, time::Duration};

Expand Down Expand Up @@ -358,6 +358,8 @@ impl ObjectStore {
digest: Some(format!("SHA-256={}", URL_SAFE.encode(digest))),
modified: Some(OffsetDateTime::now_utc()),
deleted: false,
metadata: object_meta.metadata,
headers: object_meta.headers,
};

let mut headers = HeaderMap::new();
Expand Down Expand Up @@ -722,6 +724,8 @@ impl ObjectStore {
modified: Some(OffsetDateTime::now_utc()),
digest: None,
deleted: false,
metadata: HashMap::default(),
headers: None,
};
publish_meta(self, &info).await?;
Ok(info)
Expand Down Expand Up @@ -785,6 +789,8 @@ impl ObjectStore {
modified: Some(OffsetDateTime::now_utc()),
digest: None,
deleted: false,
metadata: HashMap::default(),
headers: None,
};
publish_meta(self, &info).await?;
Ok(info)
Expand Down Expand Up @@ -1072,6 +1078,12 @@ pub struct ObjectInfo {
/// A short human readable description of the object.
#[serde(default)]
pub description: Option<String>,
/// Metadata for given object.
#[serde(default)]
pub metadata: HashMap<String, String>,
/// Headers for given object.
#[serde(default)]
pub headers: Option<HeaderMap>,
/// Link this object points to, if any.
#[serde(default)]
pub options: Option<ObjectOptions>,
Expand Down Expand Up @@ -1127,6 +1139,10 @@ pub struct ObjectMetadata {
pub description: Option<String>,
/// Max chunk size. Default is 128k.
pub chunk_size: Option<usize>,
/// Metadata for given object.
pub metadata: HashMap<String, String>,
/// Headers for given object.
pub headers: Option<HeaderMap>,
}

impl From<&str> for ObjectMetadata {
Expand Down Expand Up @@ -1158,6 +1174,8 @@ impl From<ObjectInfo> for ObjectMetadata {
ObjectMetadata {
name: info.name,
description: info.description,
metadata: info.metadata,
headers: info.headers,
chunk_size: None,
}
}
Expand Down
32 changes: 26 additions & 6 deletions async-nats/tests/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
// limitations under the License.

mod object_store {
use std::{io, time::Duration};

use async_nats::jetstream::{
object_store::{AddLinkErrorKind, ObjectMetadata, UpdateMetadata},
stream::DirectGetErrorKind,
use std::{collections::HashMap, io, time::Duration};

use async_nats::{
jetstream::{
object_store::{AddLinkErrorKind, ObjectMetadata, UpdateMetadata},
stream::DirectGetErrorKind,
},
HeaderMap,
};
use base64::Engine;
use futures::StreamExt;
Expand Down Expand Up @@ -88,6 +91,7 @@ mod object_store {
name: "BAR".to_string(),
description: Some("custom object".to_string()),
chunk_size: Some(64 * 1024),
..Default::default()
},
&mut bytes.as_slice(),
)
Expand Down Expand Up @@ -217,8 +221,21 @@ mod object_store {
.await
.unwrap();

let metadata = HashMap::from([("foo".to_string(), "bar".to_string())]);
let mut headers = HeaderMap::new();
headers.append("foo", "bar");

bucket
.put("FOO", &mut io::Cursor::new(vec![2, 3, 4, 5]))
.put(
ObjectMetadata {
name: "FOO".to_string(),
description: Some("description".to_string()),
chunk_size: None,
metadata: metadata.clone(),
headers: Some(headers.clone()),
},
&mut io::Cursor::new(vec![2, 3, 4, 5]),
)
.await
.unwrap();

Expand All @@ -228,6 +245,8 @@ mod object_store {
assert_eq!(info.name, "FOO");
assert_eq!(info.bucket, "bucket");
assert_eq!(info.size, 4);
assert_eq!(info.metadata, metadata);
assert_eq!(info.headers, Some(headers));
assert!(!info.deleted);

let modified = info.modified;
Expand Down Expand Up @@ -375,6 +394,7 @@ mod object_store {
name: "Foo".to_string(),
description: Some("foo desc".to_string()),
chunk_size: None,
..Default::default()
},
&mut "dadada".as_bytes(),
)
Expand Down

0 comments on commit 23bb978

Please sign in to comment.