You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: client/indexer-db/src/models/file.rs
+46-6Lines changed: 46 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ use shc_common::types::{FileMetadata, Fingerprint};
10
10
11
11
usecrate::{
12
12
models::{Bucket,MultiAddress},
13
-
schema::{bucket, file, file_peer_id},
13
+
schema::{bucket, file, file_peer_id, msp_file},
14
14
DbConnection,
15
15
};
16
16
@@ -137,6 +137,7 @@ impl File {
137
137
peer_ids:Vec<crate::models::PeerId>,
138
138
block_hash:Vec<u8>,
139
139
tx_hash:Option<Vec<u8>>,
140
+
is_in_bucket:bool,
140
141
) -> Result<Self, diesel::result::Error>{
141
142
let file = diesel::insert_into(file::table)
142
143
.values((
@@ -150,7 +151,7 @@ impl File {
150
151
file::step.eq(step asi32),
151
152
file::deletion_status.eq(None::<i32>),
152
153
file::deletion_signature.eq(None::<Vec<u8>>),
153
-
file::is_in_bucket.eq(false),
154
+
file::is_in_bucket.eq(is_in_bucket),
154
155
file::block_hash.eq(block_hash),
155
156
file::tx_hash.eq(tx_hash),
156
157
))
@@ -224,6 +225,26 @@ impl File {
224
225
Ok(file_record)
225
226
}
226
227
228
+
/// Check if any file record with the given file key is currently in the bucket forest.
229
+
///
230
+
/// This is useful when creating new file records for repeated storage requests
231
+
/// to inherit the bucket membership status from previous requests, since for example if the
232
+
/// MSP was already storing the file key, the `MutationsApplied` event won't be emitted for it
233
+
/// so if we default `is_in_bucket` to false it would be incorrectly marked as not in the bucket.
234
+
pubasyncfnis_file_key_in_bucket<'a>(
235
+
conn:&mutDbConnection<'a>,
236
+
file_key:implAsRef<[u8]>,
237
+
) -> Result<bool, diesel::result::Error>{
238
+
let file_key = file_key.as_ref().to_vec();
239
+
let count:i64 = file::table
240
+
.filter(file::file_key.eq(file_key))
241
+
.filter(file::is_in_bucket.eq(true))
242
+
.count()
243
+
.get_result(conn)
244
+
.await?;
245
+
Ok(count > 0)
246
+
}
247
+
227
248
pubasyncfnupdate_step<'a>(
228
249
conn:&mutDbConnection<'a>,
229
250
file_key:implAsRef<[u8]>,
@@ -306,6 +327,22 @@ impl File {
306
327
Ok(count > 0)
307
328
}
308
329
330
+
/// Check if a file has any MSP associations
331
+
///
332
+
/// TODO: This check is not used for now, but should be used in the future to prevent the
333
+
/// indexer from trying to delete a file that still has associations and getting stuck.
334
+
pubasyncfnhas_msp_associations<'a>(
335
+
conn:&mutDbConnection<'a>,
336
+
file_id:i64,
337
+
) -> Result<bool, diesel::result::Error>{
338
+
let count:i64 = msp_file::table
339
+
.filter(msp_file::file_id.eq(file_id))
340
+
.count()
341
+
.get_result(conn)
342
+
.await?;
343
+
Ok(count > 0)
344
+
}
345
+
309
346
/// Delete file only if it has no BSP associations and is not in the bucket forest.
310
347
/// The flag [`is_in_bucket`](File::is_in_bucket) is set to false or true based on the [`MutationsApplied`] event emitted by the proofs dealer pallet for catch all.
311
348
/// Returns true if all files associated with the file key were deleted, false if any still has associations.
@@ -636,8 +673,10 @@ impl File {
636
673
/// Update the bucket membership status for a file.
637
674
///
638
675
/// Updates `is_in_bucket` based on mutations applied to the bucket's forest.
639
-
/// The file is identified by both `file_key` and `onchain_bucket_id` to ensure
640
-
/// we're updating the correct file-bucket relationship.
676
+
/// The file is identified by both `file_key` and `onchain_bucket_id`.
677
+
///
678
+
/// This updates all file records with the same file key (for cases where there were
679
+
/// multiple storage requests for the same file).
641
680
pubasyncfnupdate_bucket_membership<'a>(
642
681
conn:&mutDbConnection<'a>,
643
682
file_key:implAsRef<[u8]>,
@@ -647,7 +686,8 @@ impl File {
647
686
let file_key = file_key.as_ref().to_vec();
648
687
let onchain_bucket_id = onchain_bucket_id.as_ref().to_vec();
649
688
650
-
// Get the file info
689
+
// Get the file info (bucket ID and size) from one of the file records since
0 commit comments