Skip to content

Commit 4f64b9d

Browse files
committed
CXXCBC-346: Support for maxTTL value of -1 for collection 'no expiry'
1 parent 8ceabca commit 4f64b9d

File tree

11 files changed

+392
-13
lines changed

11 files changed

+392
-13
lines changed

core/operations/management/collection_create.cxx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ collection_create_request::encode_to(encoded_request_type& encoded, http_context
3434
encoded.path = fmt::format("/pools/default/buckets/{}/scopes/{}/collections", bucket_name, scope_name);
3535
encoded.headers["content-type"] = "application/x-www-form-urlencoded";
3636
encoded.body = fmt::format("name={}", utils::string_codec::form_encode(collection_name));
37-
if (max_expiry > 0) {
38-
encoded.body.append(fmt::format("&maxTTL={}", max_expiry));
37+
if (max_expiry >= -1) {
38+
if (max_expiry != 0) {
39+
encoded.body.append(fmt::format("&maxTTL={}", max_expiry));
40+
}
41+
} else {
42+
return couchbase::errc::common::invalid_argument;
3943
}
4044
if (history.has_value()) {
4145
encoded.body.append(fmt::format("&history={}", history.value()));

core/operations/management/collection_create.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct collection_create_request {
4141
std::string bucket_name;
4242
std::string scope_name;
4343
std::string collection_name;
44-
std::uint32_t max_expiry{ 0 };
44+
std::int32_t max_expiry{ 0 };
4545
std::optional<bool> history{};
4646

4747
std::optional<std::string> client_context_id{};

core/operations/management/collection_update.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ collection_update_request::encode_to(encoded_request_type& encoded, http_context
3535
encoded.headers["content-type"] = "application/x-www-form-urlencoded";
3636
std::map<std::string, std::string> values{};
3737
if (max_expiry.has_value()) {
38-
values["maxTTL"] = std::to_string(max_expiry.value());
38+
if (max_expiry.value() >= -1) {
39+
values["maxTTL"] = std::to_string(max_expiry.value());
40+
} else {
41+
return errc::common::invalid_argument;
42+
}
3943
}
4044
if (history.has_value()) {
4145
values["history"] = history.value() ? "true" : "false";

core/operations/management/collection_update.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct collection_update_request {
4141
std::string bucket_name;
4242
std::string scope_name;
4343
std::string collection_name;
44-
std::optional<std::uint32_t> max_expiry{};
44+
std::optional<std::int32_t> max_expiry{};
4545
std::optional<bool> history{};
4646

4747
std::optional<std::string> client_context_id{};

core/topology/collections_manifest.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct collections_manifest {
2828
struct collection {
2929
std::uint64_t uid;
3030
std::string name;
31-
std::uint32_t max_expiry{ 0 };
31+
std::int32_t max_expiry{ 0 };
3232
std::optional<bool> history{};
3333
};
3434

core/topology/collections_manifest_json.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct traits<couchbase::core::topology::collections_manifest> {
4141
collection.uid = std::stoull(c.at("uid").get_string(), nullptr, 16);
4242
collection.name = c.at("name").get_string();
4343
if (const auto* max_ttl = c.find("maxTTL"); max_ttl != nullptr) {
44-
collection.max_expiry = max_ttl->template as<std::uint32_t>();
44+
collection.max_expiry = max_ttl->template as<std::int32_t>();
4545
}
4646
if (const auto* history = c.find("history"); history != nullptr) {
4747
collection.history = history->template as<std::optional<bool>>();

couchbase/create_collection_options.hxx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,52 @@
2020
#include <couchbase/common_options.hxx>
2121
#include <couchbase/manager_error_context.hxx>
2222

23+
#include <cstdint>
2324
#include <functional>
2425
#include <optional>
2526

2627
namespace couchbase
2728
{
2829
struct create_collection_options : public common_options<create_collection_options> {
2930
public:
31+
/**
32+
* Immutable value object representing consistent options.
33+
*
34+
* @since 1.0.0
35+
* @internal
36+
*/
3037
struct built : public common_options<create_collection_options>::built {
3138
};
3239

40+
/**
41+
* Validates the options and returns them as an immutable value.
42+
*
43+
* @return consistent options as an immutable value
44+
*
45+
* @exception std::system_error with code errc::common::invalid_argument if the options are not valid
46+
*
47+
* @since 1.0.0
48+
* @internal
49+
*/
3350
[[nodiscard]] auto build() const -> built
3451
{
3552
return { build_common_options() };
3653
}
3754
};
3855

56+
/**
57+
* The settings to use when creating the collection
58+
*/
3959
struct create_collection_settings {
40-
std::uint32_t max_expiry{ 0 };
60+
/**
61+
* The maximum expiry, in seconds, for documents in this collection. Values greater than or equal to -1 are valid.
62+
* Value of 0 sets max_expiry to the bucket-level setting and value of -1 to set it as no-expiry.
63+
*/
64+
std::int32_t max_expiry{ 0 };
65+
66+
/**
67+
* Whether history retention should be enabled. If unset, the bucket-level setting is used.
68+
*/
4169
std::optional<bool> history{};
4270
};
4371

couchbase/management/collection_spec.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace couchbase::management::bucket
2222
struct collection_spec {
2323
std::string name;
2424
std::string scope_name;
25-
std::uint32_t max_expiry{};
25+
std::int32_t max_expiry{};
2626
std::optional<bool> history{};
2727
};
2828

couchbase/update_collection_options.hxx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,55 @@
1717

1818
#pragma once
1919

20-
#include <functional>
20+
#include <couchbase/common_options.hxx>
21+
#include <couchbase/manager_error_context.hxx>
22+
23+
#include <cstdint>
2124
#include <memory>
2225
#include <optional>
23-
#include <string>
2426

2527
namespace couchbase
2628
{
2729
struct update_collection_options : public common_options<update_collection_options> {
2830
public:
31+
/**
32+
* Immutable value object representing consistent options.
33+
*
34+
* @since 1.0.0
35+
* @internal
36+
*/
2937
struct built : public common_options<update_collection_options>::built {
3038
};
3139

40+
/**
41+
* Validates the options and returns them as an immutable value.
42+
*
43+
* @return consistent options as an immutable value
44+
*
45+
* @exception std::system_error with code errc::common::invalid_argument if the options are not valid
46+
*
47+
* @since 1.0.0
48+
* @internal
49+
*/
3250
[[nodiscard]] auto build() const -> built
3351
{
3452
return { build_common_options() };
3553
}
3654
};
3755

56+
/**
57+
* The settings that should be updated for the collection
58+
*/
3859
struct update_collection_settings {
39-
std::optional<std::uint32_t> max_expiry{};
60+
/**
61+
* The maximum expiry, in seconds, for documents in this collection. Values greater than or equal to -1 are valid.
62+
* Value of 0 sets max_expiry to the bucket-level setting and value of -1 to set it as no-expiry.
63+
*/
64+
std::optional<std::int32_t> max_expiry{};
65+
66+
/**
67+
* Whether history retention should be enabled.
68+
*/
4069
std::optional<bool> history{};
4170
};
4271

0 commit comments

Comments
 (0)