This commit is contained in:
Jan-Erik Rediger 2019-04-24 17:11:33 +02:00
Родитель d24510ed40
Коммит 783d04b80c
2 изменённых файлов: 34 добавлений и 4 удалений

Просмотреть файл

@ -61,11 +61,18 @@ impl Glean {
self.read().upload_enabled
}
pub fn read_with_store<F>(&self, store_name: &str, transaction_fn: &mut F) where F: FnMut(&rkv::Reader, SingleStore) {
pub fn read_with_store<F>(&self, store_name: &str, mut transaction_fn: F) where F: FnMut(rkv::Reader, SingleStore) {
let inner = self.write();
let store: SingleStore = inner.rkv.open_single(store_name, StoreOptions::create()).unwrap();
let reader = inner.rkv.read().unwrap();
transaction_fn(&reader, store);
transaction_fn(reader, store);
}
pub fn write_with_store<F>(&self, store_name: &str, mut transaction_fn: F) where F: FnMut(rkv::Writer, SingleStore) {
let inner = self.write();
let store: SingleStore = inner.rkv.open_single(store_name, StoreOptions::create()).unwrap();
let writer = inner.rkv.write().unwrap();
transaction_fn(writer, store);
}
pub fn record(&self, lifetime: Lifetime, typ: &str, ping_name: &str, key: &str, value: &rkv::Value) {

Просмотреть файл

@ -27,8 +27,8 @@ impl StorageManager {
let len = store_iter.len();
let mut map = HashMap::new();
let mut snapshotter = |reader: &rkv::Reader, store: rkv::SingleStore| {
let mut iter = store.iter_from(reader, &store_iter).unwrap();
let mut snapshotter = |reader: rkv::Reader, store: rkv::SingleStore| {
let mut iter = store.iter_from(&reader, &store_iter).unwrap();
while let Some(Ok((metric_name, value))) = iter.next() {
if metric_name.len() < len || !metric_name.starts_with(store_iter.as_bytes()) {
break;
@ -58,6 +58,29 @@ impl StorageManager {
snapshot.insert(typ, map);
}
if clear_store {
let wrapped_store_name = format!("#{}#", store_name);
Glean::singleton().write_with_store(Lifetime::Ping.as_str(), |mut writer, store| {
let mut metrics = Vec::new();
{
let mut iter = store.iter_start(&writer).unwrap();
while let Some(Ok((metric_name, _))) = iter.next() {
if let Ok(metric_name) = std::str::from_utf8(metric_name) {
if metric_name.contains(&wrapped_store_name) {
metrics.push(metric_name.to_owned());
}
}
}
}
for to_delete in metrics {
store.delete(&mut writer, to_delete).unwrap();
}
writer.commit().unwrap();
});
}
::serde_json::to_string_pretty(&snapshot).unwrap()
}
}