YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/table/mock_table.h
Line
Count
Source (jump to first uncovered line)
1
// Use of this source code is governed by a BSD-style license that can be
2
// found in the LICENSE file. See the AUTHORS file for names of contributors.
3
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
4
//  This source code is licensed under the BSD-style license found in the
5
//  LICENSE file in the root directory of this source tree. An additional grant
6
//  of patent rights can be found in the PATENTS file in the same directory.
7
//
8
// The following only applies to changes made to this file as part of YugaByte development.
9
//
10
// Portions Copyright (c) YugaByte, Inc.
11
//
12
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
13
// in compliance with the License.  You may obtain a copy of the License at
14
//
15
// http://www.apache.org/licenses/LICENSE-2.0
16
//
17
// Unless required by applicable law or agreed to in writing, software distributed under the License
18
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
19
// or implied.  See the License for the specific language governing permissions and limitations
20
// under the License.
21
//
22
#ifndef YB_ROCKSDB_TABLE_MOCK_TABLE_H
23
#define YB_ROCKSDB_TABLE_MOCK_TABLE_H
24
25
#include <algorithm>
26
#include <atomic>
27
#include <map>
28
#include <memory>
29
#include <string>
30
#include <utility>
31
32
#include "yb/rocksdb/comparator.h"
33
#include "yb/rocksdb/port/port.h"
34
#include "yb/rocksdb/table.h"
35
#include "yb/rocksdb/table/internal_iterator.h"
36
#include "yb/rocksdb/table/table_builder.h"
37
#include "yb/rocksdb/table/table_reader.h"
38
#include "yb/rocksdb/util/kv_map.h"
39
#include "yb/rocksdb/util/mutexlock.h"
40
41
namespace rocksdb {
42
namespace mock {
43
44
stl_wrappers::KVMap MakeMockFile(
45
    std::initializer_list<std::pair<const std::string, std::string>> l = {});
46
47
struct MockTableFileSystem {
48
  port::Mutex mutex;
49
  std::map<uint32_t, stl_wrappers::KVMap> files;
50
};
51
52
class MockTableReader : public TableReader {
53
 public:
54
124
  explicit MockTableReader(const stl_wrappers::KVMap& table) : table_(table) {}
55
56
124
  bool IsSplitSst() const override { return false; }
57
58
0
  void SetDataFileReader(unique_ptr<RandomAccessFileReader>&& data_file) override { assert(false); }
59
60
  InternalIterator* NewIterator(const ReadOptions&, Arena* arena,
61
                                bool skip_filters = false) override;
62
63
  Status Get(const ReadOptions&, const Slice& key, GetContext* get_context,
64
             bool skip_filters = false) override;
65
66
0
  uint64_t ApproximateOffsetOf(const Slice& key) override { return 0; }
67
68
0
  virtual size_t ApproximateMemoryUsage() const override { return 0; }
69
70
91
  void SetupForCompaction() override {}
71
72
  std::shared_ptr<const TableProperties> GetTableProperties() const override;
73
74
124
  ~MockTableReader() {}
75
76
 private:
77
  const stl_wrappers::KVMap& table_;
78
};
79
80
class MockTableIterator : public InternalIterator {
81
 public:
82
189
  explicit MockTableIterator(const stl_wrappers::KVMap& table) : table_(table) {
83
189
    itr_ = table_.end();
84
189
  }
85
86
42.4k
  bool Valid() const override { return itr_ != table_.end(); }
87
88
91
  void SeekToFirst() override { itr_ = table_.begin(); }
89
90
0
  void SeekToLast() override {
91
0
    itr_ = table_.end();
92
0
    --itr_;
93
0
  }
94
95
10
  void Seek(const Slice& target) override {
96
10
    std::string str_target(target.cdata(), target.size());
97
10
    itr_ = table_.lower_bound(str_target);
98
10
  }
99
100
42.2k
  void Next() override { ++itr_; }
101
102
0
  void Prev() override {
103
0
    if (itr_ == table_.begin()) {
104
0
      itr_ = table_.end();
105
0
    } else {
106
0
      --itr_;
107
0
    }
108
0
  }
109
110
42.2k
  Slice key() const override { return Slice(itr_->first); }
111
112
42.2k
  Slice value() const override { return Slice(itr_->second); }
113
114
350
  Status status() const override { return Status::OK(); }
115
116
 private:
117
  const stl_wrappers::KVMap& table_;
118
  stl_wrappers::KVMap::const_iterator itr_;
119
};
120
121
class MockTableBuilder : public TableBuilder {
122
 public:
123
  MockTableBuilder(uint32_t id, MockTableFileSystem* file_system)
124
88
      : id_(id), file_system_(file_system) {
125
88
    table_ = MakeMockFile({});
126
88
  }
127
128
  // REQUIRES: Either Finish() or Abandon() has been called.
129
88
  ~MockTableBuilder() {}
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
55.7k
  void Add(const Slice& key, const Slice& value) override {
135
55.7k
    table_.insert({key.ToString(), value.ToString()});
136
55.7k
  }
137
138
  // Return non-ok iff some error has been detected.
139
0
  Status status() const override { return Status::OK(); }
140
141
88
  Status Finish() override {
142
88
    MutexLock lock_guard(&file_system_->mutex);
143
88
    file_system_->files.insert({id_, table_});
144
88
    return Status::OK();
145
88
  }
146
147
0
  void Abandon() override {}
148
149
88
  uint64_t NumEntries() const override { return table_.size(); }
150
151
32.4k
  uint64_t TotalFileSize() const override { return table_.size(); }
152
153
88
  uint64_t BaseFileSize() const override { return table_.size(); }
154
155
88
  TableProperties GetTableProperties() const override {
156
88
    return TableProperties();
157
88
  }
158
159
 private:
160
  uint32_t id_;
161
  MockTableFileSystem* file_system_;
162
  stl_wrappers::KVMap table_;
163
};
164
165
class MockTableFactory : public TableFactory {
166
 public:
167
  MockTableFactory();
168
11
  const char* Name() const override { return "MockTable"; }
169
  Status NewTableReader(const TableReaderOptions& table_reader_options,
170
                        unique_ptr<RandomAccessFileReader>&& file,
171
                        uint64_t file_size,
172
                        unique_ptr<TableReader>* table_reader) const override;
173
174
149
  bool IsSplitSstForWriteSupported() const override { return false; }
175
176
  TableBuilder* NewTableBuilder(
177
      const TableBuilderOptions& table_builder_options, uint32_t column_familly_id,
178
      WritableFileWriter* base_file, WritableFileWriter* data_file = nullptr) const override;
179
180
  // This function will directly create mock table instead of going through
181
  // MockTableBuilder. file_contents has to have a format of <internal_key,
182
  // value>. Those key-value pairs will then be inserted into the mock table.
183
  Status CreateMockTable(Env* env, const std::string& fname,
184
                         stl_wrappers::KVMap file_contents);
185
186
  virtual Status SanitizeOptions(
187
      const DBOptions& db_opts,
188
2
      const ColumnFamilyOptions& cf_opts) const override {
189
2
    return Status::OK();
190
2
  }
191
192
2
  virtual std::string GetPrintableTableOptions() const override {
193
2
    return std::string();
194
2
  }
195
196
  // This function will assert that only a single file exists and that the
197
  // contents are equal to file_contents
198
  void AssertSingleFile(const stl_wrappers::KVMap& file_contents);
199
  void AssertLatestFile(const stl_wrappers::KVMap& file_contents);
200
201
 private:
202
  uint32_t GetAndWriteNextID(WritableFileWriter* file) const;
203
  uint32_t GetIDFromFile(RandomAccessFileReader* file) const;
204
205
  mutable MockTableFileSystem file_system_;
206
  mutable std::atomic<uint32_t> next_id_;
207
};
208
209
}  // namespace mock
210
}  // namespace rocksdb
211
212
#endif  // YB_ROCKSDB_TABLE_MOCK_TABLE_H