Skip to content

Commit 739855a

Browse files
authored
Merge pull request #205 from muzarski/cass-result-cleanup
result: small cleanups before bump to 0.15
2 parents 74c1cc9 + b249bff commit 739855a

File tree

7 files changed

+96
-108
lines changed

7 files changed

+96
-108
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SCYLLA_TEST_FILTER := $(subst ${SPACE},${EMPTY},ClusterTests.*\
2626
:HeartbeatTests.Integration_Cassandra_HeartbeatFailed\
2727
:ExecutionProfileTest.InvalidName\
2828
:*NoCompactEnabledConnection\
29-
:PreparedMetadataTests.Integration_Cassandra_AlterDoesntUpdateColumnCount\
29+
:PreparedMetadataTests.Integration_Cassandra_AlterProperlyUpdatesColumnCount\
3030
:UseKeyspaceCaseSensitiveTests.Integration_Cassandra_ConnectWithKeyspace)
3131
endif
3232

@@ -56,7 +56,7 @@ CASSANDRA_TEST_FILTER := $(subst ${SPACE},${EMPTY},ClusterTests.*\
5656
:SslTests.Integration_Cassandra_ReconnectAfterClusterCrashAndRestart\
5757
:ExecutionProfileTest.InvalidName\
5858
:*NoCompactEnabledConnection\
59-
:PreparedMetadataTests.Integration_Cassandra_AlterDoesntUpdateColumnCount\
59+
:PreparedMetadataTests.Integration_Cassandra_AlterProperlyUpdatesColumnCount\
6060
:UseKeyspaceCaseSensitiveTests.Integration_Cassandra_ConnectWithKeyspace)
6161
endif
6262

scylla-rust-wrapper/src/cass_types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ pub enum MapDataType {
138138
KeyAndValue(Arc<CassDataType>, Arc<CassDataType>),
139139
}
140140

141+
#[derive(Debug)]
142+
pub struct CassColumnSpec {
143+
pub name: String,
144+
pub data_type: Arc<CassDataType>,
145+
}
146+
141147
#[derive(Clone, Debug, PartialEq, Eq)]
142148
pub enum CassDataType {
143149
Value(CassValueType),

scylla-rust-wrapper/src/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ pub unsafe extern "C" fn cass_future_tracing_id(
398398
tracing_id: *mut CassUuid,
399399
) -> CassError {
400400
ptr_to_ref(future).with_waited_result(|r: &mut CassFutureResult| match r {
401-
Ok(CassResultValue::QueryResult(result)) => match result.metadata.tracing_id {
401+
Ok(CassResultValue::QueryResult(result)) => match result.tracing_id {
402402
Some(id) => {
403403
*tracing_id = CassUuid::from(id);
404404
CassError::CASS_OK

scylla-rust-wrapper/src/prepared.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
argconv::*,
66
cass_error::CassError,
77
cass_types::{get_column_type, CassDataType},
8+
query_result::CassResultMetadata,
89
statement::{CassStatement, Statement},
910
types::size_t,
1011
};
@@ -14,11 +15,10 @@ use scylla::prepared_statement::PreparedStatement;
1415
pub struct CassPrepared {
1516
// Data types of columns from PreparedMetadata.
1617
pub variable_col_data_types: Vec<Arc<CassDataType>>,
17-
// Data types of columns from ResultMetadata.
18-
//
19-
// Arc<CassDataType> -> to share each data type with other structs such as `CassValue`
20-
// Arc<Vec<...>> -> to share the whole vector with `CassResultData`.
21-
pub result_col_data_types: Arc<Vec<Arc<CassDataType>>>,
18+
19+
// Cached result metadata. Arc'ed since we want to share it
20+
// with result metadata after execution.
21+
pub result_metadata: Arc<CassResultMetadata>,
2222
pub statement: PreparedStatement,
2323
}
2424

@@ -30,17 +30,13 @@ impl CassPrepared {
3030
.map(|col_spec| Arc::new(get_column_type(col_spec.typ())))
3131
.collect();
3232

33-
let result_col_data_types: Arc<Vec<Arc<CassDataType>>> = Arc::new(
34-
statement
35-
.get_result_set_col_specs()
36-
.iter()
37-
.map(|col_spec| Arc::new(get_column_type(col_spec.typ())))
38-
.collect(),
39-
);
33+
let result_metadata = Arc::new(CassResultMetadata::from_column_specs(
34+
statement.get_result_set_col_specs(),
35+
));
4036

4137
Self {
4238
variable_col_data_types,
43-
result_col_data_types,
39+
result_metadata,
4440
statement,
4541
}
4642
}

scylla-rust-wrapper/src/query_result.rs

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::argconv::*;
22
use crate::cass_error::CassError;
33
use crate::cass_types::{
4-
cass_data_type_type, get_column_type, CassDataType, CassValueType, MapDataType,
4+
cass_data_type_type, get_column_type, CassColumnSpec, CassDataType, CassValueType, MapDataType,
55
};
66
use crate::inet::CassInet;
77
use crate::metadata::{
@@ -18,50 +18,37 @@ use uuid::Uuid;
1818

1919
pub struct CassResult {
2020
pub rows: Option<Vec<CassRow>>,
21-
pub metadata: Arc<CassResultData>,
21+
pub metadata: Arc<CassResultMetadata>,
22+
pub tracing_id: Option<Uuid>,
23+
pub paging_state_response: PagingStateResponse,
2224
}
2325

24-
pub struct CassResultData {
25-
pub paging_state_response: PagingStateResponse,
26-
pub col_specs: Vec<ColumnSpec<'static>>,
27-
pub col_data_types: Arc<Vec<Arc<CassDataType>>>,
28-
pub tracing_id: Option<Uuid>,
26+
#[derive(Debug)]
27+
pub struct CassResultMetadata {
28+
pub col_specs: Vec<CassColumnSpec>,
2929
}
3030

31-
impl CassResultData {
32-
pub fn from_result_payload(
33-
paging_state_response: PagingStateResponse,
34-
col_specs: Vec<ColumnSpec<'static>>,
35-
maybe_col_data_types: Option<Arc<Vec<Arc<CassDataType>>>>,
36-
tracing_id: Option<Uuid>,
37-
) -> CassResultData {
38-
// `maybe_col_data_types` is:
39-
// - Some(_) for prepared statements executions
40-
// - None for unprepared (simple) queries executions
41-
let col_data_types = maybe_col_data_types.unwrap_or_else(|| {
42-
// This allocation is unfortunately necessary, because of the type of CassResultData::col_data_types.
43-
Arc::new(
44-
col_specs
45-
.iter()
46-
.map(|col_spec| Arc::new(get_column_type(col_spec.typ())))
47-
.collect(),
48-
)
49-
});
50-
51-
CassResultData {
52-
paging_state_response,
53-
col_specs,
54-
col_data_types,
55-
tracing_id,
56-
}
31+
impl CassResultMetadata {
32+
pub fn from_column_specs(col_specs: &[ColumnSpec<'_>]) -> CassResultMetadata {
33+
let col_specs = col_specs
34+
.iter()
35+
.map(|col_spec| {
36+
let name = col_spec.name().to_owned();
37+
let data_type = Arc::new(get_column_type(col_spec.typ()));
38+
39+
CassColumnSpec { name, data_type }
40+
})
41+
.collect();
42+
43+
CassResultMetadata { col_specs }
5744
}
5845
}
5946

6047
/// The lifetime of CassRow is bound to CassResult.
6148
/// It will be freed, when CassResult is freed.(see #[cass_result_free])
6249
pub struct CassRow {
6350
pub columns: Vec<CassValue>,
64-
pub result_metadata: Arc<CassResultData>,
51+
pub result_metadata: Arc<CassResultMetadata>,
6552
}
6653

6754
pub enum Value {
@@ -851,7 +838,7 @@ pub unsafe extern "C" fn cass_result_free(result_raw: *const CassResult) {
851838
#[no_mangle]
852839
pub unsafe extern "C" fn cass_result_has_more_pages(result: *const CassResult) -> cass_bool_t {
853840
let result = ptr_to_ref(result);
854-
(!result.metadata.paging_state_response.finished()) as cass_bool_t
841+
(!result.paging_state_response.finished()) as cass_bool_t
855842
}
856843

857844
#[no_mangle]
@@ -902,9 +889,9 @@ pub unsafe extern "C" fn cass_row_get_column_by_name_n(
902889
.col_specs
903890
.iter()
904891
.enumerate()
905-
.find(|(_, spec)| {
906-
is_case_sensitive && spec.name() == name_str
907-
|| !is_case_sensitive && spec.name().eq_ignore_ascii_case(name_str)
892+
.find(|(_, col_spec)| {
893+
is_case_sensitive && col_spec.name == name_str
894+
|| !is_case_sensitive && col_spec.name.eq_ignore_ascii_case(name_str)
908895
})
909896
.map(|(index, _)| {
910897
return match row_from_raw.columns.get(index) {
@@ -929,8 +916,12 @@ pub unsafe extern "C" fn cass_result_column_name(
929916
return CassError::CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS;
930917
}
931918

932-
let column_spec: &ColumnSpec = result_from_raw.metadata.col_specs.get(index_usize).unwrap();
933-
let column_name = column_spec.name();
919+
let column_name = &result_from_raw
920+
.metadata
921+
.col_specs
922+
.get(index_usize)
923+
.unwrap()
924+
.name;
934925

935926
write_str_to_c(column_name, name, name_length);
936927

@@ -961,9 +952,9 @@ pub unsafe extern "C" fn cass_result_column_data_type(
961952

962953
result_from_raw
963954
.metadata
964-
.col_data_types
955+
.col_specs
965956
.get(index_usize)
966-
.map(Arc::as_ptr)
957+
.map(|col_spec| Arc::as_ptr(&col_spec.data_type))
967958
.unwrap_or(std::ptr::null())
968959
}
969960

@@ -1365,7 +1356,7 @@ pub unsafe extern "C" fn cass_result_paging_state_token(
13651356

13661357
let result_from_raw = ptr_to_ref(result);
13671358

1368-
match &result_from_raw.metadata.paging_state_response {
1359+
match &result_from_raw.paging_state_response {
13691360
PagingStateResponse::HasMorePages { state } => match state.as_bytes_slice() {
13701361
Some(result_paging_state) => {
13711362
*paging_state_size = result_paging_state.len() as u64;
@@ -1404,7 +1395,9 @@ mod tests {
14041395
session::create_cass_rows_from_rows,
14051396
};
14061397

1407-
use super::{cass_result_column_count, cass_result_column_type, CassResult, CassResultData};
1398+
use super::{
1399+
cass_result_column_count, cass_result_column_type, CassResult, CassResultMetadata,
1400+
};
14081401

14091402
fn col_spec(name: &'static str, typ: ColumnType<'static>) -> ColumnSpec<'static> {
14101403
ColumnSpec::borrowed(name, typ, TableSpec::borrowed("ks", "tbl"))
@@ -1414,19 +1407,14 @@ mod tests {
14141407
const SECOND_COLUMN_NAME: &str = "varint_col";
14151408
const THIRD_COLUMN_NAME: &str = "list_double_col";
14161409
fn create_cass_rows_result() -> CassResult {
1417-
let metadata = Arc::new(CassResultData::from_result_payload(
1418-
PagingStateResponse::NoMorePages,
1419-
vec![
1420-
col_spec(FIRST_COLUMN_NAME, ColumnType::BigInt),
1421-
col_spec(SECOND_COLUMN_NAME, ColumnType::Varint),
1422-
col_spec(
1423-
THIRD_COLUMN_NAME,
1424-
ColumnType::List(Box::new(ColumnType::Double)),
1425-
),
1426-
],
1427-
None,
1428-
None,
1429-
));
1410+
let metadata = Arc::new(CassResultMetadata::from_column_specs(&[
1411+
col_spec(FIRST_COLUMN_NAME, ColumnType::BigInt),
1412+
col_spec(SECOND_COLUMN_NAME, ColumnType::Varint),
1413+
col_spec(
1414+
THIRD_COLUMN_NAME,
1415+
ColumnType::List(Box::new(ColumnType::Double)),
1416+
),
1417+
]));
14301418

14311419
let rows = create_cass_rows_from_rows(
14321420
vec![Row {
@@ -1446,6 +1434,8 @@ mod tests {
14461434
CassResult {
14471435
rows: Some(rows),
14481436
metadata,
1437+
tracing_id: None,
1438+
paging_state_response: PagingStateResponse::NoMorePages,
14491439
}
14501440
}
14511441

@@ -1532,15 +1522,12 @@ mod tests {
15321522
}
15331523

15341524
fn create_non_rows_cass_result() -> CassResult {
1535-
let metadata = Arc::new(CassResultData::from_result_payload(
1536-
PagingStateResponse::NoMorePages,
1537-
vec![],
1538-
None,
1539-
None,
1540-
));
1525+
let metadata = Arc::new(CassResultMetadata::from_column_specs(&[]));
15411526
CassResult {
15421527
rows: None,
15431528
metadata,
1529+
tracing_id: None,
1530+
paging_state_response: PagingStateResponse::NoMorePages,
15441531
}
15451532
}
15461533

0 commit comments

Comments
 (0)