Skip to content

Commit 6aeb9aa

Browse files
committed
chore: refactor request_id
1 parent 05fd172 commit 6aeb9aa

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/bulk.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ use crate::table::{Column, Row, Table};
4040
use crate::{error, Result};
4141
use snafu::{ensure, ResultExt};
4242

43+
pub type RequestId = i64;
44+
4345
// Macro to generate array conversion for simple types
4446
macro_rules! build_primitive_array {
4547
($rows:expr, $col_idx:expr, $getter:ident, $array_type:ty) => {{
@@ -157,16 +159,16 @@ pub struct BulkStreamWriter {
157159
table_schema: Vec<Column>,
158160
// Cache the Arrow schema to avoid recreating it for each batch
159161
arrow_schema: Arc<Schema>,
160-
next_request_id: i64,
162+
next_request_id: RequestId,
161163
encoder: FlightEncoder,
162164
schema_sent: bool,
163165
// Parallel processing fields
164166
parallelism: usize,
165167
timeout: Duration,
166168
// Track pending requests: request_id -> sent_time
167-
pending_requests: HashMap<i64, Instant>,
169+
pending_requests: HashMap<RequestId, Instant>,
168170
// Cache completed responses that were processed but not yet retrieved
169-
completed_responses: HashMap<i64, DoPutResponse>,
171+
completed_responses: HashMap<RequestId, DoPutResponse>,
170172
}
171173

172174
impl BulkStreamWriter {
@@ -219,23 +221,14 @@ impl BulkStreamWriter {
219221

220222
/// Write rows to the stream using the fixed table schema
221223
pub async fn write_rows(&mut self, rows: Vec<Row>) -> Result<DoPutResponse> {
222-
if rows.is_empty() {
223-
// Return a dummy response for empty input
224-
return Ok(DoPutResponse::new(0, 0));
225-
}
226-
227224
// Use the async implementation and wait for the response
228225
let request_id = self.write_rows_async(rows).await?;
229226
self.wait_for_response(request_id).await
230227
}
231228

232229
/// Submit rows for writing without waiting for response
233230
/// Returns a request_id that can be used to wait for the specific response
234-
pub async fn write_rows_async(&mut self, rows: Vec<Row>) -> Result<i64> {
235-
if rows.is_empty() {
236-
return Ok(0);
237-
}
238-
231+
pub async fn write_rows_async(&mut self, rows: Vec<Row>) -> Result<RequestId> {
239232
let record_batch = self.rows_to_record_batch(&rows)?;
240233
let request_id = self.submit_record_batch(record_batch).await?;
241234

@@ -381,8 +374,7 @@ impl BulkStreamWriter {
381374
}
382375

383376
// Send the request
384-
self.next_request_id += 1;
385-
let request_id = self.next_request_id;
377+
let request_id = self.next_request_id();
386378
let message = FlightMessage::RecordBatch(batch);
387379
let mut data = self.encoder.encode(message);
388380
let metadata = DoPutMetadata::new(request_id);
@@ -437,7 +429,7 @@ impl BulkStreamWriter {
437429

438430
/// Convert rows to Arrow RecordBatch using cached schema
439431
fn rows_to_record_batch(&self, rows: &[Row]) -> Result<RecordBatch> {
440-
ensure!(!rows.is_empty(), error::EmptyTableSnafu);
432+
ensure!(!rows.is_empty(), error::EmptyRowsSnafu);
441433

442434
// Convert all rows to arrays
443435
let arrays = self.rows_to_arrays(rows)?;
@@ -618,6 +610,15 @@ impl BulkStreamWriter {
618610

619611
Ok(all_responses)
620612
}
613+
614+
fn next_request_id(&mut self) -> RequestId {
615+
// Skip ID 0 as it's reserved for special cases
616+
self.next_request_id = self.next_request_id.wrapping_add(1);
617+
if self.next_request_id == 0 {
618+
self.next_request_id = 1;
619+
}
620+
self.next_request_id
621+
}
621622
}
622623

623624
// Helper function to convert ColumnDataType to Arrow DataType

src/error.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ pub enum Error {
9696
location: Location,
9797
},
9898

99-
#[snafu(display("Cannot create RecordBatch from empty table"))]
100-
EmptyTable {
101-
#[snafu(implicit)]
102-
location: Location,
103-
},
104-
10599
#[snafu(display("Failed to create Arrow RecordBatch"))]
106100
CreateRecordBatch {
107101
#[snafu(source)]

0 commit comments

Comments
 (0)