Skip to content

Commit 62a1958

Browse files
committed
Minor clean up
1 parent cedf7cc commit 62a1958

File tree

2 files changed

+76
-71
lines changed

2 files changed

+76
-71
lines changed

crates/catalog/inmemory/src/catalog.rs

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ impl Catalog for InMemoryCatalog {
8787
let root_namespace_state = self.root_namespace_state.lock().await;
8888

8989
match maybe_parent {
90-
None => Ok(root_namespace_state
91-
.list_top_level_namespaces()
92-
.into_iter()
93-
.map(|str| NamespaceIdent::new(str.to_string()))
94-
.collect_vec()),
90+
None => {
91+
let namespaces = root_namespace_state
92+
.list_top_level_namespaces()
93+
.into_iter()
94+
.map(|str| NamespaceIdent::new(str.to_string()))
95+
.collect_vec();
96+
97+
Ok(namespaces)
98+
}
9599
Some(parent_namespace_ident) => {
96100
let namespaces = root_namespace_state
97101
.list_namespaces_under(parent_namespace_ident)?
@@ -184,55 +188,44 @@ impl Catalog for InMemoryCatalog {
184188

185189
let table_name = table_creation.name.clone();
186190
let table_ident = TableIdent::new(namespace_ident.clone(), table_name);
187-
let table_exists = root_namespace_state.table_exists(&table_ident)?;
188-
189-
if table_exists {
190-
Err(Error::new(
191-
ErrorKind::Unexpected,
192-
format!(
193-
"Cannot create table {:?}. Table already exists",
194-
&table_ident
195-
),
196-
))
197-
} else {
198-
let (table_creation, location) = match table_creation.location.clone() {
199-
Some(location) => (table_creation, location),
200-
None => {
201-
let location = format!(
202-
"{}/{}/{}",
203-
self.default_table_root_location,
204-
table_ident.namespace().join("/"),
205-
table_ident.name()
206-
);
207-
208-
let new_table_creation = TableCreation {
209-
location: Some(location.clone()),
210-
..table_creation
211-
};
212-
213-
(new_table_creation, location)
214-
}
215-
};
216-
217-
let metadata = TableMetadataBuilder::from_table_creation(table_creation)?.build()?;
218-
let metadata_location = create_metadata_location(&location, 0)?;
219-
220-
self.file_io
221-
.new_output(&metadata_location)?
222-
.write(serde_json::to_vec(&metadata)?.into())
223-
.await?;
224-
225-
root_namespace_state.insert_new_table(&table_ident, metadata_location.clone())?;
226-
227-
let table = Table::builder()
228-
.file_io(self.file_io.clone())
229-
.metadata_location(metadata_location)
230-
.metadata(metadata)
231-
.identifier(table_ident)
232-
.build();
233-
234-
Ok(table)
235-
}
191+
192+
let (table_creation, location) = match table_creation.location.clone() {
193+
Some(location) => (table_creation, location),
194+
None => {
195+
let location = format!(
196+
"{}/{}/{}",
197+
self.default_table_root_location,
198+
table_ident.namespace().join("/"),
199+
table_ident.name()
200+
);
201+
202+
let new_table_creation = TableCreation {
203+
location: Some(location.clone()),
204+
..table_creation
205+
};
206+
207+
(new_table_creation, location)
208+
}
209+
};
210+
211+
let metadata = TableMetadataBuilder::from_table_creation(table_creation)?.build()?;
212+
let metadata_location = create_metadata_location(&location, 0)?;
213+
214+
self.file_io
215+
.new_output(&metadata_location)?
216+
.write(serde_json::to_vec(&metadata)?.into())
217+
.await?;
218+
219+
root_namespace_state.insert_new_table(&table_ident, metadata_location.clone())?;
220+
221+
let table = Table::builder()
222+
.file_io(self.file_io.clone())
223+
.metadata_location(metadata_location)
224+
.metadata(metadata)
225+
.identifier(table_ident)
226+
.build();
227+
228+
Ok(table)
236229
}
237230

238231
/// Load table from the catalog.
@@ -282,6 +275,7 @@ impl Catalog for InMemoryCatalog {
282275
new_root_namespace_state.remove_existing_table(src_table_ident)?;
283276
new_root_namespace_state.insert_new_table(dst_table_ident, metadata_location)?;
284277
*root_namespace_state = new_root_namespace_state;
278+
285279
Ok(())
286280
}
287281

@@ -604,7 +598,7 @@ mod tests {
604598
.unwrap_err()
605599
.to_string(),
606600
format!(
607-
"Unexpected => Cannot create namespace {:?}. Namespace already exists",
601+
"Unexpected => Cannot create namespace {:?}. Namespace already exists.",
608602
&namespace_ident
609603
)
610604
);
@@ -1092,7 +1086,7 @@ mod tests {
10921086
.unwrap_err()
10931087
.to_string(),
10941088
format!(
1095-
"Unexpected => Cannot create table {:?}. Table already exists",
1089+
"Unexpected => Cannot create table {:?}. Table already exists.",
10961090
&table_ident
10971091
)
10981092
);
@@ -1513,7 +1507,7 @@ mod tests {
15131507
.unwrap_err()
15141508
.to_string(),
15151509
format!(
1516-
"Unexpected => Cannot insert table {:? }. Table already exists.",
1510+
"Unexpected => Cannot create table {:? }. Table already exists.",
15171511
&dst_table_ident
15181512
),
15191513
);

crates/catalog/inmemory/src/namespace_state.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ fn no_such_table_err<T>(table_ident: &TableIdent) -> Result<T> {
4444
))
4545
}
4646

47+
fn namespace_already_exists_err<T>(namespace_ident: &NamespaceIdent) -> Result<T> {
48+
Err(Error::new(
49+
ErrorKind::Unexpected,
50+
format!(
51+
"Cannot create namespace {:?}. Namespace already exists.",
52+
namespace_ident
53+
),
54+
))
55+
}
56+
57+
fn table_already_exists_err<T>(table_ident: &TableIdent) -> Result<T> {
58+
Err(Error::new(
59+
ErrorKind::Unexpected,
60+
format!(
61+
"Cannot create table {:?}. Table already exists.",
62+
table_ident
63+
),
64+
))
65+
}
66+
4767
impl NamespaceState {
4868
// Creates a new namespace state
4969
pub(crate) fn new() -> Self {
@@ -157,19 +177,14 @@ impl NamespaceState {
157177
.namespaces
158178
.entry(child_namespace_name)
159179
{
160-
hash_map::Entry::Occupied(_) => Err(Error::new(
161-
ErrorKind::Unexpected,
162-
format!(
163-
"Cannot create namespace {:?}. Namespace already exists",
164-
namespace_ident
165-
),
166-
)),
180+
hash_map::Entry::Occupied(_) => namespace_already_exists_err(namespace_ident),
167181
hash_map::Entry::Vacant(entry) => {
168182
let _ = entry.insert(NamespaceState {
169183
properties,
170184
namespaces: HashMap::new(),
171185
table_metadata_locations: HashMap::new(),
172186
});
187+
173188
Ok(())
174189
}
175190
}
@@ -220,6 +235,7 @@ impl NamespaceState {
220235
) -> Result<()> {
221236
let properties = self.get_mut_properties(namespace_ident)?;
222237
*properties = new_properties;
238+
223239
Ok(())
224240
}
225241

@@ -266,15 +282,10 @@ impl NamespaceState {
266282
.table_metadata_locations
267283
.entry(table_ident.name().to_string())
268284
{
269-
hash_map::Entry::Occupied(_) => Err(Error::new(
270-
ErrorKind::Unexpected,
271-
format!(
272-
"Cannot insert table {:?}. Table already exists.",
273-
table_ident
274-
),
275-
)),
285+
hash_map::Entry::Occupied(_) => table_already_exists_err(table_ident),
276286
hash_map::Entry::Vacant(entry) => {
277287
let _ = entry.insert(metadata_location);
288+
278289
Ok(())
279290
}
280291
}

0 commit comments

Comments
 (0)