append(): check for not_modified
This commit is contained in:
parent
91d69474ed
commit
602cc1f0f3
16
src/lib.rs
16
src/lib.rs
|
@ -153,7 +153,21 @@ impl<B: StoreBackend> Store<B> {
|
|||
}
|
||||
|
||||
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)? };
|
||||
Ok(())
|
||||
}
|
||||
|
|
14
src/test.rs
14
src/test.rs
|
@ -130,6 +130,20 @@ fn write_many() {
|
|||
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]
|
||||
fn no_flash() {
|
||||
let mut store = Store::new(no_flash::NoFlash);
|
||||
|
|
Loading…
Reference in New Issue