-
-
Save daudrain/da4cb802aa99af22b426e96d4ba0484e to your computer and use it in GitHub Desktop.
Add ChangeListener to Couchbase Database that logs conflicting documents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
database.addChangeListener(new Database.ChangeListener() { | |
@Override | |
public void changed(Database.ChangeEvent event) { | |
List<DocumentChange> changes = event.getChanges(); | |
if (changes != null && changes.size() > 0) { | |
Log.d("CBL", String.format("Found %d changes", changes.size())); | |
for (int i = 0; i < changes.size(); i++) { | |
DocumentChange documentChange = changes.get(i); | |
if (documentChange.isConflict()) { | |
Log.w("CBL", String.format("Conflict document %s", documentChange.getDocumentId())); | |
} | |
} | |
} | |
} | |
}); | |
public static final int listConflicts(Context context, String databaseName) { | |
final CBAndroidContext cbAndroidContext = new CBAndroidContext(context); | |
Manager manager = null; | |
try { | |
manager = new Manager(cbAndroidContext, Manager.DEFAULT_OPTIONS); | |
} catch (IOException e) { | |
Log.e("CBL", "Could not create manager", e); | |
} | |
int result = 0; | |
if (manager != null) | |
{ | |
try { | |
Database database = manager.getDatabase(databaseName); | |
final Query query = database.createAllDocumentsQuery(); | |
//query.setAllDocsMode(Query.AllDocsMode.ONLY_CONFLICTS); | |
QueryEnumerator queryEnumerator = query.run(); | |
Iterator<QueryRow> iterator = queryEnumerator.iterator(); | |
while (iterator.hasNext()) | |
{ | |
QueryRow row = iterator.next(); | |
List<SavedRevision> revisions = row.getDocument().getConflictingRevisions(); | |
if (revisions.size() > 1) | |
{ | |
for (SavedRevision revision : revisions ) { | |
Log.d("CBL", String.format("conflict %s", revision.toString())); | |
result++; | |
} | |
} | |
/* | |
for (SavedRevision revision : row.getDocument().getRevisionHistory()) { | |
Log.d("CBL", String.format("history %s", revision.toString())); | |
} | |
*/ | |
} | |
database.close(); | |
} catch (CouchbaseLiteException e) | |
{ | |
Log.e("CBL", "Could not open database", e); | |
} | |
manager.close(); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment