diff options
author | Tor Egge <Tor.Egge@online.no> | 2021-12-08 13:07:05 +0100 |
---|---|---|
committer | Tor Egge <Tor.Egge@online.no> | 2021-12-08 13:07:05 +0100 |
commit | 7553a7267623edf2c45dd5a0fd1cea883ab691f7 (patch) | |
tree | a928677c7b0ac2d4b60ed0f5b33c0ba815210ac2 /vespalib | |
parent | 2dd0a7ad258fa182ed3ac8a199751dd60f73b0f7 (diff) |
Move CompactionStrategy from searchcommon to vespalib.
Diffstat (limited to 'vespalib')
3 files changed, 70 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/datastore/CMakeLists.txt b/vespalib/src/vespa/vespalib/datastore/CMakeLists.txt index 9b796c62232..c36077e4dd0 100644 --- a/vespalib/src/vespa/vespalib/datastore/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/datastore/CMakeLists.txt @@ -5,6 +5,7 @@ vespa_add_library(vespalib_vespalib_datastore OBJECT array_store_config.cpp buffer_type.cpp bufferstate.cpp + compaction_strategy.cpp datastore.cpp datastorebase.cpp entryref.cpp diff --git a/vespalib/src/vespa/vespalib/datastore/compaction_strategy.cpp b/vespalib/src/vespa/vespalib/datastore/compaction_strategy.cpp new file mode 100644 index 00000000000..43da1837427 --- /dev/null +++ b/vespalib/src/vespa/vespalib/datastore/compaction_strategy.cpp @@ -0,0 +1,16 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "compaction_strategy.h" +#include <iostream> + +namespace vespalib::datastore { + +std::ostream& operator<<(std::ostream& os, const CompactionStrategy& compaction_strategy) +{ + os << "{maxDeadBytesRatio=" << compaction_strategy.getMaxDeadBytesRatio() << + ", maxDeadAddressSpaceRatio=" << compaction_strategy.getMaxDeadAddressSpaceRatio() << + "}"; + return os; +} + +} diff --git a/vespalib/src/vespa/vespalib/datastore/compaction_strategy.h b/vespalib/src/vespa/vespalib/datastore/compaction_strategy.h new file mode 100644 index 00000000000..06d54786122 --- /dev/null +++ b/vespalib/src/vespa/vespalib/datastore/compaction_strategy.h @@ -0,0 +1,53 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <iosfwd> + +namespace vespalib::datastore { + +/* + * Class describing compaction strategy for a compactable data structure. + */ +class CompactionStrategy +{ +private: + double _maxDeadBytesRatio; // Max ratio of dead bytes before compaction + double _maxDeadAddressSpaceRatio; // Max ratio of dead address space before compaction +public: + CompactionStrategy() noexcept + : _maxDeadBytesRatio(0.05), + _maxDeadAddressSpaceRatio(0.2) + { + } + CompactionStrategy(double maxDeadBytesRatio, double maxDeadAddressSpaceRatio) noexcept + : _maxDeadBytesRatio(maxDeadBytesRatio), + _maxDeadAddressSpaceRatio(maxDeadAddressSpaceRatio) + { + } + double getMaxDeadBytesRatio() const { return _maxDeadBytesRatio; } + double getMaxDeadAddressSpaceRatio() const { return _maxDeadAddressSpaceRatio; } + bool operator==(const CompactionStrategy & rhs) const { + return _maxDeadBytesRatio == rhs._maxDeadBytesRatio && + _maxDeadAddressSpaceRatio == rhs._maxDeadAddressSpaceRatio; + } + bool operator!=(const CompactionStrategy & rhs) const { return !(operator==(rhs)); } + + static constexpr size_t DEAD_BYTES_SLACK = 0x10000u; + + bool should_compact_memory(size_t used_bytes, size_t dead_bytes) const { + return ((dead_bytes >= DEAD_BYTES_SLACK) && + (dead_bytes > used_bytes * getMaxDeadBytesRatio())); + } + + static constexpr size_t DEAD_ADDRESS_SPACE_SLACK = 0x10000u; + + bool should_compact_address_space(size_t used_address_space, size_t dead_address_space) const { + return ((dead_address_space >= DEAD_ADDRESS_SPACE_SLACK) && + (dead_address_space > used_address_space * getMaxDeadAddressSpaceRatio())); + } +}; + +std::ostream& operator<<(std::ostream& os, const CompactionStrategy& compaction_strategy); + +} |