use of org.apache.ignite.internal.processors.platform.memory.PlatformMemory in project ignite by apache.
the class PlatformFutureUtils method listen.
/**
* Listen future.
*
* @param ctx Context.
* @param listenable Listenable entry.
* @param futPtr Native future pointer.
* @param typ Expected return type.
* @param writer Optional writer.
*/
@SuppressWarnings("unchecked")
public static void listen(final PlatformContext ctx, PlatformListenable listenable, final long futPtr, final int typ, @Nullable final Writer writer, final PlatformTarget target) {
final PlatformCallbackGateway gate = ctx.gateway();
listenable.listen(new IgniteBiInClosure<Object, Throwable>() {
private static final long serialVersionUID = 0L;
@Override
public void apply(Object res, Throwable err) {
if (err instanceof Exception)
err = target.convertException((Exception) err);
if (writer != null && writeToWriter(res, err, ctx, writer, futPtr))
return;
if (err != null) {
writeFutureError(ctx, futPtr, err);
return;
}
try {
if (typ == TYP_OBJ) {
if (res == null)
gate.futureNullResult(futPtr);
else {
try (PlatformMemory mem = ctx.memory().allocate()) {
PlatformOutputStream out = mem.output();
BinaryRawWriterEx outWriter = ctx.writer(out);
outWriter.writeObjectDetached(res);
out.synchronize();
gate.futureObjectResult(futPtr, mem.pointer());
}
}
} else if (res == null)
gate.futureNullResult(futPtr);
else {
switch(typ) {
case TYP_BYTE:
gate.futureByteResult(futPtr, (byte) res);
break;
case TYP_BOOL:
gate.futureBoolResult(futPtr, (boolean) res ? 1 : 0);
break;
case TYP_SHORT:
gate.futureShortResult(futPtr, (short) res);
break;
case TYP_CHAR:
gate.futureCharResult(futPtr, (char) res);
break;
case TYP_INT:
gate.futureIntResult(futPtr, (int) res);
break;
case TYP_FLOAT:
gate.futureFloatResult(futPtr, Float.floatToIntBits((float) res));
break;
case TYP_LONG:
gate.futureLongResult(futPtr, (long) res);
break;
case TYP_DOUBLE:
gate.futureDoubleResult(futPtr, Double.doubleToLongBits((double) res));
break;
default:
assert false : "Should not reach this: " + typ;
}
}
} catch (Throwable t) {
writeFutureError(ctx, futPtr, t);
if (t instanceof Error)
throw t;
}
}
});
}
use of org.apache.ignite.internal.processors.platform.memory.PlatformMemory in project ignite by apache.
the class PlatformFutureUtils method writeFutureError.
/**
* Write future error.
*
* @param ctx Context.
* @param futPtr Future pointer.
* @param err Error.
*/
private static void writeFutureError(final PlatformContext ctx, long futPtr, Throwable err) {
try (PlatformMemory mem = ctx.memory().allocate()) {
PlatformOutputStream out = mem.output();
BinaryRawWriterEx outWriter = ctx.writer(out);
PlatformUtils.writeError(err, outWriter);
PlatformUtils.writeErrorData(err, outWriter);
out.synchronize();
ctx.gateway().futureError(futPtr, mem.pointer());
}
}
use of org.apache.ignite.internal.processors.platform.memory.PlatformMemory in project ignite by apache.
the class PlatformFutureUtils method writeToWriter.
/**
* Write result to a custom writer
*
* @param obj Object to write.
* @param err Error to write.
* @param ctx Context.
* @param writer Writer.
* @param futPtr Future pointer.
* @return Value indicating whether custom write was performed. When false, default write will be used.
*/
private static boolean writeToWriter(Object obj, Throwable err, PlatformContext ctx, Writer writer, long futPtr) {
boolean canWrite = writer.canWrite(obj, err);
if (!canWrite)
return false;
try (PlatformMemory mem = ctx.memory().allocate()) {
PlatformOutputStream out = mem.output();
BinaryRawWriterEx outWriter = ctx.writer(out);
writer.write(outWriter, obj, err);
out.synchronize();
ctx.gateway().futureObjectResult(futPtr, mem.pointer());
}
return true;
}
use of org.apache.ignite.internal.processors.platform.memory.PlatformMemory in project ignite by apache.
the class PlatformUtils method evaluateContinuousQueryEvent.
/**
* Evaluate the filter.
*
* @param ctx Context.
* @param filterPtr Native filter pointer.
* @param evt Event.
* @return Result.
* @throws CacheEntryListenerException In case of failure.
*/
public static boolean evaluateContinuousQueryEvent(PlatformContext ctx, long filterPtr, CacheEntryEvent evt) throws CacheEntryListenerException {
assert filterPtr != 0;
try (PlatformMemory mem = ctx.memory().allocate()) {
PlatformOutputStream out = mem.output();
out.writeLong(filterPtr);
writeCacheEntryEvent(ctx.writer(out), evt);
out.synchronize();
return ctx.gateway().continuousQueryFilterApply(mem.pointer()) == 1;
} catch (Exception e) {
throw toCacheEntryListenerException(e);
}
}
Aggregations