Skip to content

Commit 904a971

Browse files
committed
result: replace col_specs with col_names
Currently, we heavily depend on the fact that `QueryResult::col_specs()` returns a `&[ColumnSpec<'static>]`. This won't be true once we bump rust-driver dependency to 0.15 - the lifetime of ColumnSpec will be tied to the lifetime of `QueryRowsResult` object. It looks like most of the information held by `col_spec` was reduntant. It was only used to retrieve the name of the column. This is why, we will only hold the information about the column names. The column types are stored in `col_data_types`. This also allows us to get rid of `'static` bound from `CassResultData::from_result_payload`. It will be helpful when bumping to 0.15. As a bonus, we can also replace the `Vec<>` with a slice `&[]` in `from_result_payload`, since we do not need to own the column specs anymore.
1 parent 5e5f585 commit 904a971

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

scylla-rust-wrapper/src/query_result.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub struct CassResult {
2424
}
2525

2626
pub struct CassResultData {
27-
pub col_specs: Vec<ColumnSpec<'static>>,
27+
pub col_names: Vec<String>,
2828
pub col_data_types: Arc<Vec<Arc<CassDataType>>>,
2929
}
3030

3131
impl CassResultData {
3232
pub fn from_result_payload(
33-
col_specs: Vec<ColumnSpec<'static>>,
33+
col_specs: &[ColumnSpec<'_>],
3434
maybe_col_data_types: Option<Arc<Vec<Arc<CassDataType>>>>,
3535
) -> CassResultData {
3636
// `maybe_col_data_types` is:
@@ -46,8 +46,13 @@ impl CassResultData {
4646
)
4747
});
4848

49+
let col_names = col_specs
50+
.iter()
51+
.map(|col_spec| col_spec.name().to_owned())
52+
.collect();
53+
4954
CassResultData {
50-
col_specs,
55+
col_names,
5156
col_data_types,
5257
}
5358
}
@@ -895,12 +900,12 @@ pub unsafe extern "C" fn cass_row_get_column_by_name_n(
895900

896901
return row_from_raw
897902
.result_metadata
898-
.col_specs
903+
.col_names
899904
.iter()
900905
.enumerate()
901-
.find(|(_, spec)| {
902-
is_case_sensitive && spec.name() == name_str
903-
|| !is_case_sensitive && spec.name().eq_ignore_ascii_case(name_str)
906+
.find(|(_, col_name)| {
907+
is_case_sensitive && *col_name == name_str
908+
|| !is_case_sensitive && col_name.eq_ignore_ascii_case(name_str)
904909
})
905910
.map(|(index, _)| {
906911
return match row_from_raw.columns.get(index) {
@@ -921,12 +926,11 @@ pub unsafe extern "C" fn cass_result_column_name(
921926
let result_from_raw = ptr_to_ref(result);
922927
let index_usize: usize = index.try_into().unwrap();
923928

924-
if index_usize >= result_from_raw.metadata.col_specs.len() {
929+
if index_usize >= result_from_raw.metadata.col_data_types.len() {
925930
return CassError::CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS;
926931
}
927932

928-
let column_spec: &ColumnSpec = result_from_raw.metadata.col_specs.get(index_usize).unwrap();
929-
let column_name = column_spec.name();
933+
let column_name = result_from_raw.metadata.col_names.get(index_usize).unwrap();
930934

931935
write_str_to_c(column_name, name, name_length);
932936

@@ -1334,7 +1338,7 @@ pub unsafe extern "C" fn cass_result_row_count(result_raw: *const CassResult) ->
13341338
pub unsafe extern "C" fn cass_result_column_count(result_raw: *const CassResult) -> size_t {
13351339
let result = ptr_to_ref(result_raw);
13361340

1337-
result.metadata.col_specs.len() as size_t
1341+
result.metadata.col_data_types.len() as size_t
13381342
}
13391343

13401344
#[no_mangle]
@@ -1411,7 +1415,7 @@ mod tests {
14111415
const THIRD_COLUMN_NAME: &str = "list_double_col";
14121416
fn create_cass_rows_result() -> CassResult {
14131417
let metadata = Arc::new(CassResultData::from_result_payload(
1414-
vec![
1418+
&[
14151419
col_spec(FIRST_COLUMN_NAME, ColumnType::BigInt),
14161420
col_spec(SECOND_COLUMN_NAME, ColumnType::Varint),
14171421
col_spec(
@@ -1528,7 +1532,7 @@ mod tests {
15281532
}
15291533

15301534
fn create_non_rows_cass_result() -> CassResult {
1531-
let metadata = Arc::new(CassResultData::from_result_payload(vec![], None));
1535+
let metadata = Arc::new(CassResultData::from_result_payload(&[], None));
15321536
CassResult {
15331537
rows: None,
15341538
metadata,

scylla-rust-wrapper/src/session.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub unsafe extern "C" fn cass_session_execute_batch(
220220
match query_res {
221221
Ok(_result) => Ok(CassResultValue::QueryResult(Arc::new(CassResult {
222222
rows: None,
223-
metadata: Arc::new(CassResultData::from_result_payload(vec![], None)),
223+
metadata: Arc::new(CassResultData::from_result_payload(&[], None)),
224224
tracing_id: None,
225225
paging_state_response: PagingStateResponse::NoMorePages,
226226
}))),
@@ -355,7 +355,7 @@ pub unsafe extern "C" fn cass_session_execute(
355355
match query_res {
356356
Ok((result, paging_state_response, maybe_col_data_types)) => {
357357
let metadata = Arc::new(CassResultData::from_result_payload(
358-
result.col_specs().to_vec(),
358+
result.col_specs(),
359359
maybe_col_data_types,
360360
));
361361
let cass_rows = result

0 commit comments

Comments
 (0)