Java Code Examples for com.google.cloud.datastore.DatastoreReaderWriter

The following examples show how to use com.google.cloud.datastore.DatastoreReaderWriter. These examples are extracted from open source projects. 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 Project: spring-cloud-gcp   Source File: DatastoreTemplate.java    License: Apache License 2.0 6 votes vote down vote up
public DatastoreTemplate(Supplier<? extends DatastoreReaderWriter> datastore,
		DatastoreEntityConverter datastoreEntityConverter,
		DatastoreMappingContext datastoreMappingContext,
		ObjectToKeyFactory objectToKeyFactory) {
	Assert.notNull(datastore, "A non-null Datastore service object is required.");
	Assert.notNull(datastoreEntityConverter,
			"A non-null DatastoreEntityConverter is required.");
	Assert.notNull(datastoreMappingContext,
			"A non-null DatastoreMappingContext is required.");
	Assert.notNull(objectToKeyFactory,
			"A non-null Object to Key factory is required.");
	this.datastore = datastore;
	this.datastoreEntityConverter = datastoreEntityConverter;
	this.datastoreMappingContext = datastoreMappingContext;
	this.objectToKeyFactory = objectToKeyFactory;
}
 
Example 2
Source Project: spring-cloud-gcp   Source File: DatastoreTemplate.java    License: Apache License 2.0 6 votes vote down vote up
private <T> T computeReferencedField(BaseEntity entity, ReadContext context,
		DatastorePersistentProperty referenceProperty, String fieldName, Class<T> type) {
	T referenced;
	if (referenceProperty.isLazyLoaded()) {
		DatastoreReaderWriter originalTx = getDatastoreReadWriter();
		referenced = LazyUtil.wrapSimpleLazyProxy(() -> {
			if (getDatastoreReadWriter() != originalTx) {
				throw new DatastoreDataException("Lazy load should be invoked within the same transaction");
			}
			return (T) findReferenced(entity, referenceProperty, context);
		}, type, entity.getValue(fieldName));
	}
	else {
		referenced = (T) findReferenced(entity, referenceProperty, context);
	}
	return referenced;
}
 
Example 3
Source Project: spring-cloud-gcp   Source File: DatastoreTemplateTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void performTransactionTest() {

	DatastoreReaderWriter transactionContext = mock(DatastoreReaderWriter.class);

	when(this.datastore.runInTransaction(any())).thenAnswer((invocation) -> {
		TransactionCallable<String> callable = invocation.getArgument(0);
		return callable.run(transactionContext);
	});

	List<Entity> e1 = Collections
			.singletonList(this.e1);
	when(transactionContext.fetch(ArgumentMatchers.<Key[]>any())).thenReturn(e1);

	String finalResult = this.datastoreTemplate
			.performTransaction((datastoreOperations) -> {
				datastoreOperations.save(this.ob2);
				datastoreOperations.findById("ignored", TestEntity.class);
				return "all done";
			});

	assertThat(finalResult).isEqualTo("all done");
	verify(transactionContext, times(1)).put(ArgumentMatchers.<FullEntity[]>any());
	verify(transactionContext, times(2)).fetch((Key[]) any());
}
 
Example 4
Source Project: google-cloud-java   Source File: DatastoreSnippets.java    License: Apache License 2.0 6 votes vote down vote up
/** Example of running in a transaction. */
// [TARGET runInTransaction(TransactionCallable)]
// [VARIABLE "my_callable_result"]
public String runInTransaction(final String callableResult) {
  // [START runInTransaction]
  TransactionCallable<String> callable =
      new TransactionCallable<String>() {
        public String run(DatastoreReaderWriter readerWriter) {
          // use readerWriter to run in transaction
          return callableResult;
        }
      };
  String result = datastore.runInTransaction(callable);
  // [END runInTransaction]
  return result;
}
 
Example 5
Source Project: spring-cloud-gcp   Source File: DatastoreTemplate.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <A> A performTransaction(Function<DatastoreOperations, A> operations) {
	if (!(getDatastoreReadWriter() instanceof Datastore)) {
		throw new DatastoreDataException(
				"This DatastoreReadWriter cannot be used to run transactions. A full Datastore service"
						+ " object is required to run functions as transactions. Ensure that this method "
						+ "was not called in an ongoing transaction.");
	}
	return ((Datastore) getDatastoreReadWriter())
			.runInTransaction(
			(DatastoreReaderWriter readerWriter) -> operations.apply(new DatastoreTemplate(() -> readerWriter,
					DatastoreTemplate.this.datastoreEntityConverter,
					DatastoreTemplate.this.datastoreMappingContext,
					DatastoreTemplate.this.objectToKeyFactory)));
}
 
Example 6
Source Project: spring-cloud-gcp   Source File: DatastoreTemplate.java    License: Apache License 2.0 5 votes vote down vote up
private DatastoreReaderWriter getDatastoreReadWriter() {
	if (TransactionSynchronizationManager.isActualTransactionActive()) {
		DatastoreTransactionManager.Tx tx = (DatastoreTransactionManager.Tx) TransactionSynchronizationManager
				.getResource(this.datastore.get());
		if (tx != null && tx.getTransaction() != null) {
			return tx.getTransaction();
		}
	}
	return this.datastore.get();
}
 
Example 7
@Bean
@ConditionalOnMissingBean
public DatastoreTemplate datastoreTemplate(Supplier<? extends DatastoreReaderWriter> datastore,
		DatastoreMappingContext datastoreMappingContext,
		DatastoreEntityConverter datastoreEntityConverter, ObjectToKeyFactory objectToKeyFactory) {
	return new DatastoreTemplate(datastore, datastoreEntityConverter, datastoreMappingContext, objectToKeyFactory);
}
 
Example 8
Source Project: styx   Source File: InstrumentedDatastoreReaderWriter.java    License: Apache License 2.0 5 votes vote down vote up
static InstrumentedDatastoreReaderWriter of(Stats stats, DatastoreReaderWriter readerWriter) {
  Objects.requireNonNull(stats, "stats");
  Objects.requireNonNull(readerWriter, "readerWriter");
  return new InstrumentedDatastoreReaderWriter() {
    @Override
    public DatastoreReaderWriter readerWriter() {
      return readerWriter;
    }

    @Override
    public Stats stats() {
      return stats;
    }
  };
}
 
Example 9
Source Project: styx   Source File: CheckedDatastoreReaderWriter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link CheckedDatastoreReaderWriter} wrapping a {@link DatastoreReaderWriter}.
 */
CheckedDatastoreReaderWriter(DatastoreReaderWriter rw) {
  this.rw = Objects.requireNonNull(rw);
}
 
Example 10
Source Project: styx   Source File: InstrumentedTransaction.java    License: Apache License 2.0 4 votes vote down vote up
@Override
default DatastoreReaderWriter readerWriter() {
  return transaction();
}
 
Example 11
Source Project: styx   Source File: InstrumentedDatastore.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public DatastoreReaderWriter readerWriter() {
  return delegate;
}
 
Example 12
Source Project: styx   Source File: InstrumentedDatastoreTest.java    License: Apache License 2.0 4 votes vote down vote up
private void testReaderWriter(DatastoreReaderWriter instrumented, DatastoreReaderWriter delegate) {
  testReader(instrumented, delegate);
  testWriter(instrumented, delegate);
}
 
Example 13
Source Project: styx   Source File: InstrumentedDatastoreReaderWriter.java    License: Apache License 2.0 votes vote down vote up
DatastoreReaderWriter readerWriter();