作者: 韩晨旭@ArcueidType(Arcueid) 10225101440 李畅@wesley 10225102463 设计文档为PLAN.md,md版本报告为README.md,pdf版本报告为Report.pdf
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.2 KiB

  1. #ifndef VTABLE_FORMAT_H
  2. #define VTABLE_FORMAT_H
  3. #include "leveldb/slice.h"
  4. #include "leveldb/status.h"
  5. #include "table/format.h"
  6. namespace leveldb {
  7. const uint64_t kRecordHeaderSize = 4;
  8. struct VTableRecord {
  9. Slice key;
  10. Slice value;
  11. void Encode(std::string* target) const;
  12. Status Decode(Slice* input);
  13. size_t size() const { return key.size() + value.size(); }
  14. friend bool operator==(const VTableRecord& a, const VTableRecord& b) {
  15. return a.key == b.key && a.value == b.value;
  16. }
  17. };
  18. class RecordEncoder {
  19. public:
  20. // TODO: Support compression while encoding a record
  21. RecordEncoder() = default;
  22. // Encode a vTable record
  23. void Encode(const VTableRecord& record);
  24. // Get the size of encoded record
  25. size_t GetEncodedSize() const { return sizeof(header_) + record_.size(); }
  26. // Get the header
  27. Slice GetHeader() const { return {header_, sizeof(header_)}; }
  28. // Get the encoded record
  29. Slice GetRecord() const {return record_; }
  30. private:
  31. char header_[kRecordHeaderSize];
  32. Slice record_;
  33. std::string record_buff_;
  34. };
  35. class RecordDecoder {
  36. public:
  37. Status DecodeHeader(Slice* input);
  38. Status DecodeRecord(Slice* input, VTableRecord* record) const;
  39. size_t GetDecodedSize() const { return record_size_; }
  40. private:
  41. uint32_t record_size_{0};
  42. };
  43. struct VTableHandle {
  44. uint64_t offset{0};
  45. uint64_t size{0};
  46. void Encode(std::string* target) const;
  47. Status Decode(Slice* input);
  48. friend bool operator==(const VTableHandle& a, const VTableHandle& b) {
  49. return a.offset == b.offset && a.size == b.size;
  50. }
  51. };
  52. struct VTableIndex {
  53. enum Type : unsigned char {
  54. kVTableIndex = 1,
  55. };
  56. uint64_t file_number{0};
  57. VTableHandle vtable_handle;
  58. void Encode(std::string* target) const;
  59. Status Decode(Slice* input);
  60. friend bool operator==(const VTableIndex& a, const VTableIndex& b) {
  61. return a.file_number == b.file_number && a.vtable_handle == b.vtable_handle;
  62. }
  63. };
  64. template <typename T>
  65. Status DecodeSrcIntoObj(const Slice& src, T* target) {
  66. Slice input = src;
  67. Status s = target->Decode(&input);
  68. if (s.ok() && !input.empty()) {
  69. s = Status::Corruption(Slice());
  70. }
  71. return s;
  72. }
  73. } // namespace leveldb
  74. #endif //VTABLE_FORMAT_H