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 #pragma once 11 12 #include <stdint.h> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include "db/dbformat.h" 17 #include "db/table_properties_collector.h" 18 #include "file/writable_file_writer.h" 19 #include "options/cf_options.h" 20 #include "rocksdb/options.h" 21 #include "rocksdb/table_properties.h" 22 #include "trace_replay/block_cache_tracer.h" 23 24 namespace ROCKSDB_NAMESPACE { 25 26 class Slice; 27 class Status; 28 29 struct TableReaderOptions { 30 // @param skip_filters Disables loading/accessing the filter block 31 TableReaderOptions(const ImmutableCFOptions& _ioptions, 32 const SliceTransform* _prefix_extractor, 33 const EnvOptions& _env_options, 34 const InternalKeyComparator& _internal_comparator, 35 bool _skip_filters = false, bool _immortal = false, 36 int _level = -1, 37 BlockCacheTracer* const _block_cache_tracer = nullptr) 38 : TableReaderOptions(_ioptions, _prefix_extractor, _env_options, 39 _internal_comparator, _skip_filters, _immortal, 40 _level, 0 /* _largest_seqno */, 41 _block_cache_tracer) {} 42 43 // @param skip_filters Disables loading/accessing the filter block TableReaderOptionsTableReaderOptions44 TableReaderOptions(const ImmutableCFOptions& _ioptions, 45 const SliceTransform* _prefix_extractor, 46 const EnvOptions& _env_options, 47 const InternalKeyComparator& _internal_comparator, 48 bool _skip_filters, bool _immortal, int _level, 49 SequenceNumber _largest_seqno, 50 BlockCacheTracer* const _block_cache_tracer) 51 : ioptions(_ioptions), 52 prefix_extractor(_prefix_extractor), 53 env_options(_env_options), 54 internal_comparator(_internal_comparator), 55 skip_filters(_skip_filters), 56 immortal(_immortal), 57 level(_level), 58 largest_seqno(_largest_seqno), 59 block_cache_tracer(_block_cache_tracer) {} 60 61 const ImmutableCFOptions& ioptions; 62 const SliceTransform* prefix_extractor; 63 const EnvOptions& env_options; 64 const InternalKeyComparator& internal_comparator; 65 // This is only used for BlockBasedTable (reader) 66 bool skip_filters; 67 // Whether the table will be valid as long as the DB is open 68 bool immortal; 69 // what level this table/file is on, -1 for "not set, don't know" 70 int level; 71 // largest seqno in the table 72 SequenceNumber largest_seqno; 73 BlockCacheTracer* const block_cache_tracer; 74 }; 75 76 struct TableBuilderOptions { 77 TableBuilderOptions( 78 const ImmutableCFOptions& _ioptions, const MutableCFOptions& _moptions, 79 const InternalKeyComparator& _internal_comparator, 80 const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>* 81 _int_tbl_prop_collector_factories, 82 CompressionType _compression_type, uint64_t _sample_for_compression, 83 const CompressionOptions& _compression_opts, bool _skip_filters, 84 const std::string& _column_family_name, int _level, 85 const uint64_t _creation_time = 0, const int64_t _oldest_key_time = 0, 86 const uint64_t _target_file_size = 0, 87 const uint64_t _file_creation_time = 0) ioptionsTableBuilderOptions88 : ioptions(_ioptions), 89 moptions(_moptions), 90 internal_comparator(_internal_comparator), 91 int_tbl_prop_collector_factories(_int_tbl_prop_collector_factories), 92 compression_type(_compression_type), 93 sample_for_compression(_sample_for_compression), 94 compression_opts(_compression_opts), 95 skip_filters(_skip_filters), 96 column_family_name(_column_family_name), 97 level(_level), 98 creation_time(_creation_time), 99 oldest_key_time(_oldest_key_time), 100 target_file_size(_target_file_size), 101 file_creation_time(_file_creation_time) {} 102 const ImmutableCFOptions& ioptions; 103 const MutableCFOptions& moptions; 104 const InternalKeyComparator& internal_comparator; 105 const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>* 106 int_tbl_prop_collector_factories; 107 CompressionType compression_type; 108 uint64_t sample_for_compression; 109 const CompressionOptions& compression_opts; 110 bool skip_filters; // only used by BlockBasedTableBuilder 111 const std::string& column_family_name; 112 int level; // what level this table/file is on, -1 for "not set, don't know" 113 const uint64_t creation_time; 114 const int64_t oldest_key_time; 115 const uint64_t target_file_size; 116 const uint64_t file_creation_time; 117 }; 118 119 // TableBuilder provides the interface used to build a Table 120 // (an immutable and sorted map from keys to values). 121 // 122 // Multiple threads can invoke const methods on a TableBuilder without 123 // external synchronization, but if any of the threads may call a 124 // non-const method, all threads accessing the same TableBuilder must use 125 // external synchronization. 126 class TableBuilder { 127 public: 128 // REQUIRES: Either Finish() or Abandon() has been called. ~TableBuilder()129 virtual ~TableBuilder() {} 130 131 // Add key,value to the table being constructed. 132 // REQUIRES: key is after any previously added key according to comparator. 133 // REQUIRES: Finish(), Abandon() have not been called 134 virtual void Add(const Slice& key, const Slice& value) = 0; 135 136 // Return non-ok iff some error has been detected. 137 virtual Status status() const = 0; 138 139 // Return non-ok iff some error happens during IO. 140 virtual IOStatus io_status() const = 0; 141 142 // Finish building the table. 143 // REQUIRES: Finish(), Abandon() have not been called 144 virtual Status Finish() = 0; 145 146 // Indicate that the contents of this builder should be abandoned. 147 // If the caller is not going to call Finish(), it must call Abandon() 148 // before destroying this builder. 149 // REQUIRES: Finish(), Abandon() have not been called 150 virtual void Abandon() = 0; 151 152 // Number of calls to Add() so far. 153 virtual uint64_t NumEntries() const = 0; 154 155 // Size of the file generated so far. If invoked after a successful 156 // Finish() call, returns the size of the final generated file. 157 virtual uint64_t FileSize() const = 0; 158 159 // If the user defined table properties collector suggest the file to 160 // be further compacted. NeedCompact()161 virtual bool NeedCompact() const { return false; } 162 163 // Returns table properties 164 virtual TableProperties GetTableProperties() const = 0; 165 166 // Return file checksum 167 virtual std::string GetFileChecksum() const = 0; 168 169 // Return file checksum function name 170 virtual const char* GetFileChecksumFuncName() const = 0; 171 }; 172 173 } // namespace ROCKSDB_NAMESPACE 174