Search in sources :

Example 31 with BinaryRawWriterEx

use of org.apache.ignite.internal.binary.BinaryRawWriterEx 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());
    }
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Example 32 with BinaryRawWriterEx

use of org.apache.ignite.internal.binary.BinaryRawWriterEx 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;
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Example 33 with BinaryRawWriterEx

use of org.apache.ignite.internal.binary.BinaryRawWriterEx 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;
            }
        }
    });
}
Also used : PlatformCallbackGateway(org.apache.ignite.internal.processors.platform.callback.PlatformCallbackGateway) PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Example 34 with BinaryRawWriterEx

use of org.apache.ignite.internal.binary.BinaryRawWriterEx in project ignite by apache.

the class PlatformUtils method applyContinuousQueryEvents.

/**
 * Apply continuous query events to listener.
 *
 * @param ctx Context.
 * @param lsnrPtr Listener pointer.
 * @param evts Events.
 * @throws javax.cache.event.CacheEntryListenerException In case of failure.
 */
public static void applyContinuousQueryEvents(PlatformContext ctx, long lsnrPtr, Iterable<CacheEntryEvent> evts) throws CacheEntryListenerException {
    assert lsnrPtr != 0;
    assert evts != null;
    try (PlatformMemory mem = ctx.memory().allocate()) {
        PlatformOutputStream out = mem.output();
        BinaryRawWriterEx writer = ctx.writer(out);
        writer.writeLong(lsnrPtr);
        int cntPos = writer.reserveInt();
        int cnt = 0;
        for (CacheEntryEvent evt : evts) {
            writeCacheEntryEvent(writer, evt);
            cnt++;
        }
        writer.writeInt(cntPos, cnt);
        out.synchronize();
        ctx.gateway().continuousQueryListenerApply(mem.pointer());
    } catch (Exception e) {
        throw toCacheEntryListenerException(e);
    }
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) PlatformExtendedException(org.apache.ignite.internal.processors.platform.PlatformExtendedException) CacheException(javax.cache.CacheException) PlatformNativeException(org.apache.ignite.internal.processors.platform.PlatformNativeException)

Example 35 with BinaryRawWriterEx

use of org.apache.ignite.internal.binary.BinaryRawWriterEx in project ignite by apache.

the class PlatformAbstractJob method createJob.

/**
 * Create job in native platform if needed.
 *
 * @param ctx Context.
 * @return {@code True} if job was created, {@code false} if this is local job and creation is not necessary.
 * @throws org.apache.ignite.IgniteCheckedException If failed.
 */
protected boolean createJob(PlatformContext ctx) throws IgniteCheckedException {
    if (ptr == 0) {
        try (PlatformMemory mem = ctx.memory().allocate()) {
            PlatformOutputStream out = mem.output();
            BinaryRawWriterEx writer = ctx.writer(out);
            writer.writeObject(job);
            out.synchronize();
            ptr = ctx.gateway().computeJobCreate(mem.pointer());
        }
        return true;
    } else
        return false;
}
Also used : PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Aggregations

BinaryRawWriterEx (org.apache.ignite.internal.binary.BinaryRawWriterEx)50 PlatformOutputStream (org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream)43 PlatformMemory (org.apache.ignite.internal.processors.platform.memory.PlatformMemory)42 BinaryRawReaderEx (org.apache.ignite.internal.binary.BinaryRawReaderEx)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IgniteException (org.apache.ignite.IgniteException)8 PlatformInputStream (org.apache.ignite.internal.processors.platform.memory.PlatformInputStream)5 PlatformContext (org.apache.ignite.internal.processors.platform.PlatformContext)3 PlatformNativeException (org.apache.ignite.internal.processors.platform.PlatformNativeException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)2 CacheLoaderException (javax.cache.integration.CacheLoaderException)2 PlatformExtendedException (org.apache.ignite.internal.processors.platform.PlatformExtendedException)2 PlatformTargetProxyImpl (org.apache.ignite.internal.processors.platform.PlatformTargetProxyImpl)2 PlatformFutureUtils (org.apache.ignite.internal.processors.platform.utils.PlatformFutureUtils)2 IgniteFuture (org.apache.ignite.lang.IgniteFuture)2 Nullable (org.jetbrains.annotations.Nullable)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1