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   // Finish building the table.
140   // REQUIRES: Finish(), Abandon() have not been called
141   virtual Status Finish() = 0;
142 
143   // Indicate that the contents of this builder should be abandoned.
144   // If the caller is not going to call Finish(), it must call Abandon()
145   // before destroying this builder.
146   // REQUIRES: Finish(), Abandon() have not been called
147   virtual void Abandon() = 0;
148 
149   // Number of calls to Add() so far.
150   virtual uint64_t NumEntries() const = 0;
151 
152   // Size of the file generated so far.  If invoked after a successful
153   // Finish() call, returns the size of the final generated file.
154   virtual uint64_t FileSize() const = 0;
155 
156   // If the user defined table properties collector suggest the file to
157   // be further compacted.
NeedCompact()158   virtual bool NeedCompact() const { return false; }
159 
160   // Returns table properties
161   virtual TableProperties GetTableProperties() const = 0;
162 
163   // Return file checksum
164   virtual const std::string& GetFileChecksum() const = 0;
165 
166   // Return file checksum function name
167   virtual const char* GetFileChecksumFuncName() const = 0;
168 };
169 
170 }  // namespace ROCKSDB_NAMESPACE
171