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

The following examples show how to use com.google.cloud.datastore.Transaction#commit() . 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: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
void transferFunds(Key fromKey, Key toKey, long amount) {
  Transaction txn = datastore.newTransaction();
  try {
    List<Entity> entities = txn.fetch(fromKey, toKey);
    Entity from = entities.get(0);
    Entity updatedFrom =
        Entity.newBuilder(from).set("balance", from.getLong("balance") - amount).build();
    Entity to = entities.get(1);
    Entity updatedTo = Entity.newBuilder(to).set("balance", to.getLong("balance") + amount)
        .build();
    txn.put(updatedFrom, updatedTo);
    txn.commit();
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
}
 
Example 3
Source File: TaskList.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Marks a task entity as done.
 *
 * @param id The ID of the task entity as given by {@link Key#id()}
 * @return true if the task was found, false if not
 * @throws DatastoreException if the transaction fails
 */
boolean markDone(long id) {
  Transaction transaction = datastore.newTransaction();
  try {
    Entity task = transaction.get(keyFactory.newKey(id));
    if (task != null) {
      transaction.put(Entity.newBuilder(task).set("done", true).build());
    }
    transaction.commit();
    return task != null;
  } 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: 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 6
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 7
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionalGetOrCreate() {
  // [START datastore_transactional_get_or_create]
  Entity task;
  Transaction txn = datastore.newTransaction();
  try {
    task = txn.get(taskKey);
    if (task == null) {
      task = Entity.newBuilder(taskKey).build();
      txn.put(task);
      txn.commit();
    }
  } finally {
    if (txn.isActive()) {
      txn.rollback();
    }
  }
  // [END datastore_transactional_get_or_create]
  assertEquals(task, datastore.get(taskKey));
}
 
Example 8
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public void addCounts(UUID jobId, Map<String, Integer> newCounts) throws IOException {
  if (newCounts == null) {
    return;
  }

  Transaction transaction = datastore.newTransaction();

  for (String dataType : newCounts.keySet()) {
    Key key = getCountsKey(jobId, dataType);
    Entity current = datastore.get(key);
    Integer oldCount = 0;

    if (current != null && current.getNames().contains(COUNTS_FIELD)) {
      // Datastore only allows Long properties, but we only ever write Integers through this
      // interface so the conversion is OK
      oldCount = Math.toIntExact(current.getLong(COUNTS_FIELD));
    }
    transaction.put(
        GoogleCloudUtils.createEntityBuilder(
                key, ImmutableMap.of(COUNTS_FIELD, oldCount + newCounts.get(dataType)))
            .build());
  }
  transaction.commit();
}
 
Example 9
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends DataModel> void update(UUID jobId, String key, T model) {
  Transaction transaction = datastore.newTransaction();
  Key entityKey = getDataKey(jobId, key);

  try {
    Entity previousEntity = transaction.get(entityKey);
    if (previousEntity == null) {
      throw new IOException("Could not find record for data key: " + entityKey.getName());
    }

    String serialized = objectMapper.writeValueAsString(model);
    Entity entity =
        Entity.newBuilder(entityKey)
            .set(CREATED_FIELD, Timestamp.now())
            .set(model.getClass().getName(), serialized)
            .build();

    transaction.put(entity);
    transaction.commit();
  } catch (IOException t) {
    transaction.rollback();
    throw new RuntimeException("Failed atomic update of key: " + key, t);
  }
}
 
Example 10
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts a new {@link PortabilityJob} keyed by {@code jobId} in Datastore.
 *
 * <p>To update an existing {@link PortabilityJob} instead, use {@link JobStore#update}.
 *
 * @throws IOException if a job already exists for {@code jobId}, or if there was a different
 *     problem inserting the job.
 */
@Override
public void createJob(UUID jobId, PortabilityJob job) throws IOException {
  Preconditions.checkNotNull(jobId);
  Transaction transaction = datastore.newTransaction();
  Entity shouldNotExist = transaction.get(getJobKey(jobId));
  if (shouldNotExist != null) {
    transaction.rollback();
    throw new IOException(
        "Record already exists for jobID: " + jobId + ". Record: " + shouldNotExist);
  }
  Entity entity = createNewEntity(jobId, job.toMap());
  try {
    transaction.put(entity);
  } catch (DatastoreException e) {
    transaction.rollback();
    throw new IOException(
        "Could not create initial record for jobID: " + jobId + ". Record: " + entity, e);
  }
  transaction.commit();
}
 
Example 11
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
/**
 * Worker method for updating the given entity with optimistic locking.
 * 
 * @param entity
 *          the entity to update
 * @param versionMetadata
 *          the metadata for optimistic locking
 * @return the updated entity
 */
@SuppressWarnings("unchecked")
protected <E> E updateWithOptimisticLockingInternal(E entity, PropertyMetadata versionMetadata) {
  Transaction transaction = null;
  try {
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entity);
    Entity nativeEntity = (Entity) Marshaller.marshal(entityManager, entity, Intent.UPDATE);
    transaction = datastore.newTransaction();
    Entity storedNativeEntity = transaction.get(nativeEntity.getKey());
    if (storedNativeEntity == null) {
      throw new OptimisticLockException(
          String.format("Entity does not exist: %s", nativeEntity.getKey()));
    }
    String versionPropertyName = versionMetadata.getMappedName();
    long version = nativeEntity.getLong(versionPropertyName) - 1;
    long storedVersion = storedNativeEntity.getLong(versionPropertyName);
    if (version != storedVersion) {
      throw new OptimisticLockException(
          String.format("Expecting version %d, but found %d", version, storedVersion));
    }
    transaction.update(nativeEntity);
    transaction.commit();
    E updatedEntity = (E) Unmarshaller.unmarshal(nativeEntity, entity.getClass());
    entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntity);
    return updatedEntity;
  } catch (DatastoreException exp) {
    throw DatastoreUtils.wrap(exp);
  } finally {
    rollbackIfActive(transaction);
  }
}
 
Example 12
Source File: InstrumentedDatastoreTest.java    From styx with Apache License 2.0 5 votes vote down vote up
private void testTransaction(Transaction instrumented) {
  testReaderWriter(instrumented, transaction);

  // void putWithDeferredIdAllocation(FullEntity<?>... entities);
  instrumented.putWithDeferredIdAllocation(TEST_ENTITY_1, TEST_ENTITY_2);
  verify(transaction).putWithDeferredIdAllocation(TEST_ENTITY_1, TEST_ENTITY_2);
  verify(stats).recordDatastoreEntityWrites(TEST_KIND_1, 1);
  verify(stats).recordDatastoreEntityWrites(TEST_KIND_2, 1);
  verifyNoMoreInteractions(stats);
  reset(stats);

  // boolean isActive();
  when(transaction.isActive()).thenReturn(true);
  assertThat(instrumented.isActive(), is(true));
  verify(transaction).isActive();

  // void rollback();
  instrumented.rollback();
  verify(transaction).rollback();

  // Response commit();
  instrumented.commit();
  verify(transaction).commit();

  // Datastore getDatastore();
  when(transaction.getDatastore()).thenReturn(datastore);
  var instrumentedDatastore = instrumented.getDatastore();
  assertThat(instrumentedDatastore, instanceOf(InstrumentedDatastore.class));
  assertThat(((InstrumentedDatastore) instrumentedDatastore).delegate(), is(datastore));
  verify(transaction).getDatastore();

  verifyNoMoreInteractions(transaction);
  reset(transaction);
}
 
Example 13
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the state value in each key-value pair in the project's datastore.
 * @param sessionId Request from which to extract session.
 * @param varName the name of the desired session variable
 * @param varValue the value of the desired session variable
 */
protected void setSessionVariables(String sessionId, Map<String, String> setMap) {
  if (sessionId.equals("")) {
    return;
  }
  Key key = keyFactory.newKey(sessionId);
  Transaction transaction = datastore.newTransaction();
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  dt.toString(dtf);
  try {
    Entity stateEntity = transaction.get(key);
    Entity.Builder seBuilder;
    if (stateEntity == null) {
      seBuilder = Entity.newBuilder(key);
    } else {
      seBuilder = Entity.newBuilder(stateEntity);
    }
    for (String varName : setMap.keySet()) {
      seBuilder.set(varName, setMap.get(varName));
    }
    transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build());
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 14
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the state value in each key-value pair in the project's datastore.
 *
 * @param sessionId Request from which to extract session.
 * @param varName   the name of the desired session variable
 * @param varValue  the value of the desired session variable
 */
protected void setSessionVariables(String sessionId, Map<String, String> setMap) {
  if (sessionId.equals("")) {
    return;
  }
  Key key = keyFactory.newKey(sessionId);
  Transaction transaction = datastore.newTransaction();
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  dt.toString(dtf);
  try {
    Entity stateEntity = transaction.get(key);
    Entity.Builder seBuilder;
    if (stateEntity == null) {
      seBuilder = Entity.newBuilder(key);
    } else {
      seBuilder = Entity.newBuilder(stateEntity);
    }
    for (String varName : setMap.keySet()) {
      seBuilder.set(varName, setMap.get(varName));
    }
    transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build());
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 15
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the state value in each key-value pair in the project's datastore.
 *
 * @param sessionId Request from which to extract session.
 * @param varName   the name of the desired session variable
 * @param varValue  the value of the desired session variable
 */
protected void setSessionVariables(String sessionId, Map<String, String> setMap) {
  if (sessionId.equals("")) {
    return;
  }
  Key key = keyFactory.newKey(sessionId);
  Transaction transaction = datastore.newTransaction();
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  dt.toString(dtf);
  try {
    Entity stateEntity = transaction.get(key);
    Entity.Builder seBuilder;
    if (stateEntity == null) {
      seBuilder = Entity.newBuilder(key);
    } else {
      seBuilder = Entity.newBuilder(stateEntity);
    }
    for (String varName : setMap.keySet()) {
      seBuilder.set(varName, setMap.get(varName));
    }
    transaction.put(seBuilder.set("lastModified", dt.toString(dtf)).build());
    transaction.commit();
  } finally {
    if (transaction.isActive()) {
      transaction.rollback();
    }
  }
}
 
Example 16
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends DataModel> void create(UUID jobId, String key, T model) throws IOException {
  Preconditions.checkNotNull(jobId);
  Transaction transaction = datastore.newTransaction();
  Key fullKey = getDataKey(jobId, key);
  Entity shouldNotExist = transaction.get(fullKey);
  if (shouldNotExist != null) {
    transaction.rollback();
    throw new IOException(
        "Record already exists for key: " + fullKey.getName() + ". Record: " + shouldNotExist);
  }

  String serialized = objectMapper.writeValueAsString(model);
  Entity entity =
      Entity.newBuilder(fullKey)
          .set(CREATED_FIELD, Timestamp.now())
          .set(model.getClass().getName(), serialized)
          .build();

  try {
    transaction.put(entity);
  } catch (DatastoreException e) {
    throw new IOException(
        "Could not create initial record for jobID: " + jobId + ". Record: " + entity, e);
  }
  transaction.commit();
}
 
Example 17
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 18
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies a {@code PortabilityJob} already exists for {@code jobId}, and updates the entry to
 * {@code job}, within a {@code Transaction}. If {@code validator} is non-null,
 * validator.validate() is called first in the transaction.
 *
 * @throws IOException if a job didn't already exist for {@code jobId} or there was a problem
 *     updating it @throws IllegalStateException if validator.validate() failed
 */
@Override
protected void updateJob(UUID jobId, PortabilityJob job, JobUpdateValidator validator)
    throws IOException {
  Preconditions.checkNotNull(jobId);
  Transaction transaction = datastore.newTransaction();
  Key key = getJobKey(jobId);

  try {
    Entity previousEntity = transaction.get(key);
    if (previousEntity == null) {
      throw new IOException("Could not find record for jobId: " + jobId);
    }

    if (validator != null) {
      PortabilityJob previousJob = PortabilityJob.fromMap(getProperties(previousEntity));
      validator.validate(previousJob, job);
    }

    Entity newEntity = createUpdatedEntity(key, job.toMap());
    transaction.put(newEntity);
    transaction.commit();
  } catch (Throwable t) {
    transaction.rollback();
    throw new IOException("Failed atomic update of jobId: " + jobId, t);
  }
}
 
Example 19
Source File: DefaultDatastoreWriter.java    From catatumbo with Apache License 2.0 4 votes vote down vote up
/**
 * Internal worker method for updating the entities using optimistic locking.
 * 
 * @param entities
 *          the entities to update
 * @param versionMetadata
 *          the metadata of the version property
 * @return the updated entities
 */
@SuppressWarnings("unchecked")
protected <E> List<E> updateWithOptimisticLockInternal(List<E> entities,
    PropertyMetadata versionMetadata) {
  Transaction transaction = null;
  try {
    entityManager.executeEntityListeners(CallbackType.PRE_UPDATE, entities);
    Entity[] nativeEntities = toNativeEntities(entities, entityManager, Intent.UPDATE);
    // The above native entities already have the version incremented by
    // the marshalling process
    Key[] nativeKeys = new Key[nativeEntities.length];
    for (int i = 0; i < nativeEntities.length; i++) {
      nativeKeys[i] = nativeEntities[i].getKey();
    }
    transaction = datastore.newTransaction();
    List<Entity> storedNativeEntities = transaction.fetch(nativeKeys);
    String versionPropertyName = versionMetadata.getMappedName();

    for (int i = 0; i < nativeEntities.length; i++) {
      long version = nativeEntities[i].getLong(versionPropertyName) - 1;
      Entity storedNativeEntity = storedNativeEntities.get(i);
      if (storedNativeEntity == null) {
        throw new OptimisticLockException(
            String.format("Entity does not exist: %s", nativeKeys[i]));
      }
      long storedVersion = storedNativeEntities.get(i).getLong(versionPropertyName);
      if (version != storedVersion) {
        throw new OptimisticLockException(
            String.format("Expecting version %d, but found %d", version, storedVersion));
      }
    }
    transaction.update(nativeEntities);
    transaction.commit();
    List<E> updatedEntities = (List<E>) toEntities(entities.get(0).getClass(), nativeEntities);
    entityManager.executeEntityListeners(CallbackType.POST_UPDATE, updatedEntities);
    return updatedEntities;

  } catch (DatastoreException exp) {
    throw DatastoreUtils.wrap(exp);
  } finally {
    rollbackIfActive(transaction);
  }

}
 
Example 20
Source File: ShardedCounterMetricsStore.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 4 votes vote down vote up
void flush() {
  if (!pending.isEmpty()) {
    log.debug("flush started, attempting to acquire permit");
    double wait = rateLimiter.acquire();
    log.debug("permit acquired after {} seconds", wait);
    Multimap<String, Mutation> toWrite = ArrayListMultimap.create();

    // drain the queue into a Map<shard, list<mutations_for_the_shard>>
    Mutation queued = pending.poll();
    while(queued != null) {
      toWrite.put(queued.getShard(), queued);
      queued = pending.poll();
    }

    // merge multimap of mutations into a list of single entities per shard to write as a batch
    List<FullEntity> list = new ArrayList<>();
    for (String shard: toWrite.keySet()) {
      Collection<Mutation> deltas = toWrite.get(shard);
      deltas.stream().reduce((deltaA, deltaB) ->
          new Mutation(shard,
              deltaA.getSizeDelta() + deltaB.getSizeDelta(),
              deltaA.getCountDelta() + deltaB.getCountDelta())
      ).ifPresent(merged -> {
        Entity shardCounter = getShardCounter(merged.getShard());
        FullEntity<Key> entity = FullEntity.newBuilder(shardCounter.getKey())
            .set(SIZE, shardCounter.getLong(SIZE) + merged.getSizeDelta())
            .set(COUNT, shardCounter.getLong(COUNT) + merged.getCountDelta())
            .build();
        list.add(entity);
      });
    }
    log.debug("sending {} mutations to datastore", list.size());
    // write the batch off to datastore
    if (!list.isEmpty()) {
      Transaction txn = datastore.newTransaction();
      try {
        txn.put(list.toArray(new FullEntity[list.size()]));
        txn.commit();
      } finally {
        if (txn.isActive()) {
          txn.rollback();
        }
      }
      log.debug("drained {} mutations to datastore", list.size());
    }
  }
}