Skip to content

Commit 05a41bd

Browse files
committed
frame: document various other items and modules
1 parent 7dba955 commit 05a41bd

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

scylla-cql/src/frame/frame_errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Low-level errors that can occur during CQL frame parsing and serialization.
2+
13
use std::error::Error;
24
use std::sync::Arc;
35

@@ -67,6 +69,8 @@ pub enum FrameHeaderParseError {
6769
#[error("Received frame marked as coming from a client")]
6870
FrameFromClient,
6971

72+
/// Received a frame marked as coming from a server, while expecting a frame
73+
/// coming from a client.
7074
// FIXME: this should not belong here. User always expects a frame from server.
7175
// This variant is only used in scylla-proxy - need to investigate it later.
7276
#[error("Received frame marked as coming from the server")]
@@ -151,6 +155,7 @@ pub enum CqlResponseParseError {
151155
}
152156

153157
impl CqlResponseParseError {
158+
/// Retrieves the kind of CQL response that this error corresponds to.
154159
pub fn to_response_kind(&self) -> CqlResponseKind {
155160
match self {
156161
CqlResponseParseError::CqlErrorParseError(_) => CqlResponseKind::Error,

scylla-cql/src/frame/mod.rs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//! Abstractions of the CQL wire protocol:
2+
//! - request and response frames' representation and ser/de;
3+
//! - frame header and body;
4+
//! - serialization and deserialization of low-level CQL protocol types;
5+
//! - protocol features negotiation;
6+
//! - compression, tracing, custom payload support;
7+
//! - consistency levels;
8+
//! - errors that can occur during the above operations.
9+
//!
10+
111
pub mod frame_errors;
212
pub mod protocol_features;
313
pub mod request;
@@ -25,9 +35,17 @@ const HEADER_SIZE: usize = 9;
2535

2636
pub mod flag {
2737
//! Frame flags
38+
39+
/// The frame contains a compressed body.
2840
pub const COMPRESSION: u8 = 0x01;
41+
42+
/// The frame contains tracing ID.
2943
pub const TRACING: u8 = 0x02;
44+
45+
/// The frame contains a custom payload.
3046
pub const CUSTOM_PAYLOAD: u8 = 0x04;
47+
48+
/// The frame contains warnings.
3149
pub const WARNING: u8 = 0x08;
3250
}
3351

@@ -54,6 +72,7 @@ pub enum Compression {
5472
}
5573

5674
impl Compression {
75+
/// Returns the string representation of the compression algorithm.
5776
pub fn as_str(&self) -> &'static str {
5877
match self {
5978
Compression::Lz4 => "lz4",
@@ -89,11 +108,21 @@ impl Display for Compression {
89108
}
90109
}
91110

111+
/// A serialized CQL request frame, nearly ready to be sent over the wire.
112+
///
113+
/// The only difference from a real frame is that it does not contain the stream number yet.
114+
/// The stream number is set by the `set_stream` method before sending.
92115
pub struct SerializedRequest {
93116
data: Vec<u8>,
94117
}
95118

96119
impl SerializedRequest {
120+
/// Creates a new serialized request frame from a request object.
121+
///
122+
/// # Parameters
123+
/// - `req`: The request object to serialize. Must implement `SerializableRequest`.
124+
/// - `compression`: An optional compression algorithm to use for the request body.
125+
/// - `tracing`: A boolean indicating whether to request tracing information in the response.
97126
pub fn make<R: SerializableRequest>(
98127
req: &R,
99128
compression: Option<Compression>,
@@ -125,20 +154,32 @@ impl SerializedRequest {
125154
Ok(Self { data })
126155
}
127156

157+
/// Sets the stream number for this request frame.
158+
/// Intended to be called before sending the request,
159+
/// once a stream ID has been assigned.
128160
pub fn set_stream(&mut self, stream: i16) {
129161
self.data[2..4].copy_from_slice(&stream.to_be_bytes());
130162
}
131163

164+
/// Returns the serialized frame data, including the header and body.
132165
pub fn get_data(&self) -> &[u8] {
133166
&self.data[..]
134167
}
135168
}
136169

137-
// Parts of the frame header which are not determined by the request/response type.
170+
/// Parts of the frame header which are not determined by the request/response type.
138171
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
139172
pub struct FrameParams {
173+
/// The version of the frame protocol. Currently, only version 4 is supported.
174+
/// The most significant bit (0x80) is treated specially:
175+
/// it indicates whether the frame is from the client or server.
140176
pub version: u8,
177+
178+
/// Flags for the frame, indicating features like compression, tracing, etc.
141179
pub flags: u8,
180+
181+
/// The stream ID for this frame, which allows matching requests and responses
182+
/// in a multiplexed connection.
142183
pub stream: i16,
143184
}
144185

@@ -152,6 +193,8 @@ impl Default for FrameParams {
152193
}
153194
}
154195

196+
/// Reads a response frame from the provided reader (usually, a socket).
197+
/// Then parses and validates the frame header and extracts the body.
155198
pub async fn read_response_frame(
156199
reader: &mut (impl AsyncRead + Unpin),
157200
) -> Result<(FrameParams, ResponseOpcode, Bytes), FrameHeaderParseError> {
@@ -203,13 +246,29 @@ pub async fn read_response_frame(
203246
Ok((frame_params, opcode, raw_body.into_inner().into()))
204247
}
205248

249+
/// Represents the already parsed response body extensions,
250+
/// including trace ID, warnings, and custom payload,
251+
/// and the remaining body raw data.
206252
pub struct ResponseBodyWithExtensions {
253+
/// The trace ID if tracing was requested in the request.
254+
///
255+
/// This can be used to issue a follow-up request to the server
256+
/// to get detailed tracing information about the request.
207257
pub trace_id: Option<Uuid>,
258+
259+
/// Warnings returned by the server, if any.
208260
pub warnings: Vec<String>,
209-
pub body: Bytes,
261+
262+
/// Custom payload (see [the CQL protocol description of the feature](https://github.com/apache/cassandra/blob/a39f3b066f010d465a1be1038d5e06f1e31b0391/doc/native_protocol_v4.spec#L276))
263+
/// returned by the server, if any.
210264
pub custom_payload: Option<HashMap<String, Bytes>>,
265+
266+
/// The remaining body data after parsing the extensions.
267+
pub body: Bytes,
211268
}
212269

270+
/// Decompresses the response body if compression is enabled,
271+
/// and parses any extensions like trace ID, warnings, and custom payload.
213272
pub fn parse_response_body_extensions(
214273
flags: u8,
215274
compression: Option<Compression>,
@@ -260,11 +319,13 @@ pub fn parse_response_body_extensions(
260319
Ok(ResponseBodyWithExtensions {
261320
trace_id,
262321
warnings,
263-
body,
264322
custom_payload,
323+
body,
265324
})
266325
}
267326

327+
/// Compresses the request body using the specified compression algorithm,
328+
/// appending the compressed data to the provided output buffer.
268329
pub fn compress_append(
269330
uncomp_body: &[u8],
270331
compression: Compression,
@@ -291,6 +352,8 @@ pub fn compress_append(
291352
}
292353
}
293354

355+
/// Deompresses the response body using the specified compression algorithm
356+
/// and returns the decompressed data as an owned buffer.
294357
pub fn decompress(
295358
mut comp_body: &[u8],
296359
compression: Compression,

scylla-cql/src/frame/server_event_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
//! This module defines the `EventType` enum, which represents different types of CQL events.
2+
// TODO(2.0): Move this to a more appropriate location in the crate structure.
3+
14
use std::fmt;
25
use std::str::FromStr;
36

47
use super::frame_errors::CqlEventParseError;
58

9+
/// Represents the type of a CQL event.
610
// Check triggers because all variants end with "Change".
711
// TODO(2.0): Remove the "Change" postfix from variants.
812
#[expect(clippy::enum_variant_names)]
913
pub enum EventType {
14+
/// Represents a change in the cluster topology, such as node addition or removal.
1015
TopologyChange,
16+
/// Represents a change in the status of a node, such as up or down.
1117
StatusChange,
18+
/// Represents a change in the schema, such as table creation or modification.
1219
SchemaChange,
1320
}
1421

scylla-cql/src/frame/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl TryFrom<i16> for SerialConsistency {
8787
}
8888

8989
impl Consistency {
90+
/// Checks if the consistency is a serial consistency.
9091
pub fn is_serial(&self) -> bool {
9192
matches!(self, Consistency::Serial | Consistency::LocalSerial)
9293
}

0 commit comments

Comments
 (0)