Skip to content

Commit

Permalink
CBL-5203 : Collection’s full name accessor (#3207)
Browse files Browse the repository at this point in the history
* Implemented Collection’s fullName accessor in Objective-C and Swift.
* Fixed XCode static analysic warning in CBLCoflict+Internal.h
* Fixed XCode static analysic warning in DatabaseTest.m.
  • Loading branch information
pasin authored Dec 15, 2023
1 parent 335339e commit 26d5cc2
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Objective-C/CBLCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ extern NSString* const kCBLDefaultCollectionName;
*/
@interface CBLCollection : NSObject<CBLCollectionChangeObservable, CBLIndexable, NSCopying>

/** Collection name.*/
/** Collection's name.*/
@property (readonly, nonatomic) NSString* name;

/** Collection's fully qualified name in the '<scope-name>.<collection-name>' format. */
@property (readonly, nonatomic) NSString* fullName;

/** The scope of the collection. */
@property (readonly, nonatomic) CBLScope* scope;

Expand Down
19 changes: 10 additions & 9 deletions Objective-C/CBLCollection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,23 @@ - (NSUInteger) hash {
return [self.name hash] ^ [self.scope.name hash];
}

- (id) copyWithZone: (nullable NSZone*)zone {
return [[[self class] alloc] initWithDB: _db c4collection: _c4col];
}

#pragma mark - Properties

- (NSString*) fullName {
return $sprintf(@"%@.%@", _scope.name, _name);
}

- (uint64_t) count {
CBL_LOCK(_mutex) {
_count = c4coll_isValid(_c4col) ? c4coll_getDocumentCount(_c4col) : 0;
}

return _count;
}

- (id) copyWithZone: (nullable NSZone*)zone {
return [[[self class] alloc] initWithDB: _db c4collection: _c4col];
}

#pragma mark - Indexable

- (BOOL) createIndexWithName: (NSString*)name
Expand Down Expand Up @@ -496,10 +501,6 @@ - (C4CollectionSpec) c4spec {
return { .name = name, .scope = scopeName };
}

- (NSString*) fullName {
return $sprintf(@"%@.%@", _scope.name, _name);
}

- (BOOL) isEqual: (id)object {
if (self == object)
return YES;
Expand Down
2 changes: 0 additions & 2 deletions Objective-C/Internal/CBLCollection+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly) C4CollectionSpec c4spec;

@property (nonatomic, readonly) NSString* fullName;

/** This constructor will return CBLCollection for the c4collection. */
- (instancetype) initWithDB: (CBLDatabase*)db
c4collection: (C4Collection*)c4collection;
Expand Down
4 changes: 2 additions & 2 deletions Objective-C/Internal/Replicator/CBLConflict+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface CBLConflict ()

- (instancetype) initWithID: (NSString*)documentID
localDocument: (CBLDocument *)localDoc
remoteDocument: (CBLDocument *)remoteDoc;
localDocument: (nullable CBLDocument*)localDoc
remoteDocument: (nullable CBLDocument*)remoteDoc;

@end

Expand Down
33 changes: 33 additions & 0 deletions Objective-C/Tests/CollectionTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,39 @@ - (void) testScopeNameCaseSensitive {
Assert([(@[@"scopeA", @"SCOPEa", kCBLDefaultScopeName]) containsObject: scopes[1].name]);
}

#pragma mark - Collection Full Name

// Spec: https://docs.google.com/document/d/1nUgaCgXIB3lLViudf6Pw6H9nPa_OeYU6uM_9xAd08M0

- (void) testCollectionFullName {
NSError* error;

// 3.1 TestGetFullNameFromDefaultCollection
CBLCollection* col1 = [self.db defaultCollection: &error];
AssertNotNil(col1);
AssertEqualObjects(col1.fullName, @"_default._default");

// 3.2 TestGetFullNameFromNewCollectionInDefaultScope
CBLCollection* col2 = [self.db createCollectionWithName: @"colA" scope: nil error: &error];
AssertNotNil(col2);
AssertEqualObjects(col2.fullName, @"_default.colA");

// 3.3 TestGetFullNameFromNewCollectionInCustomScope
CBLCollection* col3 = [self.db createCollectionWithName: @"colA" scope: @"scopeA" error: &error];
AssertNotNil(col3);
AssertEqualObjects(col3.fullName, @"scopeA.colA");

// 3.4 TestGetFullNameFromExistingCollectionInDefaultScope
CBLCollection* col4 = [self.db collectionWithName: @"colA" scope: nil error: &error];
AssertNotNil(col4);
AssertEqualObjects(col4.fullName, @"_default.colA");

// 3.5 TestGetFullNameFromNewCollectionInCustomScope
CBLCollection* col5 = [self.db collectionWithName: @"colA" scope: @"scopeA" error: &error];
AssertNotNil(col5);
AssertEqualObjects(col5.fullName, @"scopeA.colA");
}

#pragma mark - 8.3 Collections and Cross Database Instance

- (void) testCreateThenGetCollectionFromDifferentDatabaseInstance {
Expand Down
3 changes: 3 additions & 0 deletions Objective-C/Tests/DatabaseTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,7 @@ - (void) testCreateCollection {

// Create in Custom Scope
c = [self.db createCollectionWithName: @"collection2" scope: @"scope1" error: &error];
AssertNotNil(c);

// verify
collections = [self.db collections: @"scope1" error: &error];
Expand Down Expand Up @@ -2634,7 +2635,9 @@ - (void) testCreateDuplicateCollection {

// Create in Custom Scope
c1 = [self.db createCollectionWithName: @"collection2" scope: @"scope1" error: &error];
AssertNotNil(c1);
c2 = [self.db createCollectionWithName: @"collection2" scope: @"scope1" error: &error];
AssertNotNil(c2);

// verify no duplicate is created.
collections = [self.db collections: @"scope1" error: &error];
Expand Down
5 changes: 4 additions & 1 deletion Swift/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ public final class Collection : CollectionChangeObservable, Indexable, Equatable
/// The default scope name constant
public static let defaultCollectionName: String = kCBLDefaultCollectionName

/// Collection name.
/// Collection's name.
public var name: String { impl.name }

/// Collection's fully qualified name in the '<scope-name>.<collection-name>' format.
public var fullName: String { impl.fullName }

/// The scope of the collection.
public let scope: Scope

Expand Down
31 changes: 31 additions & 0 deletions Swift/Tests/CollectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ class CollectionTest: CBLTestCase {
XCTAssertEqual(scopes[0].name, Scope.defaultScopeName)
}

// MARK: Collection Full Name

// Spec: https://docs.google.com/document/d/1nUgaCgXIB3lLViudf6Pw6H9nPa_OeYU6uM_9xAd08M0

func testCollectionFullName() throws {
// 3.1 TestGetFullNameFromDefaultCollection
let col1 = try self.db.defaultCollection()
XCTAssertNotNil(col1)
XCTAssertEqual(col1.fullName, "_default._default")

// 3.2 TestGetFullNameFromNewCollectionInDefaultScope
let col2 = try self.db.createCollection(name: "colA")
XCTAssertNotNil(col2)
XCTAssertEqual(col2.fullName, "_default.colA")

// 3.3 TestGetFullNameFromNewCollectionInCustomScope
let col3 = try self.db.createCollection(name: "colA", scope: "scopeA")
XCTAssertNotNil(col3)
XCTAssertEqual(col3.fullName, "scopeA.colA")

// 3.4 TestGetFullNameFromExistingCollectionInDefaultScope
let col4 = try self.db.collection(name: "colA")
XCTAssertNotNil(col4)
XCTAssertEqual(col4!.fullName, "_default.colA")

// 3.5 TestGetFullNameFromNewCollectionInCustomScope
let col5 = try self.db.collection(name: "colA", scope: "scopeA")
XCTAssertNotNil(col5)
XCTAssertEqual(col5!.fullName, "scopeA.colA")
}

// MARK: 8.2 Collections

func testCreateAndGetCollectionsInDefaultScope() throws {
Expand Down

0 comments on commit 26d5cc2

Please sign in to comment.