ScaleOut C++ Native Client API  5.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
named_cache.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 <string>
20 #include <stdint.h>
21 #include <sstream>
22 #include <stdexcept>
23 #include <vector>
24 #include <boost/shared_ptr.hpp>
25 
26 #include "soss_client/exceptions.h"
27 #include "soss_client/internal/soss_handle.h"
28 #include "soss_client/internal/client_cache.h"
29 #include "soss_client/soss_key.h"
30 #include "soss_client/remove_result.h"
31 #include "soss_client/object_policy.h"
32 #include "soss_client/get_options.h"
33 #include "soss_client/get_result.h"
34 #include "soss_client/put_result.h"
35 #include "soss_client/object_policy.h"
36 #include "soss_client/object_metadata.h"
37 #include "soss_client/lock_ticket.h"
38 #include "soss_client/lock_options.h"
39 #include "soss_client/lock_result.h"
40 #include "soss_client/query_result.h"
41 #include "soss_client/pmicli_invoke_options.h"
42 #include "soss_client/pmicli_invoke_result.h"
43 #include "soss_client/filter.h"
44 
45 #include "soss_svccli.h"
46 
47 namespace sosscli {
48 
52 {
53 public:
54 
55  /* Constants */
56 
58  static const size_t MAX_CACHE_NAME_LENGTH = SOSSLIB_DEF_MAX_APPNM_LEN;
59 
61  virtual ~NamedCache() {}
62 
63  /* Cache Operations */
64 
75  RemoveResult remove(SossKey key, bool object_may_not_exist = true, bool throw_on_error = true);
76 
89  RemoveResult remove_locked(SossKey key, LockTicket lock_ticket, bool object_may_not_exist = true, bool throw_on_error = true);
90 
112 
125  LockResult lock_refresh(SossKey key, LockTicket lock_ticket, bool throw_on_error = true);
126 
137  LockResult unlock(SossKey key, LockTicket lock_ticket, bool throw_on_error = true);
138 
143  int32_t clear(bool throw_on_error = true);
144 
156 
164  const std::wstring& get_key_string(SossKey &key, bool force_refresh=false) const;
165 
171  int64_t query_obj_count(bool throw_on_error = true);
172 
179  boost::shared_ptr<QueryResult> query(const Filter &filter, bool throw_on_error = true);
180 
189  boost::shared_ptr<PmicliInvokeResult> pmicli_invoke(const Filter &filter, int eval_id, int merge_id, const std::vector<uint8_t> &param, const PmicliInvokeOptions &options = PmicliInvokeOptions::CACHE_DEFAULTS);
190 
191  /* Accessors */
192 
195  uint32_t app_id() const {return app_id_;}
196 
201  std::string name() const {return name_;}
202 
203  /* Nested Classes */
204 
212  {
213  public:
214  template<typename T> friend class TypedNamedCache;
215 
218 
220  virtual ~DefaultCachePolicy() {}
221 
222  /* Accessors */
223 
225  boost::posix_time::time_duration timeout() const { return timeout_; }
227  void set_timeout(boost::posix_time::time_duration timeout) { timeout_ = timeout; }
228 
230  ObjectPolicy::TimeoutType timeout_type() const { return timeout_type_; }
233 
235  ObjectPolicy::PreemptionPriority preemption_priority() const { return preemption_priority_; }
238 
240  bool allow_push_repl() const { return allow_push_repl_; }
242  void set_allow_push_repl(bool allow_push_repl) { allow_push_repl_ = allow_push_repl; }
243 
245  bool use_client_cache() { return use_client_cache_; }
247  void set_use_client_cache(bool use_client_cache) { use_client_cache_ = use_client_cache; }
248 
251  bool throw_on_error() const { return throw_on_error_; }
254  void set_throw_on_error(bool throw_on_error) { throw_on_error_ = throw_on_error; }
255 
257  int32_t max_lock_retry_count() const { return max_lock_retry_count_; }
259  void set_max_lock_retry_count(int32_t max_lock_retry_count) { max_lock_retry_count_ = max_lock_retry_count; }
260 
262  int32_t lock_retry_interval_ms() const { return lock_retry_interval_ms_; }
264  void set_lock_retry_interval_ms(int32_t lock_retry_interval_ms) { lock_retry_interval_ms_ = lock_retry_interval_ms; }
265 
267  int32_t max_read_pending_attempts() const { return max_read_pending_attempts_; }
270 
272  int32_t read_pending_retry_interval_ms() const { return read_pending_retry_interval_ms_; }
275 
276  private:
277 
280  SOSSLIB_OBJ_PARAMS_EXT &copy_to(SOSSLIB_OBJ_PARAMS_EXT &params) const;
281 
282  boost::posix_time::time_duration timeout_;
283  ObjectPolicy::TimeoutType timeout_type_;
284  ObjectPolicy::PreemptionPriority preemption_priority_;
285  bool allow_push_repl_;
286  bool use_client_cache_;
287  bool throw_on_error_;
288  int32_t max_lock_retry_count_;
289  int32_t lock_retry_interval_ms_;
290  int32_t max_read_pending_attempts_;
291  int32_t read_pending_retry_interval_ms_;
292  };
293 
296  DefaultCachePolicy& default_cache_policy() { return default_cache_policy_; }
297 
300 
302  static internal::ClientCache& client_cache();
303 
304 protected:
305 
309 
310  /* Initialization routines, intended to be called by constructors in subclasses */
311 
314  void init(const uint32_t app_id);
315 
319  void init(const std::string &name);
320 
322  virtual void serialize_filter(const Filter &filter, std::vector<uint8_t> &bytes_out, size_t bytes_to_reserve_for_header) {}
323 
325  static internal::ClientCache client_cache_;
326 
328  static boost::mutex init_mutex_;
329 
331  static bool is_init_;
332 
333 private:
334 
335  uint32_t app_id_;
336  std::string name_;
337  DefaultCachePolicy default_cache_policy_;
338 };
339 
340 }