1
0
Fork 0
arangodb/arangod/Cache/Metadata.h

148 lines
6.1 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2017 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Daniel H. Larkin
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGODB_CACHE_METADATA_H
#define ARANGODB_CACHE_METADATA_H
#include "Basics/Common.h"
#include "Cache/State.h"
#include <atomic>
#include <cstdint>
namespace arangodb {
namespace cache {
class Cache; // forward declaration
////////////////////////////////////////////////////////////////////////////////
/// @brief Metadata object to facilitate information sharing between individual
/// Cache instances and Manager.
////////////////////////////////////////////////////////////////////////////////
struct Metadata {
// info about allocated memory
uint64_t fixedSize;
uint64_t tableSize;
uint64_t maxSize;
uint64_t allocatedSize;
uint64_t deservedSize;
// vital information about memory usage
uint64_t usage;
uint64_t softUsageLimit;
uint64_t hardUsageLimit;
//////////////////////////////////////////////////////////////////////////////
/// @brief Default constructor for placeholder objects.
//////////////////////////////////////////////////////////////////////////////
Metadata();
//////////////////////////////////////////////////////////////////////////////
/// @brief Initializes record with given information.
//////////////////////////////////////////////////////////////////////////////
Metadata(uint64_t usage, uint64_t fixed, uint64_t table, uint64_t max);
//////////////////////////////////////////////////////////////////////////////
/// @brief Initializes record from an existing record.
//////////////////////////////////////////////////////////////////////////////
Metadata(Metadata const& other);
//////////////////////////////////////////////////////////////////////////////
/// @brief Initializes record from an existing record.
//////////////////////////////////////////////////////////////////////////////
Metadata& operator=(Metadata const& other);
//////////////////////////////////////////////////////////////////////////////
/// @brief Locks the record.
//////////////////////////////////////////////////////////////////////////////
void lock();
//////////////////////////////////////////////////////////////////////////////
/// @brief Unlocks the record. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
void unlock();
//////////////////////////////////////////////////////////////////////////////
/// @brief Returns true if the record is locked, false otherwise.
//////////////////////////////////////////////////////////////////////////////
bool isLocked() const;
//////////////////////////////////////////////////////////////////////////////
/// @brief Adjusts usage by the specified amount if it will not violate
/// limits. Requires record to be locked.
///
/// Returns true if adjusted, false otherwise. Used by caches to check-and-set
/// in a single operation to determine whether they can afford to store a new
/// value.
//////////////////////////////////////////////////////////////////////////////
bool adjustUsageIfAllowed(int64_t usageChange);
//////////////////////////////////////////////////////////////////////////////
/// @brief Sets the soft and hard usage limits. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
bool adjustLimits(uint64_t softLimit, uint64_t hardLimit);
//////////////////////////////////////////////////////////////////////////////
/// @brief Sets the deserved size. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
uint64_t adjustDeserved(uint64_t deserved);
//////////////////////////////////////////////////////////////////////////////
/// @brief Calculates the new usage limit based on deserved size and other
/// values. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
uint64_t newLimit();
//////////////////////////////////////////////////////////////////////////////
/// @brief Checks feasibility of new table size prior to migration. Requires
/// record to be locked.
///
/// If migrating to table of new size would exceed either deserved or maximum
/// size, then returns false.
//////////////////////////////////////////////////////////////////////////////
bool migrationAllowed(uint64_t newTableSize);
//////////////////////////////////////////////////////////////////////////////
/// @brief Sets the table size after migration. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
void changeTable(uint64_t newTableSize);
//////////////////////////////////////////////////////////////////////////////
/// @brief Checks if flag is set in state. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
bool isSet(State::Flag flag) const;
//////////////////////////////////////////////////////////////////////////////
/// @brief Toggles flag in state. Requires record to be locked.
//////////////////////////////////////////////////////////////////////////////
void toggleFlag(State::Flag flag);
private:
State _state;
};
}; // end namespace cache
}; // end namespace arangodb
#endif