Skip to content

Commit 99b149d

Browse files
committed
feat: align poll watcher behavior with other backends
1 parent 3fbd975 commit 99b149d

File tree

5 files changed

+396
-260
lines changed

5 files changed

+396
-260
lines changed

notify/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub enum TargetMode {
8686
/// If the underlying physical entity (inode/File ID) is replaced
8787
/// (e.g., by a move/rename operation), the watch stops monitoring.
8888
///
89-
/// TODO: fsevents backend and Windows backend does not unwatch on physical entity change yet. <https://github.com/rolldown/notify/issues/33>
89+
/// TODO: fsevents backend and Windows backend and polling backend does not unwatch on physical entity change yet. <https://github.com/rolldown/notify/issues/33>
9090
NoTrack,
9191
}
9292

notify/src/consolidating_path_trie.rs

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,17 @@ impl<'a, T> PathTrieIter<'a, T> {
172172
}
173173

174174
pub struct ConsolidatingPathTrie {
175+
children_consolidation: bool,
176+
175177
trie: PathTrie<()>,
176178
}
177179

178180
impl ConsolidatingPathTrie {
179181
const CHILDREN_CONSOLIDATION_THRESHOLD: usize = 10;
180182

181-
pub fn new() -> Self {
183+
pub fn new(children_consolidation: bool) -> Self {
182184
Self {
185+
children_consolidation,
183186
trie: PathTrie::new(),
184187
}
185188
}
@@ -192,14 +195,16 @@ impl ConsolidatingPathTrie {
192195
let inserted = self.trie.insert(path, ());
193196
inserted.remove_children();
194197

195-
for ancestor_path in path.ancestors().skip(1) {
196-
if let Some(parent_node) = self.trie.get_node_mut(ancestor_path)
197-
&& parent_node.children_len() >= Self::CHILDREN_CONSOLIDATION_THRESHOLD
198-
{
199-
parent_node.remove_children();
200-
parent_node.set_value(());
201-
} else {
202-
break;
198+
if self.children_consolidation {
199+
for ancestor_path in path.ancestors().skip(1) {
200+
if let Some(parent_node) = self.trie.get_node_mut(ancestor_path)
201+
&& parent_node.children_len() >= Self::CHILDREN_CONSOLIDATION_THRESHOLD
202+
{
203+
parent_node.remove_children();
204+
parent_node.set_value(());
205+
} else {
206+
break;
207+
}
203208
}
204209
}
205210
}
@@ -235,54 +240,68 @@ mod tests {
235240

236241
#[test]
237242
fn consolidate_no_siblings() {
238-
let mut ct = ConsolidatingPathTrie::new();
239-
ct.insert(PathBuf::from("/a/b"));
240-
ct.insert(PathBuf::from("/a/c"));
241-
assert_eq!(
242-
ct.values(),
243-
vec![PathBuf::from("/a/b"), PathBuf::from("/a/c")]
244-
);
243+
for children_consolidation in [true, false] {
244+
let mut ct = ConsolidatingPathTrie::new(children_consolidation);
245+
ct.insert(PathBuf::from("/a/b"));
246+
ct.insert(PathBuf::from("/a/c"));
247+
assert_eq!(
248+
ct.values(),
249+
vec![PathBuf::from("/a/b"), PathBuf::from("/a/c")]
250+
);
251+
}
245252
}
246253

247254
#[test]
248255
fn consolidate_no_siblings2() {
249-
let mut ct = ConsolidatingPathTrie::new();
250-
ct.insert(PathBuf::from("/a/b1"));
251-
ct.insert(PathBuf::from("/a/b2"));
252-
assert_eq!(
253-
ct.values(),
254-
vec![PathBuf::from("/a/b1"), PathBuf::from("/a/b2")]
255-
);
256+
for children_consolidation in [true, false] {
257+
let mut ct = ConsolidatingPathTrie::new(children_consolidation);
258+
ct.insert(PathBuf::from("/a/b1"));
259+
ct.insert(PathBuf::from("/a/b2"));
260+
assert_eq!(
261+
ct.values(),
262+
vec![PathBuf::from("/a/b1"), PathBuf::from("/a/b2")]
263+
);
264+
}
256265
}
257266

258267
#[test]
259268
fn consolidate_children() {
260-
let mut ct = ConsolidatingPathTrie::new();
261-
ct.insert(PathBuf::from("/a/b"));
262-
ct.insert(PathBuf::from("/a/b/c"));
263-
assert_eq!(ct.values(), vec![PathBuf::from("/a/b")]);
269+
for children_consolidation in [true, false] {
270+
let mut ct = ConsolidatingPathTrie::new(children_consolidation);
271+
ct.insert(PathBuf::from("/a/b"));
272+
ct.insert(PathBuf::from("/a/b/c"));
273+
assert_eq!(ct.values(), vec![PathBuf::from("/a/b")]);
274+
}
264275
}
265276

266277
#[test]
267278
fn consolidate_parent() {
268-
let mut ct = ConsolidatingPathTrie::new();
269-
ct.insert(PathBuf::from("/a/b/c"));
270-
ct.insert(PathBuf::from("/a/b"));
271-
assert_eq!(ct.values(), vec![PathBuf::from("/a/b")]);
279+
for children_consolidation in [true, false] {
280+
let mut ct = ConsolidatingPathTrie::new(children_consolidation);
281+
ct.insert(PathBuf::from("/a/b/c"));
282+
ct.insert(PathBuf::from("/a/b"));
283+
assert_eq!(ct.values(), vec![PathBuf::from("/a/b")]);
284+
}
272285
}
273286

274287
#[test]
275288
fn consolidate_to_single_parent() {
276-
let mut cr = ConsolidatingPathTrie::new();
289+
let mut cr = ConsolidatingPathTrie::new(true);
277290
for i in 1..=ConsolidatingPathTrie::CHILDREN_CONSOLIDATION_THRESHOLD {
278291
cr.insert(PathBuf::from(format!("/a/b/c{i}")));
279292
}
280293
assert_eq!(cr.values(), vec![PathBuf::from("/a/b")]);
294+
295+
let mut cr = ConsolidatingPathTrie::new(false);
296+
for i in 1..=ConsolidatingPathTrie::CHILDREN_CONSOLIDATION_THRESHOLD {
297+
cr.insert(PathBuf::from(format!("/a/b/c{i}")));
298+
}
299+
assert!(cr.values().len() > 1);
281300
}
282301

283302
#[test]
284303
fn consolidate_to_single_parent_nested1() {
285-
let mut cr = ConsolidatingPathTrie::new();
304+
let mut cr = ConsolidatingPathTrie::new(true);
286305
for i in 1..ConsolidatingPathTrie::CHILDREN_CONSOLIDATION_THRESHOLD {
287306
cr.insert(PathBuf::from(format!("/a/b/c{i}")));
288307
}
@@ -294,7 +313,7 @@ mod tests {
294313

295314
#[test]
296315
fn consolidate_to_single_parent_nested2() {
297-
let mut cr = ConsolidatingPathTrie::new();
316+
let mut cr = ConsolidatingPathTrie::new(true);
298317
cr.insert(PathBuf::from("/a/b/c1"));
299318
cr.insert(PathBuf::from("/a/b/c2"));
300319
for i in 1..=ConsolidatingPathTrie::CHILDREN_CONSOLIDATION_THRESHOLD {

notify/src/fsevent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl FsEventWatcher {
364364

365365
fn update_paths_based_on_watches(&mut self) {
366366
let paths_to_watch = {
367-
let mut trie = ConsolidatingPathTrie::new();
367+
let mut trie = ConsolidatingPathTrie::new(true);
368368
for path in self.watches.keys() {
369369
trie.insert(path.clone());
370370
}

notify/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ pub mod poll;
234234

235235
mod bimap;
236236
mod config;
237-
#[cfg(all(target_os = "macos", not(feature = "macos_kqueue")))]
238237
mod consolidating_path_trie;
239238
mod error;
240239

0 commit comments

Comments
 (0)