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 // This file defines a collection of statistics collectors. 7 #pragma once 8 9 #include "rocksdb/table_properties.h" 10 11 #include <memory> 12 #include <string> 13 #include <vector> 14 15 namespace ROCKSDB_NAMESPACE { 16 17 // Base class for internal table properties collector. 18 class IntTblPropCollector { 19 public: ~IntTblPropCollector()20 virtual ~IntTblPropCollector() {} 21 virtual Status Finish(UserCollectedProperties* properties) = 0; 22 23 virtual const char* Name() const = 0; 24 25 // @params key the user key that is inserted into the table. 26 // @params value the value that is inserted into the table. 27 virtual Status InternalAdd(const Slice& key, const Slice& value, 28 uint64_t file_size) = 0; 29 30 virtual void BlockAdd(uint64_t blockRawBytes, 31 uint64_t blockCompressedBytesFast, 32 uint64_t blockCompressedBytesSlow) = 0; 33 34 virtual UserCollectedProperties GetReadableProperties() const = 0; 35 NeedCompact()36 virtual bool NeedCompact() const { return false; } 37 }; 38 39 // Factory for internal table properties collector. 40 class IntTblPropCollectorFactory { 41 public: ~IntTblPropCollectorFactory()42 virtual ~IntTblPropCollectorFactory() {} 43 // has to be thread-safe 44 virtual IntTblPropCollector* CreateIntTblPropCollector( 45 uint32_t column_family_id) = 0; 46 47 // The name of the properties collector can be used for debugging purpose. 48 virtual const char* Name() const = 0; 49 }; 50 51 // When rocksdb creates a new table, it will encode all "user keys" into 52 // "internal keys", which contains meta information of a given entry. 53 // 54 // This class extracts user key from the encoded internal key when Add() is 55 // invoked. 56 class UserKeyTablePropertiesCollector : public IntTblPropCollector { 57 public: 58 // transfer of ownership UserKeyTablePropertiesCollector(TablePropertiesCollector * collector)59 explicit UserKeyTablePropertiesCollector(TablePropertiesCollector* collector) 60 : collector_(collector) {} 61 ~UserKeyTablePropertiesCollector()62 virtual ~UserKeyTablePropertiesCollector() {} 63 64 virtual Status InternalAdd(const Slice& key, const Slice& value, 65 uint64_t file_size) override; 66 67 virtual void BlockAdd(uint64_t blockRawBytes, 68 uint64_t blockCompressedBytesFast, 69 uint64_t blockCompressedBytesSlow) override; 70 71 virtual Status Finish(UserCollectedProperties* properties) override; 72 Name()73 virtual const char* Name() const override { return collector_->Name(); } 74 75 UserCollectedProperties GetReadableProperties() const override; 76 NeedCompact()77 virtual bool NeedCompact() const override { 78 return collector_->NeedCompact(); 79 } 80 81 protected: 82 std::unique_ptr<TablePropertiesCollector> collector_; 83 }; 84 85 class UserKeyTablePropertiesCollectorFactory 86 : public IntTblPropCollectorFactory { 87 public: UserKeyTablePropertiesCollectorFactory(std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory)88 explicit UserKeyTablePropertiesCollectorFactory( 89 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory) 90 : user_collector_factory_(user_collector_factory) {} CreateIntTblPropCollector(uint32_t column_family_id)91 virtual IntTblPropCollector* CreateIntTblPropCollector( 92 uint32_t column_family_id) override { 93 TablePropertiesCollectorFactory::Context context; 94 context.column_family_id = column_family_id; 95 return new UserKeyTablePropertiesCollector( 96 user_collector_factory_->CreateTablePropertiesCollector(context)); 97 } 98 Name()99 virtual const char* Name() const override { 100 return user_collector_factory_->Name(); 101 } 102 103 private: 104 std::shared_ptr<TablePropertiesCollectorFactory> user_collector_factory_; 105 }; 106 107 } // namespace ROCKSDB_NAMESPACE 108