Skip to content

Commit

Permalink
Detect Vector Search Extension and Set Extension Path (#3219)
Browse files Browse the repository at this point in the history
* Detect vector search extension framework with bundle id “com.couchbase.vectorSearchExtension”.
* Set the extension path to the vector search extension bundle’s path.
* Group checkFileLogging and setExtensionPath call together in CBLInit and remove checkFileLogging call from Swift.
  • Loading branch information
pasin authored Feb 7, 2024
1 parent a389f12 commit e4cd750
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
55 changes: 41 additions & 14 deletions Objective-C/CBLDatabase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@

#define kDBExtension @"cblite2"

#ifdef COUCHBASE_ENTERPRISE
#define kVectorSearchExtIdentifier @"com.couchbase.vectorSearchExtension"
#endif

static NSString* kBlobTypeProperty = @kC4ObjectTypeProperty;
static NSString* kBlobDigestProperty = @kC4BlobDigestProperty;
static NSString* kBlobDataProperty = @kC4BlobDataProperty;
Expand Down Expand Up @@ -104,12 +108,46 @@ @implementation CBLDatabase {
.flags = (kC4DB_Create | kC4DB_AutoCompact),
};

/**
Static Initializer called when CBL library is loaded.
@note: App may not configure its logging yet when this initialize() method is called. So if possible, any operations that may log are better
to be done in CBLInit()which is called only once when the first CBLDatabase is created
*/
+ (void) initialize {
if (self == [CBLDatabase class]) {
NSLog(@"%@", [CBLVersion userAgent]);
// Initialize logging
CBLAssertNotNil(CBLLog.sharedInstance);
CBLLogInfo(Database, @"%@", [CBLVersion userAgent]);
}
}

/** Called when the first CBLDatabase object is created. */
+ (void) CBLInit {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self checkFileLogging];
[self setExtensionPath];
});
}

/** Detect and set extension path */
+ (void) setExtensionPath {
#ifdef COUCHBASE_ENTERPRISE
// Note: For non app, the bundle will not be found.
NSBundle* vsBundle = [NSBundle bundleWithIdentifier: kVectorSearchExtIdentifier];
if (vsBundle.bundlePath) {
CBLLogInfo(Database, @"Set extension path : %@", vsBundle.bundlePath);
CBLStringBytes path(vsBundle.bundlePath);
c4_setExtensionPath(path);
}
#endif
}

/** Check and show warning if file logging is not configured. */
+ (void) checkFileLogging {
if (!CBLDatabase.log.file.config) {
CBLWarn(Database, @"Database.log.file.config is nil, meaning file logging is disabled. "
"Log files required for product support are not being generated.");
}
}

Expand All @@ -125,7 +163,8 @@ - (instancetype) initWithName: (NSString*)name

self = [super init];
if (self) {
[[self class] checkFileLogging: NO];
// Perform only once when the first CBLDatabase object is created:
[[self class] CBLInit];

_name = name;
_config = [[CBLDatabaseConfiguration alloc] initWithConfig: config readonly: YES];
Expand Down Expand Up @@ -1116,16 +1155,4 @@ - (uint64_t) activeStoppableCount {
}
}

/** Check and show warning if file logging is not configured. */
+ (void) checkFileLogging: (BOOL)swift {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!CBLDatabase.log.file.config) {
NSString* clazz = swift ? @"Database" : @"CBLDatabase";
CBLWarn(Database, @"%@.log.file.config is nil, meaning file logging is disabled. "
"Log files required for product support are not being generated.", clazz);
}
});
}

@end
2 changes: 0 additions & 2 deletions Objective-C/Internal/CBLDatabase+Swift.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

@interface CBLDatabase ()

+ (void) checkFileLogging: (BOOL)swift;

- (BOOL) inBatch: (NSError**)error usingBlockWithError: (void (NS_NOESCAPE ^)(NSError**))block NS_REFINED_FOR_SWIFT;

@end
1 change: 0 additions & 1 deletion Swift/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public final class Database {
/// - config: The database options, or nil for the default options.
/// - Throws: An error when the database cannot be opened.
public init(name: String, config: DatabaseConfiguration = DatabaseConfiguration()) throws {
CBLDatabase.checkFileLogging(true);
self.config = DatabaseConfiguration(config: config)
self.impl = try CBLDatabase(name: name, config: self.config.toImpl())
}
Expand Down

0 comments on commit e4cd750

Please sign in to comment.