Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
EvictionConfig.h
1 /*
2  * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_CLIENT_CONFIG_EVICTIONCONFIG_H_
17 #define HAZELCAST_CLIENT_CONFIG_EVICTIONCONFIG_H_
18 
19 #include <string>
20 #include <stdint.h>
21 #include <boost/shared_ptr.hpp>
22 
23 #include "hazelcast/client/internal/eviction/EvictionPolicyComparator.h"
24 #include "hazelcast/util/Preconditions.h"
25 #include "hazelcast/util/HazelcastDll.h"
26 #include "hazelcast/client/internal/eviction/EvictionConfiguration.h"
27 #include "hazelcast/client/config/EvictionPolicy.h"
28 
29 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
30 #pragma warning(push)
31 #pragma warning(disable: 4251) //for dll export
32 #endif
33 
34 namespace hazelcast {
35  namespace client {
36  namespace config {
41  template<typename K, typename V>
43  public:
44  virtual ~EvictionConfig() {
45  }
46 
55  /* TODO,
56  *
57  * Policy based on maximum used native memory in megabytes per data structure (map, cache etc)
58  * on each Hazelcast instance
59 
60  USED_NATIVE_MEMORY_SIZE,
61  *
62  * Policy based on maximum used native memory percentage per data structure (map, cache etc)
63  * on each Hazelcast instance
64 
65  USED_NATIVE_MEMORY_PERCENTAGE,
66  *
67  * Policy based on minimum free native memory in megabytes per Hazelcast instance
68 
69  FREE_NATIVE_MEMORY_SIZE,
70  *
71  * Policy based on minimum free native memory percentage per Hazelcast instance
72 
73  FREE_NATIVE_MEMORY_PERCENTAGE*/
74  };
75 
79  static const int32_t DEFAULT_MAX_ENTRY_COUNT;
80 
85 
89  static const EvictionPolicy DEFAULT_EVICTION_POLICY;
90 
92  evictionPolicy(DEFAULT_EVICTION_POLICY) {
93  }
94 
95 
96  EvictionConfig(int size, MaxSizePolicy maxSizePolicy,
97  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > &comparator) {
98  this->size = util::Preconditions::checkPositive(size, "Size must be positive number!");
99  this->maxSizePolicy = maxSizePolicy;
100  this->comparator = util::Preconditions::checkNotNull<internal::eviction::EvictionPolicyComparator>(
101  comparator, "Comparator cannot be null!");
102  }
103 
104  int32_t getSize() const {
105  return size;
106  }
107 
108  EvictionConfig &setSize(int32_t size) {
109  this->size = util::Preconditions::checkPositive(size, "Size must be positive number!");
110  return *this;
111  }
112 
113  MaxSizePolicy getMaximumSizePolicy() const {
114  return maxSizePolicy;
115  }
116 
117  EvictionConfig &setMaximumSizePolicy(const MaxSizePolicy &maxSizePolicy) {
118  this->maxSizePolicy = maxSizePolicy;
119  return *this;
120  }
121 
122  EvictionPolicy getEvictionPolicy() const {
123  return evictionPolicy;
124  }
125 
126  EvictionConfig<K, V> &setEvictionPolicy(EvictionPolicy policy) {
127  this->evictionPolicy = policy;
128  return *this;
129  }
130 
131  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > getComparator() const {
132  return comparator;
133  }
134 
135  EvictionConfig &setComparator(
136  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > &comparator) {
137  this->comparator = comparator;
138  return *this;
139  }
140 
142  // TODO: add support for other/custom eviction strategies
144  }
145 
146  internal::eviction::EvictionPolicyType getEvictionPolicyType() const {
147  if (evictionPolicy == LFU) {
148  return internal::eviction::LFU;
149  } else if (evictionPolicy == LRU) {
150  return internal::eviction::LRU;
151  } else if (evictionPolicy == RANDOM) {
152  return internal::eviction::RANDOM;
153  } else if (evictionPolicy == NONE) {
154  return internal::eviction::NONE;
155  } else {
156  assert(0);
157  }
158  return internal::eviction::NONE;
159  }
160 
161  std::ostream &operator<<(std::ostream &out) {
162  out << "EvictionConfig{"
163  << "size=" << getSize()
164  << ", maxSizePolicy=" << getMaximumSizePolicy()
165  << ", evictionPolicy=" << getEvictionPolicy()
166  << ", comparator=" << getComparator()
167  << '}';
168 
169  return out;
170  }
171  protected:
172  int32_t size;
173  MaxSizePolicy maxSizePolicy;
174  EvictionPolicy evictionPolicy;
175  boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > comparator;
176  };
177 
181  template <typename K, typename V>
182  const int EvictionConfig<K, V>::DEFAULT_MAX_ENTRY_COUNT = INT32_MAX;
183 
187  template <typename K, typename V>
188  const typename EvictionConfig<K, V>::MaxSizePolicy EvictionConfig<K, V>::DEFAULT_MAX_SIZE_POLICY = ENTRY_COUNT;
189 
193  template <typename K, typename V>
194  const EvictionPolicy EvictionConfig<K, V>::DEFAULT_EVICTION_POLICY = LRU;
195  }
196  }
197 }
198 
199 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
200 #pragma warning(pop)
201 #endif
202 
203 #endif /* HAZELCAST_CLIENT_CONFIG_EVICTIONCONFIG_H_ */
Configuration for eviction.
Definition: EvictionConfig.h:42
internal::eviction::EvictionPolicyType getEvictionPolicyType() const
Gets the type of eviction policy.
Definition: EvictionConfig.h:146
static const int32_t DEFAULT_MAX_ENTRY_COUNT
Default maximum entry count.
Definition: EvictionConfig.h:79
internal::eviction::EvictionStrategyType::Type getEvictionStrategyType() const
Gets the type of eviction strategy.
Definition: EvictionConfig.h:141
A kind of java.util.Comparator to be used while comparing entries to be evicted.
Definition: EvictionPolicyComparator.h:38
const boost::shared_ptr< internal::eviction::EvictionPolicyComparator< K, V > > getComparator() const
Gets the class name of the configured EvictionPolicyComparator implementation.
Definition: EvictionConfig.h:131
static const EvictionPolicy DEFAULT_EVICTION_POLICY
Default Eviction Policy.
Definition: EvictionConfig.h:89
static const MaxSizePolicy DEFAULT_MAX_SIZE_POLICY
Default Max-Size Policy.
Definition: EvictionConfig.h:84
MaxSizePolicy
Maximum Size Policy.
Definition: EvictionConfig.h:50
static const Type DEFAULT_EVICTION_STRATEGY
Default value of com.hazelcast.internal.eviction.EvictionStrategyType.
Definition: EvictionStrategyType.h:44
Policy based on maximum number of entries stored per data structure (map, cache etc) ...
Definition: EvictionConfig.h:54
Interface for configuration information about eviction.
Definition: EvictionConfiguration.h:38