InfinispanHotRodC++Client  8.2.0.Alpha1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
15 template <class T> bool unwrapResults(QueryResponse resp, std::vector<T> &res)
16 {
17  if (resp.projectionsize()>0)
18  { // Query has select
19  return false;
20  }
21  for (int i=0; i<resp.results_size(); i++)
22  {
23  const WrappedMessage &wm =resp.results(i);
24  if ( wm.has_wrappedbytes() )
25  {
26  WrappedMessage wmn;
27  wmn.ParseFromString(wm.wrappedbytes());
28  if (wmn.has_wrappedmessagebytes()) {
29  T u1;
30  u1.ParseFromString(wmn.wrappedmessagebytes());
31  res.push_back(u1);
32  }
33  }
34  }
35  return true;
36 }
37 
38 template <typename T> T unwrapSingleValue(const WrappedMessage& wm);
39 
40 template <> std::string unwrapSingleValue<std::string>(const WrappedMessage& wm)
41 {
42  if (wm.has_wrappedstring())
43  {
44  return wm.wrappedstring();
45  }
46  else
47  {
48  throw "std::string not found in response";
49  }
50 }
51 
52 template <> int unwrapSingleValue<int>(const WrappedMessage& wm)
53 {
54  if (wm.has_wrappedint32())
55  {
56  return wm.wrappedint32();
57  }
58  else if (wm.has_wrappedint64())
59  {
60  return wm.wrappedint64();
61  }
62  else
63  {
64  throw "std::string not found in response";
65  }
66 }
67 
68 template <typename T> T unwrapSingleResult(const QueryResponse &qr)
69 {
70  return unwrapSingleValue<T>(qr.results(0));
71 }
72 
73 
74 #if !defined (_MSC_VER) || (_MSC_VER>=1800)
75 template <typename H, typename... Params> std::tuple<H, Params...> popTuple(QueryResponse &resp, int &k)
76 {
77  H s = unwrapSingleValue<H>(resp.results(k++));
78  std::tuple<Params...> p=popTuple<Params... >(resp,k);
79  return std::tuple_cat(std::tie(s),p);
80 }
81 
82 template<>
83 std::tuple<std::string> popTuple<std::string>(QueryResponse & resp, int &k)
84  {
85  std::string s(unwrapSingleValue<std::string>(resp.results(k++)));
86  return std::make_tuple<std::string>(std::move(s));
87 }
88 
89 template<>
90 std::tuple<int> popTuple<int>(QueryResponse & resp, int &k)
91  {
92  int s(unwrapSingleValue<int>(resp.results(k++)));
93  return std::make_tuple<int>(std::move(s));
94 }
95 
96 
97 template<typename... Params> bool unwrapProjection(QueryResponse &resp, std::vector<std::tuple<Params...> > &prjRes)
98 {
99  if (resp.projectionsize() == 0) {
100  return false;
101  }
102  int numTuple = resp.results_size() / resp.projectionsize();
103  int k = 0;
104  for (int i = 0; i < numTuple; i++) {
105  std::tuple<Params...> tp= popTuple<Params...>(resp, k) ;
106  prjRes.push_back(tp);
107  }
108  return true;
109 }
110 #endif
111 
112 #endif /* INCLUDE_INFINISPAN_HOTROD_QUERYUTILS_H_ */
bool unwrapProjection(QueryResponse &resp, std::vector< std::tuple< Params...> > &prjRes)
Definition: QueryUtils.h:97
T unwrapSingleValue(const WrappedMessage &wm)
int unwrapSingleValue< int >(const WrappedMessage &wm)
Definition: QueryUtils.h:52
bool unwrapResults(QueryResponse resp, std::vector< T > &res)
Definition: QueryUtils.h:15
std::tuple< int > popTuple< int >(QueryResponse &resp, int &k)
Definition: QueryUtils.h:90
T unwrapSingleResult(const QueryResponse &qr)
Definition: QueryUtils.h:68
std::tuple< H, Params...> popTuple(QueryResponse &resp, int &k)
Definition: QueryUtils.h:75