2024-08-28 18:45:16 +02:00
|
|
|
use heed::RoTxn;
|
2024-10-03 18:08:09 +02:00
|
|
|
use serde_json::value::RawValue;
|
2024-08-28 18:45:16 +02:00
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
use super::document::{DocumentFromDb, DocumentFromVersions, MergedDocument};
|
|
|
|
use crate::documents::FieldIdMapper;
|
|
|
|
use crate::{DocumentId, Index, Result};
|
2024-08-28 18:45:16 +02:00
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
pub enum DocumentChange<'doc> {
|
2024-08-28 18:45:16 +02:00
|
|
|
Deletion(Deletion),
|
2024-10-03 18:08:09 +02:00
|
|
|
Update(Update<'doc>),
|
|
|
|
Insertion(Insertion<'doc>),
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Deletion {
|
2024-10-03 18:08:09 +02:00
|
|
|
docid: DocumentId,
|
|
|
|
external_document_id: String,
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
pub struct Update<'doc> {
|
|
|
|
docid: DocumentId,
|
|
|
|
external_document_id: String,
|
|
|
|
new: DocumentFromVersions<'doc>,
|
|
|
|
has_deletion: bool,
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
pub struct Insertion<'doc> {
|
|
|
|
docid: DocumentId,
|
|
|
|
external_document_id: String,
|
|
|
|
new: DocumentFromVersions<'doc>,
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
impl<'doc> DocumentChange<'doc> {
|
2024-09-03 11:02:39 +02:00
|
|
|
pub fn docid(&self) -> DocumentId {
|
2024-08-28 18:45:16 +02:00
|
|
|
match &self {
|
|
|
|
Self::Deletion(inner) => inner.docid(),
|
|
|
|
Self::Update(inner) => inner.docid(),
|
|
|
|
Self::Insertion(inner) => inner.docid(),
|
|
|
|
}
|
|
|
|
}
|
2024-10-03 18:08:09 +02:00
|
|
|
|
|
|
|
pub fn external_docid(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
DocumentChange::Deletion(deletion) => deletion.external_document_id(),
|
|
|
|
DocumentChange::Update(update) => update.external_document_id(),
|
|
|
|
DocumentChange::Insertion(insertion) => insertion.external_document_id(),
|
|
|
|
}
|
|
|
|
}
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deletion {
|
2024-10-03 18:08:09 +02:00
|
|
|
pub fn create(docid: DocumentId, external_document_id: String) -> Self {
|
|
|
|
Self { docid, external_document_id }
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
2024-09-03 11:02:39 +02:00
|
|
|
pub fn docid(&self) -> DocumentId {
|
2024-08-28 18:45:16 +02:00
|
|
|
self.docid
|
|
|
|
}
|
|
|
|
|
2024-10-02 11:32:19 +02:00
|
|
|
pub fn external_document_id(&self) -> &str {
|
|
|
|
&self.external_document_id
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
pub fn current<'a, Mapper: FieldIdMapper>(
|
2024-09-03 15:14:16 +02:00
|
|
|
&self,
|
|
|
|
rtxn: &'a RoTxn,
|
|
|
|
index: &'a Index,
|
2024-10-03 18:08:09 +02:00
|
|
|
mapper: &'a Mapper,
|
|
|
|
) -> Result<DocumentFromDb<'a, Mapper>> {
|
|
|
|
Ok(DocumentFromDb::new(self.docid, rtxn, index, mapper)?.ok_or(
|
|
|
|
crate::error::UserError::UnknownInternalDocumentId { document_id: self.docid },
|
|
|
|
)?)
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
impl<'doc> Insertion<'doc> {
|
2024-10-02 11:32:19 +02:00
|
|
|
pub fn create(
|
|
|
|
docid: DocumentId,
|
|
|
|
external_document_id: String,
|
2024-10-03 18:08:09 +02:00
|
|
|
new: DocumentFromVersions<'doc>,
|
2024-10-02 11:32:19 +02:00
|
|
|
) -> Self {
|
|
|
|
Insertion { docid, external_document_id, new }
|
2024-08-29 12:06:44 +02:00
|
|
|
}
|
|
|
|
|
2024-09-03 11:02:39 +02:00
|
|
|
pub fn docid(&self) -> DocumentId {
|
2024-08-28 18:45:16 +02:00
|
|
|
self.docid
|
|
|
|
}
|
|
|
|
|
2024-10-02 11:32:19 +02:00
|
|
|
pub fn external_document_id(&self) -> &str {
|
|
|
|
&self.external_document_id
|
|
|
|
}
|
2024-10-03 18:08:09 +02:00
|
|
|
pub fn new(&self) -> DocumentFromVersions<'doc> {
|
|
|
|
self.new
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
impl<'doc> Update<'doc> {
|
2024-08-29 12:06:44 +02:00
|
|
|
pub fn create(
|
|
|
|
docid: DocumentId,
|
2024-10-02 11:32:19 +02:00
|
|
|
external_document_id: String,
|
2024-10-03 18:08:09 +02:00
|
|
|
new: DocumentFromVersions<'doc>,
|
|
|
|
has_deletion: bool,
|
2024-08-29 12:06:44 +02:00
|
|
|
) -> Self {
|
2024-10-03 18:08:09 +02:00
|
|
|
Update { docid, new, external_document_id, has_deletion }
|
2024-08-29 12:06:44 +02:00
|
|
|
}
|
|
|
|
|
2024-09-03 11:02:39 +02:00
|
|
|
pub fn docid(&self) -> DocumentId {
|
2024-08-28 18:45:16 +02:00
|
|
|
self.docid
|
|
|
|
}
|
|
|
|
|
2024-10-02 11:32:19 +02:00
|
|
|
pub fn external_document_id(&self) -> &str {
|
|
|
|
&self.external_document_id
|
|
|
|
}
|
2024-10-03 18:08:09 +02:00
|
|
|
pub fn current<'a, Mapper: FieldIdMapper>(
|
2024-09-03 15:14:16 +02:00
|
|
|
&self,
|
|
|
|
rtxn: &'a RoTxn,
|
|
|
|
index: &'a Index,
|
2024-10-03 18:08:09 +02:00
|
|
|
mapper: &'a Mapper,
|
|
|
|
) -> Result<DocumentFromDb<'a, Mapper>> {
|
|
|
|
Ok(DocumentFromDb::new(self.docid, rtxn, index, mapper)?.ok_or(
|
|
|
|
crate::error::UserError::UnknownInternalDocumentId { document_id: self.docid },
|
|
|
|
)?)
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-03 18:08:09 +02:00
|
|
|
pub fn updated(&self) -> DocumentFromVersions<'doc> {
|
|
|
|
self.new
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|
2024-10-03 18:08:09 +02:00
|
|
|
|
|
|
|
pub fn new<'a, Mapper: FieldIdMapper>(
|
|
|
|
&self,
|
|
|
|
rtxn: &'a RoTxn,
|
|
|
|
index: &'a Index,
|
|
|
|
mapper: &'a Mapper,
|
|
|
|
) -> Result<MergedDocument<'doc, 'a, Mapper>> {
|
|
|
|
if self.has_deletion {
|
|
|
|
Ok(MergedDocument::without_db(self.new))
|
|
|
|
} else {
|
|
|
|
MergedDocument::with_db(self.docid, rtxn, index, mapper, self.new)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Entry<'doc> = (&'doc str, &'doc RawValue);
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum Versions<'doc> {
|
|
|
|
Single(&'doc [Entry<'doc>]),
|
|
|
|
Multiple(&'doc [&'doc [Entry<'doc>]]),
|
2024-08-28 18:45:16 +02:00
|
|
|
}
|