Red Hat Data Grid HotRod C++ Client  7.2.0
QueryUtils.h
Go to the documentation of this file.
1 /*
2  * QueryUtils.h
3  *
4  * Created on: Apr 7, 2016
5  * Author: rigazilla
6  */
7 
8 #ifndef INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_
9 #define INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_
10 
12 #include <tuple>
13 using namespace org::infinispan::protostream;
14 using namespace google::protobuf;
16 
17 
18 // extract a resultset of entities from a QueryResponse obj
19 template <class T> bool unwrapResults(QueryResponse resp, std::vector<T> &res)
20 {
21  if (resp.projectionsize()>0)
22  { // Query has select
23  return false;
24  }
25  for (int i=0; i<resp.results_size(); i++)
26  {
27  const WrappedMessage &wm =resp.results(i);
28  if ( wm.has_wrappedbytes() )
29  {
30  WrappedMessage wmn;
31  wmn.ParseFromString(wm.wrappedbytes());
32  if (wmn.has_wrappedmessagebytes()) {
33  T u1;
34  u1.ParseFromString(wmn.wrappedmessagebytes());
35  res.push_back(u1);
36  }
37  }
38  }
39  return true;
40 }
41 
42 // typesafe method to extract the value obj from a WrappedMessage obj
43 template <typename T> T unwrapSingleValue(const WrappedMessage& wm);
44 
45 template <> inline std::string unwrapSingleValue<std::string>(const WrappedMessage& wm)
46 {
47  if (wm.has_wrappedstring())
48  {
49  return wm.wrappedstring();
50  }
51  else
52  {
53  throw "std::string type not found in response";
54  }
55 }
56 
57 template <> inline int unwrapSingleValue<int>(const WrappedMessage& wm)
58 {
59  if (wm.has_wrappedint32())
60  {
61  return wm.wrappedint32();
62  }
63  else if (wm.has_wrappedint64())
64  {
65  return wm.wrappedint64();
66  }
67  else
68  {
69  throw "int type not found in response";
70  }
71 }
72 
73 template <> inline double unwrapSingleValue<double>(const WrappedMessage& wm)
74 {
75  if (wm.has_wrappeddouble())
76  {
77  return wm.wrappeddouble();
78  }
79  else
80  {
81  throw "double type not found in response";
82  }
83 }
84 
85 template <typename T> T unwrapSingleResult(const QueryResponse &qr)
86 {
87  return unwrapSingleValue<T>(qr.results(0));
88 }
89 
90 
91 #if !defined (_MSC_VER) || (_MSC_VER>=1800)
92 
93 // typesafe method to turn one projection row of a array of WrappedMessage objs into a tuple
94 template <typename H, typename... Params> std::tuple<H, Params...> popTuple(const RepeatedPtrField<WrappedMessage >& wMsgs, int &k)
95 {
96  H s = unwrapSingleValue<H>(wMsgs.Get(k++));
97  std::tuple<Params...> p = popTuple<Params... >(wMsgs,k);
98  return std::tuple_cat(std::tie(s),p);
99 }
100 
101 template<>
102 inline std::tuple<int> popTuple<int>(const RepeatedPtrField<WrappedMessage >& wMsgs, int &k)
103  {
104  int s(unwrapSingleValue<int>(wMsgs.Get(k++)));
105  return std::make_tuple<int>(std::move(s));
106 }
107 
108 template<>
109 inline std::tuple<std::string> popTuple<std::string>(const RepeatedPtrField<WrappedMessage >& wMsgs, int &k)
110  {
111  std::string s(unwrapSingleValue<std::string>(wMsgs.Get(k++)));
112  return std::make_tuple<std::string>(std::move(s));
113 }
114 
115 // typesafe method to turn one projection row of a QueryResponse obj into a tuple
116 template <typename H, typename... Params> std::tuple<H, Params...> popTuple(QueryResponse &resp, int &k)
117 {
118  H s = unwrapSingleValue<H>(resp.results(k++));
119  std::tuple<Params...> p=popTuple<Params... >(resp,k);
120  return std::tuple_cat(std::tie(s),p);
121 }
122 
123 template<>
124 inline std::tuple<std::string> popTuple<std::string>(QueryResponse& resp, int &k)
125  {
126  std::string s(unwrapSingleValue<std::string>(resp.results(k++)));
127  return std::make_tuple<std::string>(std::move(s));
128 }
129 
130 template<>
131 inline std::tuple<int> popTuple<int>(QueryResponse & resp, int &k)
132  {
133  int s(unwrapSingleValue<int>(resp.results(k++)));
134  return std::make_tuple<int>(std::move(s));
135 }
136 
137 template<>
138 inline std::tuple<double> popTuple<double>(QueryResponse & resp, int &k)
139  {
140  int s(unwrapSingleValue<double>(resp.results(k++)));
141  return std::make_tuple<double>(std::move(s));
142 }
143 
144 // typesafe method to convert QueryResponse containing a resultset of projections to a std::tuple
145 template<typename... Params> bool unwrapProjection(QueryResponse &resp, std::vector<std::tuple<Params...> > &prjRes)
146 {
147  if (resp.projectionsize() == 0) {
148  return false;
149  }
150  int numTuple = resp.results_size() / resp.projectionsize();
151  int k = 0;
152  for (int i = 0; i < numTuple; i++) {
153  std::tuple<Params...> tp= popTuple<Params...>(resp, k) ;
154  prjRes.push_back(tp);
155  }
156  return true;
157 }
158 #endif
159 
160 #endif /* INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_ */
T unwrapSingleValue(const WrappedMessage &wm)
bool unwrapProjection(QueryResponse &resp, std::vector< std::tuple< Params... > > &prjRes)
Definition: QueryUtils.h:145
bool unwrapResults(QueryResponse resp, std::vector< T > &res)
Definition: QueryUtils.h:19
std::tuple< H, Params... > popTuple(const RepeatedPtrField< WrappedMessage > &wMsgs, int &k)
Definition: QueryUtils.h:94
T unwrapSingleResult(const QueryResponse &qr)
Definition: QueryUtils.h:85