Search in sources :

Example 71 with MutableBoolean

use of org.apache.commons.lang3.mutable.MutableBoolean in project asterixdb by apache.

the class Value method convertValueToIntegerValue.

public static boolean convertValueToIntegerValue(Value value, Value integerValue, ClassAdObjectPool objectPool) throws HyracksDataException {
    boolean could_convert;
    AMutableCharArrayString buf = objectPool.strPool.get();
    char end;
    AMutableInt64 ivalue = objectPool.int64Pool.get();
    AMutableDouble rtvalue = objectPool.doublePool.get();
    ClassAdTime atvalue = objectPool.classAdTimePool.get();
    MutableBoolean bvalue = objectPool.boolPool.get();
    NumberFactor nf;
    switch(value.getType()) {
        case UNDEFINED_VALUE:
            integerValue.setUndefinedValue();
            could_convert = false;
            break;
        case ERROR_VALUE:
        case CLASSAD_VALUE:
        case LIST_VALUE:
        case SLIST_VALUE:
            integerValue.setErrorValue();
            could_convert = false;
            break;
        case STRING_VALUE:
            could_convert = true;
            value.isStringValue(buf);
            int endIndex = buf.firstNonDigitChar();
            if (endIndex < 0) {
                // no non digit
                ivalue.setValue(Long.parseLong(buf.toString()));
                nf = NumberFactor.NO_FACTOR;
                break;
            } else {
                ivalue.setValue(Long.parseLong(buf.substr(0, endIndex)));
                end = buf.charAt(endIndex);
                switch(Character.toUpperCase(end)) {
                    case 'B':
                        nf = NumberFactor.B_FACTOR;
                        break;
                    case 'K':
                        nf = NumberFactor.K_FACTOR;
                        break;
                    case 'M':
                        nf = NumberFactor.M_FACTOR;
                        break;
                    case 'G':
                        nf = NumberFactor.G_FACTOR;
                        break;
                    case 'T':
                        nf = NumberFactor.T_FACTOR;
                        break;
                    case '\0':
                        nf = NumberFactor.NO_FACTOR;
                        break;
                    default:
                        nf = NumberFactor.NO_FACTOR;
                        break;
                }
                if (could_convert) {
                    integerValue.setIntegerValue((long) ((ivalue.getLongValue() * Value.ScaleFactor[nf.ordinal()])));
                }
            }
            break;
        case BOOLEAN_VALUE:
            value.isBooleanValue(bvalue);
            integerValue.setIntegerValue(bvalue.booleanValue() ? 1 : 0);
            could_convert = true;
            break;
        case INTEGER_VALUE:
            integerValue.setValue(value);
            could_convert = true;
            break;
        case REAL_VALUE:
            value.isRealValue(rtvalue);
            integerValue.setIntegerValue((long) rtvalue.getDoubleValue());
            could_convert = true;
            break;
        case ABSOLUTE_TIME_VALUE:
            value.isAbsoluteTimeValue(atvalue);
            integerValue.setIntegerValue(atvalue.getTimeInMillis() / 1000L);
            could_convert = true;
            break;
        case RELATIVE_TIME_VALUE:
            value.isRelativeTimeValue(atvalue);
            integerValue.setIntegerValue((atvalue.getTime() / 1000L));
            could_convert = true;
            break;
        default:
            // Make gcc's -Wuninitalized happy
            could_convert = false;
            throw new HyracksDataException("Should not reach here");
    }
    return could_convert;
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 72 with MutableBoolean

use of org.apache.commons.lang3.mutable.MutableBoolean in project asterixdb by apache.

the class Operation method doArithmetic.

public static int doArithmetic(int op, Value v1, Value v2, Value result, ClassAdObjectPool objectPool) throws HyracksDataException {
    AMutableInt64 i1 = objectPool.int64Pool.get();
    AMutableInt64 i2 = objectPool.int64Pool.get();
    ClassAdTime t1 = objectPool.classAdTimePool.get();
    AMutableDouble r1 = objectPool.doublePool.get();
    MutableBoolean b1 = objectPool.boolPool.get();
    // ensure the operands have arithmetic types
    if ((!v1.isIntegerValue() && !v1.isRealValue() && !v1.isAbsoluteTimeValue() && !v1.isRelativeTimeValue() && !v1.isBooleanValue()) || (op != OpKind_UNARY_MINUS_OP && !v2.isBooleanValue() && !v2.isIntegerValue() && !v2.isRealValue() && !v2.isAbsoluteTimeValue() && !v2.isRelativeTimeValue())) {
        result.setErrorValue();
        return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
    }
    // take care of the unary arithmetic operators
    if (op == OpKind_UNARY_MINUS_OP) {
        if (v1.isIntegerValue(i1)) {
            result.setIntegerValue((-1L) * i1.getLongValue());
            return SigValues.SIG_CHLD1.ordinal();
        } else if (v1.isRealValue(r1)) {
            result.setRealValue((-1) * r1.getDoubleValue());
            return SigValues.SIG_CHLD1.ordinal();
        } else if (v1.isRelativeTimeValue(t1)) {
            t1.setValue((-1) * t1.getTimeInMillis());
            result.setRelativeTimeValue(t1);
            return (SigValues.SIG_CHLD1.ordinal());
        } else if (v1.isBooleanValue(b1)) {
            result.setBooleanValue(!b1.booleanValue());
        } else if (v1.isExceptional()) {
            // undefined or error --- same as operand
            result.setValue(v1);
            return SigValues.SIG_CHLD1.ordinal();
        }
        // unary minus not defined on any other operand type
        result.setErrorValue();
        return (SigValues.SIG_CHLD1.ordinal());
    }
    // perform type promotions and proceed with arithmetic
    switch(coerceToNumber(v1, v2, objectPool)) {
        case INTEGER_VALUE:
            v1.isIntegerValue(i1);
            v2.isIntegerValue(i2);
            switch(op) {
                case OpKind_ADDITION_OP:
                    result.setIntegerValue(i1.getLongValue() + i2.getLongValue());
                    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
                case OpKind_SUBTRACTION_OP:
                    result.setIntegerValue(i1.getLongValue() - i2.getLongValue());
                    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
                case OpKind_MULTIPLICATION_OP:
                    result.setIntegerValue(i1.getLongValue() * i2.getLongValue());
                    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
                case OpKind_DIVISION_OP:
                    if (i2.getLongValue() != 0L) {
                        result.setIntegerValue(i1.getLongValue() / i2.getLongValue());
                    } else {
                        result.setErrorValue();
                    }
                    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
                case OpKind_MODULUS_OP:
                    if (i2.getLongValue() != 0) {
                        result.setIntegerValue(i1.getLongValue() % i2.getLongValue());
                    } else {
                        result.setErrorValue();
                    }
                    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
                default:
                    // should not reach here
                    throw new HyracksDataException("Should not get here");
            }
        case REAL_VALUE:
            {
                return (doRealArithmetic(op, v1, v2, result, objectPool));
            }
        case ABSOLUTE_TIME_VALUE:
        case RELATIVE_TIME_VALUE:
            {
                return (doTimeArithmetic(op, v1, v2, result, objectPool));
            }
        default:
            // should not get here
            throw new HyracksDataException("Should not get here");
    }
}
Also used : AMutableDouble(org.apache.asterix.om.base.AMutableDouble) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 73 with MutableBoolean

use of org.apache.commons.lang3.mutable.MutableBoolean in project asterixdb by apache.

the class Operation method flattenSpecials.

public boolean flattenSpecials(EvalState state, Value val, ExprTreeHolder tree) throws HyracksDataException {
    ExprTreeHolder fChild1 = objectPool.mutableExprPool.get();
    ExprTreeHolder fChild2 = objectPool.mutableExprPool.get();
    ExprTreeHolder fChild3 = objectPool.mutableExprPool.get();
    Value eval1 = objectPool.valuePool.get();
    Value eval2 = objectPool.valuePool.get();
    Value eval3 = objectPool.valuePool.get();
    switch(opKind) {
        case OpKind_UNARY_PLUS_OP:
        case OpKind_UNARY_MINUS_OP:
        case OpKind_PARENTHESES_OP:
        case OpKind_LOGICAL_NOT_OP:
        case OpKind_BITWISE_NOT_OP:
            if (!child1.publicFlatten(state, eval1, fChild1)) {
                tree.setInnerTree(null);
                return false;
            }
            if (fChild1.getInnerTree() != null) {
                tree.setInnerTree(Operation.createOperation(opKind, fChild1, objectPool));
                return (tree.getInnerTree() != null);
            } else {
                privateDoOperation(opKind, eval1, null, null, true, false, false, val, objectPool);
                tree.setInnerTree(null);
                eval1.setUndefinedValue();
                return true;
            }
        case OpKind_TERNARY_OP:
            // Flatten the selector expression
            if (!child1.publicFlatten(state, eval1, fChild1)) {
                tree.setInnerTree(null);
                return false;
            }
            // check if selector expression collapsed to a non-undefined value
            if (fChild1.getInnerTree() == null && !eval1.isUndefinedValue()) {
                MutableBoolean b = objectPool.boolPool.get();
                // if the selector is not boolean-equivalent, propagate error
                if (!eval1.isBooleanValueEquiv(b)) {
                    val.setErrorValue();
                    eval1.setUndefinedValue();
                    tree.setInnerTree(null);
                    return true;
                }
                // eval1 is either a real or an integer
                if (b.booleanValue()) {
                    return child2.publicFlatten(state, val, tree);
                } else {
                    return child3.publicFlatten(state, val, tree);
                }
            } else {
                // Flatten arms of the if expression
                if (!child2.publicFlatten(state, eval2, fChild2) || !child3.publicFlatten(state, eval3, fChild3)) {
                    // clean up
                    tree.setInnerTree(null);
                    return false;
                }
                // if any arm collapsed into a value, make it a Literal
                if (fChild2.getInnerTree() == null) {
                    fChild2.setInnerTree(Literal.createLiteral(eval2, objectPool));
                }
                if (fChild3.getInnerTree() == null) {
                    fChild3.setInnerTree(Literal.createLiteral(eval3, objectPool));
                }
                if (fChild2.getInnerTree() == null || fChild3.getInnerTree() == null) {
                    tree.setInnerTree(null);
                    ;
                    return false;
                }
                // fChild1 may be NULL if child1 Flattened to UNDEFINED
                if (fChild1.getInnerTree() == null) {
                    fChild1.setInnerTree(child1.copy());
                }
                tree.setInnerTree(Operation.createOperation(opKind, fChild1, fChild2, fChild3, objectPool));
                if (tree.getInnerTree() == null) {
                    return false;
                }
                return true;
            }
        case OpKind_SUBSCRIPT_OP:
            // Flatten both arguments
            if (!child1.publicFlatten(state, eval1, fChild1) || !child2.publicFlatten(state, eval2, fChild2)) {
                tree.setInnerTree(null);
                return false;
            }
            // if both arguments Flattened to values, Evaluate now
            if (fChild1.getInnerTree() == null && fChild2.getInnerTree() == null) {
                privateDoOperation(opKind, eval1, eval2, null, true, true, false, val, objectPool);
                tree.setInnerTree(null);
                return true;
            }
            // otherwise convert Flattened values into literals
            if (fChild1.getInnerTree() == null) {
                fChild1.setInnerTree(Literal.createLiteral(eval1, objectPool));
            }
            if (fChild2.getInnerTree() == null) {
                fChild2.setInnerTree(Literal.createLiteral(eval2, objectPool));
            }
            if (fChild1.getInnerTree() == null || fChild2.getInnerTree() == null) {
                tree.setInnerTree(null);
                return false;
            }
            tree.setInnerTree(Operation.createOperation(opKind, fChild1, fChild2, objectPool));
            if (tree.getInnerTree() == null) {
                return false;
            }
            return true;
        default:
            throw new HyracksDataException("Should not get here");
    }
}
Also used : MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 74 with MutableBoolean

use of org.apache.commons.lang3.mutable.MutableBoolean in project asterixdb by apache.

the class ExprTree method debugFormatValue.

public void debugFormatValue(Value value, double time) throws HyracksDataException {
    MutableBoolean boolValue = objectPool.boolPool.get();
    AMutableInt64 intValue = objectPool.int64Pool.get();
    AMutableDouble doubleValue = objectPool.doublePool.get();
    AMutableCharArrayString stringValue = objectPool.strPool.get();
    if (NodeKind.CLASSAD_NODE == getKind()) {
        return;
    }
    PrettyPrint unp = objectPool.prettyPrintPool.get();
    AMutableCharArrayString buffer = objectPool.strPool.get();
    unp.unparse(buffer, this);
    String result = "Classad debug: ";
    if (time != 0) {
        String buf = String.format("%5.5fms", time * 1000);
        result += "[";
        result += buf;
        result += "] ";
    }
    result += buffer;
    result += " --> ";
    switch(value.getType()) {
        case NULL_VALUE:
            result += "NULL\n";
            break;
        case ERROR_VALUE:
            if ((NodeKind.FN_CALL_NODE == getKind()) && !((FunctionCall) (this)).functionIsDefined()) {
                result += "ERROR (function is not defined)\n";
            } else {
                result += "ERROR\n";
            }
            break;
        case UNDEFINED_VALUE:
            result += "UNDEFINED\n";
            break;
        case BOOLEAN_VALUE:
            if (value.isBooleanValue(boolValue)) {
                result += boolValue.booleanValue() ? "TRUE\n" : "FALSE\n";
            }
            break;
        case INTEGER_VALUE:
            if (value.isIntegerValue(intValue)) {
                result += String.format("%lld", intValue.getLongValue());
                result += "\n";
            }
            break;
        case REAL_VALUE:
            if (value.isRealValue(doubleValue)) {
                result += String.format("%lld", doubleValue.getDoubleValue());
                result += "\n";
            }
            break;
        case RELATIVE_TIME_VALUE:
            result += "RELATIVE TIME\n";
            break;
        case ABSOLUTE_TIME_VALUE:
            result += "ABSOLUTE TIME\n";
            break;
        case STRING_VALUE:
            if (value.isStringValue(stringValue)) {
                result += stringValue.toString();
                result += "\n";
            }
            break;
        case CLASSAD_VALUE:
            result += "CLASSAD\n";
            break;
        case LIST_VALUE:
            result += "LIST\n";
            break;
        case SLIST_VALUE:
            result += "SLIST\n";
            break;
    }
    debugPrint(result);
}
Also used : MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 75 with MutableBoolean

use of org.apache.commons.lang3.mutable.MutableBoolean in project bookkeeper by apache.

the class Bookie method checkIfDirsOnSameDiskPartition.

/**
 * Checks if multiple directories are in same diskpartition/filesystem/device.
 * If ALLOW_MULTIPLEDIRS_UNDER_SAME_DISKPARTITION config parameter is not enabled, and
 * if it is found that there are multiple directories in the same DiskPartition then
 * it will throw DiskPartitionDuplicationException.
 *
 * @param dirs dirs to validate
 *
 * @throws IOException
 */
private void checkIfDirsOnSameDiskPartition(List<File> dirs) throws DiskPartitionDuplicationException {
    boolean allowDiskPartitionDuplication = conf.isAllowMultipleDirsUnderSameDiskPartition();
    final MutableBoolean isDuplicationFoundAndNotAllowed = new MutableBoolean(false);
    Map<FileStore, List<File>> fileStoreDirsMap = new HashMap<FileStore, List<File>>();
    for (File dir : dirs) {
        FileStore fileStore;
        try {
            fileStore = Files.getFileStore(dir.toPath());
        } catch (IOException e) {
            LOG.error("Got IOException while trying to FileStore of {}", dir);
            throw new BookieException.DiskPartitionDuplicationException(e);
        }
        if (fileStoreDirsMap.containsKey(fileStore)) {
            fileStoreDirsMap.get(fileStore).add(dir);
        } else {
            List<File> dirsList = new ArrayList<File>();
            dirsList.add(dir);
            fileStoreDirsMap.put(fileStore, dirsList);
        }
    }
    fileStoreDirsMap.forEach((fileStore, dirsList) -> {
        if (dirsList.size() > 1) {
            if (allowDiskPartitionDuplication) {
                LOG.warn("Dirs: {} are in same DiskPartition/FileSystem: {}", dirsList, fileStore);
            } else {
                LOG.error("Dirs: {} are in same DiskPartition/FileSystem: {}", dirsList, fileStore);
                isDuplicationFoundAndNotAllowed.setValue(true);
            }
        }
    });
    if (isDuplicationFoundAndNotAllowed.getValue()) {
        throw new BookieException.DiskPartitionDuplicationException();
    }
}
Also used : DiskPartitionDuplicationException(org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException) HashMap(java.util.HashMap) ConcurrentLongHashMap(org.apache.bookkeeper.util.collections.ConcurrentLongHashMap) DiskPartitionDuplicationException(org.apache.bookkeeper.bookie.BookieException.DiskPartitionDuplicationException) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileStore(java.nio.file.FileStore) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)107 Test (org.junit.Test)28 Path (java.nio.file.Path)26 Test (org.junit.jupiter.api.Test)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)12 Query (org.apache.apex.malhar.lib.appdata.schemas.Query)11 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)11 List (java.util.List)9 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)8 MutableInt (org.apache.commons.lang3.mutable.MutableInt)7 MutableLong (org.apache.commons.lang3.mutable.MutableLong)7 Collections (java.util.Collections)6 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)6 Set (java.util.Set)5 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 PageCache (org.neo4j.io.pagecache.PageCache)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ClassAd (org.apache.asterix.external.classad.ClassAd)4