Search in sources :

Example 1 with BRefValueArray

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

the class BatchUpdate method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeBatchUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 2 with BRefValueArray

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

the class Update method execute.

@Override
public void execute(Context context) {
    try {
        BStruct bConnector = (BStruct) context.getRefArgument(0);
        String query = context.getStringArgument(0);
        BRefValueArray parameters = (BRefValueArray) context.getNullableRefArgument(1);
        SQLDatasource datasource = (SQLDatasource) bConnector.getNativeData(Constants.CLIENT_CONNECTOR);
        Map<String, String> tags = new HashMap<>();
        tags.put(TAG_KEY_DB_STATEMENT, query);
        tags.put(TAG_KEY_DB_TYPE, TAG_DB_TYPE_SQL);
        TraceUtil.getTracer(context.getParentWorkerExecutionContext()).addTags(tags);
        executeUpdate(context, datasource, query, parameters);
    } catch (Throwable e) {
        context.setReturnValues(SQLDatasourceUtils.getSQLConnectorError(context, e));
        SQLDatasourceUtils.handleErrorOnTransaction(context);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HashMap(java.util.HashMap) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) SQLDatasource(org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)

Example 3 with BRefValueArray

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

the class BTestRunner method extractArguments.

/**
 * Extract function arguments from the values sets.
 * @param valueSets user provided value sets
 * @return a list of function arguments
 */
private List<BValue[]> extractArguments(BValue[] valueSets) {
    List<BValue[]> argsList = new ArrayList<>();
    for (BValue value : valueSets) {
        if (value instanceof BRefValueArray) {
            BRefValueArray array = (BRefValueArray) value;
            for (BIterator it = array.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BNewArray) {
                    BNewArray bNewArray = (BNewArray) vals[1];
                    BValue[] args = new BValue[(int) bNewArray.size()];
                    for (int j = 0; j < bNewArray.size(); j++) {
                        args[j] = bNewArray.getBValue(j);
                    }
                    argsList.add(args);
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else if (value instanceof BJSON) {
            BJSON jsonArrayOfArrays = (BJSON) value;
            for (BIterator it = jsonArrayOfArrays.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BJSON) {
                    List<BValue> args = new ArrayList<>();
                    BJSON jsonArray = (BJSON) vals[1];
                    for (BIterator it2 = jsonArray.newIterator(); it2.hasNext(); ) {
                        BValue[] vals2 = it2.getNext(0);
                        args.add(vals2[1]);
                    }
                    argsList.add(args.toArray(new BValue[0]));
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else {
            argsList.add(new BValue[] { value });
        }
    }
    return argsList;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) ArrayList(java.util.ArrayList) List(java.util.List) BIterator(org.ballerinalang.model.values.BIterator) BJSON(org.ballerinalang.model.values.BJSON)

Example 4 with BRefValueArray

use of org.ballerinalang.model.values.BRefValueArray 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 5 with BRefValueArray

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

the class VectorNegativeTest method testRemoveIndexOutOfRange.

@Test(description = "Test case for testing removal of elements from outside of the bounds of a vector.")
public void testRemoveIndexOutOfRange() {
    long vectorSize = 10;
    long[] indices = new long[] { 0, vectorSize - 1, -1 };
    long[] expectedFinalValues = new long[] { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
    String[] expectedErrMsgs = new String[] { "Index out of range: 9", "Index out of range: -1" };
    BValue[] returns = BRunUtil.invoke(compileResult, "testRemoveIndexOutOfRange", new BValue[] { buildIntArray(indices), new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, expectedFinalValues.length);
    for (int i = 0; i < finalVectorSize; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), expectedFinalValues[i]);
    }
    BRefValueArray removedVals = (BRefValueArray) returns[1];
    // only 1 valid index
    Assert.assertEquals(removedVals.size(), 1);
    Assert.assertEquals(((BInteger) removedVals.get(0)).intValue(), 10);
    BRefValueArray errors = (BRefValueArray) returns[2];
    Assert.assertEquals(errors.size(), expectedErrMsgs.length);
    for (int i = 0; i < expectedErrMsgs.length; i++) {
        Assert.assertTrue(errors.get(i).stringValue().contains(expectedErrMsgs[i]));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3