Java Code Examples for com.google.cloud.spanner.Mutation#WriteBuilder

The following examples show how to use com.google.cloud.spanner.Mutation#WriteBuilder . 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: SpannerMutationFactoryImpl.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private WriteBuilder writeBuilder(Op op, String tableName) {
	Mutation.WriteBuilder builder = null;
	switch (op) {
	case INSERT:
		builder = Mutation.newInsertBuilder(tableName);
		break;
	case INSERT_OR_UPDATE:
		builder = Mutation.newInsertOrUpdateBuilder(tableName);
		break;
	case UPDATE:
		builder = Mutation.newUpdateBuilder(tableName);
		break;
	}
	if (builder == null) {
		throw new IllegalArgumentException(
				"Unsupported save-mutation operation: " + op);
	}
	return builder;
}
 
Example 2
Source File: SpannerMutationFactoryImpl.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private List<Mutation> saveObject(Op op, Object object,
		Set<String> includeProperties) {
	SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext
			.getPersistentEntity(object.getClass());
	List<Mutation> mutations = new ArrayList<>();
	Mutation.WriteBuilder writeBuilder = writeBuilder(op,
			persistentEntity.tableName());
	this.spannerEntityProcessor.write(object, writeBuilder::set, includeProperties);
	mutations.add(writeBuilder.build());

	persistentEntity.doWithInterleavedProperties((spannerPersistentProperty) -> {
		if (includeProperties == null
				|| includeProperties.contains(spannerPersistentProperty.getName())) {

			Iterable kids = (Iterable) persistentEntity.getPropertyAccessor(object)
					.getProperty(spannerPersistentProperty);

			if (kids != null && !ConversionUtils.ignoreForWriteLazyProxy(kids)) {
				for (Object child : kids) {
					verifyChildHasParentId(persistentEntity, object,
							this.spannerMappingContext.getPersistentEntity(
									spannerPersistentProperty.getColumnInnerType()),
							child);
					mutations.addAll(saveObject(op, child, includeProperties));
				}
			}
		}
	});
	return mutations;
}
 
Example 3
Source File: SpannerWriteIT.java    From beam with Apache License 2.0 5 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  Mutation.WriteBuilder builder = Mutation.newInsertOrUpdateBuilder(table);
  Long key = c.element();
  builder.set("Key").to(key);
  String value = injectError.apply(key) ? null : RandomUtils.randomAlphaNumeric(valueSize);
  builder.set("Value").to(value);
  Mutation mutation = builder.build();
  c.output(mutation);
}
 
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();
}