Search in sources :

Example 1 with BFunctionPointer

use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.

the class StartForever method execute.

@Override
public void execute(Context context) {
    context.setReturnValues();
    String siddhiApp = context.getStringArgument(0);
    SiddhiAppRuntime siddhiAppRuntime = StreamingRuntimeManager.getInstance().createSiddhiAppRuntime(siddhiApp);
    Set<String> streamIds = siddhiAppRuntime.getStreamDefinitionMap().keySet();
    Map<String, InputHandler> streamSpecificInputHandlerMap = new HashMap<>();
    for (String streamId : streamIds) {
        streamSpecificInputHandlerMap.put(streamId, siddhiAppRuntime.getInputHandler(streamId));
    }
    BRefValueArray inputStreamReferenceArray = (BRefValueArray) context.getRefArgument(0);
    BRefValueArray functionPointerArray = (BRefValueArray) context.getRefArgument(4);
    for (int i = 0; i < inputStreamReferenceArray.size(); i++) {
        BStream stream = (BStream) inputStreamReferenceArray.get(i);
        InputHandler inputHandler = streamSpecificInputHandlerMap.get(stream.getStreamId());
        stream.subscribe(inputHandler);
    }
    for (int i = 0; i < functionPointerArray.size(); i++) {
        BFunctionPointer functionPointer = (BFunctionPointer) functionPointerArray.get(i);
        String functionName = functionPointer.value().getFunctionName();
        String streamId = "stream" + functionName.replaceAll("\\$", "_");
        StreamingRuntimeManager.getInstance().addCallback(streamId, functionPointer, siddhiAppRuntime);
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) HashMap(java.util.HashMap) BStream(org.ballerinalang.model.values.BStream) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 2 with BFunctionPointer

use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.

the class CPU method beginTransaction.

private static void beginTransaction(WorkerExecutionContext ctx, int transactionBlockId, int retryCountRegIndex, int committedFuncIndex, int abortedFuncIndex) {
    // If global tx enabled, it is managed via transaction coordinator. Otherwise it is managed locally without
    // any interaction with the transaction coordinator.
    boolean isGlobalTransactionEnabled = ctx.getGlobalTransactionEnabled();
    // Transaction is attempted three times by default to improve resiliency
    int retryCount = TransactionConstants.DEFAULT_RETRY_COUNT;
    if (retryCountRegIndex != -1) {
        retryCount = (int) ctx.workerLocal.longRegs[retryCountRegIndex];
        if (retryCount < 0) {
            ctx.setError(BLangVMErrors.createError(ctx, BLangExceptionHelper.getErrorMessage(RuntimeErrors.INVALID_RETRY_COUNT)));
            handleError(ctx);
            return;
        }
    }
    // Register committed function handler if exists.
    if (committedFuncIndex != -1) {
        FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[committedFuncIndex];
        BFunctionPointer fpCommitted = new BFunctionPointer(funcRefCPEntry);
        TransactionResourceManager.getInstance().registerCommittedFunction(transactionBlockId, fpCommitted);
    }
    // Register aborted function handler if exists.
    if (abortedFuncIndex != -1) {
        FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[abortedFuncIndex];
        BFunctionPointer fpAborted = new BFunctionPointer(funcRefCPEntry);
        TransactionResourceManager.getInstance().registerAbortedFunction(transactionBlockId, fpAborted);
    }
    LocalTransactionInfo localTransactionInfo = ctx.getLocalTransactionInfo();
    if (localTransactionInfo == null) {
        String globalTransactionId;
        String protocol = null;
        String url = null;
        if (isGlobalTransactionEnabled) {
            BValue[] returns = TransactionUtils.notifyTransactionBegin(ctx, null, null, transactionBlockId, TransactionConstants.DEFAULT_COORDINATION_TYPE);
            BStruct txDataStruct = (BStruct) returns[0];
            globalTransactionId = txDataStruct.getStringField(1);
            protocol = txDataStruct.getStringField(2);
            url = txDataStruct.getStringField(3);
        } else {
            globalTransactionId = UUID.randomUUID().toString().replaceAll("-", "");
        }
        localTransactionInfo = new LocalTransactionInfo(globalTransactionId, url, protocol);
        ctx.setLocalTransactionInfo(localTransactionInfo);
    } else {
        if (isGlobalTransactionEnabled) {
            TransactionUtils.notifyTransactionBegin(ctx, localTransactionInfo.getGlobalTransactionId(), localTransactionInfo.getURL(), transactionBlockId, localTransactionInfo.getProtocol());
        }
    }
    localTransactionInfo.beginTransactionBlock(transactionBlockId, retryCount);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 3 with BFunctionPointer

use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.

the class TransactionResourceManager method invokeCommittedFunction.

private void invokeCommittedFunction(int transactionBlockId) {
    BFunctionPointer fp = committedFuncRegistry.get(transactionBlockId);
    if (fp != null) {
        BValue[] args = {};
        BLangFunctions.invokeCallable(fp.value().getFunctionInfo(), args);
    }
}
Also used : BValue(org.ballerinalang.model.values.BValue) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 4 with BFunctionPointer

use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.

the class BalScheduleAppointment method execute.

public void execute(Context ctx) {
    FunctionRefCPEntry onTriggerFunctionRefCPEntry;
    FunctionRefCPEntry onErrorFunctionRefCPEntry = null;
    if (ctx.getLocalWorkerData().refRegs[0] != null && ctx.getLocalWorkerData().refRegs[0] instanceof BFunctionPointer) {
        onTriggerFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(0)).value();
    } else {
        ctx.setReturnValues(new BString(""), BLangVMErrors.createError(ctx, 0, "The onTrigger function is not provided"));
        return;
    }
    if (ctx.getLocalWorkerData().refRegs[1] != null && ctx.getLocalWorkerData().refRegs[1] instanceof BFunctionPointer) {
        onErrorFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(1)).value();
    }
    String schedule = ctx.getStringArgument(0);
    try {
        Appointment appointment = new Appointment(this, ctx, schedule, onTriggerFunctionRefCPEntry, onErrorFunctionRefCPEntry);
        ctx.setReturnValues(new BString(appointment.getId()));
    } catch (SchedulingException e) {
        ctx.setReturnValues(BLangVMErrors.createError(ctx, 0, e.getMessage()));
    }
}
Also used : FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) SchedulingException(org.ballerinalang.nativeimpl.task.SchedulingException) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 5 with BFunctionPointer

use of org.ballerinalang.model.values.BFunctionPointer in project ballerina by ballerina-lang.

the class BalScheduleTimer method execute.

public void execute(Context ctx) {
    FunctionRefCPEntry onTriggerFunctionRefCPEntry;
    FunctionRefCPEntry onErrorFunctionRefCPEntry = null;
    if (ctx.getLocalWorkerData().refRegs[0] != null && ctx.getLocalWorkerData().refRegs[0] instanceof BFunctionPointer) {
        onTriggerFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(0)).value();
    } else {
        ctx.setReturnValues(new BString(""), BLangVMErrors.createError(ctx, 0, "The onTrigger function is not provided"));
        return;
    }
    if (ctx.getLocalWorkerData().refRegs[1] != null && ctx.getLocalWorkerData().refRegs[1] instanceof BFunctionPointer) {
        onErrorFunctionRefCPEntry = ((BFunctionPointer) ctx.getRefArgument(1)).value();
    }
    BStruct scheduler = (BStruct) ctx.getRefArgument(2);
    long delay = scheduler.getIntField(0);
    long interval = scheduler.getIntField(1);
    try {
        Timer timer = new Timer(this, ctx, delay, interval, onTriggerFunctionRefCPEntry, onErrorFunctionRefCPEntry);
        ctx.setReturnValues(new BString(timer.getId()));
    } catch (SchedulingException e) {
        ctx.setReturnValues(BLangVMErrors.createError(ctx, 0, e.getMessage()));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) BString(org.ballerinalang.model.values.BString) SchedulingException(org.ballerinalang.nativeimpl.task.SchedulingException) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Aggregations

BFunctionPointer (org.ballerinalang.model.values.BFunctionPointer)9 BValue (org.ballerinalang.model.values.BValue)5 BString (org.ballerinalang.model.values.BString)4 FunctionRefCPEntry (org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry)4 BStream (org.ballerinalang.model.values.BStream)3 BStruct (org.ballerinalang.model.values.BStruct)3 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)2 SchedulingException (org.ballerinalang.nativeimpl.task.SchedulingException)2 HashMap (java.util.HashMap)1 StringJoiner (java.util.StringJoiner)1 BMapType (org.ballerinalang.model.types.BMapType)1 BBlobArray (org.ballerinalang.model.values.BBlobArray)1 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)1 BFloatArray (org.ballerinalang.model.values.BFloatArray)1 BIntArray (org.ballerinalang.model.values.BIntArray)1 BJSON (org.ballerinalang.model.values.BJSON)1 BRefType (org.ballerinalang.model.values.BRefType)1 BStringArray (org.ballerinalang.model.values.BStringArray)1 BTable (org.ballerinalang.model.values.BTable)1 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)1