|
1 | 1 | use crate::Transaction;
|
| 2 | +use crate::tuple::{Subspace, pack_into, pack}; |
| 3 | +use crate::tuple::hca::HighContentionAllocator; |
2 | 4 |
|
3 |
| -pub struct Directory; |
| 5 | +use super::*; |
| 6 | +use std::result; |
| 7 | +use crate::DirectoryError::Version; |
| 8 | +use crate::directory::directory_subspace::{DirectorySubspaceResult, DirectorySubspace}; |
4 | 9 |
|
5 |
| -// use crate::transactions::{Transaction}; |
| 10 | +const LAYER_VERSION: (u8, u8, u8) = (1, 0, 0); |
| 11 | +const MAJOR_VERSION: u32 = 1; |
| 12 | +const MINOR_VERSION: u32 = 0; |
| 13 | +const PATCH_VERSION: u32 = 0; |
| 14 | +const DEFAULT_NODE_PREFIX:&[u8] = b"\xFE"; |
| 15 | + |
| 16 | +const SUBDIRS:u8 = 0; |
| 17 | + |
| 18 | +#[derive(PartialEq)] |
| 19 | +enum PermissionLevel { |
| 20 | + Read, |
| 21 | + Write |
| 22 | +} |
| 23 | + |
| 24 | +pub type DirectoryResult = result::Result<Directory, DirectoryError>; |
| 25 | + |
| 26 | +pub struct Directory { |
| 27 | + node_prefix: Subspace, |
| 28 | + content_prefix: Subspace, |
| 29 | + |
| 30 | + allow_manual_prefixes: bool, |
| 31 | + |
| 32 | + allocator: HighContentionAllocator, |
| 33 | + root_node: Subspace, |
| 34 | + |
| 35 | + path: Vec<String>, |
| 36 | + layer: Vec<u8>, |
| 37 | +} |
6 | 38 |
|
7 | 39 | impl Directory {
|
8 | 40 |
|
9 |
| - pub fn create_or_open(trx: Transaction, path: Option<&str>, layer: Option<&[u8]> ) -> bool { |
10 |
| - false |
| 41 | + pub fn root() -> Directory { |
| 42 | + Directory { |
| 43 | + node_prefix: DEFAULT_NODE_PREFIX.into(), |
| 44 | + content_prefix: Subspace::from_bytes(b""), |
| 45 | + |
| 46 | + allow_manual_prefixes: false, |
| 47 | + |
| 48 | + allocator: HighContentionAllocator::new(Subspace::from_bytes(b"hca")), |
| 49 | + root_node: DEFAULT_NODE_PREFIX.into(), |
| 50 | + |
| 51 | + path: Vec::new(), |
| 52 | + layer: Vec::new() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn contents_of_node(&self, node: Subspace, path: &[String], layer: &[u8]) -> DirectorySubspaceResult { |
| 57 | + |
| 58 | + |
| 59 | + Ok(DirectorySubspace) |
| 60 | + } |
| 61 | + |
| 62 | + // pub fn new(parent_node: Directory, path: &[String], layer: &[u8]) -> Directory { |
| 63 | + // Directory { |
| 64 | + // |
| 65 | + // allow_manual_prefixes: true, |
| 66 | + // |
| 67 | + // allocator: HighContentionAllocator::new(Subspace::from_bytes(b"hca")), |
| 68 | + // |
| 69 | + // root_node: parent_node.node_prefix.clone(), |
| 70 | + // path: path.to_vec(), |
| 71 | + // layer: layer.to_vec(), |
| 72 | + // |
| 73 | + // } |
| 74 | + // |
| 75 | + // } |
| 76 | + |
| 77 | + pub async fn create_or_open(&self, trx: Transaction, path: &[&str], layer: &[u8], prefix: &[u8], allow_create: bool, allow_open: bool) -> DirectoryResult { |
| 78 | + self.check_version(&trx, PermissionLevel::Read).await?; |
| 79 | + |
| 80 | + if prefix.len() > 0 && !self.allow_manual_prefixes { |
| 81 | + if self.path.len() == 0 { |
| 82 | + return Err(DirectoryError::Message("cannot specify a prefix unless manual prefixes are enabled".to_string())) |
| 83 | + } |
| 84 | + |
| 85 | + return Err(DirectoryError::Message("cannot specify a prefix in a partition".to_string())) |
| 86 | + } |
| 87 | + |
| 88 | + if path.len() == 0 { |
| 89 | + return Err(DirectoryError::CannotOpenRoot) |
| 90 | + } |
| 91 | + |
| 92 | + // FIND |
| 93 | + |
| 94 | + if !allow_create { |
| 95 | + return Err(DirectoryError::NotExist) |
| 96 | + } |
| 97 | + |
| 98 | + // self.initialize_directory(&trx); |
| 99 | + |
| 100 | + if prefix.len() == 0 { |
| 101 | + // let new_subspace = self.allocator.allocate(&trx).await?; |
| 102 | + // TODO: maybe check range and prefix free but I think the allocate does that already |
| 103 | + } else { |
| 104 | + let is_prefix_free = self.is_prefix_free(&trx, prefix).await?; |
| 105 | + } |
| 106 | + // |
| 107 | + // if layer != self.get_layer() && layer != &[] { |
| 108 | + // return Err(DirectoryError::LayerMismatch); |
| 109 | + // } |
| 110 | + |
| 111 | + Ok(Directory::root()) |
| 112 | + } |
| 113 | + |
| 114 | + // pub async fn find(&self, trx: Transaction, path: &[&str]) -> DirectoryResult { |
| 115 | + // |
| 116 | + // } |
| 117 | + |
| 118 | + // pub async fn initialize_directory(&self, trx: &Transaction) { |
| 119 | + // let version = [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION].to_le_bytes(); |
| 120 | + // let version_subspace: &[u8] = b"version"; |
| 121 | + // let version_key = self.root_node.subspace(&version_subspace); |
| 122 | + // |
| 123 | + // trx.set(version_key.bytes(), version).await; |
| 124 | + // } |
| 125 | + |
| 126 | + async fn is_prefix_free(&self, trx: &Transaction, prefix: &[u8]) -> Result<bool, DirectoryError> { |
| 127 | + |
| 128 | + if prefix.len() == 0 { |
| 129 | + return Ok(false); |
| 130 | + } |
| 131 | + |
| 132 | + Ok(true) |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + async fn check_version(&self, trx: &Transaction, perm_level: PermissionLevel ) -> Result<(), DirectoryError> { |
| 137 | + let version_subspace: &[u8] = b"version"; |
| 138 | + let version_key = self.root_node.subspace(&version_subspace); |
| 139 | + let version_opt = trx.get(version_key.bytes(), false).await?; |
| 140 | + |
| 141 | + match version_opt { |
| 142 | + None => { |
| 143 | + if perm_level == PermissionLevel::Write { |
| 144 | + //init |
| 145 | + return Err(Version("fix soon".to_string())); |
| 146 | + } |
| 147 | + |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | + Some(versions) => { |
| 151 | + if versions.len() < 12 { |
| 152 | + return Err(Version("incorrect version length".to_string())); |
| 153 | + } |
| 154 | + let mut arr = [0u8; 4]; |
| 155 | + arr.copy_from_slice(&versions[0..4]); |
| 156 | + let major: u32 = u32::from_be_bytes(arr); |
| 157 | + |
| 158 | + arr.copy_from_slice(&versions[4..8]); |
| 159 | + let minor: u32 = u32::from_be_bytes(arr); |
| 160 | + |
| 161 | + arr.copy_from_slice(&versions[8..12]); |
| 162 | + let patch: u32 = u32::from_be_bytes(arr); |
| 163 | + |
| 164 | + if major > MAJOR_VERSION { |
| 165 | + let msg = format!("cannot load directory with version {}.{}.{} using directory layer {}.{}.{}", major, minor, patch, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION); |
| 166 | + return Err(Version(msg)) |
| 167 | + } |
| 168 | + |
| 169 | + if minor > MINOR_VERSION && perm_level == PermissionLevel::Write { |
| 170 | + let msg = format!("directory with version {}.{}.{} is read-only when opened using directory layer {}.{}.{}", major, minor, patch, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION); |
| 171 | + return Err(Version(msg)) |
| 172 | + } |
| 173 | + |
| 174 | + Ok(()) |
| 175 | + } |
| 176 | + } |
11 | 177 | }
|
12 | 178 |
|
| 179 | + // pub async fn find(&self, trx: &Transaction, path: &[&str]) -> DirectoryResult { |
| 180 | + // let mut node = Directory::root(); |
| 181 | + // |
| 182 | + // for path_name in path { |
| 183 | + // let mut node_layer_id = vec!(SUBDIRS); |
| 184 | + // pack_into(&path_name, &mut node_layer_id); |
| 185 | + // let new_node = node.node_prefix.subspace(&node_layer_id); |
| 186 | + // |
| 187 | + // match trx.get(new_node.bytes(), false).await { |
| 188 | + // Err(_) => { |
| 189 | + // return Ok(node); |
| 190 | + // } |
| 191 | + // Result(node_name) => { |
| 192 | + // let ss = node.node_with_prefix(key); |
| 193 | + // node.node_prefix = ss; |
| 194 | + // node.path.push(path_name.to_string()) |
| 195 | + // } |
| 196 | + // } |
| 197 | + // } |
| 198 | + // |
| 199 | + // |
| 200 | + // Ok(node) |
| 201 | + // } |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + pub fn get_layer(&self) -> &[u8] { |
| 206 | + self.layer.as_slice() |
| 207 | + } |
13 | 208 | }
|
| 209 | + |
0 commit comments