Example usage for org.apache.commons.lang.mutable MutableInt setValue

List of usage examples for org.apache.commons.lang.mutable MutableInt setValue

Introduction

In this page you can find the example usage for org.apache.commons.lang.mutable MutableInt setValue.

Prototype

public void setValue(Object value) 

Source Link

Document

Sets the value from any Number instance.

Usage

From source file:sf.net.experimaestro.manager.scripting.GenericFunction.java

/**
 * Gives a score to a given declaration//from  www  . j  a v  a 2s .c  om
 *
 * @param cx          The script context
 * @param declaration The underlying method or constructor
 * @param args        The arguments
 * @param converters  A list of converters that will be filled by this method
 * @param offset      The offset for the converters
 * @return A score (minimum integer if no conversion is possible)
 */
static int score(LanguageContext lcx, ScriptContext cx, Declaration declaration, Object[] args,
        Function[] converters, MutableInt offset) {

    final Executable executable = declaration.executable();
    final Class<?>[] types = executable.getParameterTypes();
    final boolean isVarArgs = executable.isVarArgs();

    // Get the annotations
    Expose annotation = declaration.executable.getAnnotation(Expose.class);
    final boolean contextAnnotation = annotation == null ? false : annotation.context();
    final boolean scopeAnnotation = annotation == null ? false : annotation.scope();
    int optional = annotation == null ? 0 : annotation.optional();

    // Start the scoring
    Converter converter = new Converter();

    // Offset in the types
    offset.setValue(contextAnnotation || scopeAnnotation ? 2 : 0);

    // Number of "true" arguments (not scope, not vararg)
    final int nbArgs = types.length - offset.intValue() - (isVarArgs ? 1 : 0);

    // The number of arguments should be in:
    // [nbArgs - optional, ...] if varargs
    // [nbArgs - optional, nbArgs] otherwise

    if (args.length < nbArgs - optional)
        return Integer.MIN_VALUE;

    if (!isVarArgs && args.length > nbArgs)
        return Integer.MIN_VALUE;

    // If the optional arguments are at the beginning, then shift
    if (annotation != null && annotation.optionalsAtStart()) {
        offset.add(max(nbArgs - args.length, 0));
    }

    // Normal arguments
    for (int i = 0; i < args.length && i < nbArgs && converter.isOK(); i++) {
        final Object o = args[i];
        converters[i] = converter.converter(lcx, cx, o, types[i + offset.intValue()]);
    }

    // Var args
    if (isVarArgs) {
        Class<?> type = ClassUtils.primitiveToWrapper(types[types.length - 1].getComponentType());
        int nbVarArgs = args.length - nbArgs;
        for (int i = 0; i < nbVarArgs && converter.isOK(); i++) {
            final Object o = args[nbArgs + i];
            converters[nbArgs + i] = converter.converter(lcx, cx, o, type);
        }
    }

    return converter.score;
}

From source file:voldemort.store.routed.RoutedStore.java

public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys) throws VoldemortException {
    StoreUtils.assertValidKeys(keys);//from   w ww . j  av  a2s.  co  m

    Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys);

    // Keys for each node needed to satisfy storeDef.getPreferredReads() if
    // no failures.
    Map<Node, List<ByteArray>> nodeToKeysMap = Maps.newHashMap();

    // Keep track of nodes per key that might be needed if there are
    // failures during getAll
    Map<ByteArray, List<Node>> keyToExtraNodesMap = Maps.newHashMap();

    for (ByteArray key : keys) {
        List<Node> availableNodes = availableNodes(routingStrategy.routeRequest(key.get()));

        // quickly fail if there aren't enough nodes to meet the requirement
        checkRequiredReads(availableNodes);
        int preferredReads = storeDef.getPreferredReads();
        List<Node> preferredNodes = Lists.newArrayListWithCapacity(preferredReads);
        List<Node> extraNodes = Lists.newArrayListWithCapacity(3);

        for (Node node : availableNodes) {
            if (preferredNodes.size() < preferredReads)
                preferredNodes.add(node);
            else
                extraNodes.add(node);
        }

        for (Node node : preferredNodes) {
            List<ByteArray> nodeKeys = nodeToKeysMap.get(node);
            if (nodeKeys == null) {
                nodeKeys = Lists.newArrayList();
                nodeToKeysMap.put(node, nodeKeys);
            }
            nodeKeys.add(key);
        }
        if (!extraNodes.isEmpty()) {
            List<Node> nodes = keyToExtraNodesMap.get(key);
            if (nodes == null)
                keyToExtraNodesMap.put(key, extraNodes);
            else
                nodes.addAll(extraNodes);
        }
    }

    List<Callable<GetAllResult>> callables = Lists.newArrayList();
    for (Map.Entry<Node, List<ByteArray>> entry : nodeToKeysMap.entrySet()) {
        final Node node = entry.getKey();
        final Collection<ByteArray> nodeKeys = entry.getValue();
        if (failureDetector.isAvailable(node))
            callables.add(new GetAllCallable(node, nodeKeys));
    }

    // A list of thrown exceptions, indicating the number of failures
    List<Throwable> failures = Lists.newArrayList();
    List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList();

    Map<ByteArray, MutableInt> keyToSuccessCount = Maps.newHashMap();
    for (ByteArray key : keys)
        keyToSuccessCount.put(key, new MutableInt(0));

    List<Future<GetAllResult>> futures;
    try {
        // TODO What to do about timeouts? They should be longer as getAll
        // is likely to
        // take longer. At the moment, it's just timeoutMs * 3, but should
        // this be based on the number of the keys?
        futures = executor.invokeAll(callables, timeoutMs * 3, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
    }
    for (Future<GetAllResult> f : futures) {
        if (f.isCancelled()) {
            logger.warn("Get operation timed out after " + timeoutMs + " ms.");
            continue;
        }
        try {
            GetAllResult getResult = f.get();
            if (getResult.exception != null) {
                if (getResult.exception instanceof VoldemortApplicationException) {
                    throw (VoldemortException) getResult.exception;
                }
                failures.add(getResult.exception);
                continue;
            }
            for (ByteArray key : getResult.callable.nodeKeys) {
                List<Versioned<byte[]>> retrieved = getResult.retrieved.get(key);
                MutableInt successCount = keyToSuccessCount.get(key);
                successCount.increment();

                /*
                 * retrieved can be null if there are no values for the key
                 * provided
                 */
                if (retrieved != null) {
                    List<Versioned<byte[]>> existing = result.get(key);
                    if (existing == null)
                        result.put(key, Lists.newArrayList(retrieved));
                    else
                        existing.addAll(retrieved);
                }
            }
            nodeValues.addAll(getResult.nodeValues);

        } catch (InterruptedException e) {
            throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
        } catch (ExecutionException e) {
            // We catch all Throwables apart from Error in the callable, so
            // the else part
            // should never happen
            if (e.getCause() instanceof Error)
                throw (Error) e.getCause();
            else
                logger.error(e.getMessage(), e);
        }
    }

    for (ByteArray key : keys) {
        MutableInt successCountWrapper = keyToSuccessCount.get(key);
        int successCount = successCountWrapper.intValue();
        if (successCount < storeDef.getPreferredReads()) {
            List<Node> extraNodes = keyToExtraNodesMap.get(key);
            if (extraNodes != null) {
                for (Node node : extraNodes) {
                    long startNs = System.nanoTime();
                    try {
                        List<Versioned<byte[]>> values = innerStores.get(node.getId()).get(key);
                        fillRepairReadsValues(nodeValues, key, node, values);
                        List<Versioned<byte[]>> versioneds = result.get(key);
                        if (versioneds == null)
                            result.put(key, Lists.newArrayList(values));
                        else
                            versioneds.addAll(values);
                        recordSuccess(node, startNs);
                        if (++successCount >= storeDef.getPreferredReads())
                            break;

                    } catch (UnreachableStoreException e) {
                        failures.add(e);
                        recordException(node, startNs, e);
                    } catch (VoldemortApplicationException e) {
                        throw e;
                    } catch (Exception e) {
                        logger.warn("Error in GET_ALL on node " + node.getId() + "(" + node.getHost() + ")", e);
                        failures.add(e);
                    }
                }
            }
        }
        successCountWrapper.setValue(successCount);
    }

    repairReads(nodeValues);

    for (Map.Entry<ByteArray, MutableInt> mapEntry : keyToSuccessCount.entrySet()) {
        int successCount = mapEntry.getValue().intValue();
        if (successCount < storeDef.getRequiredReads())
            throw new InsufficientOperationalNodesException(
                    this.storeDef.getRequiredReads() + " reads required, but " + successCount + " succeeded.",
                    failures);
    }

    return result;
}

From source file:voldemort.store.routed.ThreadPoolRoutedStore.java

@Override
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys,
        Map<ByteArray, byte[]> transforms) throws VoldemortException {
    StoreUtils.assertValidKeys(keys);//from  ww  w .ja  v  a  2 s.com

    Map<ByteArray, List<Versioned<byte[]>>> result = StoreUtils.newEmptyHashMap(keys);

    // Keys for each node needed to satisfy storeDef.getPreferredReads() if
    // no failures.
    Map<Node, List<ByteArray>> nodeToKeysMap = Maps.newHashMap();

    // Keep track of nodes per key that might be needed if there are
    // failures during getAll
    Map<ByteArray, List<Node>> keyToExtraNodesMap = Maps.newHashMap();

    for (ByteArray key : keys) {
        List<Node> availableNodes = availableNodes(routingStrategy.routeRequest(key.get()));

        // quickly fail if there aren't enough nodes to meet the requirement
        checkRequiredReads(availableNodes);
        int preferredReads = storeDef.getPreferredReads();
        List<Node> preferredNodes = Lists.newArrayListWithCapacity(preferredReads);
        List<Node> extraNodes = Lists.newArrayListWithCapacity(3);

        for (Node node : availableNodes) {
            if (preferredNodes.size() < preferredReads)
                preferredNodes.add(node);
            else
                extraNodes.add(node);
        }

        for (Node node : preferredNodes) {
            List<ByteArray> nodeKeys = nodeToKeysMap.get(node);
            if (nodeKeys == null) {
                nodeKeys = Lists.newArrayList();
                nodeToKeysMap.put(node, nodeKeys);
            }
            nodeKeys.add(key);
        }
        if (!extraNodes.isEmpty()) {
            List<Node> nodes = keyToExtraNodesMap.get(key);
            if (nodes == null)
                keyToExtraNodesMap.put(key, extraNodes);
            else
                nodes.addAll(extraNodes);
        }
    }

    List<Callable<GetAllResult>> callables = Lists.newArrayList();
    for (Map.Entry<Node, List<ByteArray>> entry : nodeToKeysMap.entrySet()) {
        final Node node = entry.getKey();
        final Collection<ByteArray> nodeKeys = entry.getValue();
        if (failureDetector.isAvailable(node))
            callables.add(new GetAllCallable(node, nodeKeys, transforms));
    }

    // A list of thrown exceptions, indicating the number of failures
    List<Throwable> failures = Lists.newArrayList();
    List<NodeValue<ByteArray, byte[]>> nodeValues = Lists.newArrayList();

    Map<ByteArray, MutableInt> keyToSuccessCount = Maps.newHashMap();
    for (ByteArray key : keys)
        keyToSuccessCount.put(key, new MutableInt(0));

    List<Future<GetAllResult>> futures;
    long timeoutMs = timeoutConfig.getOperationTimeout(VoldemortOpCode.GET_ALL_OP_CODE);
    try {
        // TODO What to do about timeouts? They should be longer as getAll
        // is likely to
        // take longer. At the moment, it's just timeoutMs * 3, but should
        // this be based on the number of the keys?
        futures = executor.invokeAll(callables, timeoutMs * 3, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
    }
    for (Future<GetAllResult> f : futures) {
        if (f.isCancelled()) {
            logger.warn("Get operation timed out after " + timeoutMs + " ms.");
            continue;
        }
        try {
            GetAllResult getResult = f.get();
            if (getResult.exception != null) {
                if (getResult.exception instanceof VoldemortApplicationException) {
                    throw (VoldemortException) getResult.exception;
                }
                failures.add(getResult.exception);
                continue;
            }
            for (ByteArray key : getResult.callable.nodeKeys) {
                List<Versioned<byte[]>> retrieved = getResult.retrieved.get(key);
                MutableInt successCount = keyToSuccessCount.get(key);
                successCount.increment();

                /*
                 * retrieved can be null if there are no values for the key
                 * provided
                 */
                if (retrieved != null) {
                    List<Versioned<byte[]>> existing = result.get(key);
                    if (existing == null)
                        result.put(key, Lists.newArrayList(retrieved));
                    else
                        existing.addAll(retrieved);
                }
            }
            nodeValues.addAll(getResult.nodeValues);

        } catch (InterruptedException e) {
            throw new InsufficientOperationalNodesException("getAll operation interrupted.", e);
        } catch (ExecutionException e) {
            // We catch all Throwables apart from Error in the callable, so
            // the else part
            // should never happen
            if (e.getCause() instanceof Error)
                throw (Error) e.getCause();
            else
                logger.error(e.getMessage(), e);
        }
    }

    for (ByteArray key : keys) {
        MutableInt successCountWrapper = keyToSuccessCount.get(key);
        int successCount = successCountWrapper.intValue();
        if (successCount < storeDef.getPreferredReads()) {
            List<Node> extraNodes = keyToExtraNodesMap.get(key);
            if (extraNodes != null) {
                for (Node node : extraNodes) {
                    long startNs = System.nanoTime();
                    try {
                        List<Versioned<byte[]>> values = innerStores.get(node.getId()).get(key,
                                transforms == null ? null : transforms.get(key));
                        fillRepairReadsValues(nodeValues, key, node, values);
                        List<Versioned<byte[]>> versioneds = result.get(key);
                        if (versioneds == null)
                            result.put(key, Lists.newArrayList(values));
                        else
                            versioneds.addAll(values);
                        recordSuccess(node, startNs);
                        if (++successCount >= storeDef.getPreferredReads())
                            break;

                    } catch (UnreachableStoreException e) {
                        failures.add(e);
                        recordException(node, startNs, e);
                    } catch (VoldemortApplicationException e) {
                        throw e;
                    } catch (Exception e) {
                        logger.warn("Error in GET_ALL on node " + node.getId() + "(" + node.getHost() + ")", e);
                        failures.add(e);
                    }
                }
            }
        }
        successCountWrapper.setValue(successCount);
    }

    repairReads(nodeValues, repairReads && (transforms == null || transforms.size() == 0));

    for (Map.Entry<ByteArray, MutableInt> mapEntry : keyToSuccessCount.entrySet()) {
        int successCount = mapEntry.getValue().intValue();
        if (successCount < storeDef.getRequiredReads())
            throw new InsufficientOperationalNodesException(
                    this.storeDef.getRequiredReads() + " reads required, but " + successCount + " succeeded.",
                    failures);
    }

    return result;
}

From source file:voldemort.VoldemortClientShell.java

protected Object parseObject(SerializerDefinition serializerDef, String argStr, MutableInt parsePos) {
    Object obj = null;//from  w w w. j  a  v  a  2s.co m
    try {
        // TODO everything is read as json string now..
        JsonReader jsonReader = new JsonReader(new StringReader(argStr));
        obj = jsonReader.read();
        // mark how much of the original string, we blew through to
        // extract the avrostring.
        parsePos.setValue(jsonReader.getCurrentLineOffset() - 1);

        if (isAvroSchema(serializerDef.getName())) {
            // TODO Need to check all the avro siblings work
            // For avro, we hack and extract avro key/value as a string,
            // before we do the actual parsing with the schema
            String avroString = (String) obj;
            // From here on, this is just normal avro parsing.
            Schema latestSchema = Schema.parse(serializerDef.getCurrentSchemaInfo());
            try {
                JsonDecoder decoder = new JsonDecoder(latestSchema, avroString);
                GenericDatumReader<Object> datumReader = new GenericDatumReader<Object>(latestSchema);
                obj = datumReader.read(null, decoder);
            } catch (IOException io) {
                errorStream.println("Error parsing avro string " + avroString);
                io.printStackTrace();
            }
        } else {
            // all json processing does some numeric type tightening
            obj = tightenNumericTypes(obj);
        }
    } catch (EndOfFileException eof) {
        // can be thrown from the jsonReader.read(..) call indicating, we
        // have nothing more to read.
        obj = null;
    }
    return obj;
}