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
use std::convert::TryInto; use std::fmt; use async_trait::async_trait; use destream::{de, en}; use futures::TryFutureExt; use log::debug; use tc_btree::BTreeView; use tc_error::*; use tc_transact::fs::Dir; use tc_transact::{IntoView, Transaction}; use tcgeneric::{ path_label, Class, Instance, NativeClass, PathLabel, PathSegment, TCPath, TCPathBuf, }; use crate::fs; use crate::txn::Txn; const PREFIX: PathLabel = path_label(&["state", "collection"]); pub use tc_btree::BTreeType; pub type BTree = tc_btree::BTree<fs::File<tc_btree::Node>, fs::Dir, Txn>; pub type BTreeFile = tc_btree::BTreeFile<fs::File<tc_btree::Node>, fs::Dir, Txn>; #[derive(Clone, Copy, Eq, PartialEq)] pub enum CollectionType { BTree(BTreeType), } impl Class for CollectionType {} impl NativeClass for CollectionType { fn from_path(path: &[PathSegment]) -> Option<Self> { debug!("CollectionType::from_path {}", TCPath::from(path)); if path.len() > 2 && &path[0..2] == &PREFIX[..] { match path[2].as_str() { "btree" => BTreeType::from_path(path).map(Self::BTree), _ => None, } } else { None } } fn path(&self) -> TCPathBuf { match self { Self::BTree(btree) => btree.path(), } } } impl fmt::Display for CollectionType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::BTree(btt) => fmt::Display::fmt(btt, f), } } } #[derive(Clone)] pub enum Collection { BTree(BTree), } impl Instance for Collection { type Class = CollectionType; fn class(&self) -> Self::Class { match self { Self::BTree(btree) => CollectionType::BTree(btree.class()), } } } impl From<BTree> for Collection { fn from(btree: BTree) -> Self { Self::BTree(btree) } } pub struct CollectionVisitor { txn: Txn, } impl CollectionVisitor { pub fn new(txn: Txn) -> Self { Self { txn } } pub async fn visit_map_value<A: de::MapAccess>( self, class: CollectionType, access: &mut A, ) -> Result<Collection, A::Error> { match class { CollectionType::BTree(_) => { let file = self .txn .context() .create_file_tmp(*self.txn.id(), BTreeType::default().into()) .map_err(de::Error::custom) .await?; let file = file.try_into().map_err(de::Error::custom)?; access .next_value((self.txn.clone(), file)) .map_ok(Collection::BTree) .await } } } } #[async_trait] impl de::Visitor for CollectionVisitor { type Value = Collection; fn expecting() -> &'static str { "a Collection" } async fn visit_map<A: de::MapAccess>(self, mut map: A) -> Result<Self::Value, A::Error> { let classpath = map .next_key::<TCPathBuf>(()) .await? .ok_or_else(|| de::Error::custom("expected a Collection type"))?; let class = CollectionType::from_path(&classpath) .ok_or_else(|| de::Error::invalid_value(classpath, "a Collection type"))?; self.visit_map_value(class, &mut map).await } } #[async_trait] impl de::FromStream for Collection { type Context = Txn; async fn from_stream<D: de::Decoder>(txn: Txn, decoder: &mut D) -> Result<Self, D::Error> { decoder.decode_map(CollectionVisitor { txn }).await } } #[async_trait] impl<'en> IntoView<'en, fs::Dir> for Collection { type Txn = Txn; type View = CollectionView<'en>; async fn into_view(self, txn: Self::Txn) -> TCResult<Self::View> { match self { Self::BTree(btree) => btree.into_view(txn).map_ok(CollectionView::BTree).await, } } } impl fmt::Display for Collection { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::BTree(btree) => fmt::Display::fmt(btree, f), } } } pub enum CollectionView<'en> { BTree(BTreeView<'en>), } impl<'en> en::IntoStream<'en> for CollectionView<'en> { fn into_stream<E: en::Encoder<'en>>(self, encoder: E) -> Result<E::Ok, E::Error> { match self { Self::BTree(btree) => btree.into_stream(encoder), } } }