/** * Does data migration ({@link #isMigrationNeeded(Key)} and {@link #migrateEntity(Key)}) in transaction * to ensure data consistency. */ private void migrate(Key<T> entityKey) { Runnable task = () -> { try { if (!isMigrationNeeded(entityKey)) { return; } numberOfAffectedEntities.incrementAndGet(); if (!isPreview()) { migrateEntity(entityKey); numberOfUpdatedEntities.incrementAndGet(); } } catch (Exception e) { System.err.println("Problem migrating entity with key: " + entityKey); System.err.println(e.getMessage()); } }; if (shouldUseTransaction()) { ofy().transact(task); } else { task.run(); } }
private void dualWriteToHrd(final RecordChangeType changeType, final TypedRecordUpdate update, final long newVersion, final Map<ResourceId, FieldValue> values) { ofy().transact(new VoidWork() { @Override public void vrun() { FormEntity rootEntity = new FormEntity(); rootEntity.setId(activity.getSiteFormClassId()); rootEntity.setVersion(activity.getVersion()); rootEntity.setSchemaVersion(activity.getActivityVersion().getSchemaVersion()); FormRecordEntity recordEntity = new FormRecordEntity(activity.getSiteFormClassId(), update.getRecordId()); recordEntity.setVersion(newVersion); recordEntity.setSchemaVersion(activity.getActivityVersion().getSchemaVersion()); recordEntity.setFieldValues(getFormClass(), values); FormRecordSnapshotEntity snapshot = new FormRecordSnapshotEntity(update.getUserId(), changeType, recordEntity); if (changeType == RecordChangeType.DELETED) { ofy().save().entities(rootEntity, snapshot); ofy().delete().entities(recordEntity); } else { ofy().save().entities(rootEntity, recordEntity, snapshot); } } }); }
@Override public void vrun() { // For each shard, reset it. Shard Index starts at 0. for (int shardNumber = 0; shardNumber < numCounterShards; shardNumber++) { final ResetCounterShardWork resetWork = new ResetCounterShardWork(counterName, shardNumber); ObjectifyService.ofy().transact(resetWork); } } });
@Override public List<RecordVersion> getVersions(ResourceId recordId) { return ofy().transact(QueryVersions.of(formClass, recordId)); }
@Override public List<RecordVersion> getVersionsForParent(ResourceId parentRecordId) { return ofy().transact(QueryVersions.subRecords(formClass, parentRecordId)); }
@Override public void updateFormClass(FormClass formClass) { ofy().transact(new CreateOrUpdateForm(formClass)); }
@Override public List<FormRecord> getSubRecords(ResourceId parentId) { return ofy().transact(new QuerySubRecords(formClass, parentId)); }
ObjectifyService.ofy().transact(new Work<Void>()
public HrdFormStorage create(FormClass formClass) { Hrd.ofy().transact(new CreateOrUpdateForm(formClass)); return new HrdFormStorage(formClass); }
@Override public void update(final TypedRecordUpdate update) { ofy().transact(new CreateOrUpdateRecord(formClass.getId(), update)); }
@Override public void add(TypedRecordUpdate update) { ofy().transact(new CreateOrUpdateRecord(formClass.getId(), update)); }
@Override public List<RecordVersion> getVersions(ResourceId recordId) { List<RecordVersion> versions = new ArrayList<>(); // Read first from legacy sitehistory table SiteHistoryReader tableReader = new SiteHistoryReader(queryExecutor, activity, getFormClass(), CuidAdapter.getLegacyIdFromCuid(recordId)); try { versions.addAll(tableReader.read()); } catch (SQLException e) { throw new RuntimeException(e); } // Now read additional entries from HRD versions.addAll(ofy().transact(QueryVersions.of(getFormClass(), recordId))); return versions; }
return ObjectifyService.ofy().transact(new Work<CounterData>()
/** * Overidden so that all calls to {@link #increment} occur inside of an existing Transaction. * * @param counterName * @param requestedIncrementAmount * @return */ @Override public CounterOperation increment(final String counterName, final long requestedIncrementAmount) { return ObjectifyService.ofy().transact(new Work<CounterOperation>() { @Override public CounterOperation run() { // 1.) Create a random CounterShardData for simulation purposes. It doesn't do anything except // to allow us to do something else in the Datastore in the same transactional context whilst // performing all unit tests. This effectively allows us to simulate a parent transactional context // occuring with some other data operation being performed against the database. final CounterShardData counterShardData = new CounterShardData(UUID.randomUUID().toString(), 1); ObjectifyService.ofy().save().entity(counterShardData); // 2.) Operate on the counter and return. return ShardedCounterServiceTxWrapper.super.increment(counterName, requestedIncrementAmount); } }); } }
@Test public void testInserts() throws Exception { final int N = Integer.parseInt(getTestSystemProperty("benchmark.datastore.size", "6000")); log.info(String.format(">>>> N = %s", N)); final Key<Root> parent = getRootKey(); // wrap inserts in same Tx -- as expected long txStart = System.currentTimeMillis(); ObjectifyService.ofy().transact(new VoidWork() { public void vrun() { doInsert(generateData(N, parent)); } }); long txEnd = System.currentTimeMillis(); logDuration("Full Tx ", txStart, txEnd); // do it w/o Tx doInsert(generateData(N, parent)); }
Hrd.ofy().transact(new CreateOrUpdateRecord(getFormClass().getId(), update)); return;
Hrd.ofy().transact(new CreateOrUpdateRecord(getFormClass().getId(), update)); return;
ObjectifyService.ofy().transact(new VoidWork()
final CounterOperation counterShardOperationInTx = ObjectifyService.ofy().transact(atomicIncrementShardWork);