Hazelcast C++ Client
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 #include <ostream>
23 
24 #include "hazelcast/client/internal/eviction/EvictionPolicyComparator.h"
25 #include "hazelcast/util/Preconditions.h"
26 #include "hazelcast/util/HazelcastDll.h"
27 #include "hazelcast/client/internal/eviction/EvictionConfiguration.h"
28 #include "hazelcast/client/config/EvictionPolicy.h"
29 
30 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma warning(push)
32 #pragma warning(disable: 4251) //for dll export
33 #endif
34 
35 namespace hazelcast {
36  namespace client {
37  namespace config {
42  template<typename K, typename V>
44  public:
45  virtual ~EvictionConfig() {
46  }
47 
56  /* TODO,
57  *
58  * Policy based on maximum used native memory in megabytes per data structure (map, cache etc)
59  * on each Hazelcast instance
60 
61  USED_NATIVE_MEMORY_SIZE,
62  *
63  * Policy based on maximum used native memory percentage per data structure (map, cache etc)
64  * on each Hazelcast instance
65 
66  USED_NATIVE_MEMORY_PERCENTAGE,
67  *
68  * Policy based on minimum free native memory in megabytes per Hazelcast instance
69 
70  FREE_NATIVE_MEMORY_SIZE,
71  *
72  * Policy based on minimum free native memory percentage per Hazelcast instance
73 
74  FREE_NATIVE_MEMORY_PERCENTAGE*/
75  };
76 
80  static const int32_t DEFAULT_MAX_ENTRY_COUNT;
81 
86 
90  static const EvictionPolicy DEFAULT_EVICTION_POLICY;
91 
92  EvictionConfig() : size(DEFAULT_MAX_ENTRY_COUNT), maxSizePolicy(DEFAULT_MAX_SIZE_POLICY),
93  evictionPolicy(DEFAULT_EVICTION_POLICY) {
94  }
95 
96 
97  EvictionConfig(int size, MaxSizePolicy maxSizePolicy,
98  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > &comparator) {
99  this->size = util::Preconditions::checkPositive(size, "Size must be positive number!");
100  this->maxSizePolicy = maxSizePolicy;
101  this->comparator = util::Preconditions::checkNotNull<internal::eviction::EvictionPolicyComparator>(
102  comparator, "Comparator cannot be null!");
103  }
104 
105  int32_t getSize() const {
106  return size;
107  }
108 
109  EvictionConfig &setSize(int32_t size) {
110  this->size = util::Preconditions::checkPositive(size, "Size must be positive number!");
111  return *this;
112  }
113 
114  MaxSizePolicy getMaximumSizePolicy() const {
115  return maxSizePolicy;
116  }
117 
118  EvictionConfig &setMaximumSizePolicy(const MaxSizePolicy &maxSizePolicy) {
119  this->maxSizePolicy = maxSizePolicy;
120  return *this;
121  }
122 
123  EvictionPolicy getEvictionPolicy() const {
124  return evictionPolicy;
125  }
126 
127  EvictionConfig<K, V> &setEvictionPolicy(EvictionPolicy policy) {
128  this->evictionPolicy = policy;
129  return *this;
130  }
131 
132  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > getComparator() const {
133  return comparator;
134  }
135 
136  EvictionConfig &setComparator(
137  const boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > &comparator) {
138  this->comparator = comparator;
139  return *this;
140  }
141 
143  // TODO: add support for other/custom eviction strategies
145  }
146 
147  internal::eviction::EvictionPolicyType getEvictionPolicyType() const {
148  if (evictionPolicy == LFU) {
149  return internal::eviction::LFU;
150  } else if (evictionPolicy == LRU) {
151  return internal::eviction::LRU;
152  } else if (evictionPolicy == RANDOM) {
153  return internal::eviction::RANDOM;
154  } else if (evictionPolicy == NONE) {
155  return internal::eviction::NONE;
156  } else {
157  assert(0);
158  }
159  return internal::eviction::NONE;
160  }
161 
162  protected:
163  int32_t size;
164  MaxSizePolicy maxSizePolicy;
165  EvictionPolicy evictionPolicy;
166  boost::shared_ptr<internal::eviction::EvictionPolicyComparator<K, V> > comparator;
167  };
168 
169  template <typename K, typename V>
170  std::ostream &operator<<(std::ostream &out, const EvictionConfig<K, V> &config) {
171  out << "EvictionConfig{"
172  << "size=" << config.getSize()
173  << ", maxSizePolicy=" << config.getMaximumSizePolicy()
174  << ", evictionPolicy=" << config.getEvictionPolicy()
175  << ", comparator=" << config.getComparator()
176  << '}';
177 
178  return out;
179  }
180 
184  template <typename K, typename V>
185  const int EvictionConfig<K, V>::DEFAULT_MAX_ENTRY_COUNT = INT32_MAX;
186 
190  template <typename K, typename V>
191  const typename EvictionConfig<K, V>::MaxSizePolicy EvictionConfig<K, V>::DEFAULT_MAX_SIZE_POLICY = ENTRY_COUNT;
192 
196  template <typename K, typename V>
197  const EvictionPolicy EvictionConfig<K, V>::DEFAULT_EVICTION_POLICY = LRU;
198  }
199  }
200 }
201 
202 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
203 #pragma warning(pop)
204 #endif
205 
206 #endif /* HAZELCAST_CLIENT_CONFIG_EVICTIONCONFIG_H_ */
Configuration for eviction.
Definition: EvictionConfig.h:43
internal::eviction::EvictionPolicyType getEvictionPolicyType() const
Gets the type of eviction policy.
Definition: EvictionConfig.h:147
static const int32_t DEFAULT_MAX_ENTRY_COUNT
Default maximum entry count.
Definition: EvictionConfig.h:80
internal::eviction::EvictionStrategyType::Type getEvictionStrategyType() const
Gets the type of eviction strategy.
Definition: EvictionConfig.h:142
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:132
static const EvictionPolicy DEFAULT_EVICTION_POLICY
Default Eviction Policy.
Definition: EvictionConfig.h:90
static const MaxSizePolicy DEFAULT_MAX_SIZE_POLICY
Default Max-Size Policy.
Definition: EvictionConfig.h:85
MaxSizePolicy
Maximum Size Policy.
Definition: EvictionConfig.h:51
Definition: MapEntryView.h:32
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:55
Interface for configuration information about eviction.
Definition: EvictionConfiguration.h:38