Skip to content

Commit

Permalink
(DOCSP-29739): C++: Add documentation for Set data type (#3065)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc authored Nov 3, 2023
1 parent a0e50ac commit 0793ca3
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/cpp/beta/local/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FetchContent_Declare(
FetchContent_Declare(
cpprealm
GIT_REPOSITORY https://github.com/realm/realm-cpp.git
GIT_TAG v0.3.0-preview
GIT_TAG v0.4.0-preview
)

FetchContent_MakeAvailable(Catch2 cpprealm)
Expand Down
142 changes: 141 additions & 1 deletion examples/cpp/beta/local/crud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
// :replace-start: {
// "terms": {
// "Beta_": "",
// "Beta_Map_": ""
// "Beta_Map_": "",
// "Beta_Set_": ""
// }
// }

Expand Down Expand Up @@ -82,6 +83,14 @@ struct Beta_Map_Employee {
REALM_SCHEMA(Beta_Map_Employee, _id, firstName, lastName, locationByDay)
// :snippet-end:

// :snippet-start: beta-model-with-set-property
struct Beta_Set_Repository {
std::string ownerAndName;
std::set<int64_t> openPullRequestNumbers;
};
REALM_SCHEMA(Beta_Set_Repository, ownerAndName, openPullRequestNumbers)
// :snippet-end:

TEST_CASE("Beta define model example", "[write]") {
auto relative_realm_path_directory = "beta_dog/";
std::filesystem::create_directories(relative_realm_path_directory);
Expand Down Expand Up @@ -486,4 +495,135 @@ TEST_CASE("test map object with percent-encoded map key", "[write]") {
});
}
}

TEST_CASE("Test Set Object", "[write]") {
auto relative_realm_path_directory = "beta_set_repository/";
std::filesystem::create_directories(relative_realm_path_directory);
std::filesystem::path path = std::filesystem::current_path().append(relative_realm_path_directory);
path = path.append("repository_objects");
path = path.replace_extension("realm");
auto config = realm::db_config();
config.set_path(path);

// :snippet-start: write-set-object
auto realm = db(std::move(config));

// Create an object that has a set property
auto docsRealmRepo = Beta_Set_Repository {
.ownerAndName = "mongodb/docs-realm"
};

// Add the object to the realm and get the managed object
auto managedDocsRealm = realm.write([&]() {
return realm.add(std::move(docsRealmRepo));
});

// Insert items into the set
auto openPullRequestNumbers = { 3059, 3062, 3064 };

realm.write([&] {
for (auto number: openPullRequestNumbers) {
// You can only mutate the set in a write transaction.
// This means you can't set values at initialization,
// but must do it during a write.
managedDocsRealm.openPullRequestNumbers.insert(number);
}
});
// :snippet-end:

auto it3059 = managedDocsRealm.openPullRequestNumbers.find(3059);
REQUIRE(*it3059 == static_cast<long long>(3059));

// auto it3062 = managedDocsRealm.openPullRequestNumbers.find(3062);
// REQUIRE(*it3062 == static_cast<long long>(3062));

// auto it3064 = managedDocsRealm.openPullRequestNumbers.find(3064);
// REQUIRE(it3064 != managedDocsRealm.openPullRequestNumbers.end());
// REQUIRE(*it3064 == static_cast<long long>(3064));

// :snippet-start: read-set
auto repositories = realm.objects<Beta_Set_Repository>();

auto repositoriesNamedDocsRealm = repositories.where([](auto &repository) {
return repository.ownerAndName == "mongodb/docs-realm";
});

auto docsRealm = repositoriesNamedDocsRealm[0];

// You can check the size of the set
auto numberOfPullRequests = docsRealm.openPullRequestNumbers.size();
CHECK(numberOfPullRequests == 3); // :remove:

// Find an element in the set whose value is 3064
auto it = managedDocsRealm.openPullRequestNumbers.find(3064);
// :remove-start:
CHECK(it != managedDocsRealm.openPullRequestNumbers.end());
CHECK(*it == static_cast<long long>(3064));
// :remove-end:

// Get a copy of the set that exists independent of the managed set
auto openRealmPullRequests = docsRealm.openPullRequestNumbers.detach();
// :snippet-end:

CHECK(openRealmPullRequests == std::set<int64_t>({ 3059, 3062, 3064 }));
CHECK(openRealmPullRequests.size() == 3);
CHECK(docsRealm.openPullRequestNumbers.size() == 3);

// After clearing the copy of the set, the managed set is unchanged
openRealmPullRequests.clear();
CHECK(openRealmPullRequests.size() == 0);
CHECK(docsRealm.openPullRequestNumbers.size() == 3);

// :snippet-start: update-set
// Add elements to the set in a write transaction
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.insert(3066);
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4);

// Use std::set algorithms to update a set
// In this example, use std::set_union to add elements to the set
// 3064 already exists, so it won't be added, but 3065 and 3067 are unique
// values and will be added to the set.
auto newOpenPullRequests = std::set<int64_t>({ 3064, 3065, 3067 });
realm.write([&] {
std::set_union(
docsRealm.openPullRequestNumbers.begin(),
docsRealm.openPullRequestNumbers.end(),
newOpenPullRequests.begin(),
newOpenPullRequests.end(),
std::inserter(
managedDocsRealm.openPullRequestNumbers,
managedDocsRealm.openPullRequestNumbers.end()));
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 6);

// Erase elements from a set
auto it3065 = managedDocsRealm.openPullRequestNumbers.find(3065);
CHECK(it3065 != managedDocsRealm.openPullRequestNumbers.end());
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.erase(it3065);
});
// :snippet-end:
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 5);
// :snippet-start: delete-set
// Remove an element from the set with erase()
auto it3064 = managedDocsRealm.openPullRequestNumbers.find(3064);
CHECK(it3064 != managedDocsRealm.openPullRequestNumbers.end());
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.erase(it3065);
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4);

// Clear the entire contents of the set
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.clear();
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 0);
// :snippet-end:

realm.write([&] {
realm.remove(docsRealm);
});
}
// :replace-end:
12 changes: 10 additions & 2 deletions examples/cpp/beta/local/supported-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ struct Beta_AllTypesObject {
// :snippet-start: beta-required-list
std::vector<SomeType> listTypeName;
// :snippet-end:
// :snippet-start: beta-required-set
std::set<std::string> setTypeName;
// :snippet-end:
};
REALM_SCHEMA(Beta_AllTypesObject, boolName, optBoolName, intName, optIntName,
doubleName, optDoubleName, stringName, optStringName, enumName,
optEnumName, binaryDataName, optBinaryDataName, dateName, optDateName,
decimal128Name, optDecimal128Name, uuidName, optUuidName, objectIdName,
optObjectIdName, mixedName, mapName, listTypeName
optObjectIdName, mixedName, mapName, listTypeName, setTypeName
)

// :snippet-start: beta-dog-map-model
Expand Down Expand Up @@ -135,7 +138,8 @@ TEST_CASE("Test supported types", "[model][write]") {
.objectIdName = objectId,
.mixedName = realm::mixed("mixed data"),
.mapName = std::map<std::string, std::string>({{"foo", "bar"}}),
.listTypeName = std::vector<std::string>({"bar", "baz"})
.listTypeName = std::vector<std::string>({"bar", "baz"}),
.setTypeName = { "Lita", "Maui", "Ash" }
};

realm.write([&] {
Expand All @@ -159,6 +163,10 @@ TEST_CASE("Test supported types", "[model][write]") {
REQUIRE(specificAllTypeObjects.mapName["foo"] == "bar");
REQUIRE(specificAllTypeObjects.listTypeName[0] == "bar");

auto it = specificAllTypeObjects.setTypeName.find("Lita");
REQUIRE(it != specificAllTypeObjects.setTypeName.end());
REQUIRE(*it == "Lita");

realm.write([&] {
realm.remove(specificAllTypeObjects);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct Repository {
std::string ownerAndName;
std::set<int64_t> openPullRequestNumbers;
};
REALM_SCHEMA(Repository, ownerAndName, openPullRequestNumbers)
13 changes: 13 additions & 0 deletions source/examples/generated/cpp/crud.snippet.delete-set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Remove an element from the set with erase()
auto it3064 = managedDocsRealm.openPullRequestNumbers.find(3064);
CHECK(it3064 != managedDocsRealm.openPullRequestNumbers.end());
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.erase(it3065);
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4);

// Clear the entire contents of the set
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.clear();
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 0);
16 changes: 16 additions & 0 deletions source/examples/generated/cpp/crud.snippet.read-set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
auto repositories = realm.objects<Repository>();

auto repositoriesNamedDocsRealm = repositories.where([](auto &repository) {
return repository.ownerAndName == "mongodb/docs-realm";
});

auto docsRealm = repositoriesNamedDocsRealm[0];

// You can check the size of the set
auto numberOfPullRequests = docsRealm.openPullRequestNumbers.size();

// Find an element in the set whose value is 3064
auto it = managedDocsRealm.openPullRequestNumbers.find(3064);

// Get a copy of the set that exists independent of the managed set
auto openRealmPullRequests = docsRealm.openPullRequestNumbers.detach();
29 changes: 29 additions & 0 deletions source/examples/generated/cpp/crud.snippet.update-set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Add elements to the set in a write transaction
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.insert(3066);
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4);

// Use std::set algorithms to update a set
// In this example, use std::set_union to add elements to the set
// 3064 already exists, so it won't be added, but 3065 and 3067 are unique
// values and will be added to the set.
auto newOpenPullRequests = std::set<int64_t>({ 3064, 3065, 3067 });
realm.write([&] {
std::set_union(
docsRealm.openPullRequestNumbers.begin(),
docsRealm.openPullRequestNumbers.end(),
newOpenPullRequests.begin(),
newOpenPullRequests.end(),
std::inserter(
managedDocsRealm.openPullRequestNumbers,
managedDocsRealm.openPullRequestNumbers.end()));
});
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 6);

// Erase elements from a set
auto it3065 = managedDocsRealm.openPullRequestNumbers.find(3065);
CHECK(it3065 != managedDocsRealm.openPullRequestNumbers.end());
realm.write([&] {
managedDocsRealm.openPullRequestNumbers.erase(it3065);
});
23 changes: 23 additions & 0 deletions source/examples/generated/cpp/crud.snippet.write-set-object.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
auto realm = db(std::move(config));

// Create an object that has a set property
auto docsRealmRepo = Repository {
.ownerAndName = "mongodb/docs-realm"
};

// Add the object to the realm and get the managed object
auto managedDocsRealm = realm.write([&]() {
return realm.add(std::move(docsRealmRepo));
});

// Insert items into the set
auto openPullRequestNumbers = { 3059, 3062, 3064 };

realm.write([&] {
for (auto number: openPullRequestNumbers) {
// You can only mutate the set in a write transaction.
// This means you can't set values at initialization,
// but must do it during a write.
managedDocsRealm.openPullRequestNumbers.insert(number);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std::set<std::string> setTypeName;
29 changes: 29 additions & 0 deletions source/sdk/cpp/crud/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,35 @@ For more information about supported map data types, refer to:
.. literalinclude:: /examples/generated/cpp/define-object-model.snippet.model-with-map-property.cpp
:language: cpp

.. _cpp-create-object-set-property:

Create an Object with a Set Property
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: v0.4.0-preview

You can create objects that contain :cpp-sdk:`set
<structrealm_1_1experimental_1_1managed_3_01std_1_1set_3_01T_01_5_01_4_01_4.html>`
properties as you would any Realm object, but you can only mutate a set
property within a write transaction. This means you can only set the value(s)
of a set property within a write transaction.

.. literalinclude:: /examples/generated/cpp/crud.snippet.write-set-object.cpp
:language: cpp

.. include:: /includes/tip-cpp-beta-experimental-features.rst

Model
`````

For more information about supported set data types, refer to:
:ref:`cpp-set-collections`.

.. literalinclude:: /examples/generated/cpp/crud.snippet.beta-model-with-set-property.cpp
:language: cpp

.. include:: /includes/tip-cpp-beta-experimental-features.rst

.. _cpp-create-asymmetric-object:

Create an Asymmetric Object
Expand Down
16 changes: 16 additions & 0 deletions source/sdk/cpp/crud/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ pass the key name to ``erase()``:

.. literalinclude:: /examples/generated/cpp/define-object-model.snippet.delete-map-value.cpp
:language: cpp

.. _cpp-delete-set-values:

Delete Set Values
~~~~~~~~~~~~~~~~~

.. versionadded:: v0.4.0-preview

You can delete a :cpp-sdk:`set element
<structrealm_1_1experimental_1_1managed_3_01std_1_1set_3_01T_01_5_01_4_01_4.html>`
with ``erase()``, or remove all elements from a set with ``clear()``.

.. literalinclude:: /examples/generated/cpp/crud.snippet.delete-set.cpp
:language: cpp

.. include:: /includes/tip-cpp-beta-experimental-features.rst
13 changes: 13 additions & 0 deletions source/sdk/cpp/crud/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,16 @@ as you would a standard C++ `map <https://en.cppreference.com/w/cpp/container/ma

.. literalinclude:: /examples/generated/cpp/define-object-model.snippet.read-map-value.cpp
:language: cpp

.. _cpp-read-and-iterate-set:

Read a Set Property
~~~~~~~~~~~~~~~~~~~

.. versionadded:: v0.4.0-preview

You can iterate, check the size of a set, and find values in a :cpp-sdk:`set property
<structrealm_1_1experimental_1_1managed_3_01std_1_1set_3_01T_01_5_01_4_01_4.html>`:

.. literalinclude:: /examples/generated/cpp/crud.snippet.read-set.cpp
:language: cpp
Loading

0 comments on commit 0793ca3

Please sign in to comment.