ordered_code.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2017 Google
  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. // This module provides routines for encoding a sequence of typed
  17. // entities into a string. The resulting strings can be
  18. // lexicographically compared to yield the same comparison value that
  19. // would have been generated if the encoded items had been compared
  20. // one by one according to their type.
  21. //
  22. // More precisely, suppose:
  23. // 1. string A is generated by encoding the sequence of items [A_1..A_n]
  24. // 2. string B is generated by encoding the sequence of items [B_1..B_n]
  25. // 3. The types match; i.e., for all i: A_i was encoded using
  26. // the same routine as B_i
  27. // Then:
  28. // Comparing A vs. B lexicographically is the same as comparing
  29. // the vectors [A_1..A_n] and [B_1..B_n] lexicographically.
  30. //
  31. // Furthermore, if n < m, the encoding of [A_1..A_n] is a strict prefix of
  32. // [A_1..A_m] (unless m = n+1 and A_m is the empty string encoded with
  33. // WriteTrailingString, in which case the encodings are equal).
  34. //
  35. // This module is often useful when generating multi-part sstable
  36. // keys that have to be ordered in a particular fashion.
  37. #ifndef IPHONE_FIRESTORE_PORT_ORDERED_CODE_H_
  38. #define IPHONE_FIRESTORE_PORT_ORDERED_CODE_H_
  39. #include <string>
  40. namespace leveldb {
  41. class Slice;
  42. }
  43. namespace Firestore {
  44. class OrderedCode {
  45. public:
  46. // -------------------------------------------------------------------
  47. // Encoding routines: each one of the following routines append
  48. // one item to "*dest". The Write(Signed)NumIncreasing() and
  49. // Write(Signed)NumDecreasing() routines differ in whether the resulting
  50. // encoding is ordered by increasing number or decreasing number.
  51. // Similarly, WriteString() and WriteStringDecreasing() differ in whether
  52. // the resulting encoding is ordered by the original string in
  53. // lexicographically increasing or decreasing order. WriteString()
  54. // is not called WriteStringIncreasing() for convenience and backward
  55. // compatibility.
  56. static void WriteString(std::string* dest, leveldb::Slice str);
  57. static void WriteNumIncreasing(std::string* dest, uint64_t num);
  58. static void WriteSignedNumIncreasing(std::string* dest, int64_t num);
  59. // Creates an encoding for the "infinite string", a value considered to
  60. // be lexicographically after any real string. Note that in the case of
  61. // WriteInfinityDecreasing(), this would come before any real string as
  62. // the ordering puts lexicographically greater values first.
  63. static void WriteInfinity(std::string* dest);
  64. // Special string append that can only be used at the tail end of
  65. // an encoded string -- blindly appends "str" to "*dest".
  66. static void WriteTrailingString(std::string* dest, leveldb::Slice str);
  67. // -------------------------------------------------------------------
  68. // Decoding routines: these extract an item earlier encoded using
  69. // the corresponding WriteXXX() routines above. The item is read
  70. // from "*src"; "*src" is modified to point past the decoded item;
  71. // and if "result" is non-NULL, "*result" is modified to contain the
  72. // result. In case of string result, the decoded string is appended to
  73. // "*result". Returns true if the next item was read successfully, false
  74. // otherwise.
  75. static bool ReadString(leveldb::Slice* src, std::string* result);
  76. static bool ReadNumIncreasing(leveldb::Slice* src, uint64_t* result);
  77. static bool ReadSignedNumIncreasing(leveldb::Slice* src, int64_t* result);
  78. static bool ReadInfinity(leveldb::Slice* src);
  79. static bool ReadTrailingString(leveldb::Slice* src, std::string* result);
  80. // REQUIRES: next item was encoded by WriteInfinity() or WriteString()
  81. static bool ReadStringOrInfinity(leveldb::Slice* src, std::string* result, bool* inf);
  82. // Helper for testing: corrupt "*str" by changing the kth item separator
  83. // in the string.
  84. static void TEST_Corrupt(std::string* str, int k);
  85. // Helper for testing.
  86. // SkipToNextSpecialByte is an internal routine defined in the .cc file
  87. // with the following semantics. Return a pointer to the first byte
  88. // in the range "[start..limit)" whose value is 0 or 255. If no such
  89. // byte exists in the range, returns "limit".
  90. static const char* TEST_SkipToNextSpecialByte(const char* start, const char* limit);
  91. // Not an instantiable class, but the class exists to make it easy to
  92. // use with a single using statement.
  93. OrderedCode() = delete;
  94. OrderedCode(const OrderedCode&) = delete;
  95. OrderedCode& operator=(const OrderedCode&) = delete;
  96. };
  97. } // namespace Firestore
  98. #endif // IPHONE_FIRESTORE_PORT_ORDERED_CODE_H_