indexing.rs

 1use collections::HashSet;
 2use parking_lot::Mutex;
 3use project::ProjectEntryId;
 4use smol::channel;
 5use std::sync::{Arc, Weak};
 6
 7/// The set of entries that are currently being indexed.
 8pub struct IndexingEntrySet {
 9    entry_ids: Mutex<HashSet<ProjectEntryId>>,
10    tx: channel::Sender<()>,
11}
12
13/// When dropped, removes the entry from the set of entries that are being indexed.
14#[derive(Clone)]
15pub(crate) struct IndexingEntryHandle {
16    entry_id: ProjectEntryId,
17    set: Weak<IndexingEntrySet>,
18}
19
20impl IndexingEntrySet {
21    pub fn new(tx: channel::Sender<()>) -> Self {
22        Self {
23            entry_ids: Default::default(),
24            tx,
25        }
26    }
27
28    pub fn insert(self: &Arc<Self>, entry_id: ProjectEntryId) -> IndexingEntryHandle {
29        self.entry_ids.lock().insert(entry_id);
30        self.tx.send_blocking(()).ok();
31        IndexingEntryHandle {
32            entry_id,
33            set: Arc::downgrade(self),
34        }
35    }
36
37    pub fn len(&self) -> usize {
38        self.entry_ids.lock().len()
39    }
40}
41
42impl Drop for IndexingEntryHandle {
43    fn drop(&mut self) {
44        if let Some(set) = self.set.upgrade() {
45            set.tx.send_blocking(()).ok();
46            set.entry_ids.lock().remove(&self.entry_id);
47        }
48    }
49}