block_based_table_factory.cc 7.9 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.


#include "table/block_based_table_factory.h"

#include <memory>
14
#include <string>
S
Siying Dong 已提交
15
#include <stdint.h>
16

17
#include "port/port.h"
18
#include "rocksdb/flush_block_policy.h"
19
#include "rocksdb/cache.h"
S
Siying Dong 已提交
20
#include "table/block_based_table_builder.h"
S
Siying Dong 已提交
21
#include "table/block_based_table_reader.h"
22
#include "table/format.h"
S
Siying Dong 已提交
23 24 25

namespace rocksdb {

26
BlockBasedTableFactory::BlockBasedTableFactory(
27 28
    const BlockBasedTableOptions& _table_options)
    : table_options_(_table_options) {
29 30 31 32
  if (table_options_.flush_block_policy_factory == nullptr) {
    table_options_.flush_block_policy_factory.reset(
        new FlushBlockBySizePolicyFactory());
  }
33 34 35 36 37 38 39 40 41
  if (table_options_.no_block_cache) {
    table_options_.block_cache.reset();
  } else if (table_options_.block_cache == nullptr) {
    table_options_.block_cache = NewLRUCache(8 << 20);
  }
  if (table_options_.block_size_deviation < 0 ||
      table_options_.block_size_deviation > 100) {
    table_options_.block_size_deviation = 0;
  }
42 43 44
  if (table_options_.block_restart_interval < 1) {
    table_options_.block_restart_interval = 1;
  }
45 46 47
  if (table_options_.index_block_restart_interval < 1) {
    table_options_.index_block_restart_interval = 1;
  }
48 49
}

K
kailiu 已提交
50
Status BlockBasedTableFactory::NewTableReader(
51 52
    const TableReaderOptions& table_reader_options,
    unique_ptr<RandomAccessFileReader>&& file, uint64_t file_size,
53 54
    unique_ptr<TableReader>* table_reader,
    bool prefetch_index_and_filter_in_cache) const {
55 56 57
  return BlockBasedTable::Open(
      table_reader_options.ioptions, table_reader_options.env_options,
      table_options_, table_reader_options.internal_comparator, std::move(file),
58
      file_size, table_reader, prefetch_index_and_filter_in_cache,
59
      table_reader_options.skip_filters, table_reader_options.level);
S
Siying Dong 已提交
60 61
}

K
kailiu 已提交
62
TableBuilder* BlockBasedTableFactory::NewTableBuilder(
63
    const TableBuilderOptions& table_builder_options, uint32_t column_family_id,
64
    WritableFileWriter* file) const {
65
  auto table_builder = new BlockBasedTableBuilder(
66 67
      table_builder_options.ioptions, table_options_,
      table_builder_options.internal_comparator,
68 69
      table_builder_options.int_tbl_prop_collector_factories, column_family_id,
      file, table_builder_options.compression_type,
70
      table_builder_options.compression_opts,
71
      table_builder_options.compression_dict,
72 73
      table_builder_options.skip_filters,
      table_builder_options.column_family_name);
74 75

  return table_builder;
S
Siying Dong 已提交
76
}
77

78 79 80 81 82 83 84 85
Status BlockBasedTableFactory::SanitizeOptions(
    const DBOptions& db_opts,
    const ColumnFamilyOptions& cf_opts) const {
  if (table_options_.index_type == BlockBasedTableOptions::kHashSearch &&
      cf_opts.prefix_extractor == nullptr) {
    return Status::InvalidArgument("Hash index is specified for block-based "
        "table, but prefix_extractor is not given");
  }
86 87 88 89 90
  if (table_options_.cache_index_and_filter_blocks &&
      table_options_.no_block_cache) {
    return Status::InvalidArgument("Enable cache_index_and_filter_blocks, "
        ", but block cache is disabled");
  }
91 92 93 94 95 96
  if (table_options_.pin_l0_filter_and_index_blocks_in_cache &&
      table_options_.no_block_cache) {
    return Status::InvalidArgument(
        "Enable pin_l0_filter_and_index_blocks_in_cache, "
        ", but block cache is disabled");
  }
97
  if (!BlockBasedTableSupportedVersion(table_options_.format_version)) {
98
    return Status::InvalidArgument(
99 100
        "Unsupported BlockBasedTable format_version. Please check "
        "include/rocksdb/table.h for more info");
101
  }
102 103 104
  return Status::OK();
}

L
Lei Jin 已提交
105 106 107 108 109 110 111 112
std::string BlockBasedTableFactory::GetPrintableTableOptions() const {
  std::string ret;
  ret.reserve(20000);
  const int kBufferSize = 200;
  char buffer[kBufferSize];

  snprintf(buffer, kBufferSize, "  flush_block_policy_factory: %s (%p)\n",
           table_options_.flush_block_policy_factory->Name(),
113
           static_cast<void*>(table_options_.flush_block_policy_factory.get()));
L
Lei Jin 已提交
114 115 116 117
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  cache_index_and_filter_blocks: %d\n",
           table_options_.cache_index_and_filter_blocks);
  ret.append(buffer);
118 119 120 121
  snprintf(buffer, kBufferSize,
           "  pin_l0_filter_and_index_blocks_in_cache: %d\n",
           table_options_.pin_l0_filter_and_index_blocks_in_cache);
  ret.append(buffer);
L
Lei Jin 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
  snprintf(buffer, kBufferSize, "  index_type: %d\n",
           table_options_.index_type);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  hash_index_allow_collision: %d\n",
           table_options_.hash_index_allow_collision);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  checksum: %d\n",
           table_options_.checksum);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  no_block_cache: %d\n",
           table_options_.no_block_cache);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  block_cache: %p\n",
135
           static_cast<void*>(table_options_.block_cache.get()));
L
Lei Jin 已提交
136 137
  ret.append(buffer);
  if (table_options_.block_cache) {
D
Dmitri Smirnov 已提交
138
    snprintf(buffer, kBufferSize, "  block_cache_size: %" ROCKSDB_PRIszt "\n",
L
Lei Jin 已提交
139 140 141 142
             table_options_.block_cache->GetCapacity());
    ret.append(buffer);
  }
  snprintf(buffer, kBufferSize, "  block_cache_compressed: %p\n",
143
           static_cast<void*>(table_options_.block_cache_compressed.get()));
L
Lei Jin 已提交
144 145
  ret.append(buffer);
  if (table_options_.block_cache_compressed) {
S
sdong 已提交
146 147
    snprintf(buffer, kBufferSize,
             "  block_cache_compressed_size: %" ROCKSDB_PRIszt "\n",
L
Lei Jin 已提交
148 149 150
             table_options_.block_cache_compressed->GetCapacity());
    ret.append(buffer);
  }
D
Dmitri Smirnov 已提交
151
  snprintf(buffer, kBufferSize, "  block_size: %" ROCKSDB_PRIszt "\n",
L
Lei Jin 已提交
152 153 154 155 156 157 158 159
           table_options_.block_size);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  block_size_deviation: %d\n",
           table_options_.block_size_deviation);
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  block_restart_interval: %d\n",
           table_options_.block_restart_interval);
  ret.append(buffer);
160 161 162
  snprintf(buffer, kBufferSize, "  index_block_restart_interval: %d\n",
           table_options_.index_block_restart_interval);
  ret.append(buffer);
L
Lei Jin 已提交
163 164 165 166 167 168
  snprintf(buffer, kBufferSize, "  filter_policy: %s\n",
           table_options_.filter_policy == nullptr ?
             "nullptr" : table_options_.filter_policy->Name());
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  whole_key_filtering: %d\n",
           table_options_.whole_key_filtering);
169 170 171 172
  ret.append(buffer);
  snprintf(buffer, kBufferSize, "  skip_table_builder_flush: %d\n",
           table_options_.skip_table_builder_flush);
  ret.append(buffer);
173 174
  snprintf(buffer, kBufferSize, "  format_version: %d\n",
           table_options_.format_version);
L
Lei Jin 已提交
175 176 177 178
  ret.append(buffer);
  return ret;
}

S
SherlockNoMad 已提交
179
const BlockBasedTableOptions& BlockBasedTableFactory::table_options() const {
180 181 182
  return table_options_;
}

K
kailiu 已提交
183
TableFactory* NewBlockBasedTableFactory(
184 185
    const BlockBasedTableOptions& _table_options) {
  return new BlockBasedTableFactory(_table_options);
K
kailiu 已提交
186 187
}

188 189
const std::string BlockBasedTablePropertyNames::kIndexType =
    "rocksdb.block.based.table.index.type";
190 191 192 193
const std::string BlockBasedTablePropertyNames::kWholeKeyFiltering =
    "rocksdb.block.based.table.whole.key.filtering";
const std::string BlockBasedTablePropertyNames::kPrefixFiltering =
    "rocksdb.block.based.table.prefix.filtering";
K
Kai Liu 已提交
194 195 196
const std::string kHashIndexPrefixesBlock = "rocksdb.hashindex.prefixes";
const std::string kHashIndexPrefixesMetadataBlock =
    "rocksdb.hashindex.metadata";
197 198
const std::string kPropTrue = "1";
const std::string kPropFalse = "0";
199

S
Siying Dong 已提交
200
}  // namespace rocksdb