使用get()函数-C++ Hazelcast客户端IMAP问题

发布于 2025-01-27 20:42:59 字数 449 浏览 4 评论 0原文

在获取HazelCat键之前,在获得Hazelcast 5.0或更高版本中的密钥之前,有没有办法知道数据类型?

具体来说,我的C ++代码是:

hazelcast::client::imap* mapInstance = hz->get_map("MapName").get().get();;
boost::optional<bool> v;
try{
    v = mapInstance->get<std::string, bool>("key2get").get();
}
catch (...) {
    //print something
}


如果要获得的Hazelcast键是布尔值,则代码可完美。否则(例如,标签是双重),代码无法处理Try-Catch例程和崩溃。出现弹出窗口消息报告:“ abort()”,

谢谢您的帮助。

Is there a way to know the data type before of a hazelcat key before getting the key in Hazelcast 5.0 or later versions?

Specifically, my C++ code is:

hazelcast::client::imap* mapInstance = hz->get_map("MapName").get().get();;
boost::optional<bool> v;
try{
    v = mapInstance->get<std::string, bool>("key2get").get();
}
catch (...) {
    //print something
}


If the hazelcast key to get is a boolean, the code works perfectly. Otherwise (for example, the tag is a double), the code cannot handle the try-catch routine and crash. A pop-up windows message appears reporting: "Abort()"

Thank you in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2025-02-03 20:42:59

我刚刚在您的代码示例中意识到您未在行Hazelcast :: client :: imap* mapinstance = hz.get_map(“ mapName”)。get()。get();您获得临时shared_ptr的原始指针,一旦您进入下一行,它可能会立即变得无效。您可以通过以下方式更改它:auto mapInstance = hz.get_map(“ mapName”)。get();这很可能是流产的原因。通常,您应该成功地使用catch(...)来成功捕获异常,并且不会导致流产。

C ++ API强烈键入,这意味着您需要在方法模板参数上提供期望的类型。但是,我们有一个解决方案来处理这些情况,您可能不知道之前不知道数据类型,否则榛子数据结构可能在值中可能具有混合的类型。对于这些,您可以使用特殊类型 typed_data 。这种特殊类型使您可以访问类型信息并根据接收类型做出运行时决策。您不需要使用异常处理。这是一个示例代码(刚刚提交了代码示例的PR 在这里也):

using namespace hazelcast::client;

auto hz = hazelcast::new_client().get();

auto map = hz.get_map("map").get();

boost::optional<typed_data> the_value = map->get<std::string, typed_data>("key").get();
if (the_value) {
    std::cout << "value type id is: " << the_value->get_type() << std::endl;
    if (the_value->get_type().type_id == serialization::pimpl::serialization_constants::CONSTANT_TYPE_BOOLEAN) {
        // we know for sure that the value is of type `bool` and we can get it with no exception
        bool value = *the_value->get<bool>();
        std::cout << "value is: " << value << std::endl;
    }
} else {
    std::cout << "value is not set" << std::endl;
}

I just realized in your code sample that you are not using the shared_ptr properly at line hazelcast::client::imap* mapInstance = hz.get_map("MapName").get().get(); You get the raw pointer of the temporary shared_ptr which may become invalid as soon as you go to the next line. Can you please change it with: auto mapInstance = hz.get_map("MapName").get(); This is most probably the reason for the abort. Normally, you should be successfully catching the exception with catch(...) and it should not cause abort.

The C++ API is strongly typed meaning that you require to provide the type you expect on the method template parameter. However, we have a solution for handling these situations where you may not know the data type before hand or the hazelcast data structure may have mixed types in the values. For these you can use the special type typed_data. This special type allows you to access the type information and make runtime decisions based on the received type. You do not need to use exception handling. Here is an example code (Just submitted a PR for a code sample here as well):

using namespace hazelcast::client;

auto hz = hazelcast::new_client().get();

auto map = hz.get_map("map").get();

boost::optional<typed_data> the_value = map->get<std::string, typed_data>("key").get();
if (the_value) {
    std::cout << "value type id is: " << the_value->get_type() << std::endl;
    if (the_value->get_type().type_id == serialization::pimpl::serialization_constants::CONSTANT_TYPE_BOOLEAN) {
        // we know for sure that the value is of type `bool` and we can get it with no exception
        bool value = *the_value->get<bool>();
        std::cout << "value is: " << value << std::endl;
    }
} else {
    std::cout << "value is not set" << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文