Skip to content

Commit b476b2d

Browse files
committed
Make read() only allowed in tests
1 parent 467514f commit b476b2d

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

downstairs/src/extent.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::fmt::Debug;
55
use std::fs::{File, OpenOptions};
66

77
use anyhow::{anyhow, bail, Context, Result};
8-
use bytes::BufMut;
98
use nix::unistd::{sysconf, SysconfVar};
109

1110
use serde::{Deserialize, Serialize};
@@ -54,24 +53,28 @@ pub(crate) trait ExtentInner: Send + Sync + Debug {
5453
requests: &[crucible_protocol::ReadRequest],
5554
iov_max: usize,
5655
out: &mut BytesMut,
57-
) -> Result<(), CrucibleError> {
58-
// Call the existing read implementation, then do serialization by hand
59-
// here. Note that we serialize the individual ReadResponse values, but
60-
// not the leading size, because that's already placed into the buffer.
61-
let data = self.read(job_id, requests, iov_max)?;
62-
let mut w = out.writer();
63-
for d in data {
64-
bincode::serialize_into(&mut w, &d).unwrap();
65-
}
66-
Ok(())
67-
}
56+
) -> Result<(), CrucibleError>;
6857

58+
/// Read the given requests into a `Vec<ReadResponse>`
59+
///
60+
/// The default implementation defers to `read_raw`, then deserializes the
61+
/// resulting buffer.
62+
///
63+
/// This function is only built during unit tests, because it's less
64+
/// efficient than `read_raw`.
65+
#[cfg(test)]
6966
fn read(
7067
&mut self,
7168
job_id: JobId,
7269
requests: &[crucible_protocol::ReadRequest],
7370
iov_max: usize,
74-
) -> Result<Vec<crucible_protocol::ReadResponse>, CrucibleError>;
71+
) -> Result<Vec<crucible_protocol::ReadResponse>, CrucibleError> {
72+
use bytes::BufMut;
73+
let mut b = BytesMut::new();
74+
b.put_u64_le(requests.len() as u64);
75+
self.read_raw(job_id, requests, iov_max, &mut b)?;
76+
Ok(bincode::deserialize(&b).unwrap())
77+
}
7578

7679
fn write(
7780
&mut self,

downstairs/src/extent_inner_raw.rs

-12
Original file line numberDiff line numberDiff line change
@@ -430,18 +430,6 @@ impl ExtentInner for RawInner {
430430
let out = RawInner::get_block_contexts(self, block, count)?;
431431
Ok(out.into_iter().map(|v| v.into_iter().collect()).collect())
432432
}
433-
434-
fn read(
435-
&mut self,
436-
job_id: JobId,
437-
requests: &[crucible_protocol::ReadRequest],
438-
iov_max: usize,
439-
) -> Result<Vec<crucible_protocol::ReadResponse>, CrucibleError> {
440-
let mut b = BytesMut::new();
441-
b.put_u64_le(requests.len() as u64);
442-
self.read_raw(job_id, requests, iov_max, &mut b)?;
443-
Ok(bincode::deserialize(&b).unwrap())
444-
}
445433
}
446434

447435
impl RawInner {

downstairs/src/extent_inner_sqlite.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
use crucible_protocol::EncryptionContext;
1010

1111
use anyhow::{bail, Result};
12+
use bytes::{BufMut, BytesMut};
1213
use rusqlite::{params, Connection, Transaction};
1314
use slog::{error, Logger};
1415

@@ -41,13 +42,22 @@ impl ExtentInner for SqliteInner {
4142
self.0.lock().unwrap().flush(new_flush, new_gen, job_id)
4243
}
4344

44-
fn read(
45+
fn read_raw(
4546
&mut self,
4647
job_id: JobId,
4748
requests: &[crucible_protocol::ReadRequest],
4849
iov_max: usize,
49-
) -> Result<Vec<crucible_protocol::ReadResponse>, CrucibleError> {
50-
self.0.lock().unwrap().read(job_id, requests, iov_max)
50+
out: &mut BytesMut,
51+
) -> Result<(), CrucibleError> {
52+
// Call the existing read implementation, then do serialization by hand
53+
// here. Note that we serialize the individual ReadResponse values, but
54+
// not the leading size, because that's already placed into the buffer.
55+
let data = self.0.lock().unwrap().read(job_id, requests, iov_max)?;
56+
let mut w = out.writer();
57+
for d in data {
58+
bincode::serialize_into(&mut w, &d).unwrap();
59+
}
60+
Ok(())
5161
}
5262

5363
fn write(

0 commit comments

Comments
 (0)