Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uid-util.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_UID_UTIL_H
17 #define IMPALA_UTIL_UID_UTIL_H
18 
19 #include <boost/uuid/uuid.hpp>
20 #include <boost/uuid/uuid_generators.hpp>
21 
22 #include "gen-cpp/Types_types.h" // for TUniqueId
23 #include "util/debug-util.h"
24 #include "common/names.h"
25 
26 namespace impala {
28 inline std::size_t hash_value(const impala::TUniqueId& id) {
29  std::size_t seed = 0;
30  boost::hash_combine(seed, id.lo);
31  boost::hash_combine(seed, id.hi);
32  return seed;
33 }
34 
37 template <typename T>
38 inline void UUIDToTUniqueId(const boost::uuids::uuid& uuid, T* unique_id) {
39  memcpy(&(unique_id->hi), &uuid.data[0], 8);
40  memcpy(&(unique_id->lo), &uuid.data[8], 8);
41 }
42 
43 template <typename F, typename T>
44 inline T CastTUniqueId(const F& from) {
45  T to;
46  to.hi = from.hi;
47  to.lo = from.lo;
48  return to;
49 }
50 
52 inline string GenerateUUIDString() {
53  boost::uuids::basic_random_generator<boost::mt19937> gen;
54  boost::uuids::uuid u = gen();
55  string uuid(u.begin(), u.end());
56  return uuid;
57 }
58 
60 inline TUniqueId GenerateUUID() {
61  const string& u = GenerateUUIDString();
62  TUniqueId uid;
63  memcpy(&uid.hi, &u[0], sizeof(int64_t));
64  memcpy(&uid.lo, &u[0] + sizeof(int64_t), sizeof(int64_t));
65  return uid;
66 }
67 
68 } // namespace impala
69 #endif
T CastTUniqueId(const F &from)
Definition: uid-util.h:44
TUniqueId GenerateUUID()
generates a 16 byte UUID
Definition: uid-util.h:60
std::size_t hash_value(const Decimal4Value &v)
This function must be called 'hash_value' to be picked up by boost.
string GenerateUUIDString()
generates a 16 byte UUID
Definition: uid-util.h:52
void UUIDToTUniqueId(const boost::uuids::uuid &uuid, T *unique_id)
Definition: uid-util.h:38