ScaleOut C++ Native Client API  5.1
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
soss_key.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 <vector>
22 #include <boost/thread.hpp>
23 
24 #include "soss_svccli.h"
25 
26 namespace sosscli {
27 
30 class SossKey
31 {
32 public:
33 
34  /* Constants */
35 
37  static const size_t MAX_KEY_LENGTH = sizeof(((SOSS_OBJKEY*)0)->obj_id) + sizeof(((SOSS_OBJKEY*)0)->ext_obj_id);
38 
39  /* Constructors */
40 
42  SossKey();
43 
46  SossKey(const SossKey &other);
47 
50  SossKey(const SOSS_OBJKEY &obj_key);
51 
57  SossKey(const std::string &key, const uint32_t app_id);
58 
64  SossKey(const std::wstring &key, const uint32_t app_id);
65 
69  SossKey(const std::vector<uint8_t> &bytes, const uint32_t app_id);
70 
75  SossKey(const std::string &key);
76 
81  SossKey(const std::wstring &key);
82 
85  SossKey(const std::vector<uint8_t> &bytes);
86 
91  SossKey(const char *key);
92 
97  SossKey(const wchar_t *key);
98 
101  SossKey(int32_t key);
102 
106  SossKey(const int32_t key, const uint32_t app_id);
107 
110  SossKey(uint32_t key);
111 
115  SossKey(const uint32_t key, const uint32_t app_id);
116 
119  SossKey(int64_t key);
120 
124  SossKey(const int64_t key, const uint32_t app_id);
125 
128  SossKey(uint64_t key);
129 
133  SossKey(const uint64_t key, const uint32_t app_id);
134 
136  virtual ~SossKey();
137 
138  /* operator overloads */
139 
141  SossKey& operator=(const SossKey& rhs);
142 
144  friend bool operator==(const SossKey &key1, const SossKey &key2);
145 
147  friend bool operator!=(const SossKey &key1, const SossKey &key2);
148 
150  friend std::size_t hash_value(SossKey const& key);
151 
152  /* Accessors */
153 
156  SOSS_OBJKEY obj_key() const {return obj_key_;}
157 
160  uint32_t app_id() const {return obj_key_.app_id;}
161 
164  void set_app_id(uint32_t app_id);
165 
168  bool has_app_id() const {return has_app_id_;}
169 
174  bool has_string_data() const {
175  if (((obj_key_.flags & SOSSKEY_FLAG_HAS_KEYSTRING) != 0) && !original_string_key_.empty())
176  return true;
177  else
178  return false;
179  }
180 
187  const std::wstring& retrieve_original_string_key() const {
188  return original_string_key_;
189  }
190 
195  void set_local_string_key_data(const std::vector<wchar_t> &string_key_data)
196  {
197  original_string_key_.assign(string_key_data.begin(), string_key_data.end());
198  }
199 
200 protected:
201 
202 private:
203 
204  void init(const std::vector<uint8_t> &bytes, const uint32_t app_id, const std::wstring &original_string_key, const bool has_app_id);
205  void init_string_key(const std::string &key, const uint32_t app_id, const bool has_app_id);
206  void init_wstring_key(const std::wstring &key, const uint32_t app_id, const bool has_app_id);
207 
208  void hash_string(const std::wstring &key, std::vector<uint8_t> *bytes);
209 
210  SOSS_OBJKEY obj_key_;
211  std::wstring original_string_key_;
212  bool has_app_id_;
213  boost::mutex mutex_;
214 
215 };
216 
217 }
218