Skip to content

Commit e7dc3da

Browse files
committed
Use sea-orm for finding the missing blocks of a file.
1 parent 9d3c79f commit e7dc3da

File tree

4 files changed

+36
-40
lines changed

4 files changed

+36
-40
lines changed

src/db.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,31 @@ pub trait PersistenceHandle {
660660
Ok(actor_info_opt)
661661
}
662662

663+
/// Finds all the hashes of any file block that we know we don't have yet.
664+
async fn find_missing_file_blocks(&self, actor_id: i64) -> Result<Vec<IdType>> {
665+
let query = Query::select()
666+
.distinct()
667+
.column(file_block::Column::BlockHash)
668+
.from(file_block::Entity)
669+
.left_join(
670+
block::Entity,
671+
Expr::col((file_block::Entity, file_block::Column::BlockHash))
672+
.equals((block::Entity, block::Column::Hash)),
673+
)
674+
.and_where(block::Column::Id.is_null())
675+
.take();
676+
let stat = self.backend().build(&query);
677+
let results = self.inner().query_all(stat).await?;
678+
679+
// Convert query results to a list of block hashes
680+
let mut hashes = Vec::with_capacity(results.len());
681+
for result in results {
682+
let hash: IdType = result.try_get_by_index(0)?;
683+
hashes.push(hash);
684+
}
685+
Ok(hashes)
686+
}
687+
663688
async fn find_next_object_sequence(&self, actor_id: i64) -> Result<u64> {
664689
let stat = object::Entity::find()
665690
.select_only()
@@ -1105,30 +1130,6 @@ impl Database {
11051130
}
11061131

11071132
impl Connection {
1108-
/// Returns a list of hashes of blocks we're still missing but also in need
1109-
/// of
1110-
pub fn fetch_missing_file_blocks(&self) -> Result<Vec<(i64, IdType)>> {
1111-
let mut stat = self.prepare(
1112-
r#"
1113-
SELECT fb.file_id, fb.block_hash
1114-
FROM file_block AS fb
1115-
INNER JOIN file AS f ON f.id = fb.file_id
1116-
WHERE fb.block_hash NOT IN (
1117-
SELECT hash FROM block
1118-
)
1119-
"#,
1120-
)?;
1121-
1122-
let mut rows = stat.query([])?;
1123-
let mut results = Vec::new();
1124-
while let Some(row) = rows.next()? {
1125-
let file_id: i64 = row.get(0)?;
1126-
let hash: IdType = row.get(1)?;
1127-
results.push((file_id, hash));
1128-
}
1129-
Ok(results)
1130-
}
1131-
11321133
pub fn fetch_identity(&self, address: &ActorAddress) -> Result<Option<ActorInfo>> {
11331134
let mut stat = self.prepare(
11341135
r#"

src/net/actor.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,12 @@ impl ActorNode {
481481
}
482482
}
483483

484-
/// Returns a list of block hashes that we'd like to have.
485-
fn investigate_missing_blocks(&self) -> db::Result<Vec<(i64, IdType)>> {
486-
tokio::task::block_in_place(|| {
487-
let c = self.db().connect_old()?;
488-
c.fetch_missing_file_blocks()
489-
})
484+
/// Returns a list of block hashes that we'd like to have, where the first blocks are the most
485+
/// wanted by our node.
486+
async fn investigate_missing_blocks(&self) -> db::Result<Vec<IdType>> {
487+
self.db()
488+
.find_missing_file_blocks(self.base.interface.actor_id)
489+
.await
490490
}
491491

492492
async fn object_missing_files(&self, object: &ObjectPayload) -> db::Result<Vec<IdType>> {
@@ -1233,9 +1233,11 @@ impl ActorNode {
12331233
Ok(())
12341234
}
12351235

1236+
/// Searches the network for any blocks that we want to have because we have the file meta data
1237+
/// for it.
12361238
pub(super) async fn synchronize_blocks(&self) -> db::Result<()> {
1237-
let missing_blocks = self.investigate_missing_blocks()?;
1238-
for (_file_id, hash) in missing_blocks {
1239+
let missing_blocks = self.investigate_missing_blocks().await?;
1240+
for hash in missing_blocks {
12391241
if let Some(result) = self.find_block(&hash).await {
12401242
self.db().store_block(hash, &result.data).await?;
12411243
}

src/web/info.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use super::consolidated_feed::ConsolidatedObjectType;
1010
use crate::{
1111
common::{current_timestamp, IdType},
1212
core::{
13-
ActorAddress, CompressionType, OBJECT_TYPE_HOME_FILE, OBJECT_TYPE_POST,
14-
OBJECT_TYPE_PROFILE,
13+
ActorAddress, CompressionType, OBJECT_TYPE_HOME_FILE, OBJECT_TYPE_POST, OBJECT_TYPE_PROFILE,
1514
},
1615
db::{Database, Error, PersistenceHandle, Result},
1716
entity::*,

src/web/server.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ use crate::{
3434
db::{self, Database, PersistenceHandle},
3535
};
3636

37-
#[derive(Clone, Serialize)]
38-
pub struct IdentityData {
39-
label: String,
40-
address: String,
41-
}
42-
4337
#[derive(Clone, Default, Serialize)]
4438
pub struct AppState {
4539
identities: Vec<IdentityInfo>,

0 commit comments

Comments
 (0)