Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logic error in c4coll_isIndexTrained #2078

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion C/c4CAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ bool c4db_createIndex2(C4Database* database, C4Slice name, C4Slice indexSpec, C4

bool c4coll_isIndexTrained(C4Collection* collection, C4Slice name, C4Error* outError) noexcept {
if ( outError ) *outError = kC4NoError;
return tryCatch(outError, [=] { return collection->isIndexTrained(name); });
return tryCatch<bool>(outError, [=] { return collection->isIndexTrained(name); });
}

// semi-deprecated
Expand Down
28 changes: 24 additions & 4 deletions LiteCore/tests/VectorQueryTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Base64.hh"
#include "c4Database.hh"
#include "c4Collection.hh"
#include "c4Index.h"
#include "c4Database.h"

#ifdef COUCHBASE_ENTERPRISE
Expand Down Expand Up @@ -566,6 +567,7 @@ static pair<string, string> splitCollectionName(const string& input) {
// may change based on usability concerns.
TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSearch]") {
bool expectedTrained{false};
bool expectedPretrained{false};

// Undo this silliness, I'm not spending the effort to find out the name it really wants
// which is LiteCore_Tests_<random number> or something
Expand All @@ -575,6 +577,7 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
// extra collections here

SECTION("Insufficient docs") {
++expectedWarningsLogged;
SECTION("As-is") {}
SECTION("Default scope") {
collectionName = "Secondary";
Expand All @@ -585,7 +588,8 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
store = &db->getKeyStore(string(".") + collectionName);
}

expectedTrained = false;
expectedTrained = false;
expectedPretrained = false;
createVectorIndex();
readVectorDocs(100);
}
Expand All @@ -601,7 +605,8 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
store = &db->getKeyStore(string(".") + collectionName);
}

expectedTrained = true;
expectedTrained = true;
expectedPretrained = true;
createVectorIndex();
readVectorDocs(256 * 30);
}
Expand All @@ -617,7 +622,8 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
store = &db->getKeyStore(string(".") + collectionName);
}

expectedTrained = true;
expectedTrained = true;
expectedPretrained = false;
readVectorDocs(256 * 30);
createVectorIndex();
}
Expand Down Expand Up @@ -648,6 +654,15 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
} catch ( error& e ) { CHECK(e == error::InvalidParameter); }
}

bool isTrained = collection->isIndexTrained("vecIndex"_sl);
CHECK(isTrained == expectedPretrained);

C4Error err;
isTrained = c4coll_isIndexTrained(collection, "vecIndex"_sl, &err);
CHECK(err.code == 0);
CHECK(err.domain == 0);
CHECK(isTrained == expectedPretrained);

// Need to run an arbitrary query to actually train the index
string queryStr =
R"(SELECT META().id, publisher FROM )"s + collectionName + R"( WHERE VECTOR_MATCH(vecIndex, $target, 5) )";
Expand All @@ -662,7 +677,12 @@ TEST_CASE_METHOD(SIFTVectorQueryTest, "Index isTrained API", "[Query][.VectorSea
Query::Options options(enc.finish());
Retained<QueryEnumerator> e(query->createEnumerator(&options));

bool isTrained = collection->isIndexTrained("vecIndex"_sl);
isTrained = collection->isIndexTrained("vecIndex"_sl);
CHECK(isTrained == expectedTrained);

isTrained = c4coll_isIndexTrained(collection, "vecIndex"_sl, &err);
CHECK(err.code == 0);
CHECK(err.domain == 0);
CHECK(isTrained == expectedTrained);
}

Expand Down
Loading