MeiliSearch/milli/src/update/new/document_change.rs

97 lines
2.0 KiB
Rust
Raw Normal View History

use heed::RoTxn;
use obkv::KvReader;
use crate::update::new::KvReaderFieldId;
use crate::{DocumentId, FieldId, Index, Result};
pub enum DocumentChange {
Deletion(Deletion),
Update(Update),
Insertion(Insertion),
}
pub struct Deletion {
docid: DocumentId,
2024-09-12 18:01:02 +02:00
current: Box<KvReaderFieldId>,
}
pub struct Update {
docid: DocumentId,
2024-09-12 18:01:02 +02:00
current: Box<KvReaderFieldId>,
new: Box<KvReaderFieldId>,
}
pub struct Insertion {
docid: DocumentId,
new: Box<KvReaderFieldId>,
}
impl DocumentChange {
2024-09-03 11:02:39 +02:00
pub fn docid(&self) -> DocumentId {
match &self {
Self::Deletion(inner) => inner.docid(),
Self::Update(inner) => inner.docid(),
Self::Insertion(inner) => inner.docid(),
}
}
}
impl Deletion {
2024-09-12 18:01:02 +02:00
pub fn create(docid: DocumentId, current: Box<KvReaderFieldId>) -> Self {
Self { docid, current }
}
2024-09-03 11:02:39 +02:00
pub fn docid(&self) -> DocumentId {
self.docid
}
2024-09-04 17:03:09 +02:00
// TODO shouldn't we use the one in self?
pub fn current<'a>(
&self,
rtxn: &'a RoTxn,
index: &'a Index,
) -> Result<Option<&'a KvReader<FieldId>>> {
index.documents.get(rtxn, &self.docid).map_err(crate::Error::from)
}
}
impl Insertion {
2024-09-12 18:01:02 +02:00
pub fn create(docid: DocumentId, new: Box<KvReaderFieldId>) -> Self {
Insertion { docid, new }
}
2024-09-03 11:02:39 +02:00
pub fn docid(&self) -> DocumentId {
self.docid
}
2024-09-03 11:02:39 +02:00
pub fn new(&self) -> &KvReader<FieldId> {
self.new.as_ref()
}
}
impl Update {
pub fn create(
docid: DocumentId,
current: Box<KvReaderFieldId>,
new: Box<KvReaderFieldId>,
) -> Self {
2024-09-12 18:01:02 +02:00
Update { docid, current, new }
}
2024-09-03 11:02:39 +02:00
pub fn docid(&self) -> DocumentId {
self.docid
}
pub fn current<'a>(
&self,
rtxn: &'a RoTxn,
index: &'a Index,
) -> Result<Option<&'a KvReader<FieldId>>> {
index.documents.get(rtxn, &self.docid).map_err(crate::Error::from)
}
2024-09-03 11:02:39 +02:00
pub fn new(&self) -> &KvReader<FieldId> {
self.new.as_ref()
}
}