2021-06-30 10:43:12 +02:00
|
|
|
use std::collections::HashMap;
|
2021-06-16 18:33:33 +02:00
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
use heed::types::Str;
|
2023-10-28 12:56:46 +02:00
|
|
|
use heed::{Database, RoIter, RoTxn, RwTxn};
|
2020-11-22 14:48:42 +01:00
|
|
|
|
2023-10-28 12:56:46 +02:00
|
|
|
use crate::{DocumentId, BEU32};
|
2023-10-24 17:01:45 +02:00
|
|
|
|
|
|
|
pub enum DocumentOperationKind {
|
|
|
|
Create,
|
|
|
|
Delete,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DocumentOperation {
|
|
|
|
pub external_id: String,
|
|
|
|
pub internal_id: DocumentId,
|
|
|
|
pub kind: DocumentOperationKind,
|
2020-11-22 14:48:42 +01:00
|
|
|
}
|
|
|
|
|
2023-11-22 18:21:19 +01:00
|
|
|
pub struct ExternalDocumentsIds(Database<Str, BEU32>);
|
2023-10-24 17:01:45 +02:00
|
|
|
|
2023-10-28 12:56:46 +02:00
|
|
|
impl ExternalDocumentsIds {
|
2023-11-22 18:21:19 +01:00
|
|
|
pub fn new(db: Database<Str, BEU32>) -> ExternalDocumentsIds {
|
2023-10-28 12:56:46 +02:00
|
|
|
ExternalDocumentsIds(db)
|
2020-11-22 17:28:41 +01:00
|
|
|
}
|
|
|
|
|
2021-04-01 09:07:16 +02:00
|
|
|
/// Returns `true` if hard and soft external documents lists are empty.
|
2023-10-28 12:56:46 +02:00
|
|
|
pub fn is_empty(&self, rtxn: &RoTxn) -> heed::Result<bool> {
|
|
|
|
self.0.is_empty(rtxn).map_err(Into::into)
|
2021-04-01 09:07:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-28 12:56:46 +02:00
|
|
|
pub fn get<A: AsRef<str>>(&self, rtxn: &RoTxn, external_id: A) -> heed::Result<Option<u32>> {
|
2023-11-22 18:21:19 +01:00
|
|
|
Ok(self.0.get(rtxn, external_id.as_ref())?)
|
2020-11-22 14:48:42 +01:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:43:12 +02:00
|
|
|
/// An helper function to debug this type, returns an `HashMap` of both,
|
|
|
|
/// soft and hard fst maps, combined.
|
2023-10-28 12:56:46 +02:00
|
|
|
pub fn to_hash_map(&self, rtxn: &RoTxn) -> heed::Result<HashMap<String, u32>> {
|
2023-10-24 17:01:45 +02:00
|
|
|
let mut map = HashMap::default();
|
2023-10-28 12:56:46 +02:00
|
|
|
for result in self.0.iter(rtxn)? {
|
|
|
|
let (external, internal) = result?;
|
2023-11-22 18:21:19 +01:00
|
|
|
map.insert(external.to_owned(), internal);
|
2021-06-30 10:43:12 +02:00
|
|
|
}
|
2023-10-28 12:56:46 +02:00
|
|
|
Ok(map)
|
2023-05-22 11:15:14 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 10:36:34 +02:00
|
|
|
/// Applies the list of operations passed as argument, modifying the current external to internal id mapping.
|
2023-10-24 17:01:45 +02:00
|
|
|
///
|
|
|
|
/// If the list contains multiple operations on the same external id, then the result is unspecified.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// - If attempting to delete a document that doesn't exist
|
|
|
|
/// - If attempting to create a document that already exists
|
2023-10-28 12:56:46 +02:00
|
|
|
pub fn apply(&self, wtxn: &mut RwTxn, operations: Vec<DocumentOperation>) -> heed::Result<()> {
|
|
|
|
for DocumentOperation { external_id, internal_id, kind } in operations {
|
|
|
|
match kind {
|
|
|
|
DocumentOperationKind::Create => {
|
2023-11-22 18:21:19 +01:00
|
|
|
self.0.put(wtxn, &external_id, &internal_id)?;
|
2023-10-28 12:56:46 +02:00
|
|
|
}
|
|
|
|
DocumentOperationKind::Delete => {
|
|
|
|
if !self.0.delete(wtxn, &external_id)? {
|
2023-10-24 17:01:45 +02:00
|
|
|
panic!("Attempting to delete a non-existing document")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 14:48:42 +01:00
|
|
|
}
|
|
|
|
|
2023-10-28 12:56:46 +02:00
|
|
|
Ok(())
|
2020-11-22 14:48:42 +01:00
|
|
|
}
|
2023-11-02 15:37:05 +01:00
|
|
|
|
|
|
|
/// Returns an iterator over all the external ids.
|
2023-11-22 18:21:19 +01:00
|
|
|
pub fn iter<'t>(&self, rtxn: &'t RoTxn) -> heed::Result<RoIter<'t, Str, BEU32>> {
|
2023-11-02 15:37:05 +01:00
|
|
|
self.0.iter(rtxn)
|
|
|
|
}
|
2020-11-22 14:48:42 +01:00
|
|
|
}
|