Source:
private void pickWinnerOfConflicts(SQLDatabase db, long docNumericId, String docId, long previousWinnerSeq) throws DatastoreException { List<String> leafs = new ArrayList<String>(); String sql = "SELECT revs.revid FROM revs " + "WHERE revs.doc_id = ? " + "AND revs.deleted = 0 AND revs.sequence NOT IN " + "(SELECT DISTINCT parent FROM revs WHERE parent NOT NULL) "; try { Cursor cursor = db.rawQuery(sql, new String[]{"" + docNumericId}); while (cursor.moveToNext()) { leafs.add(cursor.getString(0)); } } catch (SQLException sqe) { throw new DatastoreException("Exception thrown whilst trying to fetch non-deleted leaf nodes in pickWinnerOfConflicts", sqe); } if (leafs.size() == 0) { sql = "SELECT revs.revid FROM revs " + "WHERE revs.doc_id = ? " + "AND revs.sequence NOT IN " + "(SELECT DISTINCT parent FROM revs WHERE parent NOT NULL) "; try { Cursor cursor = db.rawQuery(sql, new String[]{"" + docNumericId}); while (cursor.moveToNext()) { leafs.add(cursor.getString(0)); } } catch (SQLException sqe) { throw new DatastoreException("Exception thrown whilst trying to fetch all leaf nodes in pickWinnerOfConflicts", sqe); } } Collections.sort(leafs, new Comparator<String>() { @Override public int compare(String r1, String r2) { int generationCompare = CouchUtils.generationFromRevId(r1) - CouchUtils.generationFromRevId(r2); if (generationCompare != 0) { return -generationCompare; } else { return -CouchUtils.getRevisionIdSuffix(r1).compareTo(CouchUtils .getRevisionIdSuffix(r2)); } } }); String leaf = leafs.get(0); long newWinnerSeq = getSequenceInQueue(db, docId, leaf); if (previousWinnerSeq != newWinnerSeq) { this.changeDocumentToBeNotCurrent(db, previousWinnerSeq); this.changeDocumentToBeCurrent(db, newWinnerSeq); } }

Target:
private void pickWinnerOfConflicts(SQLDatabase db, long docNumericId, String docId, long previousWinnerSeq) throws DatastoreException { List<String> leafs = new ArrayList<String>(); Cursor cursor = null; try { cursor = db.rawQuery(GET_NON_DELETED_LEAFS, new String[]{Long.toString(docNumericId)}); while (cursor.moveToNext()) { leafs.add(cursor.getString(0)); } } catch (SQLException sqe) { throw new DatastoreException("Exception thrown whilst trying to fetch non-deleted leaf nodes in pickWinnerOfConflicts", sqe); } finally { DatabaseUtils.closeCursorQuietly(cursor); } if (leafs.size() == 0) { try { cursor = db.rawQuery(GET_ALL_LEAFS, new String[]{Long.toString(docNumericId)}); while (cursor.moveToNext()) { leafs.add(cursor.getString(0)); } } catch (SQLException sqe) { throw new DatastoreException("Exception thrown whilst trying to fetch all leaf nodes in pickWinnerOfConflicts", sqe); } finally { DatabaseUtils.closeCursorQuietly(cursor); } } Collections.sort(leafs, new Comparator<String>() { @Override public int compare(String r1, String r2) { int generationCompare = CouchUtils.generationFromRevId(r1) - CouchUtils.generationFromRevId(r2); if (generationCompare != 0) { return -generationCompare; } else { return -CouchUtils.getRevisionIdSuffix(r1).compareTo(CouchUtils .getRevisionIdSuffix(r2)); } } }); String leaf = leafs.get(0); long newWinnerSeq = getSequenceInQueue(db, docId, leaf); if (previousWinnerSeq != newWinnerSeq) { this.changeDocumentToBeNotCurrent(db, previousWinnerSeq); this.changeDocumentToBeCurrent(db, newWinnerSeq); } }