From a30206a665eea2a250c9abddda507e16f292c092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Fri, 30 Oct 2020 11:13:45 +0100 Subject: [PATCH] Prefer using iterator put_current instead of a get put method --- src/update/index_documents/mod.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/update/index_documents/mod.rs b/src/update/index_documents/mod.rs index b9db4c72d..2551a2f46 100644 --- a/src/update/index_documents/mod.rs +++ b/src/update/index_documents/mod.rs @@ -108,13 +108,17 @@ fn merge_into_lmdb_database( }, WriteMethod::GetMergePut => { while let Some((k, v)) = in_iter.next()? { - match database.get::<_, ByteSlice, ByteSlice>(wtxn, k)? { - Some(old_val) => { + let mut iter = database.prefix_iter_mut::<_, ByteSlice, ByteSlice>(wtxn, k)?; + match iter.next().transpose()? { + Some((key, old_val)) if key == k => { let vals = vec![Cow::Borrowed(old_val), Cow::Borrowed(v)]; let val = merge(k, &vals).expect("merge failed"); - database.put::<_, ByteSlice, ByteSlice>(wtxn, k, &val)? + iter.put_current(k, &val)?; + }, + _ => { + drop(iter); + database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?; }, - None => database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?, } } }, @@ -145,13 +149,17 @@ fn write_into_lmdb_database( }, WriteMethod::GetMergePut => { while let Some((k, v)) = reader.next()? { - match database.get::<_, ByteSlice, ByteSlice>(wtxn, k)? { - Some(old_val) => { + let mut iter = database.prefix_iter_mut::<_, ByteSlice, ByteSlice>(wtxn, k)?; + match iter.next().transpose()? { + Some((key, old_val)) if key == k => { let vals = vec![Cow::Borrowed(old_val), Cow::Borrowed(v)]; let val = merge(k, &vals).expect("merge failed"); - database.put::<_, ByteSlice, ByteSlice>(wtxn, k, &val)? + iter.put_current(k, &val)?; + }, + _ => { + drop(iter); + database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?; }, - None => database.put::<_, ByteSlice, ByteSlice>(wtxn, k, v)?, } } }