Skip to content

Commit 69723c4

Browse files
committed
Use HashMap to hold tables
1 parent 6f53196 commit 69723c4

File tree

1 file changed

+8
-42
lines changed

1 file changed

+8
-42
lines changed

datafusion-examples/examples/remote_catalog.rs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use datafusion::physical_plan::ExecutionPlan;
4242
use datafusion::prelude::{DataFrame, SessionContext};
4343
use datafusion_catalog::Session;
4444
use datafusion_common::{
45-
assert_batches_eq, internal_datafusion_err, plan_err, TableReference,
45+
assert_batches_eq, internal_datafusion_err, plan_err, HashMap, TableReference,
4646
};
4747
use datafusion_expr::{Expr, TableType};
4848
use futures::TryStreamExt;
@@ -194,15 +194,15 @@ struct RemoteSchema {
194194
remote_catalog_interface: Arc<RemoteCatalogInterface>,
195195
/// Local cache of tables that have been preloaded from the remote
196196
/// catalog
197-
tables: Mutex<Vec<Arc<dyn TableProvider>>>,
197+
tables: Mutex<HashMap<String, Arc<dyn TableProvider>>>,
198198
}
199199

200200
impl RemoteSchema {
201201
/// Create a new RemoteSchema
202202
pub fn new(remote_catalog_interface: Arc<RemoteCatalogInterface>) -> Self {
203203
Self {
204204
remote_catalog_interface,
205-
tables: Mutex::new(vec![]),
205+
tables: Mutex::new(HashMap::new()),
206206
}
207207
}
208208

@@ -230,7 +230,7 @@ impl RemoteSchema {
230230
self.tables
231231
.lock()
232232
.expect("mutex invalid")
233-
.push(Arc::new(remote_table));
233+
.insert(table_name.to_string(), Arc::new(remote_table));
234234
};
235235
}
236236
Ok(())
@@ -250,16 +250,8 @@ impl SchemaProvider for RemoteSchema {
250250
self.tables
251251
.lock()
252252
.expect("mutex valid")
253-
.iter()
254-
.map(|remote_table| {
255-
// note it is possible to downcast to RemoteTable and call methods on it
256-
remote_table
257-
.as_any()
258-
.downcast_ref::<RemoteTable>()
259-
.expect("downcast to RemoteTable")
260-
.name()
261-
.to_string()
262-
})
253+
.keys()
254+
.cloned()
263255
.collect()
264256
}
265257

@@ -275,35 +267,14 @@ impl SchemaProvider for RemoteSchema {
275267
.tables
276268
.lock()
277269
.expect("mutex valid")
278-
.iter()
279-
.find(|remote_table| {
280-
// note it is possible to downcast to RemoteTable and call methods on it
281-
remote_table
282-
.as_any()
283-
.downcast_ref::<RemoteTable>()
284-
.expect("downcast to RemoteTable")
285-
.name()
286-
== name
287-
})
270+
.get(name)
288271
.map(Arc::clone);
289272
Ok(table)
290273
}
291274

292275
fn table_exist(&self, name: &str) -> bool {
293276
// Look for any pre-loaded tables, note this function is also `async`
294-
self.tables
295-
.lock()
296-
.expect("mutex valid")
297-
.iter()
298-
.any(|remote_table| {
299-
// note it is possible to downcast to RemoteTable and call methods on it
300-
remote_table
301-
.as_any()
302-
.downcast_ref::<RemoteTable>()
303-
.expect("downcast to RemoteTable")
304-
.name()
305-
== name
306-
})
277+
self.tables.lock().expect("mutex valid").contains_key(name)
307278
}
308279
}
309280

@@ -328,11 +299,6 @@ impl RemoteTable {
328299
schema,
329300
}
330301
}
331-
332-
/// Return the name of this table
333-
pub fn name(&self) -> &str {
334-
&self.name
335-
}
336302
}
337303

338304
/// Implement the DataFusion Catalog API for [`RemoteTable`]

0 commit comments

Comments
 (0)