mirror of
https://github.com/lancedb/lancedb.git
synced 2026-01-05 19:32:56 +00:00
feat: allow Python and Typescript users to create Sessions (#2530)
## Summary - Exposes `Session` in Python and Typescript so users can set the `index_cache_size_bytes` and `metadata_cache_size_bytes` * The `Session` is attached to the `Connection`, and thus shared across all tables in that connection. - Adds deprecation warnings for table-level cache configuration 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -74,6 +74,10 @@ impl Connection {
|
||||
builder = builder.host_override(&host_override);
|
||||
}
|
||||
|
||||
if let Some(session) = options.session {
|
||||
builder = builder.session(session.inner.clone());
|
||||
}
|
||||
|
||||
Ok(Self::inner_new(builder.execute().await.default_error()?))
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ pub mod merge;
|
||||
mod query;
|
||||
pub mod remote;
|
||||
mod rerankers;
|
||||
mod session;
|
||||
mod table;
|
||||
mod util;
|
||||
|
||||
@@ -34,6 +35,9 @@ pub struct ConnectionOptions {
|
||||
///
|
||||
/// The available options are described at https://lancedb.github.io/lancedb/guides/storage/
|
||||
pub storage_options: Option<HashMap<String, String>>,
|
||||
/// (For LanceDB OSS only): the session to use for this connection. Holds
|
||||
/// shared caches and other session-specific state.
|
||||
pub session: Option<session::Session>,
|
||||
|
||||
/// (For LanceDB cloud only): configuration for the remote HTTP client.
|
||||
pub client_config: Option<remote::ClientConfig>,
|
||||
|
||||
102
nodejs/src/session.rs
Normal file
102
nodejs/src/session.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use lancedb::{ObjectStoreRegistry, Session as LanceSession};
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi_derive::*;
|
||||
|
||||
/// A session for managing caches and object stores across LanceDB operations.
|
||||
///
|
||||
/// Sessions allow you to configure cache sizes for index and metadata caches,
|
||||
/// which can significantly impact memory use and performance. They can
|
||||
/// also be re-used across multiple connections to share the same cache state.
|
||||
#[napi]
|
||||
#[derive(Clone)]
|
||||
pub struct Session {
|
||||
pub(crate) inner: Arc<LanceSession>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Session {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Session")
|
||||
.field("size_bytes", &self.inner.size_bytes())
|
||||
.field("approx_num_items", &self.inner.approx_num_items())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
impl Session {
|
||||
/// Create a new session with custom cache sizes.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// - `index_cache_size_bytes`: The size of the index cache in bytes.
|
||||
/// Index data is stored in memory in this cache to speed up queries.
|
||||
/// Defaults to 6GB if not specified.
|
||||
/// - `metadata_cache_size_bytes`: The size of the metadata cache in bytes.
|
||||
/// The metadata cache stores file metadata and schema information in memory.
|
||||
/// This cache improves scan and write performance.
|
||||
/// Defaults to 1GB if not specified.
|
||||
#[napi(constructor)]
|
||||
pub fn new(
|
||||
index_cache_size_bytes: Option<BigInt>,
|
||||
metadata_cache_size_bytes: Option<BigInt>,
|
||||
) -> napi::Result<Self> {
|
||||
let index_cache_size = index_cache_size_bytes
|
||||
.map(|size| size.get_u64().1 as usize)
|
||||
.unwrap_or(6 * 1024 * 1024 * 1024); // 6GB default
|
||||
|
||||
let metadata_cache_size = metadata_cache_size_bytes
|
||||
.map(|size| size.get_u64().1 as usize)
|
||||
.unwrap_or(1024 * 1024 * 1024); // 1GB default
|
||||
|
||||
let session = LanceSession::new(
|
||||
index_cache_size,
|
||||
metadata_cache_size,
|
||||
Arc::new(ObjectStoreRegistry::default()),
|
||||
);
|
||||
|
||||
Ok(Self {
|
||||
inner: Arc::new(session),
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a session with default cache sizes.
|
||||
///
|
||||
/// This is equivalent to creating a session with 6GB index cache
|
||||
/// and 1GB metadata cache.
|
||||
#[napi(factory)]
|
||||
pub fn default() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(LanceSession::default()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the current size of the session caches in bytes.
|
||||
#[napi]
|
||||
pub fn size_bytes(&self) -> BigInt {
|
||||
BigInt::from(self.inner.size_bytes())
|
||||
}
|
||||
|
||||
/// Get the approximate number of items cached in the session.
|
||||
#[napi]
|
||||
pub fn approx_num_items(&self) -> u32 {
|
||||
self.inner.approx_num_items() as u32
|
||||
}
|
||||
}
|
||||
|
||||
// Implement FromNapiValue for Session to work with napi(object)
|
||||
impl napi::bindgen_prelude::FromNapiValue for Session {
|
||||
unsafe fn from_napi_value(
|
||||
env: napi::sys::napi_env,
|
||||
napi_val: napi::sys::napi_value,
|
||||
) -> napi::Result<Self> {
|
||||
let object: napi::bindgen_prelude::ClassInstance<Session> =
|
||||
napi::bindgen_prelude::ClassInstance::from_napi_value(env, napi_val)?;
|
||||
let copy = object.clone();
|
||||
Ok(copy)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user