ScaleOut C++ Native Client API  5.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
get_options.h
1 /*
2  Copyright (c) 2013 by ScaleOut Software, Inc.
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 
17 #pragma once
18 
19 #include <stdint.h>
20 #include <memory>
21 #include "boost/function.hpp"
22 #include "boost/shared_ptr.hpp"
23 #include "soss_client/soss_key.h"
24 #include "soss_client/object_policy.h"
25 
26 namespace sosscli {
27 
30 template<typename T>
32 {
33 public:
34 
35  /* Constants */
36 
40 
41  /* Constructors */
42 
46  : use_client_cache_(true), throw_on_error_(true), max_lock_retry_count_(20000), lock_retry_interval_ms_(5) {}
47 
55  : use_client_cache_(use_client_cache), throw_on_error_(throw_on_error), max_lock_retry_count_(max_lock_retry_count), lock_retry_interval_ms_(lock_retry_interval_ms) {}
56 
58  GetOptions(const GetOptions &other)
59  : use_client_cache_(other.use_client_cache_),
60  throw_on_error_(other.throw_on_error_),
61  max_lock_retry_count_(other.max_lock_retry_count_),
62  lock_retry_interval_ms_(other.lock_retry_interval_ms_),
63  missed_object_callback_(other.missed_object_callback_)
64  {
65  if (other.missing_object_policy_)
66  missing_object_policy_ = std::unique_ptr<ObjectPolicy>(new ObjectPolicy(*other.missing_object_policy_));
67  }
68 
71  {
72  if (this != &rhs)
73  {
74  use_client_cache_ = rhs.use_client_cache_;
75  throw_on_error_ = rhs.throw_on_error_;
76  max_lock_retry_count_ = rhs.max_lock_retry_count_;
77  lock_retry_interval_ms_ = rhs.lock_retry_interval_ms_;
78  missed_object_callback_ = rhs.missed_object_callback_;
79  if (rhs.missing_object_policy_)
80  missing_object_policy_ = std::unique_ptr<ObjectPolicy>(new ObjectPolicy(*rhs.missing_object_policy_));
81  }
82  return *this;
83  }
84 
85 
87  virtual ~GetOptions() {};
88 
89  /* Accessors */
90 
93  bool use_client_cache() const { return use_client_cache_; }
94 
99  GetOptions& set_use_client_cache(bool use_client_cache) { use_client_cache_ = use_client_cache; return *this; }
100 
103  bool throw_on_error() const { return throw_on_error_; }
104 
110  GetOptions& set_throw_on_error(bool throw_on_error) { throw_on_error_ = throw_on_error; return *this; }
111 
113  int32_t max_lock_retry_count() const { return max_lock_retry_count_; }
114 
118  GetOptions& set_max_lock_retry_count(int32_t max_lock_retry_count) { max_lock_retry_count_ = max_lock_retry_count; return *this; }
119 
121  int32_t lock_retry_interval_ms() const { return lock_retry_interval_ms_; }
122 
126  GetOptions& set_lock_retry_interval_ms(int32_t lock_retry_interval_ms) { lock_retry_interval_ms_ = lock_retry_interval_ms; return *this; }
127 
146  GetOptions& set_missed_object_callback(boost::function<boost::shared_ptr<T>(const SossKey &key)> callback) { missed_object_callback_ = callback; return *this; }
147 
149  boost::function<boost::shared_ptr<T>(const SossKey &key)> missed_object_callback() const { return missed_object_callback_; }
150 
153  ObjectPolicy* missing_object_policy() const { return missing_object_policy_.get(); }
154 
170  GetOptions& set_missing_object_policy(std::unique_ptr<ObjectPolicy> policy) { missing_object_policy_ = std::move(policy); return *this; }
171 
172 protected:
173 
174 private:
175 
176  bool use_client_cache_;
177  bool throw_on_error_;
178  int32_t max_lock_retry_count_;
179  int32_t lock_retry_interval_ms_;
180 
181  // object creation callback
182  boost::function<boost::shared_ptr<T>(const SossKey &key)> missed_object_callback_;
183 
184  // Optional policy for an object inserted via the insert-once callback
185  std::unique_ptr<ObjectPolicy> missing_object_policy_;
186 };
187 
188 template<typename T> const GetOptions<T> GetOptions<T>::CACHE_DEFAULTS = GetOptions<T>();
189 
190 } // namespace