Skip to content

Commit

Permalink
Add unique constraint to volume model, refine volume fingerprint gene…
Browse files Browse the repository at this point in the history
…ration, and improve database interaction for system volumes
  • Loading branch information
jamiepine committed Nov 3, 2024
1 parent 378edcc commit fbeb6c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ model Volume {
device_id Int?
device Device? @relation(fields: [device_id], references: [id], onDelete: Cascade)
@@unique([device_id, mount_point, name, total_bytes_capacity, file_system])
@@map("volume")
}

Expand Down
25 changes: 18 additions & 7 deletions core/src/volume/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use async_channel as chan;
use sd_prisma::prisma::volume;
use serde::de;
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio::sync::{broadcast, oneshot, Mutex, RwLock};
use tracing::{debug, error, info, trace, warn};
Expand Down Expand Up @@ -273,7 +274,7 @@ impl VolumeManagerActor {
let db_volumes = library
.db
.volume()
.find_many(vec![])
.find_many(vec![volume::device_id::equals(Some(db_device.id))])
.exec()
.await?
.into_iter()
Expand All @@ -287,13 +288,23 @@ impl VolumeManagerActor {
// Create missing system volumes in the database
for v in current_volumes.iter() {
let fingerprint = v.generate_fingerprint(device_pub_id.clone());
// ensure that the volume is not already in the db and is a system volume
if !db_volumes
.iter()
.any(|db_volume| fingerprint == db_volume.fingerprint.clone().unwrap())
&& v.mount_type == MountType::System
{

// Check if the volume already exists in the database
let existing_volume = db_volumes.iter().find(|db_volume| {
db_volume
.fingerprint
.as_ref()
.map(|db_fingerprint| db_fingerprint == &fingerprint)
.unwrap_or(false)
});

if existing_volume.is_none() && v.mount_type == MountType::System {
// If the volume doesn't exist in the database and is a system volume, create a new entry
v.create(&library.db, device_pub_id.clone()).await?;
} else if let Some(existing_volume) = existing_volume {
// If the volume already exists in the database, update its information
let updated_volume = Volume::merge_with_db_volume(v, existing_volume);
updated_volume.update(&library.db).await?;
}
}

Expand Down
11 changes: 3 additions & 8 deletions core/src/volume/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,9 @@ impl Volume {
for id in current_device_pub_id {
hasher.update(&[id]);
}

// Add all mount points to make fingerprint unique
for mount_point in &self.mount_points {
hasher.update(mount_point.to_string_lossy().as_bytes());
}

// hasher.update(self.name.as_bytes());
// hasher.update(&self.total_bytes_capacity.to_be_bytes());
hasher.update(self.mount_point.to_string_lossy().as_bytes());
hasher.update(self.name.as_bytes());
hasher.update(&self.total_bytes_capacity.to_be_bytes());
hasher.update(self.file_system.to_string().as_bytes());

hasher.finalize().as_bytes().to_vec()
Expand Down

0 comments on commit fbeb6c9

Please sign in to comment.