Writes a chunk of data atomically to the file sequence. Returns the byte offset where the chunk begins.
(&self, buffer: &[u8])
| 68 | /// |
| 69 | /// Returns the byte offset where the chunk begins. |
| 70 | pub fn write_all(&self, buffer: &[u8]) -> Result<u64> { |
| 71 | // Acquire lock. |
| 72 | // Since we mostly do memcpy, contention is extremely low. |
| 73 | let mut state = self |
| 74 | .inner |
| 75 | .lock() |
| 76 | .map_err(|_| ParcodeError::Internal("Writer mutex poisoned".into()))?; |
| 77 | |
| 78 | let start_offset = state.current_offset; |
| 79 | |
| 80 | // This call usually just copies memory. |
| 81 | // It only blocks for disk I/O once every ~16MB of data. |
| 82 | state.writer.write_all(buffer)?; |
| 83 | |
| 84 | state.current_offset += buffer.len() as u64; |
| 85 | |
| 86 | Ok(start_offset) |
| 87 | } |
| 88 | |
| 89 | /// Forces data to disk. |
| 90 | pub fn flush(&self) -> Result<()> { |