Java Code Examples for com.google.cloud.datastore.Transaction#run()

The following examples show how to use com.google.cloud.datastore.Transaction#run() . 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: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 2
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 3
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 4
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 6 votes vote down vote up
protected void deleteSessionWithValue(String varName, String varValue) {
  Transaction transaction = datastore.newTransaction();
  try {
    Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("SessionVariable")
        .setFilter(PropertyFilter.eq(varName, varValue))
        .build();
    QueryResults<Entity> resultList = transaction.run(query);
    while (resultList.hasNext()) {
      Entity stateEntity = resultList.next();
      transaction.delete(stateEntity.getKey());
    }
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 5
Source File: DatastoreExample.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
@Override
public void run(Transaction tx, Key userKey, Void arg) {
  Entity user = tx.get(userKey);
  if (user == null) {
    System.out.println("Nothing to delete, user does not exist.");
    return;
  }
  Query<Key> query =
      Query.newKeyQueryBuilder()
          .setNamespace(NAMESPACE)
          .setKind(COMMENT_KIND)
          .setFilter(PropertyFilter.hasAncestor(userKey))
          .build();
  QueryResults<Key> comments = tx.run(query);
  int count = 0;
  while (comments.hasNext()) {
    tx.delete(comments.next());
    count++;
  }
  tx.delete(userKey);
  System.out.printf("Deleting user '%s' and %d comment[s].%n", userKey.getName(), count);
}
 
Example 6
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 7
Source File: DatastoreExample.java    From google-cloud-java with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Transaction tx, Key userKey, Void arg) {
  Entity user = tx.get(userKey);
  if (user == null) {
    System.out.printf("User '%s' does not exist.%n", userKey.getName());
    return;
  }
  if (user.contains("contact")) {
    FullEntity<IncompleteKey> contact = user.getEntity("contact");
    String email = contact.getString("email");
    String phone = contact.getString("phone");
    System.out.printf(
        "User '%s' email is '%s', phone is '%s'.%n", userKey.getName(), email, phone);
  }
  System.out.printf("User '%s' has %d comment[s].%n", userKey.getName(), user.getLong("count"));
  int limit = 200;
  Map<Timestamp, String> sortedComments = new TreeMap<>();
  StructuredQuery<Entity> query =
      Query.newEntityQueryBuilder()
          .setNamespace(NAMESPACE)
          .setKind(COMMENT_KIND)
          .setFilter(PropertyFilter.hasAncestor(userKey))
          .setLimit(limit)
          .build();
  while (true) {
    QueryResults<Entity> results = tx.run(query);
    int resultCount = 0;
    while (results.hasNext()) {
      Entity result = results.next();
      sortedComments.put(result.getTimestamp("timestamp"), result.getString("content"));
      resultCount++;
    }
    if (resultCount < limit) {
      break;
    }
    query = query.toBuilder().setStartCursor(results.getCursorAfter()).build();
  }
  // We could have added "ORDER BY timestamp" to the query to avoid sorting, but that would
  // require adding an ancestor index for timestamp.
  // See: https://cloud.google.com/datastore/docs/tools/indexconfig
  for (Map.Entry<Timestamp, String> entry : sortedComments.entrySet()) {
    System.out.printf("\t%s: %s%n", entry.getKey(), entry.getValue());
  }
}