unique_id_impl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #include <array>
  7. #include "rocksdb/unique_id.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. // Standard size unique ID, good enough for almost all practical purposes
  10. using UniqueId64x2 = std::array<uint64_t, 2>;
  11. // Value never used as an actual unique ID so can be used for "null"
  12. constexpr UniqueId64x2 kNullUniqueId64x2 = {};
  13. // Extended size unique ID, for extra certainty of uniqueness among SST files
  14. // spanning many hosts over a long time (rarely if ever needed)
  15. using UniqueId64x3 = std::array<uint64_t, 3>;
  16. // Value never used as an actual unique ID so can be used for "null"
  17. constexpr UniqueId64x3 kNullUniqueId64x3 = {};
  18. // Dynamic pointer wrapper for one of the two above
  19. struct UniqueIdPtr {
  20. uint64_t *ptr = nullptr;
  21. bool extended = false;
  22. /*implicit*/ UniqueIdPtr(UniqueId64x2 *id) {
  23. ptr = (*id).data();
  24. extended = false;
  25. }
  26. /*implicit*/ UniqueIdPtr(UniqueId64x3 *id) {
  27. ptr = (*id).data();
  28. extended = true;
  29. }
  30. };
  31. // Helper for GetUniqueIdFromTableProperties. This function can also be used
  32. // for temporary ids for files without sufficient information in table
  33. // properties. The internal unique id is more structured than the public
  34. // unique id, so can be manipulated in more ways but very carefully.
  35. // These must be long term stable to ensure GetUniqueIdFromTableProperties
  36. // is long term stable.
  37. Status GetSstInternalUniqueId(const std::string &db_id,
  38. const std::string &db_session_id,
  39. uint64_t file_number, UniqueIdPtr out,
  40. bool force = false);
  41. // Helper for GetUniqueIdFromTableProperties. External unique ids go through
  42. // this extra hashing layer so that prefixes of the unique id have predictable
  43. // "full" entropy. This hashing layer is 1-to-1 on the first 128 bits and on
  44. // the full 192 bits.
  45. // This transformation must be long term stable to ensure
  46. // GetUniqueIdFromTableProperties is long term stable.
  47. void InternalUniqueIdToExternal(UniqueIdPtr in_out);
  48. // Reverse of InternalUniqueIdToExternal mostly for testing purposes
  49. // (demonstrably 1-to-1 on the first 128 bits and on the full 192 bits).
  50. void ExternalUniqueIdToInternal(UniqueIdPtr in_out);
  51. // Convert numerical format to byte format for public API
  52. std::string EncodeUniqueIdBytes(UniqueIdPtr in);
  53. // Reverse of EncodeUniqueIdBytes.
  54. Status DecodeUniqueIdBytes(const std::string &unique_id, UniqueIdPtr out);
  55. // For presenting internal IDs for debugging purposes. Visually distinct from
  56. // UniqueIdToHumanString for external IDs.
  57. std::string InternalUniqueIdToHumanString(UniqueIdPtr in);
  58. // Reformat a random value down to our "DB session id" format,
  59. // which is intended to be compact and friendly for use in file names.
  60. // `lower` is fully preserved and data is lost from `upper`.
  61. //
  62. // Detail: Encoded into 20 chars in base-36 ([0-9A-Z]), which is ~103 bits of
  63. // entropy, which is enough to expect no collisions across a billion servers
  64. // each opening DBs a million times (~2^50). Benefits vs. RFC-4122 unique id:
  65. // * Save ~ dozen bytes per SST file
  66. // * Shorter shared backup file names (some platforms have low limits)
  67. // * Visually distinct from DB id format (usually RFC-4122)
  68. std::string EncodeSessionId(uint64_t upper, uint64_t lower);
  69. // Reverse of EncodeSessionId. Returns NotSupported on error rather than
  70. // Corruption because non-standard session IDs should be allowed with degraded
  71. // functionality.
  72. Status DecodeSessionId(const std::string &db_session_id, uint64_t *upper,
  73. uint64_t *lower);
  74. } // namespace ROCKSDB_NAMESPACE