Java Code Examples for com.google.cloud.spanner.Value#isNull()

The following examples show how to use com.google.cloud.spanner.Value#isNull() . 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: MutationSizeEstimator.java    From beam with Apache License 2.0 6 votes vote down vote up
private static long estimatePrimitiveValue(Value v) {
  switch (v.getType().getCode()) {
    case BOOL:
      return 1;
    case INT64:
    case FLOAT64:
      return 8;
    case DATE:
    case TIMESTAMP:
      return 12;
    case STRING:
      return v.isNull() ? 0 : v.getString().length();
    case BYTES:
      return v.isNull() ? 0 : v.getBytes().length();
    default:
      throw new IllegalArgumentException("Unsupported type " + v.getType());
  }
}
 
Example 2
Source File: MutationSizeEstimator.java    From beam with Apache License 2.0 5 votes vote down vote up
private static long estimateArrayValue(Value v) {
  if (v.isNull()) {
    return 0;
  }
  switch (v.getType().getArrayElementType().getCode()) {
    case BOOL:
      return v.getBoolArray().size();
    case INT64:
      return 8L * v.getInt64Array().size();
    case FLOAT64:
      return 8L * v.getFloat64Array().size();
    case STRING:
      long totalLength = 0;
      for (String s : v.getStringArray()) {
        if (s == null) {
          continue;
        }
        totalLength += s.length();
      }
      return totalLength;
    case BYTES:
      totalLength = 0;
      for (ByteArray bytes : v.getBytesArray()) {
        if (bytes == null) {
          continue;
        }
        totalLength += bytes.length();
      }
      return totalLength;
    case DATE:
      return 12L * v.getDateArray().size();
    case TIMESTAMP:
      return 12L * v.getTimestampArray().size();
    default:
      throw new IllegalArgumentException("Unsupported type " + v.getType());
  }
}
 
Example 3
Source File: MutationKeyEncoder.java    From beam with Apache License 2.0 5 votes vote down vote up
private void encodeKey(OrderedCode orderedCode, Mutation m) {
  Map<String, Value> mutationMap = mutationAsMap(m);
  for (SpannerSchema.KeyPart part : schema.getKeyParts(m.getTable())) {
    Value val = mutationMap.get(part.getField());
    if (val == null || val.isNull()) {
      if (part.isDesc()) {
        orderedCode.writeInfinityDecreasing();
      } else {
        orderedCode.writeInfinity();
      }
    } else {
      Type.Code code = val.getType().getCode();
      switch (code) {
        case BOOL:
          writeNumber(orderedCode, part, (long) (val.getBool() ? 0 : 1));
          break;
        case INT64:
          writeNumber(orderedCode, part, val.getInt64());
          break;
        case FLOAT64:
          writeNumber(orderedCode, part, Double.doubleToLongBits(val.getFloat64()));
          break;
        case STRING:
          writeString(orderedCode, part, val.getString());
          break;
        case BYTES:
          writeBytes(orderedCode, part, val.getBytes());
          break;
        case TIMESTAMP:
          writeTimestamp(orderedCode, part, val.getTimestamp());
          break;
        case DATE:
          writeNumber(orderedCode, part, encodeDate(val.getDate()));
          break;
        default:
          throw new IllegalArgumentException("Unknown type " + val.getType());
      }
    }
  }
}
 
Example 4
Source File: RandomInsertMutationGenerator.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
public Mutation generateMutation(Map<String, Value> overrides) {
  Mutation.WriteBuilder builder = Mutation.newInsertOrUpdateBuilder(randomCase(table.name()));
  for (Map.Entry<String, Iterator<Value>> values : valueGenerators.entrySet()) {
    String columnName = values.getKey();
    Value value = overrides.get(columnName);
    if (value == null) {
      value = values.getValue().next();
    }
    switch (value.getType().getCode()) {
      case BOOL:
        Boolean bool = value.isNull() ? null : value.getBool();
        builder.set(columnName).to(bool);
        break;
      case INT64:
        Long l = value.isNull() ? null : value.getInt64();
        builder.set(columnName).to(l);
        break;
      case FLOAT64:
        Double f = value.isNull() ? null : value.getFloat64();
        builder.set(columnName).to(f);
        break;
      case BYTES:
        ByteArray bytes = value.isNull() ? null : value.getBytes();
        builder.set(columnName).to(bytes);
        break;
      case STRING:
        String string = value.isNull() ? null : value.getString();
        builder.set(columnName).to(string);
        break;
      case TIMESTAMP:
        Timestamp timestamp = value.isNull() ? null : value.getTimestamp();
        builder.set(columnName).to(timestamp);
        break;
      case DATE:
        Date date = value.isNull() ? null : value.getDate();
        builder.set(columnName).to(date);
        break;
      case ARRAY:
        switch (value.getType().getArrayElementType().getCode()) {
          case BOOL:
            List<Boolean> bools = value.isNull() ? null : value.getBoolArray();
            builder.set(columnName).toBoolArray(bools);
            break;
          case INT64:
            List<Long> longs = value.isNull() ? null : value.getInt64Array();
            builder.set(columnName).toInt64Array(longs);
            break;
          case FLOAT64:
            List<Double> doubles = value.isNull() ? null : value.getFloat64Array();
            builder.set(columnName).toFloat64Array(doubles);
            break;
          case BYTES:
            List<ByteArray> bytesArray = value.isNull() ? null : value.getBytesArray();
            builder.set(columnName).toBytesArray(bytesArray);
            break;
          case STRING:
            List<String> strings = value.isNull() ? null : value.getStringArray();
            builder.set(columnName).toStringArray(strings);
            break;
          case TIMESTAMP:
            List<Timestamp> timestamps = value.isNull() ? null : value.getTimestampArray();
            builder.set(columnName).toTimestampArray(timestamps);
            break;
          case DATE:
            List<Date> dates = value.isNull() ? null : value.getDateArray();
            builder.set(columnName).toDateArray(dates);
            break;
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown toValue " + value);
    }
  }
  return builder.build();
}