@Override protected void migrateEntity(Key<FeedbackResponseComment> entity) { FeedbackResponseComment comment = ofy().load().key(entity).now(); comment.setCommentGiverType(FeedbackParticipantType.INSTRUCTORS); comment.setIsCommentFromFeedbackParticipant(false); ofy().save().entity(comment).now(); } }
@Override protected void migrateEntity(Key<Account> oldAccountKey) throws Exception { Account oldAccount = ofy().load().key(oldAccountKey).now(); String oldGoogleId = oldAccount.getGoogleId(); String newGoogleId = generateNewGoogleId(oldAccount); StudentProfile oldStudentProfile = ofy().load().key(oldStudentProfileKey).now(); List<CourseStudent> oldStudents = ofy().load().type(CourseStudent.class) .filter("googleId =", oldGoogleId).list(); List<Instructor> oldInstructors = ofy().load().type(Instructor.class) .filter("googleId =", oldGoogleId).list(); ofy().save().entities(oldStudents).now(); ofy().save().entities(oldInstructors).now(); instructorsDb.putDocuments( oldInstructors.stream().map(InstructorAttributes::valueOf).collect(Collectors.toList())); if (ofy().load().type(Account.class).id(newGoogleId).now() == null) { ofy().save().entity(oldAccount).now(); } else { println(String.format("Skip creation of new account as account (%s) already exists", newGoogleId)); ofy().delete().type(Account.class).id(oldGoogleId).now(); ofy().save().entity(oldStudentProfile).now(); ofy().delete().key(oldStudentProfileKey).now();
/** * 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(); } }
public void flush() { final List<Result<?>> futures = new ArrayList<>(); // Need to do this in a loop because @OnSave methods can enlist more deferred operations. Execution // of save or delete will undefer() all the relevant items, so both lists empty mean we're done. while (!operations.isEmpty() || !autogeneratedIdSaves.isEmpty()) { // Sort into two batch operations: one for save, one for delete. final List<Object> saves = new ArrayList<>(); final List<Key<?>> deletes = new ArrayList<>(); for (final Map.Entry<Key<?>, Object> entry : operations.entrySet()) { if (entry.getValue() == null) deletes.add(entry.getKey()); else saves.add(entry.getValue()); } saves.addAll(autogeneratedIdSaves); if (!saves.isEmpty()) futures.add(ofy.save().entities(saves)); if (!deletes.isEmpty()) futures.add(ofy.delete().keys(deletes)); } // Complete any pending operations for (final Result<?> future : futures) { future.now(); } }
@Override public T get() { return ofy().load().now(key()); }
@Override public <T> void delete(Class<T> clazz, long id) { this.base.delete(clazz, id); }
@Override public void vrun() { FormEntity rootEntity = ofy().load().key(FormEntity.key(formId)).safe(); FormClass formClass = ofy().load().key(FormSchemaEntity.key(formId)).safe().readFormClass(); FormRecordEntity existingEntity = ofy().load().key(FormRecordEntity.key(formClass, update.getRecordId())).now(); FormRecordEntity updated; RecordChangeType changeType; ColumnBlockUpdater blockUpdater = new ColumnBlockUpdater(rootEntity, formClass, Hrd.ofy().getTransaction()); ofy().save().entities(toSave); if(!toDelete.isEmpty()) { ofy().delete().entities(toDelete);
private void save(final SegmentHunk hunk) { ofy().save().entity(hunk); crc.update(hunk.bytes, lastFlushIndex, writer.position - lastFlushIndex); lastFlushIndex = writer.position; }
/** * Delete the segment using the specified {@link Objectify} useful for transaction. * @param objectify The {@link Objectify} to use * @param name The name of the segment to delete */ protected void deleteSegment(final Objectify objectify, final String name) { final Key<Segment> segmentKey = newSegmentKey(name); final Segment segment = objectify.load().key(segmentKey).now(); objectify.delete().keys(segment.getHunkKeys(segmentKey)); objectify.delete().key(segmentKey); }
final CounterData counterData = ObjectifyService.ofy().load().key(counterDataKey).now(); if (counterData == null) ObjectifyService.ofy().transactionless().delete().type(CounterShardOperationData.class) .parent(counterShardDataKey); ObjectifyService.ofy().transactionless().delete().keys(counterShardDataKeys).now(); ObjectifyService.ofy().transactionless().delete().key(counterData.getTypedKey()).now();
public static <T> T getOrCreate(final Key<T> key, final ObjectifyBuilder<T> builder) { final Objectify objectify = ofy(); T t = objectify.load().key(key).now(); if(t == null) { t = objectify.transactNew(4, new Work<T>() { @Override public T run() { T t = ofy().load().key(key).now(); if(t == null) { t = builder.newInstance(key); ofy().save().entity(t).now(); } return t; } }); } return t; }
@Override public UUID queue(Job job) { try { JobStatus jobStatus = new JobStatus(); UUID id = jobStatus.getId(); TaskOptions taskOptions = createTaskOptions(job, id); Queue queue = getQueue(job); Transaction transaction = ofy().getTransaction(); queue.add(transaction, taskOptions); ofy().save().entity(jobStatus); return id; } catch (Exception e) { throw new JobException(e, "Failed to queue job: %s", e.getMessage()); } }
/** * Delete the segment specified. * @param name The name of the segment */ protected void deleteSegment(final String name) { ofy().transactNew(4, new Work<Void>() { @Override public Void run() { deleteSegment(ofy(), name); return null; } }); } /**
/** * Get the current objectify instance associated with this ref */ private Objectify ofy() { // If we have an expired transaction context, we need a new context if (ofy == null || (ofy.getTransaction() != null && !ofy.getTransaction().isActive())) ofy = ObjectifyService.ofy(); return ofy; }
@Override protected boolean isMigrationNeeded(Key<FeedbackResponseComment> entity) { FeedbackResponseComment comment = ofy().load().key(entity).now(); try { Field commentGiverType = comment.getClass().getDeclaredField("commentGiverType"); commentGiverType.setAccessible(true); return commentGiverType.get(comment) == null; } catch (ReflectiveOperationException e) { return true; } }
@Override public void delete(Iterable<?> keysOrEntities) { this.base.delete(keysOrEntities); }
protected void doInsert(List<Data> list) { long start = System.currentTimeMillis(); Map<Key<Data>, Data> saved = ObjectifyService.ofy().save().entities(list).now(); long end = System.currentTimeMillis(); logDuration(String.format("Save [%d]", saved.size()), start, end); Assert.assertEquals(list.size(), saved.size()); }
@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); } } });