chunked_storage.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use crate::geode::file_sequence::FileSequence;
  19. use std::path::PathBuf;
  20. /// `ChunkedStorage` is a representation of a file or directory we're trying to
  21. /// retrieve from `Geode`.
  22. #[derive(Debug)]
  23. pub struct ChunkedStorage {
  24. /// Vector of chunk hashes and a bool which is `true` if the chunk is
  25. /// available locally.
  26. chunks: Vec<(blake3::Hash, bool)>,
  27. /// FileSequence containing the list of file paths and file sizes, it has
  28. /// a single item if this is not a directory but a single file.
  29. fileseq: FileSequence,
  30. /// Set to `true` if this ChunkedStorage is the representation of a
  31. /// directory.
  32. is_dir: bool,
  33. }
  34. impl ChunkedStorage {
  35. pub fn new(hashes: &[blake3::Hash], files: &[(PathBuf, u64)], is_dir: bool) -> Self {
  36. Self {
  37. chunks: hashes.iter().map(|x| (*x, false)).collect(),
  38. fileseq: FileSequence::new(files, is_dir),
  39. is_dir,
  40. }
  41. }
  42. /// Check whether we have all the chunks available locally.
  43. pub fn is_complete(&self) -> bool {
  44. !self.chunks.iter().any(|(_, available)| !available)
  45. }
  46. /// Return an iterator over the chunks and their availability.
  47. pub fn iter(&self) -> core::slice::Iter<'_, (blake3::Hash, bool)> {
  48. self.chunks.iter()
  49. }
  50. /// Return an mutable iterator over the chunks and their availability.
  51. pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, (blake3::Hash, bool)> {
  52. self.chunks.iter_mut()
  53. }
  54. /// Return the number of chunks.
  55. pub fn len(&self) -> usize {
  56. self.chunks.len()
  57. }
  58. /// Return `true` if the chunked file contains no chunk.
  59. pub fn is_empty(&self) -> bool {
  60. self.chunks.is_empty()
  61. }
  62. /// Return the number of chunks available locally.
  63. pub fn local_chunks(&self) -> usize {
  64. self.chunks.iter().filter(|(_, p)| *p).count()
  65. }
  66. /// Return `chunks`.
  67. pub fn get_chunks(&self) -> &Vec<(blake3::Hash, bool)> {
  68. &self.chunks
  69. }
  70. /// Return a mutable chunk from `chunks`.
  71. pub fn get_chunk_mut(&mut self, index: usize) -> &mut (blake3::Hash, bool) {
  72. &mut self.chunks[index]
  73. }
  74. /// Return the list of files from the `reader`.
  75. pub fn get_files(&self) -> &Vec<(PathBuf, u64)> {
  76. self.fileseq.get_files()
  77. }
  78. /// Return `fileseq`.
  79. pub fn get_fileseq(&mut self) -> &mut FileSequence {
  80. &mut self.fileseq
  81. }
  82. /// Return `is_dir`.
  83. pub fn is_dir(&self) -> bool {
  84. self.is_dir
  85. }
  86. }