Search in sources :

Example 41 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class BLangVMUtils method populateWorkerResultWithValues.

@SuppressWarnings("rawtypes")
public static void populateWorkerResultWithValues(WorkerData result, BValue[] vals, BType[] types) {
    if (vals == null) {
        return;
    }
    int longRegCount = 0;
    int doubleRegCount = 0;
    int stringRegCount = 0;
    int intRegCount = 0;
    int refRegCount = 0;
    int byteRegCount = 0;
    for (int i = 0; i < vals.length; i++) {
        BType retType = types[i];
        switch(retType.getTag()) {
            case TypeTags.INT_TAG:
                if (vals[i] == null) {
                    result.longRegs[longRegCount++] = 0;
                    break;
                }
                result.longRegs[longRegCount++] = ((BInteger) vals[i]).intValue();
                break;
            case TypeTags.FLOAT_TAG:
                if (vals[i] == null) {
                    result.doubleRegs[doubleRegCount++] = 0;
                    break;
                }
                result.doubleRegs[doubleRegCount++] = ((BFloat) vals[i]).floatValue();
                break;
            case TypeTags.STRING_TAG:
                if (vals[i] == null) {
                    result.stringRegs[stringRegCount++] = BLangConstants.STRING_NULL_VALUE;
                    break;
                }
                result.stringRegs[stringRegCount++] = vals[i].stringValue();
                break;
            case TypeTags.BOOLEAN_TAG:
                if (vals[i] == null) {
                    result.intRegs[intRegCount++] = 0;
                    break;
                }
                result.intRegs[intRegCount++] = ((BBoolean) vals[i]).booleanValue() ? 1 : 0;
                break;
            case TypeTags.BLOB_TAG:
                if (vals[i] == null) {
                    result.byteRegs[byteRegCount++] = new byte[0];
                    break;
                }
                result.byteRegs[byteRegCount++] = ((BBlob) vals[i]).blobValue();
                break;
            default:
                result.refRegs[refRegCount++] = (BRefType) vals[i];
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean)

Example 42 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class GetCRC32 method execute.

@Override
public void execute(Context context) {
    BValue entityBody = context.getRefArgument(0);
    Checksum checksum = new CRC32();
    byte[] bytes;
    long checksumVal;
    BType argType = entityBody.getType();
    if (argType == BTypes.typeJSON || argType == BTypes.typeXML || argType == BTypes.typeString) {
        // TODO: Look at the possibility of making the encoding configurable
        bytes = entityBody.stringValue().getBytes(StandardCharsets.UTF_8);
    } else if (argType == BTypes.typeBlob) {
        bytes = ((BBlob) entityBody).blobValue();
    } else {
        throw new BallerinaException("failed to generate hash: unsupported data type: " + entityBody.getType().getName());
    }
    checksum.update(bytes, 0, bytes.length);
    checksumVal = checksum.getValue();
    context.setReturnValues(new BString(Long.toHexString(checksumVal)));
}
Also used : CRC32(java.util.zip.CRC32) BValue(org.ballerinalang.model.values.BValue) Checksum(java.util.zip.Checksum) BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

Example 43 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class CPU method handleVariableUnlock.

private static void handleVariableUnlock(WorkerExecutionContext ctx, BType[] types, int[] varRegs) {
    for (int i = varRegs.length - 1; i > -1; i--) {
        BType paramType = types[i];
        int regIndex = varRegs[i];
        switch(paramType.getTag()) {
            case TypeTags.INT_TAG:
                ctx.programFile.getGlobalMemoryBlock().unlockIntField(regIndex);
                break;
            case TypeTags.FLOAT_TAG:
                ctx.programFile.getGlobalMemoryBlock().unlockFloatField(regIndex);
                break;
            case TypeTags.STRING_TAG:
                ctx.programFile.getGlobalMemoryBlock().unlockStringField(regIndex);
                break;
            case TypeTags.BOOLEAN_TAG:
                ctx.programFile.getGlobalMemoryBlock().unlockBooleanField(regIndex);
                break;
            case TypeTags.BLOB_TAG:
                ctx.programFile.getGlobalMemoryBlock().unlockBlobField(regIndex);
                break;
            default:
                ctx.programFile.getGlobalMemoryBlock().unlockRefField(regIndex);
        }
    }
}
Also used : BType(org.ballerinalang.model.types.BType)

Example 44 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class StreamingRuntimeManager method addCallback.

public void addCallback(String streamId, BFunctionPointer functionPointer, SiddhiAppRuntime siddhiAppRuntime) {
    BType[] parameters = functionPointer.value().getFunctionInfo().getParamTypes();
    BStructType structType = (BStructType) ((BArrayType) parameters[0]).getElementType();
    if (!(parameters[0] instanceof BArrayType)) {
        throw new BallerinaException("incompatible function: inline function needs to be a function accepting" + " a struct array");
    }
    siddhiAppRuntime.addCallback(streamId, new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            for (Event event : events) {
                AtomicInteger intVarIndex = new AtomicInteger(-1);
                AtomicInteger floatVarIndex = new AtomicInteger(-1);
                AtomicInteger boolVarIndex = new AtomicInteger(-1);
                AtomicInteger stringVarIndex = new AtomicInteger(-1);
                BStruct output = new BStruct(structType);
                for (Object field : event.getData()) {
                    if (field instanceof Long) {
                        output.setIntField(intVarIndex.incrementAndGet(), (Long) field);
                    } else if (field instanceof Double) {
                        output.setFloatField(floatVarIndex.incrementAndGet(), (Double) field);
                    } else if (field instanceof Boolean) {
                        output.setBooleanField(boolVarIndex.incrementAndGet(), (Integer) field);
                    } else if (field instanceof String) {
                        output.setStringField(stringVarIndex.incrementAndGet(), (String) field);
                    }
                }
                BValue[] args = { output };
                BLangFunctions.invokeCallable(functionPointer.value().getFunctionInfo(), args);
            }
        }
    });
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BArrayType(org.ballerinalang.model.types.BArrayType) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) BStructType(org.ballerinalang.model.types.BStructType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BType(org.ballerinalang.model.types.BType) Event(org.ballerinalang.siddhi.core.event.Event) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 45 with BType

use of org.ballerinalang.model.types.BType in project ballerina by ballerina-lang.

the class TableJSONDataSource method getStructData.

private static JsonNode getStructData(Object[] data, BStructType.StructField[] structFields, int index) {
    JsonNode jsonData = null;
    try {
        if (structFields == null) {
            jsonData = new JsonNode(Type.ARRAY);
            if (data != null) {
                for (Object value : data) {
                    if (value instanceof String) {
                        jsonData.add((String) value);
                    } else if (value instanceof Boolean) {
                        jsonData.add((Boolean) value);
                    } else if (value instanceof Long) {
                        jsonData.add((long) value);
                    } else if (value instanceof Double) {
                        jsonData.add((double) value);
                    } else if (value instanceof Integer) {
                        jsonData.add((int) value);
                    } else if (value instanceof Float) {
                        jsonData.add((float) value);
                    } else if (value instanceof BigDecimal) {
                        jsonData.add(((BigDecimal) value).doubleValue());
                    }
                }
            }
        } else {
            jsonData = new JsonNode(Type.OBJECT);
            boolean structError = true;
            if (data != null) {
                int i = 0;
                for (Object value : data) {
                    BType internaltType = structFields[index - 1].fieldType;
                    if (internaltType.getTag() == TypeTags.STRUCT_TAG) {
                        BStructType.StructField[] interanlStructFields = ((BStructType) internaltType).getStructFields();
                        if (interanlStructFields != null) {
                            if (value instanceof String) {
                                jsonData.set(interanlStructFields[i].fieldName, (String) value);
                            } else if (value instanceof Boolean) {
                                jsonData.set(interanlStructFields[i].fieldName, (Boolean) value);
                            } else if (value instanceof Long) {
                                jsonData.set(interanlStructFields[i].fieldName, (long) value);
                            } else if (value instanceof Double) {
                                jsonData.set(interanlStructFields[i].fieldName, (double) value);
                            } else if (value instanceof Integer) {
                                jsonData.set(interanlStructFields[i].fieldName, (int) value);
                            } else if (value instanceof Float) {
                                jsonData.set(interanlStructFields[i].fieldName, (float) value);
                            } else if (value instanceof BigDecimal) {
                                jsonData.set(interanlStructFields[i].fieldName, ((BigDecimal) value).doubleValue());
                            } else if (value instanceof Struct) {
                                jsonData.set(interanlStructFields[i].fieldName, getStructData(((Struct) value).getAttributes(), interanlStructFields, i + 1));
                            }
                            structError = false;
                        }
                    }
                    ++i;
                }
            }
            if (structError) {
                throw new BallerinaException("error in constructing the json object from struct type data");
            }
        }
    } catch (SQLException e) {
        throw new BallerinaException("error in retrieving struct data to construct the inner json object:" + e.getMessage());
    }
    return jsonData;
}
Also used : SQLException(java.sql.SQLException) JsonNode(org.ballerinalang.model.util.JsonNode) BigDecimal(java.math.BigDecimal) Struct(java.sql.Struct) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BType (org.ballerinalang.model.types.BType)48 BStructType (org.ballerinalang.model.types.BStructType)16 BString (org.ballerinalang.model.values.BString)16 BStruct (org.ballerinalang.model.values.BStruct)15 BValue (org.ballerinalang.model.values.BValue)13 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)12 BBoolean (org.ballerinalang.model.values.BBoolean)11 BArrayType (org.ballerinalang.model.types.BArrayType)7 BFloat (org.ballerinalang.model.values.BFloat)6 BInteger (org.ballerinalang.model.values.BInteger)6 BMapType (org.ballerinalang.model.types.BMapType)4 BRefType (org.ballerinalang.model.values.BRefType)4 SQLException (java.sql.SQLException)3 Struct (java.sql.Struct)3 ArrayList (java.util.ArrayList)3 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)3 BBlob (org.ballerinalang.model.values.BBlob)3 Message (org.ballerinalang.net.grpc.Message)3 StructFieldInfo (org.ballerinalang.util.codegen.StructFieldInfo)3 StructInfo (org.ballerinalang.util.codegen.StructInfo)3