2013-12-05 22:09:13 +01:00
|
|
|
// 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 <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2014-02-05 01:21:47 +01:00
|
|
|
#include "db/builder.h"
|
2013-12-05 22:09:13 +01:00
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2014-01-28 19:35:48 +01:00
|
|
|
#include "rocksdb/table_properties.h"
|
2013-12-05 22:09:13 +01:00
|
|
|
#include "table/block_builder.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
class BlockBuilder;
|
2013-12-06 01:51:26 +01:00
|
|
|
class BlockHandle;
|
|
|
|
class Env;
|
2013-12-05 22:09:13 +01:00
|
|
|
class Logger;
|
2013-12-06 01:51:26 +01:00
|
|
|
class RandomAccessFile;
|
2013-12-05 22:09:13 +01:00
|
|
|
struct TableProperties;
|
|
|
|
|
|
|
|
// An STL style comparator that does the bytewise comparator comparasion
|
|
|
|
// internally.
|
|
|
|
struct BytewiseLessThan {
|
|
|
|
bool operator()(const std::string& key1, const std::string& key2) const {
|
|
|
|
// smaller entries will be placed in front.
|
|
|
|
return comparator->Compare(key1, key2) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Comparator* comparator = BytewiseComparator();
|
|
|
|
};
|
|
|
|
|
|
|
|
// When writing to a block that requires entries to be sorted by
|
|
|
|
// `BytewiseComparator`, we can buffer the content to `BytewiseSortedMap`
|
|
|
|
// before writng to store.
|
|
|
|
typedef std::map<std::string, std::string, BytewiseLessThan> BytewiseSortedMap;
|
|
|
|
|
|
|
|
class MetaIndexBuilder {
|
|
|
|
public:
|
|
|
|
MetaIndexBuilder(const MetaIndexBuilder&) = delete;
|
|
|
|
MetaIndexBuilder& operator=(const MetaIndexBuilder&) = delete;
|
|
|
|
|
|
|
|
MetaIndexBuilder();
|
|
|
|
void Add(const std::string& key, const BlockHandle& handle);
|
|
|
|
|
|
|
|
// Write all the added key/value pairs to the block and return the contents
|
|
|
|
// of the block.
|
|
|
|
Slice Finish();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// store the sorted key/handle of the metablocks.
|
|
|
|
BytewiseSortedMap meta_block_handles_;
|
|
|
|
std::unique_ptr<BlockBuilder> meta_index_block_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PropertyBlockBuilder {
|
|
|
|
public:
|
|
|
|
PropertyBlockBuilder(const PropertyBlockBuilder&) = delete;
|
|
|
|
PropertyBlockBuilder& operator=(const PropertyBlockBuilder&) = delete;
|
|
|
|
|
|
|
|
PropertyBlockBuilder();
|
|
|
|
|
|
|
|
void AddTableProperty(const TableProperties& props);
|
|
|
|
void Add(const std::string& key, uint64_t value);
|
|
|
|
void Add(const std::string& key, const std::string& value);
|
|
|
|
void Add(const UserCollectedProperties& user_collected_properties);
|
|
|
|
|
|
|
|
// Write all the added entries to the block and return the block contents
|
|
|
|
Slice Finish();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<BlockBuilder> properties_block_;
|
|
|
|
BytewiseSortedMap props_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Were we encounter any error occurs during user-defined statistics collection,
|
|
|
|
// we'll write the warning message to info log.
|
|
|
|
void LogPropertiesCollectionError(
|
|
|
|
Logger* info_log, const std::string& method, const std::string& name);
|
|
|
|
|
|
|
|
// Utility functions help table builder to trigger batch events for user
|
|
|
|
// defined property collectors.
|
|
|
|
// Return value indicates if there is any error occurred; if error occurred,
|
|
|
|
// the warning message will be logged.
|
|
|
|
// NotifyCollectTableCollectorsOnAdd() triggers the `Add` event for all
|
|
|
|
// property collectors.
|
|
|
|
bool NotifyCollectTableCollectorsOnAdd(
|
|
|
|
const Slice& key,
|
|
|
|
const Slice& value,
|
|
|
|
const Options::TablePropertiesCollectors& collectors,
|
|
|
|
Logger* info_log);
|
|
|
|
|
|
|
|
// NotifyCollectTableCollectorsOnAdd() triggers the `Finish` event for all
|
|
|
|
// property collectors. The collected properties will be added to `builder`.
|
|
|
|
bool NotifyCollectTableCollectorsOnFinish(
|
|
|
|
const Options::TablePropertiesCollectors& collectors,
|
|
|
|
Logger* info_log,
|
|
|
|
PropertyBlockBuilder* builder);
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
// Read the properties from the table.
|
2014-02-08 04:26:49 +01:00
|
|
|
// @returns a status to indicate if the operation succeeded. On success,
|
|
|
|
// *table_properties will point to a heap-allocated TableProperties
|
|
|
|
// object, otherwise value of `table_properties` will not be modified.
|
|
|
|
Status ReadProperties(const Slice& handle_value, RandomAccessFile* file,
|
|
|
|
Env* env, Logger* logger,
|
|
|
|
TableProperties** table_properties);
|
2013-12-06 01:51:26 +01:00
|
|
|
|
|
|
|
// Directly read the properties from the properties block of a plain table.
|
2014-02-08 04:26:49 +01:00
|
|
|
// @returns a status to indicate if the operation succeeded. On success,
|
|
|
|
// *table_properties will point to a heap-allocated TableProperties
|
|
|
|
// object, otherwise value of `table_properties` will not be modified.
|
|
|
|
Status ReadTableProperties(RandomAccessFile* file, uint64_t file_size,
|
|
|
|
uint64_t table_magic_number, Env* env,
|
|
|
|
Logger* info_log, TableProperties** properties);
|
2013-12-06 01:51:26 +01:00
|
|
|
|
2014-02-05 01:21:47 +01:00
|
|
|
// Read the magic number of the specified file directly. The magic number
|
|
|
|
// of a valid sst table the last 8-byte of the file.
|
|
|
|
Status ReadTableMagicNumber(const std::string& file_path,
|
|
|
|
const Options& options,
|
|
|
|
const EnvOptions& env_options,
|
|
|
|
uint64_t* table_magic_number);
|
2014-02-14 01:28:21 +01:00
|
|
|
|
|
|
|
Status ReadTableMagicNumber(RandomAccessFile* file, uint64_t file_size,
|
|
|
|
const Options& options,
|
|
|
|
const EnvOptions& env_options,
|
|
|
|
uint64_t* table_magic_number);
|
2014-04-22 02:49:47 +02:00
|
|
|
|
|
|
|
// Seek to the properties block.
|
|
|
|
// If it successfully seeks to the properties block, "is_found" will be
|
|
|
|
// set to true.
|
|
|
|
extern Status SeekToPropertiesBlock(Iterator* meta_iter, bool* is_found);
|
|
|
|
|
2013-12-05 22:09:13 +01:00
|
|
|
} // namespace rocksdb
|