@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(); }
@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); } }
/** * 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(getKey(jobId)); if (shouldNotExist != null) { transaction.rollback(); throw new IOException( "Record already exists for jobID: " + jobId + ". Record: " + shouldNotExist); } Entity entity = createEntity(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(); }
transaction.put(newEntity); transaction.commit(); } catch (Throwable t) {
/** Example of putting a single entity. */ // [TARGET put(FullEntity)] // [VARIABLE "my_key_name"] public void putSingleEntity(String keyName) { Datastore datastore = transaction.getDatastore(); // [START putSingleEntity] Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName); Entity.Builder entityBuilder = Entity.newBuilder(key); entityBuilder.set("propertyName", "value"); Entity entity = entityBuilder.build(); transaction.put(entity); transaction.commit(); // [END putSingleEntity] }
@Override public Entity put(final FullEntity<?> entity) { return raw.put(entity); }
@Override public List<Entity> put(final FullEntity<?>... entities) { return raw.put(entities); }
/** Example of rolling back a transaction. */ // [TARGET rollback()] public Key rollback() { Datastore datastore = transaction.getDatastore(); // [START rollback] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "rollback()").build(); // add the entity and rollback transaction.put(entity); transaction.rollback(); // calling transaction.commit() now would fail // [END rollback] return key; }
/** Example of committing a transaction. */ // [TARGET commit()] public Key commit() { Datastore datastore = transaction.getDatastore(); // [START commit] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "commit()").build(); // add the entity and commit try { transaction.put(entity); transaction.commit(); } catch (DatastoreException ex) { // handle exception } // [END commit] return key; }
/** Example of verifying if a transaction is active. */ // [TARGET isActive()] public Key isActive() { Datastore datastore = transaction.getDatastore(); // [START isActive] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "active()").build(); // calling transaction.active() now would return true try { // add the entity and commit transaction.put(entity); transaction.commit(); } finally { // if committing succeeded // then transaction.active() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END isActive] return key; } }
/** Example of verifying if a transaction is active. */ // [TARGET active()] public Key active() { Datastore datastore = transaction.getDatastore(); // [START active] // create an entity KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind"); Key key = datastore.allocateId(keyFactory.newKey()); Entity entity = Entity.newBuilder(key).set("description", "active()").build(); // calling transaction.active() now would return true try { // add the entity and commit transaction.put(entity); transaction.commit(); } finally { // if committing succeeded // then transaction.isActive() will be false if (transaction.isActive()) { // otherwise it's true and we need to rollback transaction.rollback(); } } // [END active] return key; }
/** Example of putting multiple entities. */ // [TARGET put(FullEntity...)] // [VARIABLE "my_key_name1"] // [VARIABLE "my_key_name2"] public void multiplePutEntities(String keyName1, String keyName2) { Datastore datastore = transaction.getDatastore(); // [START multiplePutEntities] Key key1 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName1); Entity.Builder entityBuilder1 = Entity.newBuilder(key1); entityBuilder1.set("propertyName", "value1"); Entity entity1 = entityBuilder1.build(); Key key2 = datastore.newKeyFactory().setKind("MyKind").newKey(keyName2); Entity.Builder entityBuilder2 = Entity.newBuilder(key2); entityBuilder2.set("propertyName", "value2"); Entity entity2 = entityBuilder2.build(); transaction.put(entity1, entity2); transaction.commit(); // [END multiplePutEntities] }
/** * 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(); } } } // [END datastore_update_entity]
@Override public Entity put(FullEntity<?> entity) { return transaction().put(entity); }
@Override public List<Entity> put(FullEntity<?>... entities) { return transaction().put(); }