Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 7919ac3

Browse files
author
Hideki Itakura
authored
Fixed #1652 - App crash with deleteDocument with concurrencyControl (#1656)
1 parent c315f28 commit 7919ac3

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/DatabaseTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,24 @@ public void testDeleteNonExistingDoc() throws CouchbaseLiteException {
17451745
db.delete(doc1b);
17461746
assertEquals(0, db.getCount());
17471747
assertNull(db.getDocument(doc1b.getId()));
1748+
}
1749+
1750+
// https://github.com/couchbase/couchbase-lite-android/issues/1652
1751+
@Test
1752+
public void testDeleteWithOldDocInstance() throws CouchbaseLiteException {
1753+
// 1. save
1754+
MutableDocument mdoc = new MutableDocument("doc");
1755+
mdoc.setBoolean("updated", false);
1756+
db.save(mdoc);
17481757

1758+
// 2. update
1759+
Document doc = db.getDocument("doc");
1760+
mdoc = doc.toMutable();
1761+
mdoc.setBoolean("updated", true);
1762+
db.save(mdoc);
1763+
1764+
// 3. delete by previously retrived document
1765+
db.delete(doc);
1766+
assertNull(db.getDocument("doc"));
17491767
}
17501768
}

android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/LoadTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,51 @@ boolean updateMap(Map map, int i, long l) {
323323
}
324324
return true;
325325
}
326+
327+
// NOTE: not yet activated
328+
// @Test
329+
public void testAddRevisions() {
330+
final int revs = 10000;
331+
addRevisions(revs, false);
332+
addRevisions(revs, true);
333+
}
334+
335+
void addRevisions(final int revisions, final boolean retriveNewDoc) {
336+
try {
337+
db.inBatch(new Runnable() {
338+
@Override
339+
public void run() {
340+
try {
341+
MutableDocument mDoc = new MutableDocument("doc");
342+
if (retriveNewDoc)
343+
updateDocWithGetDocument(mDoc, revisions);
344+
else
345+
updateDoc(mDoc, revisions);
346+
} catch (CouchbaseLiteException e) {
347+
e.printStackTrace();
348+
}
349+
}
350+
});
351+
Document doc = db.getDocument("doc");
352+
assertEquals(revisions - 1, doc.getInt("count")); // start from 0.
353+
} catch (CouchbaseLiteException e) {
354+
e.printStackTrace();
355+
}
356+
}
357+
358+
void updateDoc(MutableDocument doc, final int revisions) throws CouchbaseLiteException {
359+
for (int i = 0; i < revisions; i++) {
360+
doc.setValue("count", i);
361+
db.save(doc);
362+
System.gc();
363+
}
364+
}
365+
366+
void updateDocWithGetDocument(MutableDocument doc, final int revisions) throws CouchbaseLiteException {
367+
for (int i = 0; i < revisions; i++) {
368+
doc.setValue("count", i);
369+
db.save(doc);
370+
doc = db.getDocument("doc").toMutable();
371+
}
372+
}
326373
}

shared/src/main/java/com/couchbase/lite/AbstractDatabase.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ public static void setLogLevel(LogDomain domain, LogLevel level) {
621621
public String toString() {
622622
return "Database@" + Integer.toHexString(hashCode()) + "{" +
623623
"name='" + name + '\'' +
624-
//"dir='" + (c4db != null ? c4db.getPath() : "[closed]") + '\'' +
625624
'}';
626625
}
627626

@@ -716,14 +715,6 @@ SharedKeys getSharedKeys() {
716715
}
717716

718717
//////// DOCUMENTS:
719-
C4Document read(String docID, boolean mustExist)
720-
throws CouchbaseLiteException {
721-
try {
722-
return getC4Database().get(docID, mustExist);
723-
} catch (LiteCoreException e) {
724-
throw CBLStatus.convertException(e);
725-
}
726-
}
727718

728719
Set<Replicator> getActiveReplications() {
729720
return activeReplications;
@@ -824,17 +815,15 @@ private File getDefaultDirectory() {
824815
}
825816

826817
private void setupDirectory(File dir) throws CouchbaseLiteException {
827-
if (!dir.exists()) {
818+
if (!dir.exists())
828819
dir.mkdirs();
829-
}
830820
if (!dir.isDirectory()) {
831821
throw new CouchbaseLiteException(String.format(Locale.ENGLISH,
832822
"Unable to create directory for: %s", dir));
833823
}
834824
}
835825

836826
private static File getDatabasePath(File dir, String name) {
837-
// TODO:
838827
name = name.replaceAll("/", ":");
839828
name = String.format(Locale.ENGLISH, "%s.%s", name, DB_EXTENSION);
840829
return new File(dir, name);

shared/src/main/java/com/couchbase/lite/Document.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,7 @@ void setC4Document(C4Document c4doc) {
420420

421421
void replaceC4Document(C4Document c4doc) {
422422
synchronized (lock) {
423-
C4Document oldC4Doc = this._c4doc;
424423
this._c4doc = c4doc;
425-
if (oldC4Doc != null && (c4doc == null || !oldC4Doc.equals(c4doc)))
426-
oldC4Doc.free();
427424
}
428425
}
429426

0 commit comments

Comments
 (0)