MeiliSearch/milli/src/update/new/document_change.rs
Clément Renault f7652186e1
WIP geo fields
2024-09-12 18:01:02 +02:00

97 lines
2.0 KiB
Rust

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,
current: Box<KvReaderFieldId>,
}
pub struct Update {
docid: DocumentId,
current: Box<KvReaderFieldId>,
new: Box<KvReaderFieldId>,
}
pub struct Insertion {
docid: DocumentId,
new: Box<KvReaderFieldId>,
}
impl DocumentChange {
pub fn docid(&self) -> DocumentId {
match &self {
Self::Deletion(inner) => inner.docid(),
Self::Update(inner) => inner.docid(),
Self::Insertion(inner) => inner.docid(),
}
}
}
impl Deletion {
pub fn create(docid: DocumentId, current: Box<KvReaderFieldId>) -> Self {
Self { docid, current }
}
pub fn docid(&self) -> DocumentId {
self.docid
}
// 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 {
pub fn create(docid: DocumentId, new: Box<KvReaderFieldId>) -> Self {
Insertion { docid, new }
}
pub fn docid(&self) -> DocumentId {
self.docid
}
pub fn new(&self) -> &KvReader<FieldId> {
self.new.as_ref()
}
}
impl Update {
pub fn create(
docid: DocumentId,
current: Box<KvReaderFieldId>,
new: Box<KvReaderFieldId>,
) -> Self {
Update { docid, current, new }
}
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)
}
pub fn new(&self) -> &KvReader<FieldId> {
self.new.as_ref()
}
}