append(): check for not_modified

This commit is contained in:
Astro 2020-12-11 20:13:33 +01:00
parent 91d69474ed
commit 602cc1f0f3
2 changed files with 29 additions and 1 deletions

View File

@ -153,7 +153,21 @@ impl<B: StoreBackend> Store<B> {
} }
fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> { fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> {
let free_offset = self.get_bytes_used()?; let free_offset = {
let mut iter = Iter::new(self.backend.data().as_ref());
let mut not_modified = false;
while let Some(result) = iter.next() {
let (record_key, record_value) = result?;
if key.as_bytes() == record_key {
not_modified = value == record_value;
}
}
if not_modified {
return Ok(())
}
iter.offset
};
unsafe { self.append_at(free_offset, key.as_bytes(), value)? }; unsafe { self.append_at(free_offset, key.as_bytes(), value)? };
Ok(()) Ok(())
} }

View File

@ -130,6 +130,20 @@ fn write_many() {
assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM"); assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM");
} }
#[test]
#[cfg(feature = "postcard-values")]
fn write_value_many() {
let mut store = make_store();
store.write_value("quux", (false, 3u8, -42i16)).unwrap();
for i in 0..1000u32 {
store.write_value("bar", i).unwrap();
assert_eq!(store.read_value("bar"), Ok(Some(i)));
store.write_value("foo", i % 2 == 0).unwrap();
assert_eq!(store.read_value("foo"), Ok(Some(i % 2 == 0)));
}
assert_eq!(store.read_value("quux"), Ok(Some((false, 3u8, -42i16))));
}
#[test] #[test]
fn no_flash() { fn no_flash() {
let mut store = Store::new(no_flash::NoFlash); let mut store = Store::new(no_flash::NoFlash);