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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};

/// Stores urls, tracking whether or not they have been visited.
#[derive(Debug)]
pub struct UrlDb(Arc<Mutex<HashMap<String, Status>>>);

impl Clone for UrlDb {
    /// Returns a clone/handle of the given UrlDb.
    fn clone(&self) -> Self {
        UrlDb(Arc::clone(&self.0))
    }
}

impl UrlDb {
    /// Returns a new UrlDb instance.
    pub fn new() -> Self {
        UrlDb(Arc::new(Mutex::new(HashMap::new())))
    }

    /// Returns an iterator over the urls that were discovered and visited.
    pub fn visited_urls_iter(&self) -> impl Iterator<Item = String> {
        let hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Visited)
            .map(|(k, _v)| k)
    }

    /// Returns an iterator over the urls that are currently staged.
    pub fn staged_urls_iter(&self) -> impl Iterator<Item = String> {
        let hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Staged)
            .map(|(k, _v)| k)
    }

    /// Returns an iterator over the urls that were discovered, but unvisited.
    pub fn unvisited_urls_iter(&self) -> impl Iterator<Item = String> {
        let hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Unvisited)
            .map(|(k, _v)| k)
    }

    /// Returns an iterator over the urls that were discovered, but skipped.
    pub fn skipped_urls_iter(&self) -> impl Iterator<Item = String> {
        let hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Skip)
            .map(|(k, _v)| k)
    }

    /// Returns an iterator over the urls that were discovered, but encountered and error while
    /// visiting.
    pub fn errored_urls_iter(&self) -> impl Iterator<Item = String> {
        let hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Error)
            .map(|(k, _v)| k)
    }

    /// Returns the number of urls that were visited.
    pub fn num_visited_urls(&self) -> usize {
        self.visited_urls_iter().count()
    }

    /// Returns the number of urls that were staged.
    pub fn num_staged_urls(&self) -> usize {
        self.staged_urls_iter().count()
    }

    /// Returns the number of urls that were unvisited.
    pub fn num_unvisited_urls(&self) -> usize {
        self.unvisited_urls_iter().count()
    }

    /// Returns the number of urls that were skipped.
    pub fn num_skipped_urls(&self) -> usize {
        self.skipped_urls_iter().count()
    }

    /// Returns the number of urls that encountered an error.
    pub fn num_errored_urls(&self) -> usize {
        self.errored_urls_iter().count()
    }

    /// Inserts and marks a url as visited.
    pub fn mark_visited(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.insert(url.to_owned(), Status::Visited);
    }

    /// Inserts and marks a url as staged.
    pub fn mark_staged(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.insert(url.to_owned(), Status::Staged);
    }

    /// Inserts and marks a url as unvisited.
    pub fn mark_unvisited(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.insert(url.to_owned(), Status::Unvisited);
    }

    /// Inserts and marks a url as skipped.
    pub fn mark_skipped(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.insert(url.to_owned(), Status::Skip);
    }

    /// Inserts and marks a url as errored.
    pub fn mark_errored(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.insert(url.to_owned(), Status::Error);
    }

    /// Inserts and marks a url as visited, only if the url is new.
    pub fn cond_mark_visited(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.entry(url.clone()).or_insert(Status::Visited);
    }

    /// Inserts and marks a url as staged, only if the url is new.
    pub fn cond_mark_staged(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.entry(url.clone()).or_insert(Status::Staged);
    }

    /// Inserts and marks a url as unvisited, only if the url is new.
    pub fn cond_mark_unvisited(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.entry(url.clone()).or_insert(Status::Unvisited);
    }

    /// Inserts and marks a url as skipped, only if the url is new.
    pub fn cond_mark_skipped(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.entry(url.clone()).or_insert(Status::Skip);
    }

    /// Inserts and marks a url as errored, only if the url is new.
    pub fn cond_mark_errored(&mut self, url: String) -> () {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        hm.entry(url.clone()).or_insert(Status::Error);
    }

    /// Move all unvisited urls onto the stage.
    pub fn stage_unvisited_urls(&mut self) {
        let mut hm: MutexGuard<HashMap<String, Status>> = match self.0.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        for (k, _v) in hm
            .clone()
            .into_iter()
            .filter(|(_k, v)| *v == Status::Unvisited)
        {
            hm.insert(k, Status::Staged);
        }
    }
}

#[derive(Copy, Debug, Clone)]
enum Status {
    /// A Url that was already visited successfully.
    Visited,
    /// A Url that has not yet been visited, but is about to be.
    Staged,
    /// A newly discovered Url that has not yet been processed or visited.
    Unvisited,
    /// A Url that has been determined to be skipped; ultimately will not be visted.
    Skip,
    /// A Url of which an error was encountered while attempting to visit.
    Error,
}

impl PartialEq for Status {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Visited, Self::Visited)
            | (Self::Staged, Self::Staged)
            | (Self::Unvisited, Self::Unvisited)
            | (Self::Skip, Self::Skip)
            | (Self::Error, Self::Error) => true,
            _ => false,
        }
    }
}