feat(homeserver): add Entry::read_content() method

This commit is contained in:
nazeh
2024-10-16 16:40:37 +03:00
parent 3c5b123afd
commit 8dde7b17da
3 changed files with 17 additions and 8 deletions

View File

@@ -1,23 +1,24 @@
use heed::{types::Bytes, Database, RoTxn};
use pubky_common::timestamp::Timestamp;
use crate::database::DB;
/// hash of the blob => bytes.
use super::entries::Entry;
/// (entry timestamp | chunk_index BE) => bytes
pub type BlobsTable = Database<Bytes, Bytes>;
pub const BLOBS_TABLE: &str = "blobs";
impl DB {
pub fn get_blob<'txn>(
pub fn read_entry_content<'txn>(
&self,
rtxn: &'txn RoTxn,
timestamp: &Timestamp,
entry: &Entry,
) -> anyhow::Result<impl Iterator<Item = Result<&'txn [u8], heed::Error>> + 'txn> {
Ok(self
.tables
.blobs
.prefix_iter(rtxn, &timestamp.to_bytes())?
.prefix_iter(rtxn, &entry.timestamp().to_bytes())?
.map(|i| i.map(|(_, bytes)| bytes)))
}
}

View File

@@ -291,6 +291,14 @@ impl Entry {
// === Public Method ===
pub fn read_content<'txn>(
&self,
db: &'txn DB,
rtxn: &'txn RoTxn,
) -> anyhow::Result<impl Iterator<Item = Result<&'txn [u8], heed::Error>> + 'txn> {
db.read_entry_content(rtxn, self)
}
pub fn serialize(&self) -> Vec<u8> {
to_allocvec(self).expect("Session::serialize")
}
@@ -464,7 +472,7 @@ mod tests {
let mut blob = vec![];
{
let mut iter = db.get_blob(&rtxn, entry.timestamp()).unwrap();
let mut iter = entry.read_content(&db, &rtxn).unwrap();
while let Some(Ok(chunk)) = iter.next() {
blob.extend_from_slice(&chunk);
@@ -504,7 +512,7 @@ mod tests {
let mut blob = vec![];
{
let mut iter = db.get_blob(&rtxn, entry.timestamp()).unwrap();
let mut iter = entry.read_content(&db, &rtxn).unwrap();
while let Some(Ok(chunk)) = iter.next() {
blob.extend_from_slice(&chunk);

View File

@@ -93,7 +93,7 @@ pub async fn get(
let option = state.db.get_entry(&rtxn, &public_key, &path)?;
if let Some(entry) = option {
let iter = state.db.get_blob(&rtxn, entry.timestamp())?;
let iter = entry.read_content(&state.db, &rtxn)?;
entry_tx.send(Some(entry))?;