codec.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "public.h"
  3. #include <optional>
  4. #include <vector>
  5. namespace NErasure {
  6. //! Describes a generic way to generate parity blocks from data blocks and
  7. //! to recover (repair) missing blocks.
  8. /*!
  9. * Given N data blocks (numbered from 0 to N - 1) one can call #Encode to generate
  10. * another M parity blocks (numbered from N to N + M - 1).
  11. *
  12. * If some of the resulting N + M blocks ever become missing one can attempt to
  13. * repair the missing blocks by calling #Decode.
  14. *
  15. * Here N and M are fixed (codec-specific) parameters.
  16. * Call #GetDataPartCount and #GetParityPartCount to figure out the
  17. * the values for N and M, respectively.
  18. *
  19. */
  20. template <class TBlobType>
  21. struct ICodec {
  22. //! Computes a sequence of parity blocks for given data blocks.
  23. /*!
  24. * The size of #blocks must be equal to #GetDataPartCount.
  25. * The size of the returned array is equal to #GetParityPartCount.
  26. */
  27. virtual std::vector<TBlobType> Encode(const std::vector<TBlobType>& blocks) const = 0;
  28. //! Decodes (repairs) missing blocks.
  29. /*!
  30. * #erasedIndices must contain the set of erased blocks indices.
  31. * #blocks must contain known blocks (in the order specified by #GetRepairIndices).
  32. * \returns The repaired blocks.
  33. */
  34. virtual std::vector<TBlobType> Decode(
  35. const std::vector<TBlobType>& blocks,
  36. const TPartIndexList& erasedIndices) const = 0;
  37. //! Given a set of missing block indices, returns |true| if missing blocks can be repaired.
  38. //! Due to performance reasons the elements of #erasedIndices must unique and sorted.
  39. virtual bool CanRepair(const TPartIndexList& erasedIndices) const = 0;
  40. //! Rapid version that works with set instead of list.
  41. virtual bool CanRepair(const TPartIndexSet& erasedIndices) const = 0;
  42. //! Given a set of missing block indices, checks if missing blocks can be repaired.
  43. /*!
  44. * \returns
  45. * If repair is not possible, returns |std::nullopt|.
  46. * Otherwise returns the indices of blocks (both data and parity) to be passed to #Decode
  47. * (in this very order). Not all known blocks may be needed for repair.
  48. */
  49. virtual std::optional<TPartIndexList> GetRepairIndices(const TPartIndexList& erasedIndices) const = 0;
  50. //! Returns the number of data blocks this codec can handle.
  51. virtual int GetDataPartCount() const = 0;
  52. //! Returns the number of parity blocks this codec can handle.
  53. virtual int GetParityPartCount() const = 0;
  54. //! Returns the maximum number of blocks that can always be repaired when missing.
  55. virtual int GetGuaranteedRepairablePartCount() const = 0;
  56. //! Every block passed to this codec must have size divisible by the result of #GetWordSize.
  57. virtual int GetWordSize() const = 0;
  58. // Extension methods
  59. //! Returns the sum of #GetDataPartCount and #GetParityPartCount.
  60. int GetTotalPartCount() const {
  61. return GetDataPartCount() + GetParityPartCount();
  62. }
  63. };
  64. } // namespace NErasure