Skip to content

Commit

Permalink
fix: use Entry Table insert instead of upsert during compact
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrezot committed Nov 27, 2023
1 parent 24dd16a commit 39ee07e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
7 changes: 2 additions & 5 deletions src/edx/entry_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,8 @@ impl<const VALUE_LENGTH: usize, Edx: EdxStore<VALUE_LENGTH>> DxEnc<VALUE_LENGTH>
.map(Into::into)
}

async fn insert(
&self,
_values: HashMap<Token, Self::EncryptedValue>,
) -> Result<(), Self::Error> {
panic!("The Entry Table does not do any insert.")
async fn insert(&self, items: HashMap<Token, Self::EncryptedValue>) -> Result<(), Self::Error> {
self.0.insert(items.into()).await.map_err(Error::Callback)
}

fn prepare(
Expand Down
20 changes: 4 additions & 16 deletions src/findex_mm/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,23 +222,11 @@ impl<
)));
};

let upsert_results = self.entry_table.upsert(HashMap::new(), new_entries).await;

let res = if let Ok(upsert_results) = upsert_results {
if upsert_results.is_empty() {
Ok(())
} else {
Err(Error::Crypto(format!(
"A conflict occurred during the upsert operation. All modifications were \
reverted. ({upsert_results:?})"
)))
}
} else {
Err(Error::Crypto(
"An error occurred during the upsert operation. All modifications were reverted."
.to_string(),
let res = self.entry_table.insert(new_entries).await.map_err(|e| {
Error::Crypto(format!(
"An error occurred during the `insert` operation, all modifications were reverted: {e}"
))
};
});

if res.is_ok() {
self.chain_table.delete(old_links).await?;
Expand Down
6 changes: 3 additions & 3 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait Index<EntryTable: DxEnc<ENTRY_LENGTH>, ChainTable: DxEnc<LINK_LENGTH>>
/// Searches the index for the given keywords.
///
/// The `interrupt` callback is fed with the results of each graph search
/// iteration. Iterations are stopped if the `interrupt` return `true`.
/// iteration. Iterations are stopped if the `interrupt` returns `true`.
async fn search<
F: Future<Output = Result<bool, String>>,
Interrupt: Fn(HashMap<Keyword, HashSet<IndexedValue<Keyword, Location>>>) -> F,
Expand All @@ -57,7 +57,7 @@ pub trait Index<EntryTable: DxEnc<ENTRY_LENGTH>, ChainTable: DxEnc<LINK_LENGTH>>

/// Adds the given associations to the index.
///
/// Returns the set of keywords added as keys to the index.
/// Returns the set of keywords added as new keys to the index.
async fn add(
&self,
key: &UserKey,
Expand All @@ -71,7 +71,7 @@ pub trait Index<EntryTable: DxEnc<ENTRY_LENGTH>, ChainTable: DxEnc<LINK_LENGTH>>
/// effectively increasing the index size. The compact operation is in charge of removing
/// associations that have been negated.
///
/// Returns the set of keywords added to the index.
/// Returns the set of keywords added as new keys to the index.
async fn delete(
&self,
key: &UserKey,
Expand Down
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ pub use parameters::*;
mod example {
use std::collections::HashSet;

use cosmian_crypto_core::{reexport::rand_core::SeedableRng, CsRng, RandomFixedSizeCBytes};

use crate::{
ChainTable, DxEnc, EntryTable, Findex, InMemoryEdx, Index, IndexedValue,
IndexedValueToKeywordsMap, Keyword, KeywordToDataMap, Keywords, Label, Location,
IndexedValueToKeywordsMap, Keyword, KeywordToDataMap, Keywords, Label, Location, UserKey,
};

#[actix_rt::test]
Expand All @@ -49,23 +51,23 @@ mod example {
* Findex instantiation.
*/

let mut rng = CsRng::from_entropy();
// Let's create a new key for our index.
let key = UserKey::new(&mut rng);
// Findex uses a public label with the private key. Let's generate a new label.
let label = Label::from("My public label");

// Let's create a new index using the provided Entry and Chain table implementation and the
// in-memory EDX implementation provided for test purpose.
let index = Findex::new(
EntryTable::setup(InMemoryEdx::default()),
ChainTable::setup(InMemoryEdx::default()),
);

// Let's create a new key for our index.
let key = index.keygen();

// Findex uses a public label with the private key. Let's generate a new label.
let label = Label::from("My public label");

////////////////////////////////////////////////////////////////////////////////
// //
// Let's associate `loc1` to `kwd1`, `loc2` to `kwd2` and `kwd2` to `kwd1`. //
// The futur state of the index can be represented as a JSON: //
// The future state of the index can be represented as a JSON: //
// //
// ```json //
// { //
Expand Down Expand Up @@ -128,8 +130,8 @@ mod example {

////////////////////////////////////////////////////////////////////////////////
// //
// Let's delete the association `kwd1` -> `kwd2`. This actually associates //
// the negation of `kwd2` to `kwd1`. //
// Let's delete the association `kwd1`->`kwd2`. This actually associates the //
// negation of `kwd2` to `kwd1`. //
// //
// ```json //
// { //
Expand Down Expand Up @@ -225,8 +227,8 @@ mod example {
////////////////////////////////////////////////////////////////////////////////
// //
// Let's delete the association `loc2`->`kwd2` and compact the index in //
// order to collapse the negation. Since `kwd2` indexes no more keyword, it //
// should be removed from the index: //
// order to collapse the negation. Since `kwd2` indexes no more keyword, //
// it should be removed from the index: //
// //
// ```json //
// { //
Expand Down

0 comments on commit 39ee07e

Please sign in to comment.