| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::cell_id::CellId; |
| 3 |
use crate::fsshttpb::data_element::storage_index::StorageIndex; |
| 4 |
use crate::fsshttpb::data_element::storage_manifest::StorageManifest; |
| 5 |
use crate::fsshttpb::packaging::OneStorePackaging; |
| 6 |
use crate::onestore::header::StoreHeader; |
| 7 |
use crate::onestore::object_space::ObjectSpace; |
| 8 |
use crate::onestore::revision::Revision; |
| 9 |
use crate::shared::guid::Guid; |
| 10 |
use std::collections::{HashMap, HashSet}; |
| 11 |
|
| 12 |
pub(crate) mod header; |
| 13 |
pub(crate) mod mapping_table; |
| 14 |
pub(crate) mod object; |
| 15 |
pub(crate) mod object_space; |
| 16 |
pub(crate) mod revision; |
| 17 |
mod revision_role; |
| 18 |
pub(crate) mod types; |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
pub(crate) struct OneStore<'a> { |
| 22 |
schema: Guid, |
| 23 |
header: StoreHeader, |
| 24 |
data_root: ObjectSpace<'a>, |
| 25 |
object_spaces: HashMap<CellId, ObjectSpace<'a>>, |
| 26 |
} |
| 27 |
|
| 28 |
impl<'a> OneStore<'a> { |
| 29 |
pub fn schema_guid(&self) -> Guid { |
| 30 |
self.schema |
| 31 |
} |
| 32 |
|
| 33 |
pub(crate) fn data_root(&'a self) -> &'a ObjectSpace { |
| 34 |
&self.data_root |
| 35 |
} |
| 36 |
|
| 37 |
pub(crate) fn object_space(&'a self, space_id: CellId) -> Option<&'a ObjectSpace<'a>> { |
| 38 |
self.object_spaces.get(&space_id) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) fn parse_store(package: &OneStorePackaging) -> Result<OneStore> { |
| 43 |
let mut parsed_object_spaces = HashSet::new(); |
| 44 |
|
| 45 |
// [ONESTORE] 2.7.1: Parse storage manifest |
| 46 |
let storage_index = package |
| 47 |
.data_element_package |
| 48 |
.find_storage_index() |
| 49 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("storage index is missing".into()))?; |
| 50 |
let storage_manifest = package |
| 51 |
.data_element_package |
| 52 |
.find_storage_manifest() |
| 53 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("storage manifest is missing".into()))?; |
| 54 |
|
| 55 |
let header_cell_id = find_header_cell_id(storage_manifest)?; |
| 56 |
|
| 57 |
let header_cell_mapping_id = storage_index |
| 58 |
.find_cell_mapping_id(header_cell_id) |
| 59 |
.ok_or_else(|| { |
| 60 |
ErrorKind::MalformedOneStoreData("header cell mapping id not found".into()) |
| 61 |
})?; |
| 62 |
|
| 63 |
// [ONESTORE] 2.7.2: Parse header cell |
| 64 |
let header_cell = package |
| 65 |
.data_element_package |
| 66 |
.find_objects(header_cell_mapping_id, &storage_index)? |
| 67 |
.into_iter() |
| 68 |
.next() |
| 69 |
.ok_or_else(|| { |
| 70 |
ErrorKind::MalformedOneStoreData("no header object in header cell".into()) |
| 71 |
})?; |
| 72 |
|
| 73 |
let header = StoreHeader::parse(header_cell)?; |
| 74 |
|
| 75 |
parsed_object_spaces.insert(header_cell_id); |
| 76 |
|
| 77 |
// FIXME: document revision cache |
| 78 |
let mut revision_cache = HashMap::new(); |
| 79 |
|
| 80 |
// Parse data root |
| 81 |
|
| 82 |
let data_root_cell_id = find_data_root_cell_id(storage_manifest)?; |
| 83 |
let (_, data_root) = parse_object_space( |
| 84 |
data_root_cell_id, |
| 85 |
storage_index, |
| 86 |
&package, |
| 87 |
&mut revision_cache, |
| 88 |
)?; |
| 89 |
|
| 90 |
parsed_object_spaces.insert(data_root_cell_id); |
| 91 |
|
| 92 |
// Parse other object spaces |
| 93 |
|
| 94 |
let mut object_spaces = HashMap::new(); |
| 95 |
|
| 96 |
for mapping in storage_index.cell_mappings.values() { |
| 97 |
if mapping.id.is_nil() { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
if parsed_object_spaces.contains(&mapping.cell_id) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
let (id, group) = parse_object_space( |
| 106 |
mapping.cell_id, |
| 107 |
storage_index, |
| 108 |
&package, |
| 109 |
&mut revision_cache, |
| 110 |
)?; |
| 111 |
object_spaces.insert(id, group); |
| 112 |
} |
| 113 |
|
| 114 |
Ok(OneStore { |
| 115 |
schema: storage_manifest.id, |
| 116 |
header, |
| 117 |
data_root, |
| 118 |
object_spaces, |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
fn parse_object_space<'a, 'b>( |
| 123 |
cell_id: CellId, |
| 124 |
storage_index: &'a StorageIndex, |
| 125 |
package: &'a OneStorePackaging, |
| 126 |
revision_cache: &'b mut HashMap<CellId, Revision<'a>>, |
| 127 |
) -> Result<(CellId, ObjectSpace<'a>)> { |
| 128 |
let mapping = storage_index |
| 129 |
.cell_mappings |
| 130 |
.get(&cell_id) |
| 131 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("cell mapping not found".into()))?; |
| 132 |
|
| 133 |
ObjectSpace::parse(mapping, storage_index, package, revision_cache) |
| 134 |
} |
| 135 |
|
| 136 |
fn find_header_cell_id(manifest: &StorageManifest) -> Result<CellId> { |
| 137 |
manifest |
| 138 |
.roots |
| 139 |
.get(&exguid!({{1A5A319C-C26B-41AA-B9C5-9BD8C44E07D4}, 1})) |
| 140 |
.copied() |
| 141 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("no header cell root".into()).into()) |
| 142 |
} |
| 143 |
|
| 144 |
fn find_data_root_cell_id(manifest: &StorageManifest) -> Result<CellId> { |
| 145 |
manifest |
| 146 |
.roots |
| 147 |
.get(&exguid!({{84DEFAB9-AAA3-4A0D-A3A8-520C77AC7073}, 2})) |
| 148 |
.copied() |
| 149 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("no header cell root".into()).into()) |
| 150 |
} |