作者: 韩晨旭@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.

92 lines
2.2 KiB

  1. #include "table/vtable_format.h"
  2. #include <locale>
  3. #include "util/coding.h"
  4. namespace leveldb {
  5. namespace {
  6. bool GetChar(Slice* input, unsigned char* value) {
  7. if (input->empty()) {
  8. return false;
  9. }
  10. *value = *input->data();
  11. input->remove_prefix(1);
  12. return true;
  13. }
  14. }
  15. void VTableRecord::Encode(std::string* target) const {
  16. PutLengthPrefixedSlice(target, key);
  17. PutLengthPrefixedSlice(target, value);
  18. }
  19. Status VTableRecord::Decode(Slice* input) {
  20. if (!GetLengthPrefixedSlice(input, &key) ||
  21. !GetLengthPrefixedSlice(input, &value)) {
  22. return Status::Corruption("Error decode VTableRecord");
  23. }
  24. return Status::OK();
  25. }
  26. void RecordEncoder::Encode(const VTableRecord& record) {
  27. record_buff_.clear();
  28. record.Encode(&record_buff_);
  29. record_ = Slice(record_buff_.data(), record_buff_.size());
  30. assert(record.size() < std::numeric_limits<uint32_t>::max());
  31. EncodeFixed32(header_, static_cast<uint32_t>(record_.size()));
  32. }
  33. Status RecordDecoder::DecodeHeader(Slice* input) {
  34. if (!GetFixed32(input, &record_size_)) {
  35. return Status::Corruption("Error decode record header");
  36. }
  37. return Status::OK();
  38. }
  39. Status RecordDecoder::DecodeRecord(Slice* input, VTableRecord* record) const {
  40. Slice record_input(input->data(), record_size_);
  41. input->remove_prefix(record_size_);
  42. return DecodeSrcIntoObj(record_input, record);
  43. }
  44. void VTableHandle::Encode(std::string* target) const {
  45. PutVarint64(target, offset);
  46. PutVarint64(target, size);
  47. }
  48. Status VTableHandle::Decode(Slice* input) {
  49. if (!GetVarint64(input, &offset) || !GetVarint64(input, &size)) {
  50. return Status::Corruption("Error decode VTableHandle");
  51. }
  52. return Status::OK();
  53. }
  54. void VTableIndex::Encode(std::string* target) const {
  55. target->push_back(kVTableIndex);
  56. PutVarint64(target, file_number);
  57. vtable_handle.Encode(target);
  58. }
  59. Status VTableIndex::Decode(Slice* input) {
  60. unsigned char type;
  61. if (!GetChar(input, &type) || type != kVTableIndex ||
  62. !GetVarint64(input, &file_number)) {
  63. return Status::Corruption("Error decode VTableIndex");
  64. }
  65. Status s = vtable_handle.Decode(input);
  66. if (!s.ok()) {
  67. return Status::Corruption("Error decode VTableHandle", s.ToString());
  68. }
  69. return s;
  70. }
  71. } // namespace leveldb