Purge does not update view on ForestDB


#1

I had some errors that there are no revision in a document.
I think that it is occurred when I purged a document. So I made a test.

public void testPurge() throws IOException, CouchbaseLiteException {
    Manager manager = new Manager(new AndroidContext(mContext), Manager.DEFAULT_OPTIONS);
    DatabaseOptions options = new DatabaseOptions();
    options.setCreate(true);
    options.setStorageType(Manager.FORESTDB_STORAGE);
    Database database = manager.openDatabase("test_purge", options);

    View view = database.getView("bookmarks");
    view.setMap(new Mapper() {
        @Override
        public void map(Map<String, Object> document, Emitter emitter) {
            String type = MapUtils.getString(document, "type");
            if (type.equals("bookmark")) {
                String name = MapUtils.getString(document, "name");
                emitter.emit(name, null);
            }
        }
    }, "1");

    Document document1 = database.createDocument();
    Map<String, Object> properties = new HashMap<>();
    properties.put("type", "bookmark");
    properties.put("name", "test");
    document1.putProperties(properties);

    Document document2 = database.createDocument();
    properties = new HashMap<>();
    properties.put("type", "tag");
    properties.put("name", "sample");
    document2.putProperties(properties);

    Query query = database.createAllDocumentsQuery();
    query.setAllDocsMode(Query.AllDocsMode.INCLUDE_DELETED);
    QueryEnumerator enumerator = query.run();
    assertEquals(2, enumerator.getCount());

    query = view.createQuery();
    enumerator = query.run();
    assertEquals(1, enumerator.getCount());
    assertEquals("test", enumerator.next().getDocument().getProperty("name"));

    document1.purge();
    document2.purge();

    query = database.createAllDocumentsQuery();
    query.setAllDocsMode(Query.AllDocsMode.INCLUDE_DELETED);
    enumerator = query.run();
    assertEquals(0, enumerator.getCount());

    query = view.createQuery();
    enumerator = query.run();
    assertEquals(0, enumerator.getCount());

    database.delete();
}

This code fails on the last assert.
This test succeses if I comment out setStorageType.

Similar test on iOS succeed.

func testPurge() {
    CBLManager.sharedInstance().storageType = kCBLForestDBStorage
    let database = try? CBLManager.sharedInstance().databaseNamed("test_purge")

    let view = database!.viewNamed("bookmarks")
    view.setMapBlock({ (doc, emit) in
       let type = doc.getString("type")
        if type == "bookmark" {
            let name = doc.getString("name")
            emit(name, nil)
        }
       }, version: "1")

    let document1 = database!.createDocument()
    var properties: [String: AnyObject] = ["type": "bookmark", "name": "test"]
    let _ = try? document1.putProperties(properties)

    let document2 = database!.createDocument()
    properties = ["type": "tag", "name": "sample"]
    let _ = try? document2.putProperties(properties)

    var query = database!.createAllDocumentsQuery()
    query.allDocsMode = .IncludeDeleted
    var enumerator = try! query.run()
    XCTAssertEqual(2, enumerator.count)

    query = view.createQuery()
    enumerator = try! query.run()
    XCTAssertEqual(1, enumerator.count)
    XCTAssertEqual("test", enumerator.nextRow()!.document!.propertyForKey("name") as? String)

    try! document1.purgeDocument()
    try! document2.purgeDocument()

   query = database!.createAllDocumentsQuery()
   query.allDocsMode = .IncludeDeleted
    enumerator = try! query.run()
    XCTAssertEqual(0, enumerator.count)

    query = view.createQuery()
    enumerator = try! query.run()
    XCTAssertEqual(0, enumerator.count)
 }

I’m using Couchbase Lite 1.2.1 on both platforms.


#2

Thanks for reporting the bug, but our bug tracker is on Github. Could you please file it in the Couchbase-lite-Java-core repo?


#3

(For later reader)

I wrote an issue, and I got an answer this problem is solved in current master branch.