@@ -2686,25 +2686,45 @@ Result<ConfigStore> ConfigStore::open(std::string_view name) {
26862686}
26872687
26882688Result<std::optional<HostString>> ConfigStore::get (std::string_view name) {
2689+ return this ->get (name, CONFIG_STORE_INITIAL_BUF_LEN);
2690+ }
2691+
2692+ Result<std::optional<HostString>> ConfigStore::get (std::string_view name,
2693+ uint32_t initial_buf_len) {
26892694 TRACE_CALL ()
26902695 Result<std::optional<HostString>> res;
26912696
26922697 auto name_str = string_view_to_world_string (name);
26932698 fastly::fastly_world_string ret;
26942699 fastly::fastly_host_error err;
2695- ret.ptr = static_cast <uint8_t *>(cabi_malloc (CONFIG_STORE_ENTRY_MAX_LEN, 1 ));
2696- if (!convert_result (fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
2697- name_str.len , reinterpret_cast <char *>(ret.ptr ),
2698- CONFIG_STORE_ENTRY_MAX_LEN, &ret.len ),
2699- &err)) {
2700+ uint32_t buf_len{initial_buf_len};
2701+
2702+ ret.ptr = static_cast <uint8_t *>(cabi_malloc (buf_len, 1 ));
2703+
2704+ bool succeeded{convert_result (
2705+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2706+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2707+ &err)};
2708+
2709+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2710+ buf_len = ret.len ;
2711+ ret.len = 0 ;
2712+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , initial_buf_len, 1 , buf_len));
2713+ succeeded = convert_result (
2714+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2715+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2716+ &err);
2717+ }
2718+
2719+ if (!succeeded) {
27002720 cabi_free (ret.ptr );
27012721 if (error_is_optional_none (err)) {
27022722 res.emplace (std::nullopt );
27032723 } else {
27042724 res.emplace_err (err);
27052725 }
27062726 } else {
2707- ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , CONFIG_STORE_ENTRY_MAX_LEN , 1 , ret.len ));
2727+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , buf_len , 1 , ret.len ));
27082728 res.emplace (make_host_string (ret));
27092729 }
27102730
0 commit comments