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).
6 // This file defines a collection of statistics collectors.
9 #include "rocksdb/table_properties.h"
15 namespace ROCKSDB_NAMESPACE
{
17 // Base class for internal table properties collector.
18 class IntTblPropCollector
{
20 virtual ~IntTblPropCollector() {}
21 virtual Status
Finish(UserCollectedProperties
* properties
) = 0;
23 virtual const char* Name() const = 0;
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;
30 virtual void BlockAdd(uint64_t blockRawBytes
,
31 uint64_t blockCompressedBytesFast
,
32 uint64_t blockCompressedBytesSlow
) = 0;
34 virtual UserCollectedProperties
GetReadableProperties() const = 0;
36 virtual bool NeedCompact() const { return false; }
39 // Factory for internal table properties collector.
40 class IntTblPropCollectorFactory
{
42 virtual ~IntTblPropCollectorFactory() {}
43 // has to be thread-safe
44 virtual IntTblPropCollector
* CreateIntTblPropCollector(
45 uint32_t column_family_id
) = 0;
47 // The name of the properties collector can be used for debugging purpose.
48 virtual const char* Name() const = 0;
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.
54 // This class extracts user key from the encoded internal key when Add() is
56 class UserKeyTablePropertiesCollector
: public IntTblPropCollector
{
58 // transfer of ownership
59 explicit UserKeyTablePropertiesCollector(TablePropertiesCollector
* collector
)
60 : collector_(collector
) {}
62 virtual ~UserKeyTablePropertiesCollector() {}
64 virtual Status
InternalAdd(const Slice
& key
, const Slice
& value
,
65 uint64_t file_size
) override
;
67 virtual void BlockAdd(uint64_t blockRawBytes
,
68 uint64_t blockCompressedBytesFast
,
69 uint64_t blockCompressedBytesSlow
) override
;
71 virtual Status
Finish(UserCollectedProperties
* properties
) override
;
73 virtual const char* Name() const override
{ return collector_
->Name(); }
75 UserCollectedProperties
GetReadableProperties() const override
;
77 virtual bool NeedCompact() const override
{
78 return collector_
->NeedCompact();
82 std::unique_ptr
<TablePropertiesCollector
> collector_
;
85 class UserKeyTablePropertiesCollectorFactory
86 : public IntTblPropCollectorFactory
{
88 explicit UserKeyTablePropertiesCollectorFactory(
89 std::shared_ptr
<TablePropertiesCollectorFactory
> user_collector_factory
)
90 : user_collector_factory_(user_collector_factory
) {}
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
));
99 virtual const char* Name() const override
{
100 return user_collector_factory_
->Name();
104 std::shared_ptr
<TablePropertiesCollectorFactory
> user_collector_factory_
;
107 } // namespace ROCKSDB_NAMESPACE