@@ -176,7 +176,7 @@ pub struct SegmentedLog {
176
176
}
177
177
178
178
impl SegmentedLog {
179
- /// Append a record to the log.
179
+ /// Append a record to the log and return its ID .
180
180
///
181
181
/// After this function returned, the data is guaranteed to be persisted.
182
182
pub fn append ( & mut self , data : & [ u8 ] ) -> Result < RecordId > {
@@ -187,7 +187,7 @@ impl SegmentedLog {
187
187
) ) ;
188
188
}
189
189
190
- let record_id = self . gen_record_id ( ) ;
190
+ let record_id = self . end_live . next ( ) ;
191
191
192
192
// If the head segment is full or doesn't exist, create a new one.
193
193
let root_dir_fsync = match self . head_segment_writer {
@@ -210,6 +210,9 @@ impl SegmentedLog {
210
210
writer. write_header ( data. len ( ) as u32 , record_id) ?;
211
211
writer. write_payload ( data) ?;
212
212
writer. fsync ( ) ?;
213
+
214
+ // Once the write succeeded, update the live range.
215
+ self . end_live = record_id;
213
216
if self . start_live . is_nil ( ) {
214
217
self . start_live = record_id;
215
218
}
@@ -255,13 +258,6 @@ impl SegmentedLog {
255
258
last_segment_id. wrapping_add ( 1 )
256
259
}
257
260
258
- /// Generate a new record ID and update the live end.
259
- fn gen_record_id ( & mut self ) -> RecordId {
260
- let id = self . end_live . next ( ) ;
261
- self . end_live = id;
262
- id
263
- }
264
-
265
261
/// Prunes the items from the back of the log (oldest records).
266
262
///
267
263
/// Prunes segments that lie outside of the new live range, i.e. deletes the old segments.
0 commit comments