uses the latest version of heed to get rid of unsafe code

This commit is contained in:
Tamo 2024-05-16 16:11:08 +02:00
parent 897d25780e
commit 273c6e8c5c
3 changed files with 19 additions and 33 deletions

View file

@ -1,5 +1,4 @@
use std::io::{ErrorKind, Write};
use std::pin::Pin;
use actix_web::http::header::CONTENT_TYPE;
use actix_web::web::Data;
@ -627,31 +626,19 @@ fn some_documents<'a, 't: 'a>(
pub struct DocumentsStreamer {
attributes_to_retrieve: Option<Vec<String>>,
documents: RoaringBitmap,
// safety: The `rtxn` contains a reference to the index thus:
// - The `rtxn` MUST BE dropped before the index.
// - The index MUST BE `Pin`ned in RAM and never moved.
rtxn: Option<RoTxn<'static>>,
index: Pin<Box<Index>>,
rtxn: RoTxn<'static>,
index: Index,
pub total_documents: u64,
}
impl Drop for DocumentsStreamer {
fn drop(&mut self) {
// safety: we drop the rtxn before the index
self.rtxn = None;
}
}
impl Serialize for DocumentsStreamer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let rtxn = self.rtxn.as_ref().unwrap();
let mut seq = serializer.serialize_seq(Some(self.documents.len() as usize)).unwrap();
let documents = some_documents(&self.index, rtxn, self.documents.iter()).unwrap();
let documents = some_documents(&self.index, &self.rtxn, self.documents.iter()).unwrap();
for document in documents {
let document = document.unwrap();
let document = match self.attributes_to_retrieve {
@ -675,9 +662,7 @@ fn retrieve_documents(
filter: Option<Value>,
attributes_to_retrieve: Option<Vec<String>>,
) -> Result<DocumentsStreamer, ResponseError> {
// safety: The index MUST NOT move while we hold the `rtxn` on it
let index = Box::pin(index);
let rtxn = index.read_txn()?;
let rtxn = index.static_read_txn()?;
let filter = &filter;
let filter = if let Some(filter) = filter {
@ -702,10 +687,7 @@ fn retrieve_documents(
total_documents: candidates.len(),
attributes_to_retrieve,
documents: candidates.into_iter().skip(offset).take(limit).collect(),
// safety: It is safe to make the lifetime in the Rtxn static because it points to the index right below.
// The index is `Pin`ned on the RAM and won't move even if the structure is moved.
// The `rtxn` is held in an `Option`, so we're able to drop it before dropping the index.
rtxn: Some(unsafe { std::mem::transmute(rtxn) }),
rtxn,
index,
})
}