Skip to content

Commit b9660be

Browse files
authored
[Storage] contiguous::Journal::append_many returns error on empty argument (#3562)
1 parent 5612313 commit b9660be

5 files changed

Lines changed: 16 additions & 9 deletions

File tree

storage/src/journal/contiguous/fixed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,10 @@ impl<E: Context, A: CodecFixedShared> Journal<E, A> {
655655
/// Append items to the journal, returning the position of the last item appended.
656656
///
657657
/// Acquires the write lock once for all items instead of per-item.
658-
/// No-ops if items is empty, returning the current size (next append position).
658+
/// Returns [Error::EmptyAppend] if items is empty.
659659
pub async fn append_many<'a>(&'a self, items: Many<'a, A>) -> Result<u64, Error> {
660660
if items.is_empty() {
661-
return Ok(self.inner.read().await.size);
661+
return Err(Error::EmptyAppend);
662662
}
663663

664664
// Encode all items into a single contiguous buffer before taking the write guard.

storage/src/journal/contiguous/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub trait Mutable: Contiguous + Send + Sync {
114114
/// The default implementation calls [Self::append] in a loop. Concrete implementations
115115
/// may override this to acquire the write lock once for all items.
116116
///
117-
/// No-ops if items is empty, returning the current size (next append position).
117+
/// Returns [Error::EmptyAppend] if items is empty.
118118
fn append_many<'a>(
119119
&'a mut self,
120120
items: Many<'a, Self::Item>,
@@ -123,6 +123,9 @@ pub trait Mutable: Contiguous + Send + Sync {
123123
Self::Item: Sync,
124124
{
125125
async move {
126+
if items.is_empty() {
127+
return Err(Error::EmptyAppend);
128+
}
126129
let mut last_pos = self.size().await;
127130
match items {
128131
Many::Flat(items) => {

storage/src/journal/contiguous/tests.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ where
11101110
}
11111111
}
11121112

1113-
/// Test append_many with empty slice is a no-op.
1113+
/// Test append_many with empty slice returns an error.
11141114
async fn test_append_many_empty<F, J>(factory: &F)
11151115
where
11161116
F: Fn(String) -> BoxFuture<'static, Result<J, Error>>,
@@ -1122,9 +1122,11 @@ where
11221122
journal.append(&10).await.unwrap();
11231123
journal.append(&20).await.unwrap();
11241124

1125-
// append_many with empty slice should no-op and return current size.
1126-
let pos = journal.append_many(Many::Flat(&[])).await.unwrap();
1127-
assert_eq!(pos, 2);
1125+
// append_many with empty slice should return an error.
1126+
assert!(matches!(
1127+
journal.append_many(Many::Flat(&[])).await,
1128+
Err(Error::EmptyAppend)
1129+
));
11281130
assert_eq!(get_bounds(&journal).await.end, 2);
11291131

11301132
journal.destroy().await.unwrap();

storage/src/journal/contiguous/variable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,10 @@ impl<E: Context, V: CodecShared> Journal<E, V> {
521521
/// Append items to the journal, returning the position of the last item appended.
522522
///
523523
/// Acquires the write lock once for all items instead of per-item.
524-
/// No-ops if items is empty, returning the current size (next append position).
524+
/// Returns [Error::EmptyAppend] if items is empty.
525525
pub async fn append_many<'a>(&'a self, items: Many<'a, V>) -> Result<u64, Error> {
526526
if items.is_empty() {
527-
return Ok(self.inner.read().await.size);
527+
return Err(Error::EmptyAppend);
528528
}
529529

530530
// Encode before grabbing write guard.

storage/src/journal/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ pub enum Error {
6363
InvalidConfiguration(String),
6464
#[error("checksum mismatch: expected={0}, found={1}")]
6565
ChecksumMismatch(u32, u32),
66+
#[error("empty append")]
67+
EmptyAppend,
6668
}

0 commit comments

Comments
 (0)