Skip to content

Commit 047592d

Browse files
committed
[IMP] Implement FifoPtrWeakHashSet based on PtrWeakhashSet
For main job_queue, we want to remove randomness due to the hash order. And to be closer to the python behaviour, importing symbols as it sees them, this commit implements a PtrWeakHashSet that is iterating in FIFO order items inside it.
1 parent a09d503 commit 047592d

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

server/src/core/odoo.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use crate::core::xml_validation::XmlValidator;
66
use crate::features::document_symbols::DocumentSymbolFeature;
77
use crate::features::references::ReferenceFeature;
88
use crate::features::workspace_symbols::WorkspaceSymbolFeature;
9+
use crate::fifo_ptr_weak_hash_set::FifoPtrWeakHashSet;
910
use crate::threads::SessionInfo;
1011
use crate::features::completion::CompletionFeature;
1112
use crate::features::definition::DefinitionFeature;
1213
use crate::features::hover::HoverFeature;
1314
use std::collections::HashMap;
1415
use std::cell::RefCell;
15-
use std::ffi::OsStr;
1616
use std::rc::{Rc, Weak};
1717
use std::sync::atomic::{AtomicBool, Ordering};
1818
use std::sync::{Arc, Mutex};
@@ -27,7 +27,6 @@ use serde_json::Value;
2727
use tracing::{error, warn, info, trace};
2828

2929
use std::collections::HashSet;
30-
use weak_table::PtrWeakHashSet;
3130
use std::process::Command;
3231
use std::fs;
3332
use std::path::{Path, PathBuf};
@@ -86,9 +85,9 @@ pub struct SyncOdoo {
8685
pub current_request_id: Option<RequestId>,
8786
pub running_request_ids: Arc<Mutex<Vec<RequestId>>>, //Arc to Server mutex for cancellation support
8887
pub watched_file_updates: u32,
89-
rebuild_arch: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
90-
rebuild_arch_eval: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
91-
rebuild_validation: PtrWeakHashSet<Weak<RefCell<Symbol>>>,
88+
rebuild_arch: FifoPtrWeakHashSet<RefCell<Symbol>>,
89+
rebuild_arch_eval: FifoPtrWeakHashSet<RefCell<Symbol>>,
90+
rebuild_validation: FifoPtrWeakHashSet<RefCell<Symbol>>,
9291
pub state_init: InitState,
9392
pub must_reload_paths: Vec<(Weak<RefCell<Symbol>>, String)>,
9493
pub load_odoo_addons: bool, //indicate if we want to load odoo addons or not
@@ -131,9 +130,9 @@ impl SyncOdoo {
131130
current_request_id: None,
132131
running_request_ids: Arc::new(Mutex::new(vec![])),
133132
watched_file_updates: 0,
134-
rebuild_arch: PtrWeakHashSet::new(),
135-
rebuild_arch_eval: PtrWeakHashSet::new(),
136-
rebuild_validation: PtrWeakHashSet::new(),
133+
rebuild_arch: FifoPtrWeakHashSet::new(),
134+
rebuild_arch_eval: FifoPtrWeakHashSet::new(),
135+
rebuild_validation: FifoPtrWeakHashSet::new(),
137136
state_init: InitState::NOT_READY,
138137
must_reload_paths: vec![],
139138
load_odoo_addons: true,
@@ -161,9 +160,9 @@ impl SyncOdoo {
161160
session.sync_odoo.stdlib_dir = SyncOdoo::default_stdlib();
162161
session.sync_odoo.modules = HashMap::new();
163162
session.sync_odoo.models = HashMap::new();
164-
session.sync_odoo.rebuild_arch = PtrWeakHashSet::new();
165-
session.sync_odoo.rebuild_arch_eval = PtrWeakHashSet::new();
166-
session.sync_odoo.rebuild_validation = PtrWeakHashSet::new();
163+
session.sync_odoo.rebuild_arch = FifoPtrWeakHashSet::new();
164+
session.sync_odoo.rebuild_arch_eval = FifoPtrWeakHashSet::new();
165+
session.sync_odoo.rebuild_validation = FifoPtrWeakHashSet::new();
167166
session.sync_odoo.state_init = InitState::NOT_READY;
168167
session.sync_odoo.load_odoo_addons = true;
169168
session.sync_odoo.need_rebuild = false;
@@ -537,7 +536,7 @@ impl SyncOdoo {
537536
let mut selected_sym: Option<Rc<RefCell<Symbol>>> = None;
538537
let mut selected_count: u32 = 999999999;
539538
let mut current_count: u32;
540-
for sym in set {
539+
for sym in set.iter() {
541540
current_count = 0;
542541
let file = sym.borrow().get_file().unwrap().upgrade().unwrap();
543542
let file = file.borrow();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::{collections::VecDeque, hash::RandomState, rc::{Rc, Weak}};
2+
use weak_table::{PtrWeakHashSet};
3+
4+
#[derive(Debug)]
5+
pub struct FifoPtrWeakHashSet<T> {
6+
set: PtrWeakHashSet<Weak<T>, RandomState>,
7+
queue: VecDeque<Weak<T>>,
8+
}
9+
10+
impl<T> FifoPtrWeakHashSet<T>
11+
{
12+
pub fn new() -> Self {
13+
Self {
14+
set: PtrWeakHashSet::new(),
15+
queue: VecDeque::new(),
16+
}
17+
}
18+
19+
pub fn insert(&mut self, v: Rc<T>) {
20+
if !self.set.insert(v.clone()) { //it returns true if absent (wrong doc)
21+
self.queue.push_back(Rc::downgrade(&v));
22+
}
23+
}
24+
25+
pub fn iter(&self) -> impl Iterator<Item = Rc<T>> {
26+
self.queue.iter().filter_map(|weak| weak.upgrade())
27+
}
28+
29+
pub fn contains(&self, v: &Rc<T>) -> bool {
30+
self.set.contains(v)
31+
}
32+
33+
pub fn clear(&mut self) {
34+
self.set.clear();
35+
self.queue.clear();
36+
}
37+
38+
pub fn remove(&mut self, v: &Rc<T>) -> bool {
39+
if self.set.remove(v) {
40+
let weak = Rc::downgrade(v);
41+
let pos = self.queue.iter().position(|x| Weak::ptr_eq(x, &weak));
42+
if let Some(pos) = pos {
43+
self.queue.remove(pos);
44+
}
45+
return true
46+
}
47+
false
48+
}
49+
50+
pub fn is_empty(&self) -> bool {
51+
self.set.is_empty()
52+
}
53+
54+
pub fn len(&self) -> usize {
55+
self.set.len()
56+
}
57+
}

server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod constants;
55
pub mod core;
66
pub mod threads;
77
pub mod features;
8+
pub mod fifo_ptr_weak_hash_set;
89
pub mod server;
910
pub mod tasks;
1011
pub mod utils;

0 commit comments

Comments
 (0)