table_properties_collector.h 3.6 KB

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