Use actual BitV and factory everywhere

This commit is contained in:
Igor Katson 2024-08-20 20:42:24 +01:00
parent a55dfc6e0e
commit bc9e72df60
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
7 changed files with 57 additions and 53 deletions

View file

@ -7,11 +7,11 @@ use crate::{
session::TorrentId,
storage::filesystem::FilesystemStorageFactory,
torrent_state::ManagedTorrentHandle,
type_aliases::BF,
ManagedTorrentState,
};
use anyhow::{bail, Context};
use async_trait::async_trait;
use bitvec::{order::Lsb0, vec::BitVec};
use futures::{stream::BoxStream, StreamExt};
use itertools::Itertools;
use librqbit_core::Id20;
@ -71,20 +71,6 @@ impl JsonSessionPersistenceStore {
})
}
async fn to_id(&self, id: TorrentIdOrHash) -> anyhow::Result<TorrentId> {
match id {
TorrentIdOrHash::Id(id) => Ok(id),
TorrentIdOrHash::Hash(h) => self
.db_content
.read()
.await
.torrents
.iter()
.find_map(|(k, v)| if v.info_hash() == &h { Some(*k) } else { None })
.context("not found"),
}
}
async fn to_hash(&self, id: TorrentIdOrHash) -> anyhow::Result<Id20> {
match id {
TorrentIdOrHash::Id(id) => self
@ -208,7 +194,7 @@ impl BitVFactory for JsonSessionPersistenceStore {
async fn store_initial_check(
&self,
id: TorrentIdOrHash,
b: BitVec<u8, Lsb0>,
b: BF,
) -> anyhow::Result<Box<dyn BitV>> {
let h = self.to_hash(id).await?;
let filename = self.bitv_filename(&h);
@ -220,8 +206,7 @@ impl BitVFactory for JsonSessionPersistenceStore {
.open(&filename)
.await
.with_context(|| format!("error opening {filename:?}"))?;
let b = b.into_vec();
tokio::io::copy(&mut &b[..], &mut dst)
tokio::io::copy(&mut b.as_raw_slice(), &mut dst)
.await
.context("error writing bitslice to {filename:?}")?;
tokio::fs::rename(tmp_filename, &filename).await?;

View file

@ -79,8 +79,6 @@ pub trait SessionPersistenceStore: core::fmt::Debug + Send + Sync + BitVFactory
) -> anyhow::Result<BoxStream<'_, anyhow::Result<(TorrentId, SerializedTorrent)>>>;
}
pub type BoxSessionPersistenceStore = Box<dyn SessionPersistenceStore>;
fn serialize_info_hash<S>(id: &Id20, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,

View file

@ -2,10 +2,9 @@ use std::path::PathBuf;
use crate::{
api::TorrentIdOrHash, bitv::BitV, bitv_factory::BitVFactory, session::TorrentId,
torrent_state::ManagedTorrentHandle,
torrent_state::ManagedTorrentHandle, type_aliases::BF,
};
use anyhow::Context;
use bitvec::{order::Lsb0, vec::BitVec};
use futures::{stream::BoxStream, StreamExt};
use librqbit_core::Id20;
use sqlx::{Pool, Postgres};
@ -183,7 +182,7 @@ impl BitVFactory for PostgresSessionStorage {
async fn store_initial_check(
&self,
_: TorrentIdOrHash,
b: BitVec<u8, Lsb0>,
b: BF,
) -> anyhow::Result<Box<dyn BitV>> {
Ok(b.into_dyn())
}