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 6 #pragma once 7 8 #include <array> 9 10 #include "rocksdb/unique_id.h" 11 12 namespace ROCKSDB_NAMESPACE { 13 14 using UniqueId64x3 = std::array<uint64_t, 3>; 15 16 // Helper for GetUniqueIdFromTableProperties. This function can also be used 17 // for temporary ids for files without sufficient information in table 18 // properties. The internal unique id is more structured than the public 19 // unique id, so can be manipulated in more ways but very carefully. 20 // These must be long term stable to ensure GetUniqueIdFromTableProperties 21 // is long term stable. 22 Status GetSstInternalUniqueId(const std::string &db_id, 23 const std::string &db_session_id, 24 uint64_t file_number, UniqueId64x3 *out); 25 26 // Helper for GetUniqueIdFromTableProperties. External unique ids go through 27 // this extra hashing layer so that prefixes of the unique id have predictable 28 // "full" entropy. This hashing layer is 1-to-1 on the first 128 bits and on 29 // the full 192 bits. 30 // This transformation must be long term stable to ensure 31 // GetUniqueIdFromTableProperties is long term stable. 32 void InternalUniqueIdToExternal(UniqueId64x3 *in_out); 33 34 // Reverse of InternalUniqueIdToExternal mostly for testing purposes 35 // (demonstrably 1-to-1 on the first 128 bits and on the full 192 bits). 36 void ExternalUniqueIdToInternal(UniqueId64x3 *in_out); 37 38 // Convert numerical format to byte format for public API 39 std::string EncodeUniqueIdBytes(const UniqueId64x3 &in); 40 41 // Reformat a random value down to our "DB session id" format, 42 // which is intended to be compact and friendly for use in file names. 43 // `lower` is fully preserved and data is lost from `upper`. 44 // 45 // Detail: Encoded into 20 chars in base-36 ([0-9A-Z]), which is ~103 bits of 46 // entropy, which is enough to expect no collisions across a billion servers 47 // each opening DBs a million times (~2^50). Benefits vs. RFC-4122 unique id: 48 // * Save ~ dozen bytes per SST file 49 // * Shorter shared backup file names (some platforms have low limits) 50 // * Visually distinct from DB id format (usually RFC-4122) 51 std::string EncodeSessionId(uint64_t upper, uint64_t lower); 52 53 // Reverse of EncodeSessionId. Returns NotSupported on error rather than 54 // Corruption because non-standard session IDs should be allowed with degraded 55 // functionality. 56 Status DecodeSessionId(const std::string &db_session_id, uint64_t *upper, 57 uint64_t *lower); 58 59 } // namespace ROCKSDB_NAMESPACE 60