data_block_footer.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (c) 2011-present, Facebook, Inc. 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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "table/block_based/data_block_footer.h"
  10. #include "rocksdb/table.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. const int kDataBlockIndexTypeBitShift = 31;
  13. // 0x7FFFFFFF
  14. const uint32_t kMaxNumRestarts = (1u << kDataBlockIndexTypeBitShift) - 1u;
  15. // 0x7FFFFFFF
  16. const uint32_t kNumRestartsMask = (1u << kDataBlockIndexTypeBitShift) - 1u;
  17. uint32_t PackIndexTypeAndNumRestarts(
  18. BlockBasedTableOptions::DataBlockIndexType index_type,
  19. uint32_t num_restarts) {
  20. if (num_restarts > kMaxNumRestarts) {
  21. assert(0); // mute travis "unused" warning
  22. }
  23. uint32_t block_footer = num_restarts;
  24. if (index_type == BlockBasedTableOptions::kDataBlockBinaryAndHash) {
  25. block_footer |= 1u << kDataBlockIndexTypeBitShift;
  26. } else if (index_type != BlockBasedTableOptions::kDataBlockBinarySearch) {
  27. assert(0);
  28. }
  29. return block_footer;
  30. }
  31. void UnPackIndexTypeAndNumRestarts(
  32. uint32_t block_footer,
  33. BlockBasedTableOptions::DataBlockIndexType* index_type,
  34. uint32_t* num_restarts) {
  35. if (index_type) {
  36. if (block_footer & 1u << kDataBlockIndexTypeBitShift) {
  37. *index_type = BlockBasedTableOptions::kDataBlockBinaryAndHash;
  38. } else {
  39. *index_type = BlockBasedTableOptions::kDataBlockBinarySearch;
  40. }
  41. }
  42. if (num_restarts) {
  43. *num_restarts = block_footer & kNumRestartsMask;
  44. assert(*num_restarts <= kMaxNumRestarts);
  45. }
  46. }
  47. } // namespace ROCKSDB_NAMESPACE