Skip to content

Commit

Permalink
CBL-5202 : Implement getting collection’s fullName (#513)
Browse files Browse the repository at this point in the history
Implemented collection’s fullName which is in "<scope-name>.<collection-name>" format.
  • Loading branch information
pasin authored Dec 8, 2023
1 parent d1962c4 commit 1435b12
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 15 deletions.
7 changes: 5 additions & 2 deletions include/cbl++/Collection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ namespace cbl {
public:
// Accessors:

/** The collection name. */
/** The collection's name. */
std::string name() const {return asString(CBLCollection_Name(ref()));}

/** The scope name. */
/** The collection's fully qualified name in the '<scope-name>.<collection-name>' format. */
std::string fullName() const {return asString(CBLCollection_FullName(ref()));}

/** The scope's name. */
std::string scopeName() const {
auto scope = CBLCollection_Scope(ref());
auto scopeName = asString(CBLScope_Name(scope));
Expand Down
7 changes: 6 additions & 1 deletion include/cbl/CBLCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,16 @@ CBLCollection* _cbl_nullable CBLDatabase_DefaultCollection(const CBLDatabase* db
@return A \ref CBLScope instance. */
CBLScope* CBLCollection_Scope(const CBLCollection* collection) CBLAPI;

/** Returns the collection name.
/** Returns the collection's name.
@param collection The collection.
@return The name of the collection. */
FLString CBLCollection_Name(const CBLCollection* collection) CBLAPI;

/** Returns the collection's fully qualified name in the '<scope-name>.<collection-name>' format.
@param collection The collection.
@return The fully qualified name of the collection. */
FLString CBLCollection_FullName(const CBLCollection* collection) CBLAPI;

/** Returns the number of documents in the collection.
@param collection The collection.
@return the number of documents in the collection. */
Expand Down
8 changes: 6 additions & 2 deletions src/CBLCollection_CAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,18 @@ CBLScope* CBLCollection_Scope(const CBLCollection* collection) noexcept {
} catchAndWarn()
}

/** Returns the collection name. */
FLString CBLCollection_Name(const CBLCollection* collection) noexcept {
try {
return collection->name();
} catchAndWarn()
}

/** Returns the number of documents in the collection. */
FLString CBLCollection_FullName(const CBLCollection* collection) noexcept {
try {
return collection->fullName();
} catchAndWarn()
}

uint64_t CBLCollection_Count(const CBLCollection* collection) noexcept {
try {
return collection->count();
Expand Down
8 changes: 7 additions & 1 deletion src/CBLCollection_Internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public:
,_scope(scope)
,_database(retain(database))
,_name(c4col->getName())
{ }
{
_fullName = alloc_slice(_scope->name());
_fullName.append(".");
_fullName.append(_name);
}

~CBLCollection() {
LOCK(_adoptMutex);
Expand All @@ -51,6 +55,7 @@ public:

Retained<CBLScope> scope() const noexcept {return _scope;}
slice name() const noexcept {return _name;}
slice fullName() const noexcept {return _fullName;}
C4CollectionSpec spec() const noexcept {return {_name, _scope->name()};}
bool isValid() const noexcept {return _c4col.isValid();}
uint64_t count() const {return _c4col.useLocked()->getDocumentCount();}
Expand Down Expand Up @@ -281,6 +286,7 @@ private:
C4CollectionAccessLock _c4col; // Shared lock with _c4db

alloc_slice _name;
alloc_slice _fullName;
Retained<CBLScope> _scope;

CBLDatabase* _database; // Retained unless being adopted
Expand Down
3 changes: 2 additions & 1 deletion src/exports/CBL_Exports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ CBLDatabase_ScopeNames

kCBLDefaultCollectionName

CBLCollection_Name
CBLCollection_Scope
CBLCollection_Name
CBLCollection_FullName
CBLCollection_Count

CBLCollection_GetDocument
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL.def
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ CBLDatabase_DefaultScope
CBLDatabase_Scope
CBLDatabase_ScopeNames
kCBLDefaultCollectionName
CBLCollection_Name
CBLCollection_Scope
CBLCollection_Name
CBLCollection_FullName
CBLCollection_Count
CBLCollection_GetDocument
CBLCollection_SaveDocument
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL.exp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ _CBLDatabase_DefaultScope
_CBLDatabase_Scope
_CBLDatabase_ScopeNames
_kCBLDefaultCollectionName
_CBLCollection_Name
_CBLCollection_Scope
_CBLCollection_Name
_CBLCollection_FullName
_CBLCollection_Count
_CBLCollection_GetDocument
_CBLCollection_SaveDocument
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ CBL_C {
CBLDatabase_Scope;
CBLDatabase_ScopeNames;
kCBLDefaultCollectionName;
CBLCollection_Name;
CBLCollection_Scope;
CBLCollection_Name;
CBLCollection_FullName;
CBLCollection_Count;
CBLCollection_GetDocument;
CBLCollection_SaveDocument;
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL_Android.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ CBL_C {
CBLDatabase_Scope;
CBLDatabase_ScopeNames;
kCBLDefaultCollectionName;
CBLCollection_Name;
CBLCollection_Scope;
CBLCollection_Name;
CBLCollection_FullName;
CBLCollection_Count;
CBLCollection_GetDocument;
CBLCollection_SaveDocument;
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL_EE.def
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ CBLDatabase_DefaultScope
CBLDatabase_Scope
CBLDatabase_ScopeNames
kCBLDefaultCollectionName
CBLCollection_Name
CBLCollection_Scope
CBLCollection_Name
CBLCollection_FullName
CBLCollection_Count
CBLCollection_GetDocument
CBLCollection_SaveDocument
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL_EE.exp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ _CBLDatabase_DefaultScope
_CBLDatabase_Scope
_CBLDatabase_ScopeNames
_kCBLDefaultCollectionName
_CBLCollection_Name
_CBLCollection_Scope
_CBLCollection_Name
_CBLCollection_FullName
_CBLCollection_Count
_CBLCollection_GetDocument
_CBLCollection_SaveDocument
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL_EE.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ CBL_C {
CBLDatabase_Scope;
CBLDatabase_ScopeNames;
kCBLDefaultCollectionName;
CBLCollection_Name;
CBLCollection_Scope;
CBLCollection_Name;
CBLCollection_FullName;
CBLCollection_Count;
CBLCollection_GetDocument;
CBLCollection_SaveDocument;
Expand Down
3 changes: 2 additions & 1 deletion src/exports/generated/CBL_EE_Android.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ CBL_C {
CBLDatabase_Scope;
CBLDatabase_ScopeNames;
kCBLDefaultCollectionName;
CBLCollection_Name;
CBLCollection_Scope;
CBLCollection_Name;
CBLCollection_FullName;
CBLCollection_Count;
CBLCollection_GetDocument;
CBLCollection_SaveDocument;
Expand Down
34 changes: 34 additions & 0 deletions test/CollectionTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,40 @@ TEST_CASE_METHOD(CollectionTest, "Scope Name Case Sensitive", "[Collection]") {
CBLCollection_Release(col1b);
}

TEST_CASE_METHOD(CollectionTest, "Collection Full Name", "[Collection]") {
CBLError error = {};

// 3.1 TestGetFullNameFromDefaultCollection
CBLCollection* col1 = CBLDatabase_DefaultCollection(db, &error);
REQUIRE(col1);
CHECK(CBLCollection_FullName(col1) == "_default._default"_sl);
CBLCollection_Release(col1);

// 3.2 TestGetFullNameFromNewCollectionInDefaultScope
CBLCollection* col2 = CBLDatabase_CreateCollection(db, "colA"_sl, kCBLDefaultScopeName, &error);
REQUIRE(col2);
CHECK(CBLCollection_FullName(col2) == "_default.colA"_sl);
CBLCollection_Release(col2);

// 3.3 TestGetFullNameFromNewCollectionInCustomScope
CBLCollection* col3 = CBLDatabase_CreateCollection(db, "colA"_sl, "scopeA"_sl, &error);
REQUIRE(col3);
CHECK(CBLCollection_FullName(col3) == "scopeA.colA"_sl);
CBLCollection_Release(col3);

// 3.4 TestGetFullNameFromExistingCollectionInDefaultScope
CBLCollection* col4 = CBLDatabase_Collection(db, "colA"_sl, kCBLDefaultScopeName, &error);
REQUIRE(col4);
CHECK(CBLCollection_FullName(col4) == "_default.colA"_sl);
CBLCollection_Release(col4);

// 3.5 TestGetFullNameFromExistingCollectionInCustomScope
CBLCollection* col5 = CBLDatabase_Collection(db, "colA"_sl, "scopeA"_sl, &error);
REQUIRE(col5);
CHECK(CBLCollection_FullName(col5) == "scopeA.colA"_sl);
CBLCollection_Release(col5);
}

TEST_CASE_METHOD(CollectionTest, "Create then Get Collection using Different DB Instances", "[Collection]") {
CBLError error = {};
CBLCollection* col1a = CBLDatabase_CreateCollection(db, "colA"_sl, "scopeA"_sl, &error);
Expand Down
27 changes: 27 additions & 0 deletions test/CollectionTest_Cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,33 @@ TEST_CASE_METHOD(CollectionTest_Cpp, "C++ Use Invalid Collection", "[Collection]
testInvalidCollection(col);
}

TEST_CASE_METHOD(CollectionTest_Cpp, "C++ Collection Full Name", "[Collection]") {
// 3.1 TestGetFullNameFromDefaultCollection
Collection col1 = db.getDefaultCollection();
REQUIRE(col1);
CHECK(col1.fullName() == "_default._default");

// 3.2 TestGetFullNameFromNewCollectionInDefaultScope
Collection col2 = db.createCollection("colA");
REQUIRE(col2);
CHECK(col2.fullName() == "_default.colA");

// 3.3 TestGetFullNameFromNewCollectionInCustomScope
Collection col3 = db.createCollection("colA", "scopeA");
REQUIRE(col3);
CHECK(col3.fullName() == "scopeA.colA");

// 3.4 TestGetFullNameFromExistingCollectionInDefaultScope
Collection col4 = db.getCollection("colA");
REQUIRE(col4);
CHECK(col4.fullName() == "_default.colA");

// 3.5 TestGetFullNameFromExistingCollectionInCustomScope
Collection col5 = db.getCollection("colA", "scopeA");
REQUIRE(col5);
CHECK(col5.fullName() == "scopeA.colA");
}

TEST_CASE_METHOD(CollectionTest_Cpp, "C++ Create, Get and Delete Index", "[Collection]") {
RetainedArray names = defaultCollection.getIndexNames();
REQUIRE(names);
Expand Down

0 comments on commit 1435b12

Please sign in to comment.