| 505 | } |
| 506 | |
| 507 | fn get_contents_ext( |
| 508 | &self, |
| 509 | node: GraphNode<Option<Hash>>, |
| 510 | buf: &mut [u8], |
| 511 | ) -> Result<usize, Self::Error> { |
| 512 | // Handle empty vertices or no hash |
| 513 | if node.end <= node.start { |
| 514 | return Ok(0); |
| 515 | } |
| 516 | |
| 517 | let hash = match node.change { |
| 518 | Some(h) => h, |
| 519 | None => return Ok(0), |
| 520 | }; |
| 521 | |
| 522 | // Load the change |
| 523 | let changes = self.changes.read().unwrap(); |
| 524 | let change = changes |
| 525 | .get(&hash) |
| 526 | .ok_or_else(|| MemoryStoreError::NotFound(hash.to_base32()))?; |
| 527 | |
| 528 | // Extract the content range |
| 529 | let start = node.start.0.as_u64() as usize; |
| 530 | let end = node.end.0.as_u64() as usize; |
| 531 | |
| 532 | if end > change.contents.len() { |
| 533 | return Err(MemoryStoreError::OutOfBounds { |
| 534 | start: start as u64, |
| 535 | end: end as u64, |
| 536 | len: change.contents.len(), |
| 537 | }); |
| 538 | } |
| 539 | |
| 540 | let content = &change.contents[start..end]; |
| 541 | let len = content.len().min(buf.len()); |
| 542 | buf[..len].copy_from_slice(&content[..len]); |
| 543 | |
| 544 | Ok(len) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // TESTS |