Skip to content

Commit

Permalink
CBL-5690 : Update Distance Metric Enum and VS SQL in Tests
Browse files Browse the repository at this point in the history
* Used new Vector Search SQL in Tests
* Separated the base xctest class to have no tests, otherwise the tests in the base class will be included in the sub classes as well.
* Used forward declaration where possibles in CBLQueryIndex / CBLIndexUpdater implementation.
* Fixed warning in URLEndpointListenerTests and Replicator Tests.
* Used LiteCore 3.2.0-215 and VS 1.0.0-56
  • Loading branch information
pasin committed Jul 11, 2024
1 parent 9b939f3 commit aee1807
Show file tree
Hide file tree
Showing 16 changed files with 496 additions and 467 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ frameworks/

# CBL files:
Tests/Extensions/CouchbaseLiteVectorSearch.xcframework
Tests/Extensions/LICENSE.txt
Tests/Extensions/Build
vendor/couchbase-lite-core-EE

# spm
Expand Down
93 changes: 45 additions & 48 deletions CouchbaseLite.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Objective-C/CBLCollectionTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// limitations under the License.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
Expand Down
2 changes: 1 addition & 1 deletion Objective-C/CBLQueryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#import <Foundation/Foundation.h>

#ifdef COUCHBASE_ENTERPRISE
#import <CouchbaseLite/CBLIndexUpdater.h>
@class CBLIndexUpdater;
#endif

@class CBLCollection;
Expand Down
3 changes: 2 additions & 1 deletion Objective-C/CBLQueryIndex.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
// limitations under the License.
//

#import "CBLCollection.h"
#import "CBLCoreBridge.h"
#import "CBLDatabase+Internal.h"
#import "CBLQueryIndex+Internal.h"
#import "CBLCoreBridge.h"
#import "CBLStatus.h"

#ifdef COUCHBASE_ENTERPRISE
Expand Down
11 changes: 5 additions & 6 deletions Objective-C/Internal/CBLQueryIndex+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
//

#import <Foundation/Foundation.h>
#import <CouchbaseLite/CBLCollection.h>
#import <CouchbaseLite/CBLQueryIndex.h>
#import "CBLQueryIndex.h"
#import "c4Index.h"

@class CBLCollection;

NS_ASSUME_NONNULL_BEGIN

@interface CBLQueryIndex ()
Expand All @@ -31,10 +32,8 @@ NS_ASSUME_NONNULL_BEGIN
- (id) mutex;

- (instancetype) initWithC4Index: (C4Index*) c4index
name: (NSString*) name
collection: (CBLCollection*) collection;
name: (NSString*) name
collection: (CBLCollection*) collection;
@end



NS_ASSUME_NONNULL_END
7 changes: 4 additions & 3 deletions Objective-C/Tests/ReplicatorTest+Collection.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ - (void)tearDown {

- (void) testCreateReplicatorWithNoCollections {
[self expectException: NSInvalidArgumentException in:^{
CBLReplicator* r = [[CBLReplicator alloc] initWithConfig: _config];
CBLReplicator* r = [[CBLReplicator alloc] initWithConfig: self->_config];
NSLog(@"%@", r);
}];

[self expectException: NSInvalidArgumentException in:^{
[_config addCollections: @[] config: nil];
[self->_config addCollections: @[] config: nil];
}];
}

Expand Down Expand Up @@ -1310,7 +1310,6 @@ - (void) testCollectionIsDocumentPending {
}

- (void) testCollectionDocumentReplicationEvents {
CBLDocumentFlags flags = 0;
NSError* error = nil;
CBLCollection* col1a = [self.db createCollectionWithName: @"colA"
scope: @"scopeA" error: &error];
Expand Down Expand Up @@ -1342,6 +1341,8 @@ - (void) testCollectionDocumentReplicationEvents {
CBLCollectionConfiguration* colConfig = [[CBLCollectionConfiguration alloc] init];
[config addCollections: @[col1a, col1b] config: colConfig];
CBLReplicator* r = [[CBLReplicator alloc] initWithConfig: config];

__block CBLDocumentFlags flags = 0;
__block int docsCount = 0;
__block NSMutableArray* docs = [[NSMutableArray alloc] init];
id token = [r addDocumentReplicationListener: ^(CBLDocumentReplication* docReplication) {
Expand Down
8 changes: 4 additions & 4 deletions Objective-C/Tests/ReplicatorTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ NS_ASSUME_NONNULL_BEGIN

- (void) eraseRemoteEndpoint: (CBLURLEndpoint*)endpoint;

- (id) sendRequestToEndpoint: (CBLURLEndpoint*)endpoint
method: (NSString*)method
path: (nullable NSString*)path
body: (nullable id)body;
- (nullable id) sendRequestToEndpoint: (CBLURLEndpoint*)endpoint
method: (NSString*)method
path: (nullable NSString*)path
body: (nullable id)body;

#pragma mark - Certifciate

Expand Down
2 changes: 1 addition & 1 deletion Objective-C/Tests/URLEndpointListenerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void) stopListener: (CBLURLEndpointListener*)listener;

// TLS Identity management
- (CBLTLSIdentity*) tlsIdentity: (BOOL)isServer;
- (nullable CBLTLSIdentity*) tlsIdentity: (BOOL)isServer;
- (void) cleanupTLSIdentity: (BOOL)isServer;
- (void) deleteFromKeyChain: (CBLTLSIdentity*)identity;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// LazyVectorIndexTest.m
// VectorSearchTest+Lazy.m
// CouchbaseLite
//
// Copyright (c) 2024 Couchbase, Inc All rights reserved.
Expand All @@ -22,25 +22,32 @@
/**
* Test Spec: https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0002-Lazy-Vector-Index.md
*
* Test 6. TestGetIndexOnClosedDatabase is tested in CollectionTest
* Test 7. TestGetIndexOnDeletedCollection is tested in CollectionTest
* Vesion: 2.0.1
*
* Test 6. TestGetIndexOnClosedDatabase is tested in CollectionTest
* Test 7. TestGetIndexOnDeletedCollection is tested in CollectionTest
*/

#define LAZY_VECTOR_INDEX_CONFIG(E, D, C) [self lazyVectorIndexConfigWithExpression: (E) dimensions: (D) centroids: (C)]

@interface VectorLazyIndexTest : VectorSearchTest
@interface VectorSearchTest_Lazy : VectorSearchTest

@end

@implementation VectorLazyIndexTest
@implementation VectorSearchTest_Lazy

/** Override the default VectorSearch Expression */
- (NSString*) wordsQueryDefaultExpression {
return @"word";
}

- (CBLQueryIndex*) wordsIndex {
CBLQueryIndex* index = [self.wordsCollection indexWithName: kWordsIndexName error: nil];
AssertNotNil(index);
return index;
}

- (CBLVectorIndexConfiguration*) lazyVectorIndexConfigWithExpression: (NSString*)expression
- (CBLVectorIndexConfiguration*) lazyVectorIndexConfigWithExpression: (NSString*)expression
dimensions: (unsigned int)dimensions
centroids: (unsigned int)centroids {
CBLVectorIndexConfiguration* config = VECTOR_INDEX_CONFIG(expression, dimensions, centroids);
Expand Down Expand Up @@ -208,7 +215,8 @@ - (void) testGetExistingVectorIndex {
* 3. Create an SQL++ query:
* - SELECT word
* FROM _default.words
* WHERE vector_match(words_index, <dinner vector>)
* ORDER BY APPROX_VECTOR_DISTANCE(word, $dinnerVector)
* LIMIT 10
* 4. Execute the query and check that 0 results are returned.
* 5. Update the documents:
* - Create _default.words.word301 with the content from _default.extwords.word1
Expand All @@ -218,7 +226,7 @@ - (void) testGetExistingVectorIndex {
- (void) testLazyVectorIndexNotAutoUpdatedChangedDocs {
[self createWordsIndexWithConfig: LAZY_VECTOR_INDEX_CONFIG(@"word", 300, 8)];

CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: nil];
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: 10];
AssertEqual(rs.allObjects.count, 0);

// Update docs:
Expand All @@ -232,7 +240,7 @@ - (void) testLazyVectorIndexNotAutoUpdatedChangedDocs {
[word1 setData: [extWord3 toDictionary]];
Assert([self.wordsCollection saveDocument: word1 error: &error]);

rs = [self executeWordsQueryNoTrainingCheckWithLimit: nil];
rs = [self executeWordsQueryNoTrainingCheckWithLimit: 10];
AssertEqual(rs.allObjects.count, 0);
}

Expand Down Expand Up @@ -261,7 +269,8 @@ - (void) testLazyVectorIndexNotAutoUpdatedChangedDocs {
* 6. Create an SQL++ query:
* - SELECT word
* FROM _default.words
* WHERE vector_match(words_index, <dinner vector>) LIMIT 300
* ORDER BY APPROX_VECTOR_DISTANCE(word, $dinnerVector)
* LIMIT 300
* 7. Execute the query and check that 1 results are returned.
* 8. Check that the word gotten from the query result is the same as the word in Step 5.
* 9. Delete _default.words.word1 doc.
Expand All @@ -284,12 +293,12 @@ - (void) testLazyVectorIndexAutoUpdateDeletedDocs {
AssertNil(error);

// Query:
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: @300];
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
AssertEqual(rs.allObjects.count, 1);

// Delete doc and requery:
[self.wordsCollection deleteDocument: [self.wordsCollection documentWithID: @"word1" error: &error] error: &error];
rs = [self executeWordsQueryNoTrainingCheckWithLimit: @300];
rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
AssertEqual(rs.allObjects.count, 0);
}

Expand Down Expand Up @@ -318,7 +327,8 @@ - (void) testLazyVectorIndexAutoUpdateDeletedDocs {
* 7. Create an SQL++ query:
* - SELECT word
* FROM _default.words
* WHERE vector_match(words_index, <dinner vector>) LIMIT 300
* ORDER BY APPROX_VECTOR_DISTANCE(word, $dinnerVector)
* LIMIT 300
* 8. Execute the query and check that 1 results are returned.
* 9. Check that the word gotten from the query result is the same as the word in Step 5.
* 10. Purge _default.words.word1 doc.
Expand All @@ -341,12 +351,12 @@ - (void) testLazyVectorIndexAutoUpdatePurgedDocs {
AssertNil(error);

// Query:
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: @300];
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
AssertEqual(rs.allObjects.count, 1);

// Delete doc and requery:
[self.wordsCollection purgeDocumentWithID: @"word1" error: &error];
rs = [self executeWordsQueryNoTrainingCheckWithLimit: @300];
rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
AssertEqual(rs.allObjects.count, 0);
}

Expand Down Expand Up @@ -827,7 +837,8 @@ - (void) testIndexUpdaterGettingValues {
* 7. Execute a vector search query.
* - SELECT word
* FROM _default.words
* WHERE vector_match(words_index, <dinner vector>) LIMIT 300
* ORDER BY APPROX_VECTOR_DISTANCE(word, $dinnerVector)
* LIMIT 300
* 8. Check that there are 10 words returned.
* 9. Check that the word is in the word set from the step 5.
*/
Expand All @@ -852,10 +863,7 @@ - (void) testIndexUpdaterSetFloatArrayVectors {

Assert([updater finishWithError: &error]);

CBLQueryResultSet* rs = [self executeWordsQueryWithLimit: @300
queryDistance: false
andClause: nil
checkTraining: false];
CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
NSDictionary<NSString*, NSString*>* wordMap = [self toDocIDWordMap: rs];
AssertEqual(wordMap.count, 10);

Expand Down Expand Up @@ -1090,10 +1098,10 @@ - (void) testIndexUpdaterCaughtUp {
* 7. Execute a vector search query.
* - SELECT word
* FROM _default.words
* WHERE vector_match(words_index, <dinner vector>) LIMIT 300
* ORDER BY APPROX_VECTOR_DISTANCE(word, $dinnerVector)
* LIMIT 300
* 8. Check that there are 0 words returned.
*/

- (void) testNonFinishedIndexUpdaterNotUpdateIndex {
[self createWordsIndexWithConfig: LAZY_VECTOR_INDEX_CONFIG(@"word", 300, 8)];

Expand All @@ -1112,11 +1120,7 @@ - (void) testNonFinishedIndexUpdaterNotUpdateIndex {

// "Release" CBLIndexUpdater
updater = nil;
CBLQueryResultSet* rs = [self executeWordsQueryWithLimit: @300
queryDistance: false
andClause: nil
checkTraining: false];

CBLQueryResultSet* rs = [self executeWordsQueryNoTrainingCheckWithLimit: 300];
AssertEqual(rs.allObjects.count, 0);
}

Expand Down
23 changes: 15 additions & 8 deletions Objective-C/Tests/VectorSearchTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ NS_ASSUME_NONNULL_BEGIN

- (void) deleteWordsIndex;

- (NSString*) wordsQueryStringWithLimit: (nullable NSNumber*)limit
queryDistance: (BOOL) queryDistance
andClause: (nullable NSString*)andClause;
/** For the test subclasses to override the default vector expression. */
- (NSString*) wordsQueryDefaultExpression;

- (CBLQueryResultSet*) executeWordsQueryWithLimit: (nullable NSNumber*)limit
queryDistance: (BOOL) queryDistance
andClause: (nullable NSString*)andClause
- (NSString*) wordsQueryStringWithLimit: (NSUInteger)limit
metric: (nullable NSString*)metric
vectorExpression: (nullable NSString*)vectorExpression
whereClause: (nullable NSString*)whereClause;

- (NSString*) wordsQueryStringWithLimit: (NSUInteger)limit;

- (CBLQueryResultSet*) executeWordsQueryWithLimit: (NSUInteger)limit
metric: (nullable NSString*)metric
vectorExpression: (nullable NSString*)vectorExpression
whereClause: (nullable NSString*)whereClause
checkTraining: (BOOL) checkTraining;

- (CBLQueryResultSet*) executeWordsQueryWithLimit: (nullable NSNumber*)limit;
- (CBLQueryResultSet*) executeWordsQueryWithLimit: (NSUInteger)limit;

- (CBLQueryResultSet*) executeWordsQueryNoTrainingCheckWithLimit: (nullable NSNumber*)limit;
- (CBLQueryResultSet*) executeWordsQueryNoTrainingCheckWithLimit: (NSUInteger)limit;

- (NSDictionary<NSString*, NSString*>*) toDocIDWordMap: (CBLQueryResultSet*)resultSet;

Expand Down
Loading

0 comments on commit aee1807

Please sign in to comment.