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 
10 #include "table/block_based/data_block_footer.h"
11 
12 #include "rocksdb/table.h"
13 
14 namespace ROCKSDB_NAMESPACE {
15 
16 const int kDataBlockIndexTypeBitShift = 31;
17 
18 // 0x7FFFFFFF
19 const uint32_t kMaxNumRestarts = (1u << kDataBlockIndexTypeBitShift) - 1u;
20 
21 // 0x7FFFFFFF
22 const uint32_t kNumRestartsMask = (1u << kDataBlockIndexTypeBitShift) - 1u;
23 
PackIndexTypeAndNumRestarts(BlockBasedTableOptions::DataBlockIndexType index_type,uint32_t num_restarts)24 uint32_t PackIndexTypeAndNumRestarts(
25     BlockBasedTableOptions::DataBlockIndexType index_type,
26     uint32_t num_restarts) {
27   if (num_restarts > kMaxNumRestarts) {
28     assert(0);  // mute travis "unused" warning
29   }
30 
31   uint32_t block_footer = num_restarts;
32   if (index_type == BlockBasedTableOptions::kDataBlockBinaryAndHash) {
33     block_footer |= 1u << kDataBlockIndexTypeBitShift;
34   } else if (index_type != BlockBasedTableOptions::kDataBlockBinarySearch) {
35     assert(0);
36   }
37 
38   return block_footer;
39 }
40 
UnPackIndexTypeAndNumRestarts(uint32_t block_footer,BlockBasedTableOptions::DataBlockIndexType * index_type,uint32_t * num_restarts)41 void UnPackIndexTypeAndNumRestarts(
42     uint32_t block_footer,
43     BlockBasedTableOptions::DataBlockIndexType* index_type,
44     uint32_t* num_restarts) {
45   if (index_type) {
46     if (block_footer & 1u << kDataBlockIndexTypeBitShift) {
47       *index_type = BlockBasedTableOptions::kDataBlockBinaryAndHash;
48     } else {
49       *index_type = BlockBasedTableOptions::kDataBlockBinarySearch;
50     }
51   }
52 
53   if (num_restarts) {
54     *num_restarts = block_footer & kNumRestartsMask;
55     assert(*num_restarts <= kMaxNumRestarts);
56   }
57 }
58 
59 }  // namespace ROCKSDB_NAMESPACE
60