-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefresh.rs
More file actions
160 lines (144 loc) · 4.83 KB
/
Copy pathrefresh.rs
File metadata and controls
160 lines (144 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Background index refresh for the daemon.
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, mpsc};
use sift_core::{Indexes, StoreMeta};
use crate::index::{ReconcileOutcome, SnapshotRefresh};
use super::ipc::DaemonResponse;
use super::watcher::CorpusWatcher;
use super::{DaemonError, Event, Phase, RefreshFollowUp, StorePaths};
pub(super) enum RefreshResult {
Success(ReconcileOutcome),
Failed,
}
pub(super) enum PendingIndex {
None,
Full,
Paths(Vec<PathBuf>),
}
impl PendingIndex {
pub(super) fn lock(pending: &Arc<Mutex<Self>>) -> Result<MutexGuard<'_, Self>, DaemonError> {
pending
.lock()
.map_err(|_| DaemonError::message("daemon pending queue lock poisoned"))
}
pub(super) fn push(&mut self, paths: Vec<PathBuf>) {
if paths.is_empty() {
*self = Self::Full;
return;
}
match self {
Self::Full => {}
Self::None => *self = Self::Paths(paths),
Self::Paths(existing) => {
for path in paths {
if !existing.contains(&path) {
existing.push(path);
}
}
}
}
}
pub(super) const fn is_pending(&self) -> bool {
!matches!(self, Self::None)
}
pub(super) fn take(&mut self) -> Option<Vec<PathBuf>> {
match std::mem::replace(self, Self::None) {
Self::None => None,
Self::Full => Some(Vec::new()),
Self::Paths(paths) => Some(paths),
}
}
pub(super) fn reconcile(
&mut self,
sift_dir: &Path,
meta: &StoreMeta,
) -> Option<ReconcileOutcome> {
let paths = self.take()?;
let result = Indexes::open(sift_dir, meta)
.and_then(|mut indexes| SnapshotRefresh::run(&mut indexes));
match result {
Ok(outcome) => Some(outcome),
Err(e) => {
eprintln!("sift-daemon: refresh failed: {e}");
self.push(paths);
None
}
}
}
}
pub(super) enum RefreshScope {
CorpusAndPending,
PendingOnly,
}
pub(super) struct IndexRefresh<'a> {
pub(super) tx: &'a mpsc::Sender<Event>,
pub(super) store: &'a Path,
pub(super) pending: &'a Arc<Mutex<PendingIndex>>,
}
impl IndexRefresh<'_> {
pub(super) fn apply_index(
&self,
paths: Vec<PathBuf>,
reply: &mpsc::Sender<DaemonResponse>,
watcher: &mut CorpusWatcher,
store: &Path,
phase: &mut Phase,
) -> Result<(), DaemonError> {
watcher.rebind(store)?;
PendingIndex::lock(self.pending)?.push(paths);
let _ = reply.send(DaemonResponse::Accepted);
if phase.is_refreshing() {
*phase = Phase::Refreshing {
follow_up: RefreshFollowUp::Queued,
};
} else {
self.begin(RefreshScope::CorpusAndPending, phase);
}
Ok(())
}
pub(super) fn begin(&self, scope: RefreshScope, phase: &mut Phase) {
self.spawn(scope);
*phase = Phase::Refreshing {
follow_up: RefreshFollowUp::None,
};
}
fn spawn(&self, scope: RefreshScope) {
let tx = self.tx.clone();
let sift_dir = self.store.to_path_buf();
let pending = Arc::clone(self.pending);
std::thread::spawn(move || {
let meta = match StorePaths::read_meta(&sift_dir) {
Ok(meta) => meta,
Err(e) => {
eprintln!("sift-daemon: {e}");
let _ = tx.send(Event::RefreshFinished(RefreshResult::Failed));
return;
}
};
let mut outcome = None;
if matches!(scope, RefreshScope::CorpusAndPending) {
let result = Indexes::open(&sift_dir, &meta)
.and_then(|mut indexes| SnapshotRefresh::run(&mut indexes));
match result {
Ok(committed) => outcome = Some(committed),
Err(e) => {
eprintln!("sift-daemon: refresh failed: {e}");
let _ = tx.send(Event::RefreshFinished(RefreshResult::Failed));
return;
}
}
}
let pending_outcome = if let Ok(mut queue) = pending.lock() {
queue.reconcile(&sift_dir, &meta)
} else {
eprintln!("sift-daemon: pending queue lock poisoned");
let _ = tx.send(Event::RefreshFinished(RefreshResult::Failed));
return;
};
let result = pending_outcome
.or(outcome)
.map_or(RefreshResult::Failed, RefreshResult::Success);
let _ = tx.send(Event::RefreshFinished(result));
});
}
}