Skip to content

Commit ff02ee3

Browse files
committed
Replace PreparedStatementWrapper with strings
Replace PreparedStatementWrapper with strings, when executing prepared statements, when calling endpoint to execute queries. With this change we only return expected types for prepared statements, instead of the whole PreparedStatementWrapper object from the Rust. This change introduces a speed improvement when executing queries through executeConcurrent, and does not slow down other endpoints.
1 parent 28e02df commit ff02ee3

File tree

4 files changed

+26
-45
lines changed

4 files changed

+26
-45
lines changed

lib/cache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const _rust = require("../index");
22

33
class PreparedCache {
44
/**
5-
* @type {Map<string, _rust.PreparedStatementWrapper>}
5+
* @type {Map<string, list<Object | string>>}
66
*/
77
#cache;
88

@@ -13,7 +13,7 @@ class PreparedCache {
1313
/**
1414
*
1515
* @param {string} key
16-
* @returns {_rust.PreparedStatementWrapper}
16+
* @returns {list<Object | string>}
1717
*/
1818
getElement(key) {
1919
return this.#cache[key];
@@ -22,7 +22,7 @@ class PreparedCache {
2222
/**
2323
*
2424
* @param {string} key
25-
* @param {_rust.PreparedStatementWrapper} element
25+
* @param {list<Object | string>} element
2626
*/
2727
storeElement(key, element) {
2828
this.#cache[key] = element;

lib/client.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,13 @@ class Client extends events.EventEmitter {
148148
/**
149149
* Manually prepare query into prepared statement
150150
* @param {string} query
151-
* @returns {Promise<list<Object | rust.PreparedStatementWrapper >>}
151+
* @returns {Promise<list<Object | string>>}
152152
* Returns a tuple of type object (the format expected by the encoder) and prepared statement wrapper
153153
* @package
154154
*/
155155
async prepareQuery(query) {
156156
let helper = await this.rustClient.prepareStatement(query);
157-
let res = [
158-
helper.getExpectedTypes().map((t) => convertComplexType(t)),
159-
helper,
160-
];
157+
let res = [helper.map((t) => convertComplexType(t)), query];
161158
return res;
162159
}
163160

@@ -305,7 +302,7 @@ class Client extends events.EventEmitter {
305302

306303
/**
307304
* Wrapper for executing queries by rust driver
308-
* @param {string | rust.PreparedStatementWrapper} query
305+
* @param {string | list<Object | string>} query
309306
* @param {Array} params
310307
* @param {ExecOptions.ExecutionOptions} execOptions
311308
* @returns {Promise<ResultSet>}
@@ -342,7 +339,7 @@ class Client extends events.EventEmitter {
342339
}
343340

344341
/**
345-
* @type {rust.PreparedStatementWrapper}
342+
* @type {string}
346343
*/
347344
let statement = query[1];
348345
/**
@@ -360,10 +357,8 @@ class Client extends events.EventEmitter {
360357
);
361358
} else {
362359
// We do not accept already prepared statements for unprepared queries
363-
if (query instanceof rust.PreparedStatementWrapper) {
364-
throw new Error(
365-
"Unexpected prepared statement wrapper for unprepared queries",
366-
);
360+
if (typeof query !== "string") {
361+
throw new Error("Expected to obtain a string query");
367362
}
368363

369364
// Parse parameters according to provided hints, with type guessing
@@ -542,7 +537,7 @@ class Client extends events.EventEmitter {
542537
}
543538

544539
/**
545-
* @type {rust.PreparedStatementWrapper}
540+
* @type {string}
546541
*/
547542
let statement = query[1];
548543
/**
@@ -561,10 +556,8 @@ class Client extends events.EventEmitter {
561556
);
562557
} else {
563558
// We do not accept already prepared statements for unprepared queries
564-
if (query instanceof rust.PreparedStatementWrapper) {
565-
throw new Error(
566-
"Unexpected prepared statement wrapper for unprepared queries",
567-
);
559+
if (typeof query !== "string") {
560+
throw new Error("Expected to obtain a string query");
568561
}
569562
// Parse parameters according to provided hints, with type guessing
570563
let encoded = encodeParams(

src/requests/request.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use scylla::statement::prepared::PreparedStatement;
33

44
use crate::{result::map_column_type_to_complex_type, types::type_wrappers::ComplexType};
55

6-
#[napi]
7-
pub struct PreparedStatementWrapper {
6+
pub(crate) struct PreparedStatementWrapper {
87
pub(crate) prepared: PreparedStatement,
98
}
109

@@ -34,9 +33,7 @@ pub struct QueryOptionsWrapper {
3433
pub trace_query: Option<bool>,
3534
}
3635

37-
#[napi]
3836
impl PreparedStatementWrapper {
39-
#[napi]
4037
/// Get array of expected types for this prepared statement.
4138
pub fn get_expected_types(&self) -> Vec<ComplexType> {
4239
self.prepared

src/session.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use scylla::client::caching_session::CachingSession;
33
use scylla::client::session_builder::SessionBuilder;
44
use scylla::response::PagingState;
55
use scylla::statement::batch::Batch;
6-
use scylla::statement::prepared::PreparedStatement;
76
use scylla::statement::{Consistency, SerialConsistency, Statement};
87

98
use crate::errors::{err_to_napi, js_error};
109
use crate::options;
1110
use crate::paging::{PagingResult, PagingStateWrapper};
1211
use crate::requests::request::QueryOptionsWrapper;
1312
use crate::types::encoded_data::EncodedValuesWrapper;
13+
use crate::types::type_wrappers::ComplexType;
1414
use crate::utils::bigint_to_i64;
1515
use crate::{requests::request::PreparedStatementWrapper, result::QueryResultWrapper};
1616

@@ -103,20 +103,18 @@ impl SessionWrapper {
103103
}
104104

105105
/// Prepares a statement through rust driver for a given session
106-
/// Return PreparedStatementWrapper that wraps object returned by the rust driver
106+
/// Return expected types for the prepared statement
107107
#[napi]
108-
pub async fn prepare_statement(
109-
&self,
110-
statement: String,
111-
) -> napi::Result<PreparedStatementWrapper> {
108+
pub async fn prepare_statement(&self, statement: String) -> napi::Result<Vec<ComplexType>> {
112109
let statement: Statement = statement.into();
113110
Ok(PreparedStatementWrapper {
114111
prepared: self
115112
.inner
116113
.add_prepared_statement(&statement) // TODO: change for add_prepared_statement_to_owned after it is made public
117114
.await
118115
.map_err(err_to_napi)?,
119-
})
116+
}
117+
.get_expected_types())
120118
}
121119

122120
/// Execute a given prepared statement against the database with provided parameters.
@@ -132,15 +130,14 @@ impl SessionWrapper {
132130
#[napi]
133131
pub async fn execute_prepared_unpaged_encoded(
134132
&self,
135-
query: &PreparedStatementWrapper,
133+
query: String,
136134
params: Vec<EncodedValuesWrapper>,
137135
options: &QueryOptionsWrapper,
138136
) -> napi::Result<QueryResultWrapper> {
139-
let query = apply_prepared_options(query.prepared.clone(), options)?;
137+
let query = apply_statement_options(query.into(), options)?;
140138
QueryResultWrapper::from_query(
141139
self.inner
142-
.get_session()
143-
.execute_unpaged(&query, params)
140+
.execute_unpaged(query, params)
144141
.await
145142
.map_err(err_to_napi)?,
146143
)
@@ -206,20 +203,19 @@ impl SessionWrapper {
206203
#[napi]
207204
pub async fn execute_single_page_encoded(
208205
&self,
209-
query: &PreparedStatementWrapper,
206+
query: String,
210207
params: Vec<EncodedValuesWrapper>,
211208
options: &QueryOptionsWrapper,
212209
paging_state: Option<&PagingStateWrapper>,
213210
) -> napi::Result<PagingResult> {
214211
let paging_state = paging_state
215212
.map(|e| e.inner.clone())
216213
.unwrap_or(PagingState::start());
217-
let prepared = apply_prepared_options(query.prepared.clone(), options)?;
214+
let prepared = apply_statement_options(query.into(), options)?;
218215

219216
let (result, paging_state) = self
220217
.inner
221-
.get_session()
222-
.execute_single_page(&prepared, params, paging_state)
218+
.execute_single_page(prepared, params, paging_state)
223219
.await
224220
.map_err(err_to_napi)?;
225221
Ok(PagingResult {
@@ -233,13 +229,13 @@ impl SessionWrapper {
233229
/// Requires each passed statement to be already prepared.
234230
#[napi]
235231
pub fn create_prepared_batch(
236-
statements: Vec<&PreparedStatementWrapper>,
232+
statements: Vec<String>,
237233
options: &QueryOptionsWrapper,
238234
) -> napi::Result<BatchWrapper> {
239235
let mut batch: Batch = Default::default();
240236
statements
241237
.iter()
242-
.for_each(|q| batch.append_statement(q.prepared.clone()));
238+
.for_each(|q| batch.append_statement(q.as_str()));
243239
batch = apply_batch_options(batch, options)?;
244240
Ok(BatchWrapper { inner: batch })
245241
}
@@ -348,11 +344,6 @@ macro_rules! make_non_batch_apply_options {
348344
}
349345

350346
make_non_batch_apply_options!(Statement, apply_statement_options, statement_opt_partial);
351-
make_non_batch_apply_options!(
352-
PreparedStatement,
353-
apply_prepared_options,
354-
prepared_opt_partial
355-
);
356347
make_apply_options!(Batch, apply_batch_options);
357348

358349
/// Provides driver self identity, filling information on application based on session options.

0 commit comments

Comments
 (0)