Source:
private void processQuestions(DatastoreService ds) { System.out.println("#Processing Questions"); final Query qq = new Query("Question"); final PreparedQuery qpq = ds.prepare(qq); List<Entity> questionsToFix = new ArrayList<Entity>(); List<Key> questionsToKill = new ArrayList<Key>(); for (Entity q : qpq.asIterable(FetchOptions.Builder.withChunkSize(500))) { Long questionId = q.getKey().getId(); Long questionSurvey = (Long) q.getProperty("surveyId"); Long questionGroup = (Long) q.getProperty("questionGroupId"); String questionText = (String) q.getProperty("text"); Long questionGroupSurvey = (Long) qgToSurvey.get(questionGroup); if (questionGroup == null || questionGroupSurvey == null) { System.out.printf("#ERR: Question %d '%s',survey %d, group %d\n", questionId, questionText, questionSurvey, questionGroup); orphanQuestions++; if (deleteOrphans){ System.out.println(q.toString()); q.setProperty("surveyId", questionGroupSurvey); questionsToKill.add(q.getKey()); } } else { qToSurvey.put(questionId, questionSurvey); if (!questionSurvey.equals(questionGroupSurvey)) { System.out.printf("#ERR: Question %d '%s' in survey %d, but group %d is in survey %d\n", questionId, questionText, questionSurvey, questionGroup, questionGroupSurvey); if (fixSurveyPointers){ System.out.println(q.toString()); q.setProperty("surveyId", questionGroupSurvey); questionsToFix.add(q); } unreachableQuestions++; } else { goodQuestions++; } } } if (questionsToFix.size() > 0) { System.out.printf("#Fixing %d Questions\n",questionsToFix.size()); batchSaveEntities(ds, questionsToFix); } if (questionsToKill.size() > 0) { System.out.printf("#Deleting %d Questions\n",questionsToKill.size()); batchDelete(ds, questionsToKill); } }

Target:
private void processQuestions(DatastoreService ds) { System.out.println("#Processing Questions"); final Query qq = new Query("Question"); final PreparedQuery qpq = ds.prepare(qq); List<Entity> questionsToFix = new ArrayList<Entity>(); List<Key> questionsToKill = new ArrayList<Key>(); for (Entity q : qpq.asIterable(FetchOptions.Builder.withChunkSize(500))) { Long questionId = q.getKey().getId(); Long questionSurvey = (Long) q.getProperty("surveyId"); Long questionGroup = (Long) q.getProperty("questionGroupId"); String questionText = (String) q.getProperty("text"); Long questionGroupSurvey = (Long) qgToSurvey.get(questionGroup); if (questionGroup == null || questionGroupSurvey == null) { System.out.printf("#ERR: Question %d '%s',survey %d, group %d\n", questionId, questionText, questionSurvey, questionGroup); orphanQuestions++; if (deleteOrphans){ System.out.println(q.toString()); q.setProperty("surveyId", questionGroupSurvey); questionsToKill.add(q.getKey()); } } else { qToSurvey.put(questionId, questionSurvey); if (!questionSurvey.equals(questionGroupSurvey)) { System.out.printf("#ERR: Question %d '%s' in survey %d, but group %d is in survey %d\n", questionId, questionText, questionSurvey, questionGroup, questionGroupSurvey); if (fixSurveyPointers){ System.out.println(q.toString()); q.setProperty("surveyId", questionGroupSurvey); questionsToFix.add(q); } unreachableQuestions++; } else { goodQuestions++; } } } if (deleteOrphans) { System.out.printf("#Fixing %d Questions\n",questionsToFix.size()); batchSaveEntities(ds, questionsToFix); } if (fixSurveyPointers) { System.out.printf("#Deleting %d Questions\n",questionsToKill.size()); batchDelete(ds, questionsToKill); } }