You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.1 KiB
99 lines
2.1 KiB
4 years ago
|
use super::*;
|
||
|
|
||
|
|
||
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||
|
pub struct Freeze
|
||
|
{
|
||
|
metadata: StoreMetadata,
|
||
|
data: Vec<Entry>,
|
||
|
}
|
||
|
|
||
|
impl Freeze
|
||
|
{
|
||
|
pub(super) fn new_ref(from: &Store) -> Self
|
||
|
{
|
||
|
Self {
|
||
|
metadata: from.metadata.clone(),
|
||
|
data: from.data.iter().map(Entry::clone).collect(),
|
||
|
}
|
||
|
}
|
||
|
pub(super) fn new_moved(from: Store) -> Self
|
||
|
{
|
||
|
Self {
|
||
|
metadata: from.metadata,
|
||
|
data: from.data.into_iter().map(Entry::with_no_cache).collect(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub(super) fn create_new(&self) -> Store
|
||
|
{
|
||
|
let mut new = Store::with_capacity(self.metadata.clone(), self.data.len());
|
||
|
|
||
|
for entry in self.data.iter()
|
||
|
{
|
||
|
let hash = entry.hash().clone();
|
||
|
let hash_idx = new.data_hashes.insert(hash);
|
||
|
|
||
|
// insert the tags
|
||
|
for tag in entry.tags.iter()
|
||
|
{
|
||
|
if let Some(&ti) = new.tags.get(tag) {
|
||
|
// This tag has an entry already, append to it
|
||
|
new.tag_mappings.get_mut(ti).unwrap().push(hash_idx);
|
||
|
} else {
|
||
|
// This tag has no entry, create it
|
||
|
let ti = new.tag_mappings.insert(vec![hash_idx]);
|
||
|
new.tags.insert(tag.clone(), ti);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
new.data.insert(entry.clone());
|
||
|
}
|
||
|
new
|
||
|
|
||
|
}
|
||
|
pub(super) fn into_new(self) -> Store
|
||
|
{
|
||
|
let Freeze { data, metadata } = self;
|
||
|
|
||
|
let mut new = Store::with_capacity(metadata, data.len());
|
||
|
for entry in data.into_iter()
|
||
|
{
|
||
|
let hash = entry.hash().clone();
|
||
|
let hash_idx = new.data_hashes.insert(hash);
|
||
|
|
||
|
// insert the tags
|
||
|
for tag in entry.tags.iter()
|
||
|
{
|
||
|
if let Some(&ti) = new.tags.get(tag) {
|
||
|
// This tag has an entry already, append to it
|
||
|
new.tag_mappings.get_mut(ti).unwrap().push(hash_idx);
|
||
|
} else {
|
||
|
// This tag has no entry, create it
|
||
|
let ti = new.tag_mappings.insert(vec![hash_idx]);
|
||
|
new.tags.insert(tag.clone(), ti);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
new.data.insert(entry);
|
||
|
}
|
||
|
new
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Store> for Freeze
|
||
|
{
|
||
|
#[inline] fn from(from: Store) -> Self
|
||
|
{
|
||
|
Self::new_moved(from)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Freeze> for Store
|
||
|
{
|
||
|
#[inline] fn from(from: Freeze) -> Self
|
||
|
{
|
||
|
from.into_new()
|
||
|
}
|
||
|
}
|