2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-08-30 14:04:52 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Fetchable Dapps support.
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use linked_hash_map::LinkedHashMap;
|
2017-10-05 12:35:01 +02:00
|
|
|
use page::local;
|
2016-09-06 22:12:52 +02:00
|
|
|
use handlers::FetchControl;
|
2016-08-30 14:04:52 +02:00
|
|
|
|
|
|
|
pub enum ContentStatus {
|
2016-12-27 11:15:02 +01:00
|
|
|
Fetching(FetchControl),
|
2017-10-05 12:35:01 +02:00
|
|
|
Ready(local::Dapp),
|
2016-08-30 14:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ContentCache {
|
|
|
|
cache: LinkedHashMap<String, ContentStatus>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentCache {
|
|
|
|
pub fn insert(&mut self, content_id: String, status: ContentStatus) -> Option<ContentStatus> {
|
|
|
|
self.cache.insert(content_id, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove(&mut self, content_id: &str) -> Option<ContentStatus> {
|
|
|
|
self.cache.remove(content_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&mut self, content_id: &str) -> Option<&mut ContentStatus> {
|
|
|
|
self.cache.get_refresh(content_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_garbage(&mut self, expected_size: usize) -> Vec<(String, ContentStatus)> {
|
2016-10-21 11:10:12 +02:00
|
|
|
let len = self.cache.len();
|
2016-08-30 14:04:52 +02:00
|
|
|
|
|
|
|
if len <= expected_size {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut removed = Vec::with_capacity(len - expected_size);
|
2016-10-20 23:41:15 +02:00
|
|
|
|
|
|
|
while self.cache.len() > expected_size {
|
|
|
|
let entry = self.cache.pop_front().expect("expected_size bounded at 0, len is greater; qed");
|
|
|
|
|
2016-08-30 14:04:52 +02:00
|
|
|
match entry.1 {
|
2016-09-06 22:12:52 +02:00
|
|
|
ContentStatus::Fetching(ref fetch) => {
|
2016-08-30 14:04:52 +02:00
|
|
|
trace!(target: "dapps", "Aborting {} because of limit.", entry.0);
|
|
|
|
// Mark as aborted
|
2016-09-06 22:12:52 +02:00
|
|
|
fetch.abort()
|
2016-08-30 14:04:52 +02:00
|
|
|
},
|
|
|
|
ContentStatus::Ready(ref endpoint) => {
|
|
|
|
trace!(target: "dapps", "Removing {} because of limit.", entry.0);
|
2016-10-24 15:09:47 +02:00
|
|
|
// Remove path (dir or file)
|
|
|
|
let res = fs::remove_dir_all(&endpoint.path()).or_else(|_| fs::remove_file(&endpoint.path()));
|
2016-08-30 14:04:52 +02:00
|
|
|
if let Err(e) = res {
|
2016-10-24 15:09:47 +02:00
|
|
|
warn!(target: "dapps", "Unable to remove dapp/content from cache: {:?}", e);
|
2016-08-30 14:04:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removed.push(entry);
|
|
|
|
}
|
|
|
|
removed
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.cache.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn only_keys(data: Vec<(String, ContentStatus)>) -> Vec<String> {
|
|
|
|
data.into_iter().map(|x| x.0).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_remove_least_recently_used() {
|
|
|
|
// given
|
|
|
|
let mut cache = ContentCache::default();
|
|
|
|
cache.insert("a".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
cache.insert("b".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
cache.insert("c".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
|
|
|
|
// when
|
|
|
|
let res = cache.clear_garbage(2);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(cache.len(), 2);
|
|
|
|
assert_eq!(only_keys(res), vec!["a"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_update_lru_if_accessed() {
|
|
|
|
// given
|
|
|
|
let mut cache = ContentCache::default();
|
|
|
|
cache.insert("a".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
cache.insert("b".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
cache.insert("c".into(), ContentStatus::Fetching(Default::default()));
|
|
|
|
|
|
|
|
// when
|
|
|
|
cache.get("a");
|
|
|
|
let res = cache.clear_garbage(2);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert_eq!(cache.len(), 2);
|
|
|
|
assert_eq!(only_keys(res), vec!["b"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|