Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Introduce CachingSession instead of Session #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 6 lib/client-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ const errors = require("./errors.js");
* @property {Number} [maxPrepared] Determines the maximum amount of different prepared queries before evicting items
* from the internal cache. Reaching a high threshold hints that the queries are not being reused, like when
* hard-coding parameter values inside the queries.
* Default: ``500``.
ZuzaOsa marked this conversation as resolved.
Show resolved Hide resolved
* [TODO: Add support for this field]
* Default: `512`.
* @property {Object} [policies]
* [TODO: Add support for this field]
* @property {LoadBalancingPolicy} [policies.loadBalancing] The load balancing policy instance to be used to determine
Expand Down Expand Up @@ -330,7 +329,7 @@ function defaultOptions() {
authProvider: null,
requestTracker: null,
metrics: new metrics.DefaultMetrics(),
maxPrepared: 500,
maxPrepared: null, // Default is 512, defined on the Rust side
refreshSchemaDelay: 1000,
isMetadataSyncEnabled: true,
prepareOnAllHosts: true,
Expand Down Expand Up @@ -668,6 +667,7 @@ function setRustOptions(options) {
rustOptions.applicationName = options.applicationName;
rustOptions.applicationVersion = options.applicationVersion;
rustOptions.keyspace = options.keyspace;
rustOptions.cacheSize = options.maxPrepared;
if (options.credentials) {
rustOptions.credentialsUsername = options.credentials.username;
rustOptions.credentialsPassword = options.credentials.password;
Expand Down
29 changes: 25 additions & 4 deletions 29 src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use scylla::client::session::Session;
use scylla::client::caching_session::CachingSession;
use scylla::client::session_builder::SessionBuilder;
use scylla::client::SelfIdentity;
use scylla::response::PagingState;
Expand All @@ -16,6 +16,8 @@ use crate::{
requests::request::PreparedStatementWrapper, result::QueryResultWrapper, utils::err_to_napi,
};

const DEFAULT_CACHE_SIZE: u32 = 512;

#[napi]
ZuzaOsa marked this conversation as resolved.
Show resolved Hide resolved
pub struct SessionOptions {
pub connect_points: Vec<String>,
Expand All @@ -24,6 +26,7 @@ pub struct SessionOptions {
pub application_version: Option<String>,
pub credentials_username: Option<String>,
pub credentials_password: Option<String>,
pub cache_size: Option<u32>,
}

#[napi]
Expand All @@ -33,7 +36,7 @@ pub struct BatchWrapper {

#[napi]
pub struct SessionWrapper {
inner: Session,
inner: CachingSession,
}

#[napi]
Expand All @@ -48,6 +51,7 @@ impl SessionOptions {
application_version: None,
credentials_username: None,
credentials_password: None,
cache_size: None,
}
}
}
Expand All @@ -59,13 +63,21 @@ impl SessionWrapper {
pub async fn create_session(options: &SessionOptions) -> napi::Result<Self> {
let builder = configure_session_builder(options);
let session = builder.build().await.map_err(err_to_napi)?;
let session: CachingSession = CachingSession::from(
session,
options.cache_size.unwrap_or(DEFAULT_CACHE_SIZE) as usize,
);
Ok(SessionWrapper { inner: session })
}

/// Returns the name of the current keyspace
#[napi]
pub fn get_keyspace(&self) -> Option<String> {
self.inner.get_keyspace().as_deref().map(ToOwned::to_owned)
self.inner
.get_session()
.get_keyspace()
.as_deref()
.map(ToOwned::to_owned)
}

/// Executes unprepared statement. This assumes the types will be either guessed or provided by user.
Expand All @@ -85,6 +97,7 @@ impl SessionWrapper {
let params_vec = MaybeUnsetQueryParameterWrapper::extract_parameters(params);
let query_result = self
.inner
.get_session()
.query_unpaged(statement, params_vec)
.await
.map_err(err_to_napi)?;
Expand All @@ -98,8 +111,13 @@ impl SessionWrapper {
&self,
statement: String,
) -> napi::Result<PreparedStatementWrapper> {
let statement: Statement = statement.into();
Ok(PreparedStatementWrapper {
prepared: self.inner.prepare(statement).await.map_err(err_to_napi)?,
prepared: self
.inner
.add_prepared_statement(&statement) // TODO: change for add_prepared_statement_to_owned after it is made public
.await
.map_err(err_to_napi)?,
})
}

Expand All @@ -123,6 +141,7 @@ impl SessionWrapper {
let query = apply_prepared_options(query.prepared.clone(), options)?;
QueryResultWrapper::from_query(
self.inner
.get_session()
.execute_unpaged(&query, params_vec)
.await
.map_err(err_to_napi)?,
Expand Down Expand Up @@ -174,6 +193,7 @@ impl SessionWrapper {

let (result, paging_state_response) = self
.inner
.get_session()
.query_single_page(statement, values, paging_state)
.await
.map_err(err_to_napi)?;
Expand Down Expand Up @@ -208,6 +228,7 @@ impl SessionWrapper {

let (result, paging_state) = self
.inner
.get_session()
.execute_single_page(&prepared, values, paging_state)
.await
.map_err(err_to_napi)?;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.