Skip to content

Commit a873bb9

Browse files
authored
CXXCBC-376 Create and Update bucket sending unnecessary fields to server (#451)
* Make optional bucket settings fields optional * Update request encoding * fix unecessary error code conversion * Fix public API bucket settings ram default value
1 parent 1a61a47 commit a873bb9

File tree

7 files changed

+96
-53
lines changed

7 files changed

+96
-53
lines changed

core/impl/get_all_buckets.cxx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,26 @@ map_bucket_settings(const couchbase::core::management::cluster::bucket_settings&
5252
couchbase::management::cluster::bucket_settings bucket_settings{};
5353
bucket_settings.name = bucket.name;
5454
bucket_settings.ram_quota_mb = bucket.ram_quota_mb;
55-
bucket_settings.max_expiry = bucket.max_expiry;
5655
bucket_settings.minimum_durability_level = bucket.minimum_durability_level;
57-
bucket_settings.num_replicas = bucket.num_replicas;
58-
bucket_settings.replica_indexes = bucket.replica_indexes;
59-
bucket_settings.flush_enabled = bucket.flush_enabled;
6056
bucket_settings.history_retention_collection_default = bucket.history_retention_collection_default;
61-
bucket_settings.history_retention_bytes = bucket.history_retention_bytes;
62-
bucket_settings.history_retention_duration = bucket.history_retention_duration;
57+
if (bucket.max_expiry.has_value()) {
58+
bucket_settings.max_expiry = bucket.max_expiry.value();
59+
}
60+
if (bucket.num_replicas.has_value()) {
61+
bucket_settings.num_replicas = bucket.num_replicas.value();
62+
}
63+
if (bucket.replica_indexes.has_value()) {
64+
bucket_settings.replica_indexes = bucket.replica_indexes.value();
65+
}
66+
if (bucket.flush_enabled.has_value()) {
67+
bucket_settings.flush_enabled = bucket.flush_enabled.value();
68+
}
69+
if (bucket.history_retention_bytes.has_value()) {
70+
bucket_settings.history_retention_bytes = bucket.history_retention_bytes.value();
71+
}
72+
if (bucket.history_retention_duration.has_value()) {
73+
bucket_settings.history_retention_duration = bucket.history_retention_duration.value();
74+
}
6375
switch (bucket.conflict_resolution_type) {
6476
case core::management::cluster::bucket_conflict_resolution::unknown:
6577
bucket_settings.conflict_resolution_type = management::cluster::bucket_conflict_resolution::unknown;

core/impl/get_bucket.cxx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,26 @@ map_bucket_settings(const couchbase::core::management::cluster::bucket_settings&
5252
couchbase::management::cluster::bucket_settings bucket_settings{};
5353
bucket_settings.name = bucket.name;
5454
bucket_settings.ram_quota_mb = bucket.ram_quota_mb;
55-
bucket_settings.max_expiry = bucket.max_expiry;
5655
bucket_settings.minimum_durability_level = bucket.minimum_durability_level;
57-
bucket_settings.num_replicas = bucket.num_replicas;
58-
bucket_settings.replica_indexes = bucket.replica_indexes;
59-
bucket_settings.flush_enabled = bucket.flush_enabled;
6056
bucket_settings.history_retention_collection_default = bucket.history_retention_collection_default;
61-
bucket_settings.history_retention_bytes = bucket.history_retention_bytes;
62-
bucket_settings.history_retention_duration = bucket.history_retention_duration;
57+
if (bucket.max_expiry.has_value()) {
58+
bucket_settings.max_expiry = bucket.max_expiry.value();
59+
}
60+
if (bucket.num_replicas.has_value()) {
61+
bucket_settings.num_replicas = bucket.num_replicas.value();
62+
}
63+
if (bucket.replica_indexes.has_value()) {
64+
bucket_settings.replica_indexes = bucket.replica_indexes.value();
65+
}
66+
if (bucket.flush_enabled.has_value()) {
67+
bucket_settings.flush_enabled = bucket.flush_enabled.value();
68+
}
69+
if (bucket.history_retention_bytes.has_value()) {
70+
bucket_settings.history_retention_bytes = bucket.history_retention_bytes.value();
71+
}
72+
if (bucket.history_retention_duration.has_value()) {
73+
bucket_settings.history_retention_duration = bucket.history_retention_duration.value();
74+
}
6375
switch (bucket.conflict_resolution_type) {
6476
case core::management::cluster::bucket_conflict_resolution::unknown:
6577
bucket_settings.conflict_resolution_type = management::cluster::bucket_conflict_resolution::unknown;

core/management/bucket_settings.hxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ struct bucket_settings {
105105

106106
std::string name;
107107
std::string uuid;
108+
std::uint64_t ram_quota_mb{ 0 }; // If not explicitly set, defaults to 100 on create_bucket, unset on update_bucket
108109
cluster::bucket_type bucket_type{ cluster::bucket_type::unknown };
109-
std::uint64_t ram_quota_mb{ 100 };
110-
std::uint32_t max_expiry{ 0 };
110+
std::optional<std::uint32_t> max_expiry{};
111111
bucket_compression compression_mode{ bucket_compression::unknown };
112112
std::optional<couchbase::durability_level> minimum_durability_level{};
113-
std::uint32_t num_replicas{ 1 };
114-
bool replica_indexes{ false };
115-
bool flush_enabled{ false };
113+
std::optional<std::uint32_t> num_replicas{};
114+
std::optional<bool> replica_indexes{};
115+
std::optional<bool> flush_enabled{};
116116
bucket_eviction_policy eviction_policy{ bucket_eviction_policy::unknown };
117117
bucket_conflict_resolution conflict_resolution_type{ bucket_conflict_resolution::unknown };
118118
std::optional<bool> history_retention_collection_default{};
119-
std::uint32_t history_retention_bytes{ 0 };
120-
std::uint32_t history_retention_duration{ 0 };
119+
std::optional<std::uint32_t> history_retention_bytes;
120+
std::optional<std::uint32_t> history_retention_duration{};
121121

122122
/**
123123
* UNCOMMITTED: This API may change in the future

core/management/bucket_settings_json.hxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ struct traits<couchbase::core::management::cluster::bucket_settings> {
3333
result.uuid = v.at("uuid").get_string();
3434
const static std::uint64_t megabyte = 1024LLU * 1024LLU;
3535
result.ram_quota_mb = v.at("quota").at("rawRAM").get_unsigned() / megabyte;
36-
result.num_replicas = v.at("replicaNumber").template as<std::uint32_t>();
36+
result.num_replicas = v.at("replicaNumber").template as<std::optional<std::uint32_t>>();
3737

3838
if (auto* max_ttl = v.find("maxTTL"); max_ttl != nullptr) {
39-
result.max_expiry = max_ttl->template as<std::uint32_t>();
39+
result.max_expiry = max_ttl->template as<std::optional<std::uint32_t>>();
4040
}
4141

4242
if (auto* history_retention_default = v.find("historyRetentionCollectionDefault"); history_retention_default != nullptr) {
4343
result.history_retention_collection_default = history_retention_default->template as<std::optional<bool>>();
4444
}
4545
if (auto* history_retention_bytes = v.find("historyRetentionBytes"); history_retention_bytes != nullptr) {
46-
result.history_retention_bytes = history_retention_bytes->template as<std::uint32_t>();
46+
result.history_retention_bytes = history_retention_bytes->template as<std::optional<std::uint32_t>>();
4747
}
4848
if (auto* history_retention_duration = v.find("historyRetentionSeconds"); history_retention_duration != nullptr) {
49-
result.history_retention_duration = history_retention_duration->template as<std::uint32_t>();
49+
result.history_retention_duration = history_retention_duration->template as<std::optional<std::uint32_t>>();
5050
}
5151

5252
if (auto& str = v.at("bucketType").get_string(); str == "couchbase" || str == "membase") {

core/operations/management/bucket_create.cxx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,35 @@ bucket_create_request::encode_to(encoded_request_type& encoded, http_context& /*
4848
case couchbase::core::management::cluster::bucket_type::unknown:
4949
break;
5050
}
51-
encoded.body.append(fmt::format("&ramQuotaMB={}", bucket.ram_quota_mb));
52-
if (bucket.bucket_type != couchbase::core::management::cluster::bucket_type::memcached) {
53-
encoded.body.append(fmt::format("&replicaNumber={}", bucket.num_replicas));
51+
if (bucket.ram_quota_mb == 0) {
52+
encoded.body.append(fmt::format("&ramQuotaMB={}", 100)); // If not explicitly set, set to prior default value of 100
53+
} else {
54+
encoded.body.append(fmt::format("&ramQuotaMB={}", bucket.ram_quota_mb));
5455
}
55-
if (bucket.max_expiry > 0) {
56-
encoded.body.append(fmt::format("&maxTTL={}", bucket.max_expiry));
56+
57+
if (bucket.bucket_type != couchbase::core::management::cluster::bucket_type::memcached && bucket.num_replicas.has_value()) {
58+
encoded.body.append(fmt::format("&replicaNumber={}", bucket.num_replicas.value()));
59+
}
60+
if (bucket.max_expiry.has_value()) {
61+
encoded.body.append(fmt::format("&maxTTL={}", bucket.max_expiry.value()));
5762
}
58-
if (bucket.bucket_type != couchbase::core::management::cluster::bucket_type::ephemeral) {
59-
encoded.body.append(fmt::format("&replicaIndex={}", bucket.replica_indexes ? "1" : "0"));
63+
if (bucket.bucket_type != couchbase::core::management::cluster::bucket_type::ephemeral && bucket.replica_indexes.has_value()) {
64+
encoded.body.append(fmt::format("&replicaIndex={}", bucket.replica_indexes.value() ? "1" : "0"));
6065
}
6166
if (bucket.history_retention_collection_default.has_value()) {
6267
encoded.body.append(
6368
fmt::format("&historyRetentionCollectionDefault={}", bucket.history_retention_collection_default.value() ? "true" : "false"));
6469
}
65-
if (bucket.history_retention_bytes > 0) {
66-
encoded.body.append(fmt::format("&historyRetentionBytes={}", bucket.history_retention_bytes));
70+
if (bucket.history_retention_bytes.has_value()) {
71+
encoded.body.append(fmt::format("&historyRetentionBytes={}", bucket.history_retention_bytes.value()));
6772
}
68-
if (bucket.history_retention_duration > 0) {
69-
encoded.body.append(fmt::format("&historyRetentionSeconds={}", bucket.history_retention_duration));
73+
if (bucket.history_retention_duration.has_value()) {
74+
encoded.body.append(fmt::format("&historyRetentionSeconds={}", bucket.history_retention_duration.value()));
7075
}
71-
encoded.body.append(fmt::format("&flushEnabled={}", bucket.flush_enabled ? "1" : "0"));
76+
if (bucket.flush_enabled.has_value()) {
77+
encoded.body.append(fmt::format("&flushEnabled={}", bucket.flush_enabled.value() ? "1" : "0"));
78+
}
79+
7280
switch (bucket.eviction_policy) {
7381
case couchbase::core::management::cluster::bucket_eviction_policy::full:
7482
encoded.body.append("&evictionPolicy=fullEviction");
@@ -111,7 +119,7 @@ bucket_create_request::encode_to(encoded_request_type& encoded, http_context& /*
111119
case couchbase::core::management::cluster::bucket_conflict_resolution::unknown:
112120
break;
113121
}
114-
if (bucket.minimum_durability_level) {
122+
if (bucket.minimum_durability_level.has_value()) {
115123
switch (bucket.minimum_durability_level.value()) {
116124
case durability_level::none:
117125
encoded.body.append("&durabilityMinLevel=none");

core/operations/management/bucket_update.cxx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,34 @@ bucket_update_request::encode_to(encoded_request_type& encoded, http_context& /*
3333
encoded.path = fmt::format("/pools/default/buckets/{}", bucket.name);
3434

3535
encoded.headers["content-type"] = "application/x-www-form-urlencoded";
36-
encoded.body.append(fmt::format("&ramQuotaMB={}", bucket.ram_quota_mb));
37-
encoded.body.append(fmt::format("&replicaNumber={}", bucket.num_replicas));
38-
if (bucket.max_expiry > 0) {
39-
encoded.body.append(fmt::format("&maxTTL={}", bucket.max_expiry));
36+
37+
if (bucket.ram_quota_mb > 0) {
38+
encoded.body.append(fmt::format("&ramQuotaMB={}", bucket.ram_quota_mb));
39+
}
40+
if (bucket.num_replicas.has_value()) {
41+
encoded.body.append(fmt::format("&replicaNumber={}", bucket.num_replicas.value()));
42+
}
43+
44+
if (bucket.max_expiry.has_value()) {
45+
encoded.body.append(fmt::format("&maxTTL={}", bucket.max_expiry.value()));
4046
}
4147
if (bucket.history_retention_collection_default.has_value()) {
4248
encoded.body.append(
4349
fmt::format("&historyRetentionCollectionDefault={}", bucket.history_retention_collection_default.value() ? "true" : "false"));
4450
}
45-
if (bucket.history_retention_bytes > 0) {
46-
encoded.body.append(fmt::format("&historyRetentionBytes={}", bucket.history_retention_bytes));
51+
if (bucket.history_retention_bytes.has_value()) {
52+
encoded.body.append(fmt::format("&historyRetentionBytes={}", bucket.history_retention_bytes.value()));
4753
}
48-
if (bucket.history_retention_duration > 0) {
49-
encoded.body.append(fmt::format("&historyRetentionSeconds={}", bucket.history_retention_duration));
54+
if (bucket.history_retention_duration.has_value()) {
55+
encoded.body.append(fmt::format("&historyRetentionSeconds={}", bucket.history_retention_duration.value()));
5056
}
51-
encoded.body.append(fmt::format("&replicaIndex={}", bucket.replica_indexes ? "1" : "0"));
52-
encoded.body.append(fmt::format("&flushEnabled={}", bucket.flush_enabled ? "1" : "0"));
57+
if (bucket.replica_indexes.has_value()) {
58+
encoded.body.append(fmt::format("&replicaIndex={}", bucket.replica_indexes.value() ? "1" : "0"));
59+
}
60+
if (bucket.flush_enabled.has_value()) {
61+
encoded.body.append(fmt::format("&flushEnabled={}", bucket.flush_enabled.value() ? "1" : "0"));
62+
}
63+
5364
switch (bucket.eviction_policy) {
5465
case couchbase::core::management::cluster::bucket_eviction_policy::full:
5566
encoded.body.append("&evictionPolicy=fullEviction");

couchbase/management/bucket_settings.hxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ struct bucket_settings {
9898

9999
std::string name;
100100
cluster::bucket_type bucket_type{ cluster::bucket_type::unknown };
101-
std::uint64_t ram_quota_mb{ 100 };
102-
std::uint32_t max_expiry{ 0 };
101+
std::uint64_t ram_quota_mb{ 0 };
102+
std::optional<std::uint32_t> max_expiry{};
103103
bucket_compression compression_mode{ bucket_compression::unknown };
104104
std::optional<couchbase::durability_level> minimum_durability_level{};
105-
std::uint32_t num_replicas{ 1 };
106-
bool replica_indexes{ false };
107-
bool flush_enabled{ false };
105+
std::optional<std::uint32_t> num_replicas{};
106+
std::optional<bool> replica_indexes{};
107+
std::optional<bool> flush_enabled{};
108108
bucket_eviction_policy eviction_policy{ bucket_eviction_policy::unknown };
109109
bucket_conflict_resolution conflict_resolution_type{ bucket_conflict_resolution::unknown };
110110
std::optional<bool> history_retention_collection_default{};
111-
std::uint32_t history_retention_bytes{ 0 };
112-
std::uint32_t history_retention_duration{ 0 };
111+
std::optional<std::uint32_t> history_retention_bytes;
112+
std::optional<std::uint32_t> history_retention_duration{};
113113

114114
/**
115115
* UNCOMMITTED: This API may change in the future

0 commit comments

Comments
 (0)