Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix notes index refresh and cleanup edge cases

setalpe668475ce596
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes.rs b/rust/apps/ensu/src-tauri/src/commands/notes.rs
index 25facfec4c..a9df9026a2 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes.rs
@@ -14,7 +14,7 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH};
use ente_ensu::model;
use ente_ensu::notes::{
NoteSourceReference, NotesCollectionIndex, NotesError, NotesIndexOutcome, NotesIndexProgress,
- notes_content_revision,
+ cleanup_unreferenced_notes_shards, notes_content_revision,
};
use notify::RecommendedWatcher;
use serde::Serialize;
@@ -445,7 +445,10 @@ impl State {
) -> Result<UpdateSnapshot, ApiError> {
let mut updates = self.updates.lock().unwrap_or_else(PoisonError::into_inner);
let Some(update) = updates.get_mut(collection_id) else {
- return Ok(UpdateSnapshot::default());
+ return Ok(UpdateSnapshot {
+ force_full_hash: force,
+ ..UpdateSnapshot::default()
+ });
};
if force {
update.bypass_quiet_period = true;
@@ -463,7 +466,7 @@ impl State {
Ok(UpdateSnapshot {
generation: update.generation,
forced_document_ids: update.forced_document_ids.iter().cloned().collect(),
- force_full_hash: update.force_full_hash,
+ force_full_hash: force || update.force_full_hash,
rebuild_derived_index: update.rebuild_derived_index,
})
}
@@ -1078,7 +1081,10 @@ pub async fn notes_index_collection(
let opened_index = if outcome.ready() {
notes_state.evict_cached_index(&collection_id);
match NotesCollectionIndex::open(&notes_state.index_root, collection_id.clone()) {
- Ok(index) => Some(index),
+ Ok(index) => {
+ let _ = cleanup_unreferenced_notes_shards(&notes_state.index_root, &collection_id);
+ Some(index)
+ }
Err(error) => {
let error = notes_error(error);
report_index_error(
@@ -1122,6 +1128,7 @@ pub async fn notes_index_collection(
.insert(collection_id.clone(), index);
} else {
notes_state.evict_cached_index(&collection_id);
+ let _ = cleanup_unreferenced_notes_shards(&notes_state.index_root, &collection_id);
}
}
emit_state_changed(&app, &collection_id);
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/source.rs b/rust/apps/ensu/src-tauri/src/commands/notes/source.rs
index db7c0961a0..371c472bf6 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes/source.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes/source.rs
@@ -226,13 +226,27 @@ fn open_collection_source(root: &Path, document_id: &str, _path: &Path) -> Resul
libc::openat(
directory.as_raw_fd(),
file_name.as_ptr(),
- libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW,
+ libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NOFOLLOW | libc::O_NONBLOCK,
)
};
if fd < 0 {
return Err(scoped_open_error(std::io::Error::last_os_error()));
}
- Ok(unsafe { File::from_raw_fd(fd) })
+ let file = unsafe { File::from_raw_fd(fd) };
+ if !file.metadata().map_err(source_file_error)?.is_file() {
+ return Err(ApiError::new(
+ "invalid_document",
+ "Source note is not supported",
+ ));
+ }
+ let flags = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GETFL) };
+ if flags < 0 {
+ return Err(source_file_error(std::io::Error::last_os_error()));
+ }
+ if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_SETFL, flags & !libc::O_NONBLOCK) } < 0 {
+ return Err(source_file_error(std::io::Error::last_os_error()));
+ }
+ Ok(file)
}
#[cfg(unix)]
diff --git a/rust/crates/ensu/src/notes.rs b/rust/crates/ensu/src/notes.rs
index 448b6e985c..1ea826d8d9 100644
--- a/rust/crates/ensu/src/notes.rs
+++ b/rust/crates/ensu/src/notes.rs
@@ -53,7 +53,7 @@ pub use indexing::{
};
pub use reconcile::NotesReconciliationPlan;
pub use source::NoteSourceReference;
-pub use writer::{NotesIndexWriter, ValidatedNotesDocument};
+pub use writer::{NotesIndexWriter, ValidatedNotesDocument, cleanup_unreferenced_notes_shards};
pub fn notes_content_revision(bytes: &[u8]) -> String {
sha256_hex(bytes)
diff --git a/rust/crates/ensu/src/notes/index.rs b/rust/crates/ensu/src/notes/index.rs
index ae01dcb2a0..dde364b2c2 100644
--- a/rust/crates/ensu/src/notes/index.rs
+++ b/rust/crates/ensu/src/notes/index.rs
@@ -307,7 +307,7 @@ mod tests {
use crate::notes::shard::{NOTES_SHARD_FILE, NOTES_VECTORS_FILE};
use crate::notes::{
NotesIndexWriter, NotesSourceDocument, PreparedNotesChunk, PreparedNotesDocument,
- notes_content_revision,
+ cleanup_unreferenced_notes_shards, notes_content_revision,
};
const COLLECTION_ID: &str = "123e4567-e89b-12d3-a456-426614174000";
@@ -407,7 +407,7 @@ mod tests {
drop(index);
let replacement_index =
NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
- writer.publish(true).unwrap();
+ cleanup_unreferenced_notes_shards(temp.path(), COLLECTION_ID).unwrap();
assert!(!original_directory.exists());
assert_eq!(
replacement_index.search(&embedding(1, 1.0)).unwrap()[0].text,
diff --git a/rust/crates/ensu/src/notes/writer.rs b/rust/crates/ensu/src/notes/writer.rs
index ffd57c7ef0..146d97a2ab 100644
--- a/rust/crates/ensu/src/notes/writer.rs
+++ b/rust/crates/ensu/src/notes/writer.rs
@@ -10,7 +10,7 @@ use uuid::Uuid;
use super::manifest::{
NOTES_DOCUMENTS_DIRECTORY, NOTES_MANIFEST_BACKUP_FILE, NOTES_MANIFEST_FILE, NotesManifest,
NotesManifestDocument, collection_directory, empty_manifest, load_manifest_file,
- notes_index_contract, serialize_manifest_for_publish, shard_directory,
+ load_published_manifest, notes_index_contract, serialize_manifest_for_publish, shard_directory,
};
use super::reconcile::{IndexedNotesDocument, plan_notes_reconciliation};
use super::shard::{
@@ -41,6 +41,17 @@ pub struct ValidatedNotesDocument<'a> {
shard_sha256: String,
}
+pub fn cleanup_unreferenced_notes_shards(
+ index_root: impl AsRef<Path>,
+ collection_id: &str,
+) -> Result<(), NotesError> {
+ validate_collection_id(collection_id)?;
+ let collection_directory =
+ fs::canonicalize(collection_directory(index_root.as_ref(), collection_id))?;
+ let (manifest, _) = load_published_manifest(&collection_directory, collection_id, false)?;
+ cleanup_unreferenced_shards(&collection_directory, &manifest.documents)
+}
+
impl NotesIndexWriter {
pub fn validate_inventory_capacity(
collection_id: &str,