| 229 | } |
| 230 | |
| 231 | pub fn load_node(&mut self, offset: usize) -> io::Result<Node<K, V>> { |
| 232 | let original_offset = offset.clone(); |
| 233 | let mut offset = offset.clone(); |
| 234 | |
| 235 | // println!("Loading node at offset: {}", offset); |
| 236 | |
| 237 | let mut serialized = Vec::new(); |
| 238 | |
| 239 | let mut is_primary; |
| 240 | |
| 241 | let mut serialized_len; |
| 242 | |
| 243 | let mut bytes_read = 0; |
| 244 | |
| 245 | self.acquire_block_lock(original_offset)?; |
| 246 | |
| 247 | loop { |
| 248 | let block_is_primary = |
| 249 | u8::from_le_bytes(self.read_from_offset(offset, 1).try_into().unwrap()); |
| 250 | |
| 251 | if block_is_primary == 0 { |
| 252 | is_primary = false; |
| 253 | } else if block_is_primary == 1 { |
| 254 | is_primary = true; |
| 255 | } else { |
| 256 | return Err(io::Error::new( |
| 257 | io::ErrorKind::InvalidData, |
| 258 | "Invalid block type", |
| 259 | )); |
| 260 | } |
| 261 | |
| 262 | if !is_primary && bytes_read == 0 { |
| 263 | return Err(io::Error::new( |
| 264 | io::ErrorKind::InvalidData, |
| 265 | "Primary block not found", |
| 266 | )); |
| 267 | } |
| 268 | |
| 269 | offset += 1; // one for the primary byte |
| 270 | |
| 271 | serialized_len = usize::from_le_bytes( |
| 272 | self.read_from_offset(offset, SIZE_OF_USIZE) |
| 273 | .try_into() |
| 274 | .unwrap(), |
| 275 | ); |
| 276 | |
| 277 | offset += SIZE_OF_USIZE; |
| 278 | |
| 279 | // println!("Serialized len: {}", serialized_len); |
| 280 | |
| 281 | let bytes_to_read = std::cmp::min(serialized_len - bytes_read, BLOCK_DATA_SIZE); |
| 282 | // println!( |
| 283 | // "Bytes read: {}, bytes to read: {}", |
| 284 | // bytes_read, bytes_to_read |
| 285 | // ); |
| 286 | |
| 287 | bytes_read += bytes_to_read; |
| 288 | |