Red Hat Data Grid HotRod C++ Client  7.2.0
RemoteCacheBase.h
Go to the documentation of this file.
1 #ifndef ISPN_HOTROD_REMOTECACHEBASE_H
2 #define ISPN_HOTROD_REMOTECACHEBASE_H
3 
4 
5 
10 #include "infinispan/hotrod/Flag.h"
15 #include <map>
16 #include <set>
17 #include <vector>
18 
20 using namespace infinispan::hotrod::event;
21 
22 namespace infinispan {
23 namespace hotrod {
24 
25 namespace operations {
26 class OperationsFactory;
27 }
28 
29 namespace event {
30 template <class K, class V> class CacheClientListener;
31 #ifndef SWIGCSHARP
32 template <typename... Params> class ContinuousQueryListener;
33 #endif
34 }
35 typedef void (*MarshallHelperFn) (void*, const void*, std::vector<char> &);
36 typedef void* (*UnmarshallHelperFn) (void*, const std::vector<char> &);
37 
38 class KeyUnmarshallerFtor;
39 class ValueUnmarshallerFtor;
40 class RemoteCacheImpl;
41 
42 class RemoteCacheBase
43 {
44 public:
45  virtual ~RemoteCacheBase() {}
46 protected:
47  HR_EXTERN const char *base_getName();
48  HR_EXTERN void *base_get(const void *key);
49  HR_EXTERN std::map<std::vector<char>,std::vector<char> > base_getAll(const std::set<std::vector<char> >& keySet);
50  HR_EXTERN void *base_put(const void *key, const void *value, int64_t life, int64_t idle);
51  HR_EXTERN void *base_putIfAbsent(const void *key, const void *value, int64_t life, int64_t idle);
52  HR_EXTERN void *base_replace(const void *key, const void *value, int64_t life, int64_t idle);
53  HR_EXTERN void *base_remove(const void *key);
54  HR_EXTERN bool base_containsKey(const void *key);
55  HR_EXTERN void base_ping();
56  HR_EXTERN bool base_replaceWithVersion(const void *key, const void *value, int64_t version, int64_t life, int64_t idle);
57  HR_EXTERN bool base_removeWithVersion(const void *key, int64_t version);
58  HR_EXTERN void *base_getWithVersion(const void* key, VersionedValue* version);
59  HR_EXTERN void *base_getWithMetadata(const void* key, MetadataValue* metadata);
60  HR_EXTERN void base_getBulk(int size, std::map<void*, void*> &mbuf);
61  HR_EXTERN void base_keySet(int scope, std::vector<void*> &sbuf);
62  HR_EXTERN void base_stats(std::map<std::string,std::string> &sbuf);
63  HR_EXTERN void base_clear();
64  HR_EXTERN uint64_t base_size();
65  HR_EXTERN void base_withFlags(Flag flag);
66  HR_EXTERN std::vector<unsigned char> base_execute(const std::string &cmdName, const std::map<std::string,std::string>& args);
67  HR_EXTERN std::vector<unsigned char> base_execute(const std::string &cmdName, const std::map<std::vector<char> ,std::vector<char> >& args);
68  HR_EXTERN std::vector<char> base_execute(const std::vector<char> &cmdName, const std::map<std::vector<char> ,std::vector<char> >& args);
69  HR_EXTERN CacheTopologyInfo base_getCacheTopologyInfo();
70  HR_EXTERN QueryResponse base_query(const QueryRequest &qr);
71  HR_EXTERN std::vector<unsigned char> base_query_char(std::vector<unsigned char> qr, size_t size);
72 
73  HR_EXTERN void base_addClientListener(ClientListener &clientListener, const std::vector<std::vector<char> > filterFactoryParam, const std::vector<std::vector<char> > converterFactoryParams, const std::function<void()> &recoveryCallback);
74  HR_EXTERN void base_removeClientListener(ClientListener &clientListener);
75  HR_EXTERN void putScript(const std::vector<char>& name, const std::vector<char>& script);
76 
77  RemoteCacheBase() {}
78  HR_EXTERN void setMarshallers(void* rc, MarshallHelperFn kf, MarshallHelperFn vf, UnmarshallHelperFn ukf, UnmarshallHelperFn uvf);
79 
80 #ifndef SWIGCSHARP
81  template<class K, class V, typename... Params>
82  void base_addContinuousQueryListener(ContinuousQueryListener<K, V, Params...>& cql) {
83  static char CONTINUOUS_QUERY_FILTER_FACTORY_NAME[] =
84  "continuous-query-filter-converter-factory";
85  cql.cl.filterFactoryName = std::vector<char>(
86  CONTINUOUS_QUERY_FILTER_FACTORY_NAME,
87  CONTINUOUS_QUERY_FILTER_FACTORY_NAME
88  + strlen(CONTINUOUS_QUERY_FILTER_FACTORY_NAME));
89  cql.cl.converterFactoryName = std::vector<char>(
90  CONTINUOUS_QUERY_FILTER_FACTORY_NAME,
91  CONTINUOUS_QUERY_FILTER_FACTORY_NAME
92  + strlen(CONTINUOUS_QUERY_FILTER_FACTORY_NAME));
93  BasicTypesProtoStreamMarshaller<std::string> paramMarshaller;
94  std::vector<std::vector<char> > filterFactoryParams;
95  std::vector<char> param;
96  paramMarshaller.marshall(cql.getQuery(), param);
97  filterFactoryParams.push_back(param);
98  std::vector<std::vector<char> > converterFactoryParams;
99  cql.cl.useRawData = true;
100  cql.listenerCustomEvent =
101  [this, &cql](ClientCacheEntryCustomEvent e) {
102  ContinuousQueryResult r;
103  WrappedMessage wm;
104  wm.ParseFromArray(e.getEventData().data(), e.getEventData().size());
105  r.ParseFromString(wm.wrappedmessagebytes());
106  auto resultType = r.resulttype();
107  auto i=0;
108  std::tuple<Params...> tp= popTuple<Params...>(r.projection(), i) ;
109  K* k = (K*)this->baseKeyUnmarshall(std::vector<char>(r.key().begin(), r.key().end()));
110  switch (resultType) {
111  case ContinuousQueryResult_ResultType_JOINING:
112  cql.getJoiningListener()(*k, tp);
113  break;
114  case ContinuousQueryResult_ResultType_LEAVING:
115  cql.getLeavingListener()(*k, tp);
116  break;
117  case ContinuousQueryResult_ResultType_UPDATED:
118  cql.getUpdatedListener()(*k, tp);
119  break;
120  default:
121  //uIgnore unknown types
122  break;
123  }
124  };
125  cql.cl.add_listener(cql.listenerCustomEvent);
126  this->base_addClientListener(cql.cl, filterFactoryParams,
127  converterFactoryParams, cql.getFailoverListener());
128  }
129  template <class K, class V>
130  void base_addContinuousQueryListener(ContinuousQueryListener<K, V>& cql) {
131  static char CONTINUOUS_QUERY_FILTER_FACTORY_NAME[] =
132  "continuous-query-filter-converter-factory";
133  cql.cl.filterFactoryName = std::vector<char>(
134  CONTINUOUS_QUERY_FILTER_FACTORY_NAME,
135  CONTINUOUS_QUERY_FILTER_FACTORY_NAME
136  + strlen(CONTINUOUS_QUERY_FILTER_FACTORY_NAME));
137  cql.cl.converterFactoryName = std::vector<char>(
138  CONTINUOUS_QUERY_FILTER_FACTORY_NAME,
139  CONTINUOUS_QUERY_FILTER_FACTORY_NAME
140  + strlen(CONTINUOUS_QUERY_FILTER_FACTORY_NAME));
141  BasicTypesProtoStreamMarshaller<std::string> paramMarshaller;
142  std::vector<std::vector<char> > filterFactoryParams;
143  std::vector<char> param;
144  paramMarshaller.marshall(cql.getQuery(), param);
145  filterFactoryParams.push_back(param);
146  std::vector<std::vector<char> > converterFactoryParams;
147  cql.cl.useRawData = true;
148  cql.listenerCustomEvent =
149  [this, &cql](ClientCacheEntryCustomEvent e) {
150  ContinuousQueryResult r;
151  WrappedMessage wm;
152  wm.ParseFromArray(e.getEventData().data(), e.getEventData().size());
153  r.ParseFromString(wm.wrappedmessagebytes());
154  auto resultType = r.resulttype();
155  K* k = (K*)this->baseKeyUnmarshall(std::vector<char>(r.key().begin(), r.key().end()));
156  V* v = (V*)this->baseValueUnmarshall(std::vector<char>(r.value().begin(), r.value().end()));
157  switch (resultType) {
158  case ContinuousQueryResult_ResultType_JOINING:
159  cql.getJoiningListener()(*k, *v);
160  break;
161  case ContinuousQueryResult_ResultType_LEAVING:
162  cql.getLeavingListener()(*k, *v);
163  break;
164  case ContinuousQueryResult_ResultType_UPDATED:
165  cql.getUpdatedListener()(*k, *v);
166  break;
167  default:
168  //uIgnore unknown types
169  break;
170  }
171  };
172  cql.cl.add_listener(cql.listenerCustomEvent);
173  this->base_addClientListener(cql.cl, filterFactoryParams,
174  converterFactoryParams, cql.getFailoverListener());
175  }
176 #endif
177 
178 
179 private:
180  std::shared_ptr<RemoteCacheImpl> impl; // pointer to RemoteCacheImpl;
181  void *remoteCachePtr=nullptr; // TODO: pointer to self, is it really necessary?
182  MarshallHelperFn baseKeyMarshallFn;
183  MarshallHelperFn baseValueMarshallFn;
184  HR_EXTERN void baseKeyMarshall(const void* k, std::vector<char> &buf);
185  HR_EXTERN void baseValueMarshall(const void* v, std::vector<char> &buf);
186 
187  UnmarshallHelperFn baseKeyUnmarshallFn;
188  UnmarshallHelperFn baseValueUnmarshallFn;
189  HR_EXTERN void* baseKeyUnmarshall(const std::vector<char> &buf);
190  HR_EXTERN void* baseValueUnmarshall(const std::vector<char> &buf);
191 
192 friend class RemoteCacheManager;
193 friend class RemoteCacheImpl;
194 friend class NearRemoteCacheImpl;
195 friend class KeyUnmarshallerFtor;
196 friend class ValueUnmarshallerFtor;
197 template <class K, class V>
198 friend class ::infinispan::hotrod::event::CacheClientListener;
199 #ifndef SWIGCSHARP
200 template <typename... Params>
201 friend class ::infinispan::hotrod::event::ContinuousQueryListener;
202 template <class M> friend class RemoteExecution;
203 
204 #endif
205 };
206 
207 }} // namespace
208 
209 #endif /* ISPN_HOTROD_REMOTECACHEBASE_H */
#define HR_EXTERN
Definition: ImportExport.h:35
Definition: CacheClientListener.h:30
Definition: AuthenticationConfiguration.h:10
void(* MarshallHelperFn)(void *, const void *, std::vector< char > &)
Definition: RemoteCacheBase.h:35
void *(* UnmarshallHelperFn)(void *, const std::vector< char > &)
Definition: RemoteCacheBase.h:36
std::tuple< H, Params... > popTuple(const RepeatedPtrField< WrappedMessage > &wMsgs, int &k)
Definition: QueryUtils.h:94
Definition: RemoteCacheManager.h:38
Definition: ClientListener.h:35
Definition: CacheClientListener.h:33
Definition: ContinuousQueryListener.h:34
Definition: MetadataValue.h:9
Flag
Definition: Flag.h:7
Definition: RemoteExecution.h:29
Definition: VersionedValue.h:9