| 438 | } |
| 439 | |
| 440 | fn get_contents<F>( |
| 441 | &self, |
| 442 | hash_fn: F, |
| 443 | node: GraphNode<NodeId>, |
| 444 | buf: &mut [u8], |
| 445 | ) -> Result<usize, Self::Error> |
| 446 | where |
| 447 | F: Fn(NodeId) -> Option<Hash>, |
| 448 | { |
| 449 | // Handle empty vertices |
| 450 | if node.end <= node.start || node.is_root() { |
| 451 | return Ok(0); |
| 452 | } |
| 453 | |
| 454 | // Get the hash for this change |
| 455 | let hash = hash_fn(node.change) |
| 456 | .ok_or_else(|| MemoryStoreError::NotFound(format!("NodeId({})", node.change.0)))?; |
| 457 | |
| 458 | // Load the change |
| 459 | let changes = self.changes.read().unwrap(); |
| 460 | let change = changes |
| 461 | .get(&hash) |
| 462 | .ok_or_else(|| MemoryStoreError::NotFound(hash.to_base32()))?; |
| 463 | |
| 464 | // Extract the content range |
| 465 | let start = node.start.0.as_u64() as usize; |
| 466 | let end = node.end.0.as_u64() as usize; |
| 467 | |
| 468 | if end > change.contents.len() { |
| 469 | return Err(MemoryStoreError::OutOfBounds { |
| 470 | start: start as u64, |
| 471 | end: end as u64, |
| 472 | len: change.contents.len(), |
| 473 | }); |
| 474 | } |
| 475 | |
| 476 | let content = &change.contents[start..end]; |
| 477 | let len = content.len().min(buf.len()); |
| 478 | buf[..len].copy_from_slice(&content[..len]); |
| 479 | |
| 480 | Ok(len) |
| 481 | } |
| 482 | |
| 483 | fn get_contents_ext( |
| 484 | &self, |