com.google.datastore.v1.TransactionOptions Java Examples

The following examples show how to use com.google.datastore.v1.TransactionOptions. 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: ShardedCounterMetricsStore.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 6 votes vote down vote up
private Long getCount(String fieldName) {
  Transaction txn = datastore.newTransaction(
      TransactionOptions.newBuilder()
          .setReadOnly(ReadOnly.newBuilder().build())
          .build()
  );

  QueryResults<ProjectionEntity> results;
  try {
    Query<ProjectionEntity> countQuery = Query.newProjectionEntityQueryBuilder()
        .setKind(SHARD)
        .setNamespace(namespace)
        .setProjection(fieldName)
        .build();

    results = datastore.run(countQuery);
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(results, Spliterator.NONNULL), false)
        .map(entity -> Long.valueOf(entity.getLong(fieldName)))
        .reduce(0L, (valueA, valueB) -> valueA + valueB);
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
}
 
Example #2
Source File: InstrumentedDatastoreTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void newTransaction() {
  // Transaction newTransaction(TransactionOptions options);
  when(datastore.newTransaction(any(TransactionOptions.class))).thenReturn(transaction);
  var transactionOptions = TransactionOptions.newBuilder().build();
  var instrumented1 = instrumentedDatastore.newTransaction(transactionOptions);
  verify(datastore).newTransaction(transactionOptions);
  verifyNoMoreInteractions(datastore);
  reset(datastore);
  testTransaction(instrumented1);

  // Transaction newTransaction();
  when(datastore.newTransaction()).thenReturn(transaction);
  var instrumented2 = instrumentedDatastore.newTransaction();
  verify(datastore).newTransaction();
  verifyNoMoreInteractions(datastore);
  reset(datastore);
  testTransaction(instrumented2);
}
 
Example #3
Source File: InstrumentedDatastoreTest.java    From styx with Apache License 2.0 6 votes vote down vote up
@Test
public void runInTransactionWithOptions() {
  when(datastore.runInTransaction(any(), any())).then(a ->
      a.<TransactionCallable<String>>getArgument(0)
          .run(transaction));

  var transactionOptions = TransactionOptions.newBuilder().build();

  var foobar = instrumentedDatastore.runInTransaction(tx -> {
    testReaderWriter(tx, transaction);
    return "foobar";
  }, transactionOptions);

  verify(datastore).runInTransaction(any(), eq(transactionOptions));
  verifyNoMoreInteractions(datastore);

  assertThat(foobar, is("foobar"));
}
 
Example #4
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionalSingleEntityGroupReadOnly() {
  setUpQueryTests();
  Key taskListKey = datastore.newKeyFactory().setKind("TaskList").newKey("default");
  Entity taskListEntity = Entity.newBuilder(taskListKey).build();
  datastore.put(taskListEntity);
  // [START datastore_transactional_single_entity_group_read_only]
  Entity taskList;
  QueryResults<Entity> tasks;
  Transaction txn = datastore.newTransaction(
      TransactionOptions.newBuilder()
          .setReadOnly(ReadOnly.newBuilder().build())
          .build()
  );
  try {
    taskList = txn.get(taskListKey);
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("Task")
        .setFilter(PropertyFilter.hasAncestor(taskListKey))
        .build();
    tasks = txn.run(query);
    txn.commit();
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
  // [END datastore_transactional_single_entity_group_read_only]
  assertEquals(taskListEntity, taskList);
  assertNotNull(tasks.next());
  assertFalse(tasks.hasNext());
}
 
Example #5
Source File: InstrumentedDatastore.java    From styx with Apache License 2.0 4 votes vote down vote up
@Override
public Transaction newTransaction(TransactionOptions transactionOptions) {
  return InstrumentedTransaction.of(stats, delegate.newTransaction(transactionOptions));
}
 
Example #6
Source File: InstrumentedDatastore.java    From styx with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T runInTransaction(TransactionCallable<T> transactionCallable, TransactionOptions transactionOptions) {
  return delegate.runInTransaction(rw ->
      transactionCallable.run(InstrumentedDatastoreReaderWriter.of(stats, rw)), transactionOptions);
}