Skip to content

Commit 6eba649

Browse files
committed
PreparedStatement: Introduce mutable metadata
This introduces second ResultMetadata field to PreparedStatement: current_result_metadata. It is stored in ArcSwap, and updateable via new `update_current_result_metadata` method. The original field (`initial_result_metadata`) is kept. It can't be removed because of backwards compatibility. The only way it is exposed is public `get_result_set_col_specs` method, which will become deprecated in future commit. Internal getter (`get_result_metadata`) is renamed to `get_current_result_metadata`, and code in connection.rs adjusted. Because of how the code is structured and all the preparation in previous PRs, this is enough to cover all cases (_iter, retries etc). Currently the update method is unused, intentionally. It will become used when we implement Scylla's metadata extension.
1 parent 3b845f3 commit 6eba649

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

scylla/src/network/connection.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,14 +930,14 @@ impl Connection {
930930

931931
let cached_metadata = prepared_statement
932932
.get_use_cached_result_metadata()
933-
.then(|| prepared_statement.get_result_metadata());
933+
.then(|| prepared_statement.get_current_result_metadata());
934934

935935
let query_response = self
936936
.send_request(
937937
&execute_frame,
938938
true,
939939
prepared_statement.config.tracing,
940-
cached_metadata,
940+
cached_metadata.as_ref(),
941941
)
942942
.await?;
943943

@@ -967,7 +967,7 @@ impl Connection {
967967
&execute_frame,
968968
true,
969969
prepared_statement.config.tracing,
970-
cached_metadata,
970+
cached_metadata.as_ref(),
971971
)
972972
.await?;
973973

scylla/src/statement/prepared.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Defines the [`PreparedStatement`] type, which represents a statement
22
//! that has been prepared in advance on the server.
33
4+
use arc_swap::{ArcSwap, Guard};
45
use bytes::{Bytes, BytesMut};
56
use scylla_cql::frame::response::result::{
67
ColumnSpec, PartitionKeyIndex, ResultMetadata, TableSpec,
@@ -187,6 +188,7 @@ struct PreparedStatementSharedData {
187188
id: Bytes,
188189
metadata: PreparedMetadata,
189190
initial_result_metadata: Arc<ResultMetadata<'static>>,
191+
current_result_metadata: ArcSwap<ResultMetadata<'static>>,
190192
statement: String,
191193
is_confirmed_lwt: bool,
192194
}
@@ -203,6 +205,18 @@ impl Clone for PreparedStatement {
203205
}
204206
}
205207

208+
/// Stores a snapshot of current result metadata column specs.
209+
pub struct ColumnSpecsGuard {
210+
result: Guard<Arc<ResultMetadata<'static>>>,
211+
}
212+
213+
impl ColumnSpecsGuard {
214+
/// Retrieves current result metadata column specs.
215+
pub fn get(&self) -> ColumnSpecs<'_, 'static> {
216+
ColumnSpecs::new(self.result.col_specs())
217+
}
218+
}
219+
206220
impl PreparedStatement {
207221
fn new(
208222
id: Bytes,
@@ -217,7 +231,8 @@ impl PreparedStatement {
217231
shared: Arc::new(PreparedStatementSharedData {
218232
id,
219233
metadata,
220-
initial_result_metadata: result_metadata,
234+
initial_result_metadata: Arc::clone(&result_metadata),
235+
current_result_metadata: ArcSwap::from(result_metadata),
221236
statement,
222237
is_confirmed_lwt: is_lwt,
223238
}),
@@ -507,8 +522,18 @@ impl PreparedStatement {
507522
}
508523

509524
/// Access metadata about the result of prepared statement returned by the database
510-
pub(crate) fn get_result_metadata(&self) -> &Arc<ResultMetadata<'static>> {
511-
&self.shared.initial_result_metadata
525+
pub(crate) fn get_current_result_metadata(&self) -> Arc<ResultMetadata<'static>> {
526+
self.shared.current_result_metadata.load_full()
527+
}
528+
529+
/// Update metadata about the result of prepared statement.
530+
// Will be used when we implement support for metadata id extension.
531+
#[allow(dead_code)]
532+
pub(crate) fn update_current_result_metadata(
533+
&self,
534+
new_metadata: Arc<ResultMetadata<'static>>,
535+
) {
536+
self.shared.current_result_metadata.store(new_metadata);
512537
}
513538

514539
/// Access column specifications of the result set returned after the execution of this statement

0 commit comments

Comments
 (0)