journal_id.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTC_JOURNAL_ID
  17. #define __DTC_JOURNAL_ID
  18. #include <stdint.h>
  19. struct JournalID {
  20. uint32_t serial;
  21. uint32_t offset;
  22. JournalID()
  23. {
  24. serial = 0;
  25. offset = 0;
  26. }
  27. JournalID(const JournalID &v)
  28. {
  29. serial = v.serial;
  30. offset = v.offset;
  31. }
  32. JournalID(uint32_t s, uint32_t o)
  33. {
  34. serial = s;
  35. offset = o;
  36. }
  37. JournalID &operator=(const JournalID &v)
  38. {
  39. serial = v.serial;
  40. offset = v.offset;
  41. return *this;
  42. }
  43. uint32_t Serial() const
  44. {
  45. return serial;
  46. }
  47. uint32_t get_offset() const
  48. {
  49. return offset;
  50. }
  51. /*
  52. * 对外接口全部打包为uint64_t, 方便操作。
  53. */
  54. JournalID &operator=(const uint64_t v)
  55. {
  56. serial = v >> 32;
  57. offset = v & 0xFFFFFFFFULL;
  58. return *this;
  59. }
  60. JournalID(uint64_t v)
  61. {
  62. serial = v >> 32;
  63. offset = v & 0xFFFFFFFFULL;
  64. }
  65. operator uint64_t() const
  66. {
  67. uint64_t v = serial;
  68. v <<= 32;
  69. v += offset;
  70. return v;
  71. }
  72. int Zero() const
  73. {
  74. return serial == 0 && offset == 0;
  75. }
  76. int GE(const JournalID &v)
  77. {
  78. return serial > v.serial ||
  79. (serial == v.serial && offset >= v.offset);
  80. }
  81. } __attribute__((packed));
  82. #endif