Skip to content

Commit 59ca315

Browse files
committed
Update deom
1 parent f4a2efe commit 59ca315

File tree

3 files changed

+33
-63
lines changed

3 files changed

+33
-63
lines changed

crates/catalog/loader/src/lib.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::future::Future;
23
use std::pin::Pin;
34
use std::sync::Arc;
@@ -8,34 +9,13 @@ use iceberg_catalog_rest::RestCatalogBuilder;
89
type BoxedCatalogBuilderFuture = Pin<Box<dyn Future<Output = Result<Arc<dyn Catalog>>>>>;
910

1011
pub trait BoxedCatalogBuilder {
11-
fn name(&mut self, name: String);
12-
fn uri(&mut self, uri: String);
13-
fn warehouse(&mut self, warehouse: String);
14-
fn with_prop(&mut self, key: String, value: String);
15-
16-
fn build(self: Box<Self>) -> BoxedCatalogBuilderFuture;
12+
fn load(self: Box<Self>, name: String, props: HashMap<String, String>) -> BoxedCatalogBuilderFuture;
1713
}
1814

1915
impl<T: CatalogBuilder + 'static> BoxedCatalogBuilder for T {
20-
fn name(&mut self, name: String) {
21-
self.name(name);
22-
}
23-
24-
fn uri(&mut self, uri: String) {
25-
self.uri(uri);
26-
}
27-
28-
fn warehouse(&mut self, warehouse: String) {
29-
self.warehouse(warehouse);
30-
}
31-
32-
fn with_prop(&mut self, key: String, value: String) {
33-
self.with_prop(key, value);
34-
}
35-
36-
fn build(self: Box<Self>) -> BoxedCatalogBuilderFuture {
16+
fn load(self: Box<Self>, name: String, props: HashMap<String, String>) -> BoxedCatalogBuilderFuture {
3717
let builder = *self;
38-
Box::pin(async move { Ok(Arc::new(builder.build().await.unwrap()) as Arc<dyn Catalog>) })
18+
Box::pin(async move { Ok(Arc::new(builder.load(name, props).await.unwrap()) as Arc<dyn Catalog>) })
3919
}
4020
}
4121

@@ -51,14 +31,14 @@ pub fn load(r#type: &str) -> Result<Box<dyn BoxedCatalogBuilder>> {
5131

5232
#[cfg(test)]
5333
mod tests {
34+
use std::collections::HashMap;
5435
use crate::load;
5536

5637
#[tokio::test]
5738
async fn test_load() {
5839
let mut catalog = load("rest").unwrap();
59-
catalog.name("rest".to_string());
60-
catalog.with_prop("key".to_string(), "value".to_string());
61-
62-
catalog.build().await.unwrap();
40+
catalog.load("rest".to_string(), HashMap::from(
41+
[("key".to_string(), "value".to_string())]
42+
)).await.unwrap();
6343
}
6444
}

crates/catalog/rest/src/catalog.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ use crate::types::{
4545
RenameTableRequest,
4646
};
4747

48+
const REST_CATALOG_PROP_URI: &str = "uri";
49+
const REST_CATALOG_PROP_WAREHOUSE: &str = "warehouse";
4850
const ICEBERG_REST_SPEC_VERSION: &str = "0.14.1";
4951
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
5052
const PATH_V1: &str = "v1";
@@ -245,27 +247,22 @@ impl Default for RestCatalogBuilder {
245247
impl CatalogBuilder for RestCatalogBuilder {
246248
type C = RestCatalog;
247249

248-
fn name(&mut self, name: impl Into<String>) -> &mut Self {
250+
fn load(mut self, name: impl Into<String>, props: HashMap<String, String>) -> impl Future<Output=Result<Self::C>> {
249251
self.0.name = Some(name.into());
250-
self
251-
}
252-
253-
fn uri(&mut self, uri: impl Into<String>) -> &mut Self {
254-
self.0.uri = uri.into();
255-
self
256-
}
257-
258-
fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self {
259-
self.0.warehouse = Some(warehouse.into());
260-
self
261-
}
262-
263-
fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
264-
self.0.props.insert(key.into(), value.into());
265-
self
266-
}
252+
if props.contains_key(REST_CATALOG_PROP_URI) {
253+
self.0.uri = props
254+
.get(REST_CATALOG_PROP_URI)
255+
.cloned()
256+
.unwrap_or_default();
257+
}
258+
259+
if props.contains_key(REST_CATALOG_PROP_WAREHOUSE) {
260+
self.0.warehouse = props
261+
.get(REST_CATALOG_PROP_WAREHOUSE)
262+
.cloned()
263+
.map(|s| s.into());
264+
}
267265

268-
fn build(self) -> impl Future<Output = Result<Self::C>> {
269266
let result = {
270267
if self.0.name.is_none() {
271268
Err(Error::new(
@@ -283,6 +280,7 @@ impl CatalogBuilder for RestCatalogBuilder {
283280
};
284281

285282
std::future::ready(result)
283+
286284
}
287285
}
288286

@@ -2332,12 +2330,12 @@ mod tests {
23322330
#[tokio::test]
23332331
async fn test_create_rest_catalog() {
23342332
let mut builder = RestCatalogBuilder::default();
2335-
builder
2336-
.name("test")
2337-
.uri("http://localhost:8080")
2338-
.with_client(Client::new())
2339-
.with_prop("a", "b");
2340-
let catalog = builder.build().await;
2333+
builder.with_client(Client::new());
2334+
2335+
let catalog = builder.load("test", HashMap::from([
2336+
("uri".to_string(), "http://localhost:8080".to_string()),
2337+
("a".to_string(), "b".to_string()),
2338+
])).await;
23412339

23422340
assert!(catalog.is_ok());
23432341
}

crates/iceberg/src/catalog/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,8 @@ pub trait Catalog: Debug + Sync + Send {
101101
pub trait CatalogBuilder: Default + Debug + Send + Sync {
102102
/// The catalog type that this builder creates.
103103
type C: Catalog;
104-
/// Configure the name of the catalog.
105-
fn name(&mut self, name: impl Into<String>) -> &mut Self;
106-
/// Configure uri of the catalog.
107-
fn uri(&mut self, uri: impl Into<String>) -> &mut Self;
108-
/// Configure the warehouse location of the catalog.
109-
fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self;
110-
/// Configure properties of the catalog.
111-
fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self;
112-
/// Create the catalog
113-
fn build(self) -> impl Future<Output = Result<Self::C>>;
104+
/// Create a new catalog instance.
105+
fn load(self, name: impl Into<String>, props: HashMap<String, String>) -> impl Future<Output = Result<Self::C>>;
114106
}
115107

116108
/// NamespaceIdent represents the identifier of a namespace in the catalog.

0 commit comments

Comments
 (0)