|
3 | 3 |
|
4 | 4 | pub mod registry; |
5 | 5 |
|
6 | | -use std::any::Any; |
7 | | -use std::any::TypeId; |
8 | | -use std::any::type_name; |
| 6 | +use std::any::{Any, TypeId, type_name}; |
9 | 7 | use std::fmt::Debug; |
10 | | -use std::hash::BuildHasherDefault; |
11 | | -use std::hash::Hasher; |
12 | | -use std::ops::Deref; |
13 | | -use std::ops::DerefMut; |
| 8 | +use std::hash::{BuildHasherDefault, Hasher}; |
| 9 | +use std::ops::{Deref, DerefMut}; |
14 | 10 | use std::sync::Arc; |
15 | 11 |
|
16 | | -use dashmap::DashMap; |
17 | | -use dashmap::Entry; |
18 | | -use vortex_error::VortexExpect; |
19 | | -use vortex_error::vortex_panic; |
| 12 | +use dashmap::{DashMap, Entry}; |
| 13 | +use vortex_error::{VortexExpect, vortex_panic}; |
20 | 14 |
|
21 | 15 | /// A Vortex session encapsulates the set of extensible arrays, layouts, compute functions, dtypes, |
22 | 16 | /// etc. that are available for use in a given context. |
@@ -52,6 +46,26 @@ impl VortexSession { |
52 | 46 | } |
53 | 47 | self |
54 | 48 | } |
| 49 | + |
| 50 | + /// Inserts a new session variable of type `V` with the supplied value. |
| 51 | + /// |
| 52 | + /// # Panics |
| 53 | + /// |
| 54 | + /// If a variable of that type already exists. |
| 55 | + pub fn set<V: SessionVar>(self, val: V) -> Self { |
| 56 | + match self.0.entry(TypeId::of::<V>()) { |
| 57 | + Entry::Occupied(_) => { |
| 58 | + vortex_panic!( |
| 59 | + "Session variable of type {} already exists", |
| 60 | + type_name::<V>() |
| 61 | + ); |
| 62 | + } |
| 63 | + Entry::Vacant(e) => { |
| 64 | + e.insert(Box::new(val)); |
| 65 | + } |
| 66 | + } |
| 67 | + self |
| 68 | + } |
55 | 69 | } |
56 | 70 |
|
57 | 71 | /// Trait for accessing and modifying the state of a Vortex session. |
@@ -88,18 +102,6 @@ impl SessionExt for VortexSession { |
88 | 102 |
|
89 | 103 | /// Returns the scope variable of type `V`, or inserts a default one if it does not exist. |
90 | 104 | fn get<V: SessionVar + Default>(&self) -> Ref<'_, V> { |
91 | | - // NOTE(ngates): we don't use `entry().or_insert_with_key()` here because the DashMap |
92 | | - // would immediately acquire an exclusive write lock. |
93 | | - if let Some(v) = self.0.get(&TypeId::of::<V>()) { |
94 | | - return Ref(v.map(|v| { |
95 | | - (**v) |
96 | | - .as_any() |
97 | | - .downcast_ref::<V>() |
98 | | - .vortex_expect("Type mismatch - this is a bug") |
99 | | - })); |
100 | | - } |
101 | | - |
102 | | - // If we get here, the value was not present, so we insert the default with a write lock. |
103 | 105 | Ref(self |
104 | 106 | .0 |
105 | 107 | .entry(TypeId::of::<V>()) |
|
0 commit comments