org.msgpack.value.ArrayValue Java Examples

The following examples show how to use org.msgpack.value.ArrayValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TdOperatorFactory.java    From digdag with Apache License 2.0 6 votes vote down vote up
static void downloadPreviewRows(TDJobOperator j, String description, TaskState state, DurationInterval retryInterval)
{
    StringWriter out = new StringWriter();

    try {
        addCsvHeader(out, j.getResultColumnNames());

        List<ArrayValue> rows = downloadFirstResults(j, PREVIEW_ROWS, state, PREVIEW, retryInterval);
        if (rows.isEmpty()) {
            logger.info("preview of {}: (no results)", description, j.getJobId());
            return;
        }
        for (ArrayValue row : rows) {
            addCsvRow(out, row);
        }
    }
    catch (Exception ex) {
        logger.warn("Getting rows for preview failed. Ignoring this error.", ex);
        return;
    }

    logger.info("preview of {}:\r\n{}", description, out.toString());
}
 
Example #2
Source File: TdWaitOperatorFactory.java    From digdag with Apache License 2.0 6 votes vote down vote up
private boolean fetchJobResult(TDJobOperator job)
{
    Optional<ArrayValue> firstRow = pollingRetryExecutor(state, RESULT)
            .retryUnless(TDOperator::isDeterministicClientException)
            .withErrorMessage("Failed to download result of job '%s'", job.getJobId())
            .run(s -> job.getResult(
                    ite -> ite.hasNext()
                            ? Optional.of(ite.next())
                            : Optional.absent()));

    // There must be at least one row in the result for the wait condition to be fulfilled.
    if (!firstRow.isPresent()) {
        return false;
    }

    ArrayValue row = firstRow.get();
    if (row.size() < 1) {
        throw new TaskExecutionException("Got empty row in result of query");
    }

    Value firstCol = row.get(0);
    return isTruthy(firstCol);
}
 
Example #3
Source File: Deserializer.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
private Object unpackProperty(final ArrayValue packedValueAndType) {
  final Iterator<Value> iter = packedValueAndType.iterator();
  final byte valueTypeId = iter.next().asIntegerValue().asByte();
  final Value value = iter.next();

  switch (ValueTypes.lookup(valueTypeId)) {
    case BOOLEAN:
      return value.asBooleanValue().getBoolean();
    case STRING:
      return value.asStringValue().asString();
    case BYTE:
      return value.asIntegerValue().asByte();
    case SHORT:
      return value.asIntegerValue().asShort();
    case INTEGER:
      return value.asIntegerValue().asInt();
    case LONG:
      return value.asIntegerValue().asLong();
    case FLOAT:
      return value.asFloatValue().toFloat();
    case DOUBLE:
      return Double.valueOf(value.asFloatValue().toFloat());
    case LIST:
      final ArrayValue arrayValue = value.asArrayValue();
      List deserializedArray = new ArrayList(arrayValue.size());
      final Iterator<Value> valueIterator = arrayValue.iterator();
      while (valueIterator.hasNext()) {
        deserializedArray.add(unpackProperty(valueIterator.next().asArrayValue()));
      }
      return deserializedArray;
    default:
      throw new NotImplementedException("unknown valueTypeId=`" + valueTypeId);
  }
}
 
Example #4
Source File: TdWaitTableOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
private boolean fetchJobResult(int rows, TDJobOperator job)
{
    Optional<ArrayValue> firstRow = pollingRetryExecutor(state, RESULT)
            .retryUnless(TDOperator::isDeterministicClientException)
            .withErrorMessage("Failed to download result of job '%s'", job.getJobId())
            .withRetryInterval(retryInterval)
            .run(s -> job.getResult(
                    ite -> ite.hasNext()
                            ? Optional.of(ite.next())
                            : Optional.absent()));

    if (!firstRow.isPresent()) {
        throw new TaskExecutionException("Got unexpected empty result for count job: " + job.getJobId());
    }
    ArrayValue row = firstRow.get();
    if (row.size() != 1) {
        throw new TaskExecutionException("Got unexpected result row size for count job: " + row.size());
    }
    Value count = row.get(0);
    IntegerValue actualRows;
    try {
        actualRows = count.asIntegerValue();
    }
    catch (MessageTypeCastException e) {
        throw new TaskExecutionException("Got unexpected value type count job: " + count.getValueType());
    }

    return BigInteger.valueOf(rows).compareTo(actualRows.asBigInteger()) <= 0;
}
 
Example #5
Source File: TdOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
private static void addCsvRow(Writer out, ArrayValue row)
    throws IOException
{
    boolean first = true;
    for (Value v : row) {
        if (first) { first = false; }
        else { out.write(DELIMITER_CHAR); }
        addCsvValue(out, v);
    }
    out.write("\r\n");
}
 
Example #6
Source File: TdOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
static Config buildStoreParams(ConfigFactory cf, TDJobOperator j, boolean storeLastResults, TaskState state, DurationInterval retryInterval)
{
    if (storeLastResults) {
        Config td = cf.create();

        List<ArrayValue> results = downloadFirstResults(j, 1, state, RESULT, retryInterval);
        Map<RawValue, Value> map = new LinkedHashMap<>();
        if (!results.isEmpty()) {
            ArrayValue row = results.get(0);
            List<String> columnNames = j.getResultColumnNames();
            for (int i = 0; i < Math.min(row.size(), columnNames.size()); i++) {
                map.put(ValueFactory.newString(columnNames.get(i)), row.get(i));
            }
        }
        MapValue lastResults = ValueFactory.newMap(map);
        try {
            td.set("last_results", new ObjectMapper().readTree(lastResults.toJson()));
        }
        catch (IOException ex) {
            throw Throwables.propagate(ex);
        }

        return cf.create().set("td", td);
    }
    else {
        return cf.create();
    }
}
 
Example #7
Source File: TdOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
private static List<ArrayValue> downloadFirstResults(TDJobOperator j, int max, TaskState state, String stateKey, DurationInterval retryInterval)
{
    return pollingRetryExecutor(state, stateKey)
            .retryUnless(TDOperator::isDeterministicClientException)
            .withRetryInterval(retryInterval)
            .withErrorMessage("Failed to download result of job '%s'", j.getJobId())
            .run(s -> {
                try {
                    return j.getResult(ite -> {
                        List<ArrayValue> results = new ArrayList<>(max);
                        for (int i = 0; i < max; i++) {
                            if (ite.hasNext()) {
                                ArrayValue row = ite.next().asArrayValue();
                                results.add(row);
                            }
                            else {
                                break;
                            }
                        }
                        return results;
                    });
                }
                catch (TDClientHttpNotFoundException ex) {
                    // this happens if query is INSERT or CREATE. return empty results
                    return ImmutableList.of();
                }
            });
}
 
Example #8
Source File: TdForEachOperatorFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
private Config row(List<String> keys, ArrayValue values)
{
    Config config = configFactory.create();
    // TODO: fail on keys and values count mismatch?
    int n = Math.min(keys.size(), values.size());
    for (int i = 0; i < n; i++) {
        config.set(keys.get(i), value(values.get(i)));
    }
    return config;
}
 
Example #9
Source File: MessagePackDeserializer.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
private Collection<?> deserializeCollection( ModuleDescriptor module, CollectionType collectionType,
                                             ArrayValue value ) throws IOException
{
    Collection<?> collection = collectionType.isSet() ? new LinkedHashSet( value.size() )
                                                      : new ArrayList( value.size() );
    for( Value element : value.list() )
    {
        collection.add( doDeserialize( module, collectionType.collectedType(), element ) );
    }
    return collection;
}
 
Example #10
Source File: MessagePackSerializer.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private ArrayValue serializeIterable( Options options, Iterable<?> iterable )
{
    return serializeStream( options, StreamSupport.stream( iterable.spliterator(), false ) );
}
 
Example #11
Source File: MessagePackSerializer.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private ArrayValue serializeStream( Options options, Stream<?> stream )
{
    return ValueFactory.newArray( stream.map( element -> doSerialize( options, element, false ) )
                                        .collect( toList() ) );
}