-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PlatformAbstractTarget.java
347 lines (295 loc) · 10.6 KB
/
PlatformAbstractTarget.java
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.platform;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.binary.BinaryRawReaderEx;
import org.apache.ignite.internal.binary.BinaryRawWriterEx;
import org.apache.ignite.internal.processors.platform.memory.PlatformMemory;
import org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream;
import org.apache.ignite.internal.processors.platform.utils.PlatformFutureUtils;
import org.apache.ignite.internal.processors.platform.utils.PlatformListenable;
import org.jetbrains.annotations.Nullable;
/**
* Abstract interop target.
*/
public abstract class PlatformAbstractTarget implements PlatformTarget {
/** Constant: TRUE.*/
protected static final int TRUE = 1;
/** Constant: FALSE. */
protected static final int FALSE = 0;
/** */
private static final int OP_META = -1;
/** Context. */
protected final PlatformContext platformCtx;
/** Logger. */
protected final IgniteLogger log;
/**
* Constructor.
*
* @param platformCtx Context.
*/
protected PlatformAbstractTarget(PlatformContext platformCtx) {
this.platformCtx = platformCtx;
log = platformCtx.kernalContext().log(PlatformAbstractTarget.class);
}
/** {@inheritDoc} */
@Override public long inLongOutLong(int type, long val) throws Exception {
try {
return processInLongOutLong(type, val);
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public long inStreamOutLong(int type, long memPtr) throws Exception {
try (PlatformMemory mem = platformCtx.memory().get(memPtr)) {
BinaryRawReaderEx reader = platformCtx.reader(mem);
if (type == OP_META) {
platformCtx.processMetadata(reader);
return TRUE;
}
else
return processInStreamOutLong(type, reader);
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public Object inStreamOutObject(int type, long memPtr) throws Exception {
try (PlatformMemory mem = memPtr != 0 ? platformCtx.memory().get(memPtr) : null) {
BinaryRawReaderEx reader = mem != null ? platformCtx.reader(mem) : null;
return processInStreamOutObject(type, reader);
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public void outStream(int type, long memPtr) throws Exception {
try (PlatformMemory mem = platformCtx.memory().get(memPtr)) {
PlatformOutputStream out = mem.output();
BinaryRawWriterEx writer = platformCtx.writer(out);
processOutStream(type, writer);
out.synchronize();
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public Object outObject(int type) throws Exception {
try {
return processOutObject(type);
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public void inStreamOutStream(int type, long inMemPtr, long outMemPtr) throws Exception {
try (PlatformMemory inMem = platformCtx.memory().get(inMemPtr)) {
BinaryRawReaderEx reader = platformCtx.reader(inMem);
try (PlatformMemory outMem = platformCtx.memory().get(outMemPtr)) {
PlatformOutputStream out = outMem.output();
BinaryRawWriterEx writer = platformCtx.writer(out);
processInStreamOutStream(type, reader, writer);
out.synchronize();
}
}
catch (Exception e) {
throw convertException(e);
}
}
/** {@inheritDoc} */
@Override public Object inObjectStreamOutObjectStream(int type, Object arg, long inMemPtr, long outMemPtr)
throws Exception {
PlatformMemory inMem = null;
PlatformMemory outMem = null;
try {
BinaryRawReaderEx reader = null;
if (inMemPtr != 0) {
inMem = platformCtx.memory().get(inMemPtr);
reader = platformCtx.reader(inMem);
}
PlatformOutputStream out = null;
BinaryRawWriterEx writer = null;
if (outMemPtr != 0) {
outMem = platformCtx.memory().get(outMemPtr);
out = outMem.output();
writer = platformCtx.writer(out);
}
Object res = processInObjectStreamOutObjectStream(type, arg, reader, writer);
if (out != null)
out.synchronize();
return res;
}
catch (Exception e) {
throw convertException(e);
}
finally {
try {
if (inMem != null)
inMem.close();
}
finally {
if (outMem != null)
outMem.close();
}
}
}
/**
* Convert caught exception.
*
* @param e Exception to convert.
* @return Converted exception.
*/
public Exception convertException(Exception e) {
return e;
}
/**
* @return Context.
*/
public PlatformContext platformContext() {
return platformCtx;
}
/** {@inheritDoc} */
@Override public void listenFuture(final long futId, int typ) throws Exception {
listenFutureAndGet(futId, typ);
}
/** {@inheritDoc} */
@Override public void listenFutureForOperation(final long futId, int typ, int opId) throws Exception {
listenFutureForOperationAndGet(futId, typ, opId);
}
/** {@inheritDoc} */
@Override public PlatformListenable listenFutureAndGet(final long futId, int typ) throws Exception {
return PlatformFutureUtils.listen(platformCtx, currentFuture(), futId, typ, null, this);
}
/** {@inheritDoc} */
@Override public PlatformListenable listenFutureForOperationAndGet(final long futId, int typ, int opId)
throws Exception {
return PlatformFutureUtils.listen(platformCtx, currentFuture(), futId, typ, futureWriter(opId), this);
}
/**
* When overridden in a derived class, gets future for the current operation.
*
* @return current future.
* @throws IgniteCheckedException If failed.
*/
protected IgniteInternalFuture currentFuture() throws IgniteCheckedException {
throw new IgniteCheckedException("Future listening is not supported in " + getClass());
}
/**
* When overridden in a derived class, gets a custom future writer.
*
* @param opId Operation id.
* @return A custom writer for given op id.
*/
@Nullable protected PlatformFutureUtils.Writer futureWriter(int opId){
return null;
}
/**
* Process IN operation.
*
* @param type Type.
* @param val Value.
* @return Result.
* @throws IgniteCheckedException In case of exception.
*/
protected long processInLongOutLong(int type, long val) throws IgniteCheckedException {
return throwUnsupported(type);
}
/**
* Process IN operation.
*
* @param type Type.
* @param reader Binary reader.
* @return Result.
* @throws IgniteCheckedException In case of exception.
*/
protected long processInStreamOutLong(int type, BinaryRawReaderEx reader) throws IgniteCheckedException {
return throwUnsupported(type);
}
/**
* Process IN-OUT operation.
*
* @param type Type.
* @param reader Binary reader.
* @param writer Binary writer.
* @throws IgniteCheckedException In case of exception.
*/
protected void processInStreamOutStream(int type, BinaryRawReaderEx reader, BinaryRawWriterEx writer)
throws IgniteCheckedException {
throwUnsupported(type);
}
/**
* Process IN operation with managed object as result.
*
* @param type Type.
* @param reader Binary reader.
* @return Result.
* @throws IgniteCheckedException In case of exception.
*/
protected Object processInStreamOutObject(int type, BinaryRawReaderEx reader) throws IgniteCheckedException {
return throwUnsupported(type);
}
/**
* Process IN-OUT operation.
*
* @param type Type.
* @param arg Argument.
* @param reader Binary reader.
* @param writer Binary writer.
* @throws IgniteCheckedException In case of exception.
*/
protected Object processInObjectStreamOutObjectStream(int type, @Nullable Object arg, BinaryRawReaderEx reader,
BinaryRawWriterEx writer) throws IgniteCheckedException {
return throwUnsupported(type);
}
/**
* Process OUT operation.
*
* @param type Type.
* @param writer Binary writer.
* @throws IgniteCheckedException In case of exception.
*/
protected void processOutStream(int type, BinaryRawWriterEx writer) throws IgniteCheckedException {
throwUnsupported(type);
}
/**
* Process OUT operation.
*
* @param type Type.
* @throws IgniteCheckedException In case of exception.
*/
protected Object processOutObject(int type) throws IgniteCheckedException {
return throwUnsupported(type);
}
/**
* Throw an exception rendering unsupported operation type.
*
* @param type Operation type.
* @return Dummy value which is never returned.
* @throws IgniteCheckedException Exception to be thrown.
*/
protected <T> T throwUnsupported(int type) throws IgniteCheckedException {
throw new IgniteCheckedException("Unsupported operation type: " + type);
}
}