forked from infinispan/cpp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteCacheImpl.cpp
250 lines (219 loc) · 9.92 KB
/
RemoteCacheImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "infinispan/hotrod/types.h"
#include "hotrod/sys/Msg.h"
#include "hotrod/impl/RemoteCacheImpl.h"
#include "hotrod/impl/RemoteCacheManagerImpl.h"
#include "hotrod/impl/operations/OperationsFactory.h"
#include "hotrod/impl/operations/GetOperation.h"
#include "hotrod/impl/operations/PutOperation.h"
#include "hotrod/impl/operations/PingOperation.h"
#include "hotrod/impl/operations/PutIfAbsentOperation.h"
#include "hotrod/impl/operations/ReplaceOperation.h"
#include "hotrod/impl/operations/RemoveOperation.h"
#include "hotrod/impl/operations/ContainsKeyOperation.h"
#include "hotrod/impl/operations/ReplaceIfUnmodifiedOperation.h"
#include "hotrod/impl/operations/RemoveIfUnmodifiedOperation.h"
#include "hotrod/impl/operations/GetWithMetadataOperation.h"
#include "hotrod/impl/operations/GetWithVersionOperation.h"
#include "hotrod/impl/operations/BulkGetOperation.h"
#include "hotrod/impl/operations/BulkGetKeysOperation.h"
#include "hotrod/impl/operations/StatsOperation.h"
#include "hotrod/impl/operations/ClearOperation.h"
#include "hotrod/impl/operations/FaultTolerantPingOperation.h"
#include "hotrod/impl/transport/TransportFactory.h"
#include "hotrod/impl/protocol/CodecFactory.h"
#include "hotrod/impl/VersionedOperationResponse.h"
#include "hotrod/impl/MetadataValueImpl.h"
#include <iostream>
namespace infinispan {
namespace hotrod {
using namespace operations;
using namespace transport;
using namespace protocol;
using namespace sys;
RemoteCacheImpl::RemoteCacheImpl(RemoteCacheManagerImpl& rcm, const std::string& n)
:remoteCacheManager(rcm), name(n) {}
void RemoteCacheImpl::get(RemoteCacheBase& remoteCacheBase, const void *k, void* b) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
ScopedBuffer& vbuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<GetOperation> gco(operationsFactory->newGetKeyOperation(keyBytes));
hrbytes bytes = gco->execute();
bytes.releaseTo(vbuf);
}
void RemoteCacheImpl::put(RemoteCacheBase& remoteCacheBase, const void *k, const void* v, uint64_t life, uint64_t idle, void* b) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf, vbuf;
ScopedBuffer& obuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
remoteCacheBase.baseValueMarshall(v, &vbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hrbytes valueBytes(vbuf.getBytes(), vbuf.getLength());
applyDefaultExpirationFlags(life, idle);
hr_scoped_ptr<PutOperation> op(operationsFactory->newPutKeyValueOperation(keyBytes, valueBytes,life,idle));
hrbytes bytes = op->execute();
bytes.releaseTo(obuf);
}
void RemoteCacheImpl::putIfAbsent(RemoteCacheBase& remoteCacheBase, const void *k, const void* v, uint64_t life, uint64_t idle, void* b) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf, vbuf;
ScopedBuffer& obuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
remoteCacheBase.baseValueMarshall(v, &vbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hrbytes valueBytes(vbuf.getBytes(), vbuf.getLength());
applyDefaultExpirationFlags(life, idle);
hr_scoped_ptr<PutIfAbsentOperation> op(operationsFactory->newPutIfAbsentOperation(keyBytes, valueBytes,life,idle));
hrbytes bytes = op->execute();
bytes.releaseTo(obuf);
}
PingResult RemoteCacheImpl::ping() {
hr_scoped_ptr<FaultTolerantPingOperation> op(operationsFactory->newFaultTolerantPingOperation());
return op->execute();
}
void RemoteCacheImpl::replace(RemoteCacheBase& remoteCacheBase, const void *k, const void* v, uint64_t life, uint64_t idle, void* b) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf, vbuf;
ScopedBuffer& obuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
remoteCacheBase.baseValueMarshall(v, &vbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hrbytes valueBytes(vbuf.getBytes(), vbuf.getLength());
applyDefaultExpirationFlags(life, idle);
hr_scoped_ptr<ReplaceOperation> op(operationsFactory->newReplaceOperation(keyBytes, valueBytes,life,idle));
hrbytes bytes = op->execute();
bytes.releaseTo(obuf);
}
void RemoteCacheImpl::remove(RemoteCacheBase& remoteCacheBase, const void* k, void* b) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
ScopedBuffer& vbuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<RemoveOperation> gco(operationsFactory->newRemoveOperation(keyBytes));
hrbytes bytes = gco->execute();
bytes.releaseTo(vbuf);
}
void RemoteCacheImpl::containsKey(RemoteCacheBase& remoteCacheBase, const void* k, bool* r) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<ContainsKeyOperation> gco(
operationsFactory->newContainsKeyOperation(keyBytes));
*r = gco->execute();
}
void RemoteCacheImpl::replaceWithVersion(RemoteCacheBase& remoteCacheBase,
const void* k, const void* v, uint64_t version, uint64_t life, uint64_t idle, bool* res)
{
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf, vbuf;
remoteCacheBase.baseKeyMarshall(k, &kbuf);
remoteCacheBase.baseValueMarshall(v, &vbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hrbytes valueBytes(vbuf.getBytes(), vbuf.getLength());
hr_scoped_ptr<ReplaceIfUnmodifiedOperation> op(operationsFactory->newReplaceIfUnmodifiedOperation(keyBytes, valueBytes,life,idle,version));
VersionedOperationResponse response = op->execute();
*res = response.isUpdated();
}
void RemoteCacheImpl::removeWithVersion(RemoteCacheBase& remoteCacheBase, const void* k, uint64_t version, bool* res) {
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<RemoveIfUnmodifiedOperation> gco(operationsFactory->newRemoveIfUnmodifiedOperation(keyBytes, version));
VersionedOperationResponse response = gco->execute();
*res = response.isUpdated();
}
void RemoteCacheImpl::getWithVersion(RemoteCacheBase& remoteCacheBase, const void *k, void* b, VersionedValue* version)
{
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
ScopedBuffer& vbuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<GetWithVersionOperation> gco(operationsFactory->newGetWithVersionOperation(keyBytes));
VersionedValueImpl<hrbytes> m = gco->execute();
m.getValue().releaseTo(vbuf);
version->version = m.version;
}
void RemoteCacheImpl::getWithMetadata(RemoteCacheBase& remoteCacheBase, const void *k, void* b, MetadataValue* metadata)
{
assertRemoteCacheManagerIsStarted();
ScopedBuffer kbuf;
ScopedBuffer& vbuf(*(ScopedBuffer *)b);
remoteCacheBase.baseKeyMarshall(k, &kbuf);
hrbytes keyBytes(kbuf.getBytes(), kbuf.getLength());
hr_scoped_ptr<GetWithMetadataOperation> gco(operationsFactory->newGetWithMetadataOperation(keyBytes));
MetadataValueImpl<hrbytes> m = gco->execute();
m.getValue().releaseTo(vbuf);
metadata->version = m.version;
metadata->created = m.created;
metadata->lifespan = m.lifespan;
metadata->lastUsed = m.lastUsed;
metadata->maxIdle = m.maxIdle;
}
void RemoteCacheImpl::getBulk(RemoteCacheBase& remoteCacheBase, std::map<void*, void*>* mbuf) {
getBulk(remoteCacheBase, 0, mbuf);
}
void RemoteCacheImpl::getBulk(RemoteCacheBase& remoteCacheBase, int size, std::map<void*, void*>* mbuf) {
assertRemoteCacheManagerIsStarted();
hr_scoped_ptr<BulkGetOperation> gco(operationsFactory->newBulkGetOperation(size));
std::map<hrbytes,hrbytes> res = gco->execute();
for(std::map<hrbytes,hrbytes>::iterator i = res.begin(); i != res.end(); i++) {
ScopedBuffer kbuf, vbuf;
i->first.releaseTo(kbuf);
void* k = remoteCacheBase.baseKeyUnmarshall(&kbuf);
i->second.releaseTo(vbuf);
void* v = remoteCacheBase.baseValueUnmarshall(&vbuf);
mbuf->insert(std::make_pair(k,v));
}
}
void RemoteCacheImpl::keySet(RemoteCacheBase& remoteCacheBase, int scope, std::set<void*>* result) {
assertRemoteCacheManagerIsStarted();
hr_scoped_ptr<BulkGetKeysOperation> gco(operationsFactory->newBulkGetKeysOperation(scope));
std::set<hrbytes> res = gco->execute();
for(std::set<hrbytes>::const_iterator i = res.begin(); i != res.end(); i++) {
ScopedBuffer kbuf;
i->releaseTo(kbuf);
void* k = remoteCacheBase.baseKeyUnmarshall(&kbuf);
result->insert(k);
}
}
void RemoteCacheImpl::stats(std::map<std::string,std::string>* statistics) {
assertRemoteCacheManagerIsStarted();
hr_scoped_ptr<StatsOperation> gco(operationsFactory->newStatsOperation());
*statistics = gco->execute();
}
void RemoteCacheImpl::clear() {
assertRemoteCacheManagerIsStarted();
hr_scoped_ptr<ClearOperation> gco(operationsFactory->newClearOperation());
gco->execute();
}
const std::string& RemoteCacheImpl::getName() const {
return name;
}
void RemoteCacheImpl::init(OperationsFactory* of) {
operationsFactory.reset(of);
}
void RemoteCacheImpl::withFlags(Flag flags) {
operationsFactory->setFlags(flags);
}
void RemoteCacheImpl::applyDefaultExpirationFlags(uint64_t lifespan, uint64_t maxIdle) {
if (lifespan == 0) {
operationsFactory->addFlags(DEFAULT_LIFESPAN);
}
if (maxIdle == 0) {
operationsFactory->addFlags(DEFAULT_MAXIDLE);
}
}
void RemoteCacheImpl::assertRemoteCacheManagerIsStarted() {
if (!remoteCacheManager.isStarted()) {
// TODO: log
throw RemoteCacheManagerNotStartedException(
Msg() << "Cannot perform operations on a cache associated with an unstarted RemoteCacheManager. "
<< "Use RemoteCacheManager.start before using the remote cache.");
}
}
}} /* namespace */