2014-10-29 00:52:32 +00:00
|
|
|
// 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.
|
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// 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.
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <memory>
|
2014-10-29 01:10:55 +00:00
|
|
|
#include <atomic>
|
2014-10-29 00:52:32 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "rocksdb/table.h"
|
|
|
|
#include "table/table_reader.h"
|
|
|
|
#include "table/table_builder.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
2014-11-14 19:35:48 +00:00
|
|
|
namespace mock {
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2014-11-14 19:35:48 +00:00
|
|
|
typedef std::map<std::string, std::string> MockFileContents;
|
2014-10-29 00:52:32 +00:00
|
|
|
// NOTE this currently only supports bitwise comparator
|
|
|
|
|
|
|
|
struct MockTableFileSystem {
|
|
|
|
port::Mutex mutex;
|
2014-11-14 19:35:48 +00:00
|
|
|
std::map<uint32_t, MockFileContents> files;
|
2014-10-29 00:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockTableReader : public TableReader {
|
|
|
|
public:
|
2014-11-14 19:35:48 +00:00
|
|
|
explicit MockTableReader(const MockFileContents& table) : table_(table) {}
|
2014-10-29 00:52:32 +00:00
|
|
|
|
|
|
|
Iterator* NewIterator(const ReadOptions&, Arena* arena) override;
|
|
|
|
|
|
|
|
Status Get(const ReadOptions&, const Slice& key,
|
|
|
|
GetContext* get_context) override;
|
|
|
|
|
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key) override { return 0; }
|
|
|
|
|
|
|
|
virtual size_t ApproximateMemoryUsage() const override { return 0; }
|
|
|
|
|
|
|
|
void SetupForCompaction() override {}
|
|
|
|
|
|
|
|
std::shared_ptr<const TableProperties> GetTableProperties() const override;
|
|
|
|
|
|
|
|
~MockTableReader() {}
|
|
|
|
|
|
|
|
private:
|
2014-11-14 19:35:48 +00:00
|
|
|
const MockFileContents& table_;
|
2014-10-29 00:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockTableIterator : public Iterator {
|
|
|
|
public:
|
2014-11-14 19:35:48 +00:00
|
|
|
explicit MockTableIterator(const MockFileContents& table) : table_(table) {
|
2014-10-29 00:52:32 +00:00
|
|
|
itr_ = table_.end();
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
bool Valid() const override { return itr_ != table_.end(); }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void SeekToFirst() override { itr_ = table_.begin(); }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void SeekToLast() override {
|
2014-10-29 00:52:32 +00:00
|
|
|
itr_ = table_.end();
|
|
|
|
--itr_;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Seek(const Slice& target) override {
|
2014-10-29 00:52:32 +00:00
|
|
|
std::string str_target(target.data(), target.size());
|
|
|
|
itr_ = table_.lower_bound(str_target);
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Next() override { ++itr_; }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
void Prev() override {
|
2014-10-29 00:52:32 +00:00
|
|
|
if (itr_ == table_.begin()) {
|
|
|
|
itr_ = table_.end();
|
|
|
|
} else {
|
|
|
|
--itr_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Slice key() const override { return Slice(itr_->first); }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Slice value() const override { return Slice(itr_->second); }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
Status status() const override { return Status::OK(); }
|
2014-10-29 00:52:32 +00:00
|
|
|
|
|
|
|
private:
|
2014-11-14 19:35:48 +00:00
|
|
|
const MockFileContents& table_;
|
|
|
|
MockFileContents::const_iterator itr_;
|
2014-10-29 00:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockTableBuilder : public TableBuilder {
|
|
|
|
public:
|
2015-03-10 19:35:15 +00:00
|
|
|
MockTableBuilder(uint32_t id, MockTableFileSystem* file_system,
|
|
|
|
CompressionType compression_type)
|
|
|
|
: id_(id),
|
|
|
|
file_system_(file_system),
|
|
|
|
compression_type_(compression_type) {}
|
2014-10-29 00:52:32 +00:00
|
|
|
|
|
|
|
// REQUIRES: Either Finish() or Abandon() has been called.
|
|
|
|
~MockTableBuilder() {}
|
|
|
|
|
|
|
|
// Add key,value to the table being constructed.
|
|
|
|
// REQUIRES: key is after any previously added key according to comparator.
|
|
|
|
// REQUIRES: Finish(), Abandon() have not been called
|
|
|
|
void Add(const Slice& key, const Slice& value) override {
|
|
|
|
table_.insert({key.ToString(), value.ToString()});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return non-ok iff some error has been detected.
|
|
|
|
Status status() const override { return Status::OK(); }
|
|
|
|
|
|
|
|
Status Finish() override {
|
2015-03-10 19:35:15 +00:00
|
|
|
if (finish_cb_ != nullptr) {
|
|
|
|
(*finish_cb_)(compression_type_, FileSize());
|
|
|
|
}
|
2014-10-29 00:52:32 +00:00
|
|
|
MutexLock lock_guard(&file_system_->mutex);
|
|
|
|
file_system_->files.insert({id_, table_});
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Abandon() override {}
|
|
|
|
|
|
|
|
uint64_t NumEntries() const override { return table_.size(); }
|
|
|
|
|
|
|
|
uint64_t FileSize() const override { return table_.size(); }
|
|
|
|
|
2015-03-10 19:35:15 +00:00
|
|
|
static std::function<void(const CompressionType&, uint64_t)>* finish_cb_;
|
|
|
|
|
2014-10-29 00:52:32 +00:00
|
|
|
private:
|
|
|
|
uint32_t id_;
|
|
|
|
MockTableFileSystem* file_system_;
|
2014-11-14 19:35:48 +00:00
|
|
|
MockFileContents table_;
|
2015-03-10 19:35:15 +00:00
|
|
|
CompressionType compression_type_;
|
2014-10-29 00:52:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockTableFactory : public TableFactory {
|
|
|
|
public:
|
|
|
|
MockTableFactory();
|
|
|
|
const char* Name() const override { return "MockTable"; }
|
|
|
|
Status NewTableReader(const ImmutableCFOptions& ioptions,
|
2015-02-17 16:03:45 +00:00
|
|
|
const EnvOptions& env_options,
|
|
|
|
const InternalKeyComparator& internal_key,
|
|
|
|
unique_ptr<RandomAccessFile>&& file, uint64_t file_size,
|
|
|
|
unique_ptr<TableReader>* table_reader) const override;
|
A new call back to TablePropertiesCollector to allow users know the entry is add, delete or merge
Summary:
Currently users have no idea a key is add, delete or merge from TablePropertiesCollector call back. Add a new function to add it.
Also refactor the codes so that
(1) make table property collector and internal table property collector two separate data structures with the later one now exposed
(2) table builders only receive internal table properties
Test Plan: Add cases in table_properties_collector_test to cover both of old and new ways of using TablePropertiesCollector.
Reviewers: yhchiang, igor.sugak, rven, igor
Reviewed By: rven, igor
Subscribers: meyering, yoshinorim, maykov, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D35373
2015-04-06 17:04:30 +00:00
|
|
|
TableBuilder* NewTableBuilder(
|
|
|
|
const TableBuilderOptions& table_builder_options,
|
|
|
|
WritableFile* file) const override;
|
2014-10-29 00:52:32 +00:00
|
|
|
|
2014-11-14 19:35:48 +00:00
|
|
|
// This function will directly create mock table instead of going through
|
|
|
|
// MockTableBuilder. MockFileContents has to have a format of <internal_key,
|
|
|
|
// value>. Those key-value pairs will then be inserted into the mock table
|
|
|
|
Status CreateMockTable(Env* env, const std::string& fname,
|
|
|
|
MockFileContents file_contents);
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
virtual Status SanitizeOptions(
|
|
|
|
const DBOptions& db_opts,
|
|
|
|
const ColumnFamilyOptions& cf_opts) const override {
|
2014-10-29 00:52:32 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string GetPrintableTableOptions() const override {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function will assert that only a single file exists and that the
|
|
|
|
// contents are equal to file_contents
|
2014-11-14 19:35:48 +00:00
|
|
|
void AssertSingleFile(const MockFileContents& file_contents);
|
|
|
|
void AssertLatestFile(const MockFileContents& file_contents);
|
2014-10-29 00:52:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t GetAndWriteNextID(WritableFile* file) const;
|
|
|
|
uint32_t GetIDFromFile(RandomAccessFile* file) const;
|
|
|
|
|
|
|
|
mutable MockTableFileSystem file_system_;
|
|
|
|
mutable std::atomic<uint32_t> next_id_;
|
|
|
|
};
|
|
|
|
|
2014-11-14 19:35:48 +00:00
|
|
|
} // namespace mock
|
2014-10-29 00:52:32 +00:00
|
|
|
} // namespace rocksdb
|