@@ -41,13 +41,32 @@ use url::Url;
4141/// Execution runtime environment that manages system resources such
4242/// as memory, disk, cache and storage.
4343///
44- /// A [`RuntimeEnv`] is created from a [`RuntimeEnvBuilder`] and has the
44+ /// A [`RuntimeEnv`] can be created using [`RuntimeEnvBuilder`] and has the
4545/// following resource management functionality:
4646///
4747/// * [`MemoryPool`]: Manage memory
4848/// * [`DiskManager`]: Manage temporary files on local disk
4949/// * [`CacheManager`]: Manage temporary cache data during the session lifetime
5050/// * [`ObjectStoreRegistry`]: Manage mapping URLs to object store instances
51+ ///
52+ /// # Example: Create default `RuntimeEnv`
53+ /// ```
54+ /// # use datafusion_execution::runtime_env::RuntimeEnv;
55+ /// let runtime_env = RuntimeEnv::default();
56+ /// ```
57+ ///
58+ /// # Example: Create a `RuntimeEnv` from [`RuntimeEnvBuilder`] with a new memory pool
59+ /// ```
60+ /// # use std::sync::Arc;
61+ /// # use datafusion_execution::memory_pool::GreedyMemoryPool;
62+ /// # use datafusion_execution::runtime_env::{RuntimeEnv, RuntimeEnvBuilder};
63+ /// // restrict to using at most 100MB of memory
64+ /// let pool_size = 100 * 1024 * 1024;
65+ /// let runtime_env = RuntimeEnvBuilder::new()
66+ /// .with_memory_pool(Arc::new(GreedyMemoryPool::new(pool_size)))
67+ /// .build()
68+ /// .unwrap();
69+ /// ```
5170pub struct RuntimeEnv {
5271 /// Runtime memory management
5372 pub memory_pool : Arc < dyn MemoryPool > ,
@@ -66,28 +85,16 @@ impl Debug for RuntimeEnv {
6685}
6786
6887impl RuntimeEnv {
69- #[ deprecated( since = "43.0.0" , note = "please use `try_new` instead" ) ]
88+ #[ deprecated( since = "43.0.0" , note = "please use `RuntimeEnvBuilder` instead" ) ]
89+ #[ allow( deprecated) ]
7090 pub fn new ( config : RuntimeConfig ) -> Result < Self > {
7191 Self :: try_new ( config)
7292 }
7393 /// Create env based on configuration
94+ #[ deprecated( since = "44.0.0" , note = "please use `RuntimeEnvBuilder` instead" ) ]
95+ #[ allow( deprecated) ]
7496 pub fn try_new ( config : RuntimeConfig ) -> Result < Self > {
75- let RuntimeConfig {
76- memory_pool,
77- disk_manager,
78- cache_manager,
79- object_store_registry,
80- } = config;
81-
82- let memory_pool =
83- memory_pool. unwrap_or_else ( || Arc :: new ( UnboundedMemoryPool :: default ( ) ) ) ;
84-
85- Ok ( Self {
86- memory_pool,
87- disk_manager : DiskManager :: try_new ( disk_manager) ?,
88- cache_manager : CacheManager :: try_new ( & cache_manager) ?,
89- object_store_registry,
90- } )
97+ config. build ( )
9198 }
9299
93100 /// Registers a custom `ObjectStore` to be used with a specific url.
@@ -104,7 +111,7 @@ impl RuntimeEnv {
104111 /// # use std::sync::Arc;
105112 /// # use url::Url;
106113 /// # use datafusion_execution::runtime_env::RuntimeEnv;
107- /// # let runtime_env = RuntimeEnv::try_new(Default:: default()).unwrap ();
114+ /// # let runtime_env = RuntimeEnv::default();
108115 /// let url = Url::try_from("file://").unwrap();
109116 /// let object_store = object_store::local::LocalFileSystem::new();
110117 /// // register the object store with the runtime environment
@@ -119,11 +126,12 @@ impl RuntimeEnv {
119126 /// # use std::sync::Arc;
120127 /// # use url::Url;
121128 /// # use datafusion_execution::runtime_env::RuntimeEnv;
122- /// # let runtime_env = RuntimeEnv::try_new(Default:: default()).unwrap ();
129+ /// # let runtime_env = RuntimeEnv::default();
123130 /// # // use local store for example as http feature is not enabled
124131 /// # let http_store = object_store::local::LocalFileSystem::new();
125132 /// // create a new object store via object_store::http::HttpBuilder;
126133 /// let base_url = Url::parse("https://github.com").unwrap();
134+ /// // (note this example can't depend on the http feature)
127135 /// // let http_store = HttpBuilder::new()
128136 /// // .with_url(base_url.clone())
129137 /// // .build()
@@ -155,12 +163,15 @@ impl Default for RuntimeEnv {
155163 }
156164}
157165
158- /// Please see: <https://github.com/apache/datafusion/issues/12156 >
166+ /// Please see: <https://github.com/apache/datafusion/issues/12156a >
159167/// This a type alias for backwards compatibility.
168+ #[ deprecated( since = "43.0.0" , note = "please use `RuntimeEnvBuilder` instead" ) ]
160169pub type RuntimeConfig = RuntimeEnvBuilder ;
161170
162171#[ derive( Clone ) ]
163- /// Execution runtime configuration
172+ /// Execution runtime configuration builder.
173+ ///
174+ /// See example on [`RuntimeEnv`]
164175pub struct RuntimeEnvBuilder {
165176 /// DiskManager to manage temporary disk file usage
166177 pub disk_manager : DiskManagerConfig ,
@@ -239,15 +250,20 @@ impl RuntimeEnvBuilder {
239250
240251 /// Build a RuntimeEnv
241252 pub fn build ( self ) -> Result < RuntimeEnv > {
242- let memory_pool = self
243- . memory_pool
244- . unwrap_or_else ( || Arc :: new ( UnboundedMemoryPool :: default ( ) ) ) ;
253+ let Self {
254+ disk_manager,
255+ memory_pool,
256+ cache_manager,
257+ object_store_registry,
258+ } = self ;
259+ let memory_pool =
260+ memory_pool. unwrap_or_else ( || Arc :: new ( UnboundedMemoryPool :: default ( ) ) ) ;
245261
246262 Ok ( RuntimeEnv {
247263 memory_pool,
248- disk_manager : DiskManager :: try_new ( self . disk_manager ) ?,
249- cache_manager : CacheManager :: try_new ( & self . cache_manager ) ?,
250- object_store_registry : self . object_store_registry ,
264+ disk_manager : DiskManager :: try_new ( disk_manager) ?,
265+ cache_manager : CacheManager :: try_new ( & cache_manager) ?,
266+ object_store_registry,
251267 } )
252268 }
253269
0 commit comments