dracoooooo commented on code in PR #1556:
URL: https://github.com/apache/horaedb/pull/1556#discussion_r1740576597
##########
src/wal/src/local_storage_impl/segment.rs:
##########
@@ -502,14 +542,38 @@ impl Region {
next_sequence_num += 1;
}
- // TODO: spawn a new task to write to segment
- // TODO: maybe need a write mutex?
+ {
+ let segment = self.get_segment(*current)?;
+ let segment = segment.read().unwrap();
+
+ // Check if the current segment has enough space for the new data
+ // If not, create a new segment and update the current segment ID
+ if segment.size + data.len() as u64 > segment.max_file_size {
+ let new_segment_id = *current + 1;
+ let new_segment = Segment::new(
+ format!("{}/segment_{}.wal", self._segment_dir,
new_segment_id),
+ new_segment_id,
+ self.max_file_size,
+ )?;
+ let new_segment = Arc::new(RwLock::new(new_segment));
+
+ let mut all_segments = self.all_segments.lock().unwrap();
+ all_segments.insert(new_segment_id, new_segment.clone());
+ *current = new_segment_id;
+ }
+ }
+
+ let segment = self.get_segment(*current)?;
+ let mut segment = segment.write().unwrap();
Review Comment:
`current` is essentially a write mutex, so this newly created segment will
not have any other threads writing to it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]