• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / github_pull_request_281750

30 Oct 2023 03:37PM UTC coverage: 90.528% (-1.0%) from 91.571%
github_pull_request_281750

Pull #6073

Evergreen

jedelbo
Log free space and history sizes when opening file
Pull Request #6073: Merge next-major

95488 of 175952 branches covered (0.0%)

8973 of 12277 new or added lines in 149 files covered. (73.09%)

622 existing lines in 51 files now uncovered.

233503 of 257934 relevant lines covered (90.53%)

6533720.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.18
/src/realm/object-store/c_api/dictionary.cpp
1
#include "realm/object-store/c_api/types.hpp"
2
#include "realm/object-store/dictionary.hpp"
3
#include <realm/object-store/c_api/util.hpp>
4

5
namespace realm::c_api {
6

7
RLM_API bool realm_dictionary_size(const realm_dictionary_t* dict, size_t* out_size)
8
{
20✔
9
    return wrap_err([&]() {
20✔
10
        size_t size = dict->size();
20✔
11
        if (out_size)
20✔
12
            *out_size = size;
20✔
13
        return true;
20✔
14
    });
20✔
15
}
20✔
16

17
RLM_API bool realm_dictionary_get_property(const realm_dictionary_t* dict, realm_property_info_t* out_property_info)
18
{
×
19
    static_cast<void>(dict);
×
20
    static_cast<void>(out_property_info);
×
21
    REALM_TERMINATE("Not implemented yet.");
×
22
}
×
23

24
RLM_API bool realm_dictionary_find(const realm_dictionary_t* dict, realm_value_t key, realm_value_t* out_value,
25
                                   bool* out_found)
26
{
70✔
27
    if (key.type != RLM_TYPE_STRING) {
70✔
28
        if (out_found)
2✔
29
            *out_found = false;
2✔
30
        return true;
2✔
31
    }
2✔
32

34✔
33
    return wrap_err([&]() {
68✔
34
        dict->verify_attached();
68✔
35
        StringData k{key.string.data, key.string.size};
68✔
36
        auto val = dict->try_get_any(k);
68✔
37
        if (!val) {
68✔
38
            if (out_found)
2✔
39
                *out_found = false;
2✔
40
        }
2✔
41
        else {
66✔
42
            if (out_value)
66✔
43
                *out_value = to_capi(*val);
66✔
44
            if (out_found)
66✔
45
                *out_found = true;
66✔
46
        }
66✔
47
        return true;
68✔
48
    });
68✔
49
}
68✔
50

51
RLM_API bool realm_dictionary_get(const realm_dictionary_t* dict, size_t index, realm_value_t* out_key,
52
                                  realm_value_t* out_value)
53
{
44✔
54
    return wrap_err([&]() {
44✔
55
        dict->verify_attached();
44✔
56
        auto [key, value] = dict->get_pair(index);
44✔
57
        if (out_key) {
44✔
58
            out_key->type = RLM_TYPE_STRING;
42✔
59
            out_key->string = to_capi(key);
42✔
60
        }
42✔
61
        if (out_value)
44✔
62
            *out_value = to_capi(value);
42✔
63
        return true;
44✔
64
    });
44✔
65
}
44✔
66

67
RLM_API bool realm_dictionary_insert(realm_dictionary_t* dict, realm_value_t key, realm_value_t value,
68
                                     size_t* out_index, bool* out_inserted)
69
{
136✔
70
    return wrap_err([&]() {
136✔
71
        if (key.type != RLM_TYPE_STRING) {
136✔
72
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
2✔
73
        }
2✔
74

67✔
75
        StringData k{key.string.data, key.string.size};
134✔
76
        auto val = from_capi(value);
134✔
77
        check_value_assignable(*dict, val);
134✔
78
        auto [index, inserted] = dict->insert_any(k, val);
134✔
79

67✔
80
        if (out_index)
134✔
81
            *out_index = index;
94✔
82
        if (out_inserted)
134✔
83
            *out_inserted = inserted;
112✔
84

67✔
85
        return true;
134✔
86
    });
134✔
87
}
136✔
88

89
RLM_API realm_object_t* realm_dictionary_insert_embedded(realm_dictionary_t* dict, realm_value_t key)
90
{
×
91
    return wrap_err([&]() {
×
92
        if (key.type != RLM_TYPE_STRING) {
×
93
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
94
        }
×
95

96
        StringData k{key.string.data, key.string.size};
×
97
        return new realm_object_t({dict->get_realm(), dict->insert_embedded(k)});
×
98
    });
×
99
}
×
100

101
RLM_API realm_list_t* realm_dictionary_insert_list(realm_dictionary_t* dictionary, realm_value_t key)
102
{
6✔
103
    return wrap_err([&]() {
6✔
104
        if (key.type != RLM_TYPE_STRING) {
6✔
NEW
105
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
106
        }
×
107

3✔
108
        StringData k{key.string.data, key.string.size};
6✔
109
        dictionary->insert_collection(k, CollectionType::List);
6✔
110
        return new realm_list_t{dictionary->get_list(k)};
6✔
111
    });
6✔
112
}
6✔
113

114
RLM_API realm_set_t* realm_dictionary_insert_set(realm_dictionary_t* dictionary, realm_value_t key)
115
{
4✔
116
    return wrap_err([&]() {
4✔
117
        if (key.type != RLM_TYPE_STRING) {
4✔
NEW
118
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
119
        }
×
120

2✔
121
        StringData k{key.string.data, key.string.size};
4✔
122
        dictionary->insert_collection(k, CollectionType::Set);
4✔
123
        return new realm_set_t{dictionary->get_set(k)};
4✔
124
    });
4✔
125
}
4✔
126

127
RLM_API realm_dictionary_t* realm_dictionary_insert_dictionary(realm_dictionary_t* dictionary, realm_value_t key)
128
{
4✔
129
    return wrap_err([&]() {
4✔
130
        if (key.type != RLM_TYPE_STRING) {
4✔
NEW
131
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
132
        }
×
133

2✔
134
        StringData k{key.string.data, key.string.size};
4✔
135
        dictionary->insert_collection(k, CollectionType::Dictionary);
4✔
136
        return new realm_dictionary_t{dictionary->get_dictionary(k)};
4✔
137
    });
4✔
138
}
4✔
139

140

141
RLM_API realm_list_t* realm_dictionary_get_list(realm_dictionary_t* dictionary, realm_value_t key)
NEW
142
{
×
NEW
143
    return wrap_err([&]() {
×
NEW
144
        if (key.type != RLM_TYPE_STRING) {
×
NEW
145
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
146
        }
×
147

NEW
148
        StringData k{key.string.data, key.string.size};
×
NEW
149
        return new realm_list_t{dictionary->get_list(k)};
×
NEW
150
    });
×
NEW
151
}
×
152

153
RLM_API realm_dictionary_t* realm_dictionary_get_dictionary(realm_dictionary_t* dictionary, realm_value_t key)
NEW
154
{
×
NEW
155
    return wrap_err([&]() {
×
NEW
156
        if (key.type != RLM_TYPE_STRING) {
×
NEW
157
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
158
        }
×
159

NEW
160
        StringData k{key.string.data, key.string.size};
×
NEW
161
        return new realm_dictionary_t{dictionary->get_dictionary(k)};
×
NEW
162
    });
×
NEW
163
}
×
164

165
RLM_API realm_set_t* realm_dictionary_get_set(realm_dictionary_t* dictionary, realm_value_t key)
NEW
166
{
×
NEW
167
    return wrap_err([&]() {
×
NEW
168
        if (key.type != RLM_TYPE_STRING) {
×
NEW
169
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
NEW
170
        }
×
171

NEW
172
        StringData k{key.string.data, key.string.size};
×
NEW
173
        return new realm_set_t{dictionary->get_set(k)};
×
NEW
174
    });
×
NEW
175
}
×
176

177
RLM_API realm_object_t* realm_dictionary_get_linked_object(realm_dictionary_t* dict, realm_value_t key)
178
{
×
179
    return wrap_err([&]() {
×
180
        if (key.type != RLM_TYPE_STRING) {
×
181
            throw InvalidArgument{"Only string keys are supported in dictionaries"};
×
182
        }
×
183

184
        StringData k{key.string.data, key.string.size};
×
185
        auto o = dict->get_object(k);
×
186
        return o ? new realm_object_t({dict->get_realm(), o}) : nullptr;
×
187
    });
×
188
}
×
189

190
RLM_API bool realm_dictionary_erase(realm_dictionary_t* dict, realm_value_t key, bool* out_erased)
191
{
6✔
192
    return wrap_err([&]() {
6✔
193
        bool erased = false;
6✔
194
        if (key.type == RLM_TYPE_STRING) {
6✔
195
            StringData k{key.string.data, key.string.size};
4✔
196
            erased = dict->try_erase(k);
4✔
197
        }
4✔
198

3✔
199
        if (out_erased)
6✔
200
            *out_erased = erased;
4✔
201
        return true;
6✔
202
    });
6✔
203
}
6✔
204

205
RLM_API bool realm_dictionary_get_keys(realm_dictionary_t* dict, size_t* out_size, realm_results_t** out_keys)
206
{
2✔
207
    return wrap_err([&]() {
2✔
208
        auto keys = dict->get_keys();
2✔
209
        *out_size = keys.size();
2✔
210
        *out_keys = new realm_results_t{keys};
2✔
211
        return true;
2✔
212
    });
2✔
213
}
2✔
214

215
RLM_API bool realm_dictionary_contains_key(const realm_dictionary_t* dict, realm_value_t key, bool* found)
216
{
6✔
217
    return wrap_err([&]() {
6✔
218
        StringData k{key.string.data, key.string.size};
6✔
219
        *found = dict->contains(k);
6✔
220
        return true;
6✔
221
    });
6✔
222
}
6✔
223

224
RLM_API bool realm_dictionary_contains_value(const realm_dictionary_t* dict, realm_value_t value, size_t* index)
225
{
6✔
226
    return wrap_err([&]() {
6✔
227
        auto val = from_capi(value);
6✔
228
        *index = dict->find_any(val);
6✔
229
        return true;
6✔
230
    });
6✔
231
}
6✔
232

233
RLM_API bool realm_dictionary_clear(realm_dictionary_t* dict)
234
{
4✔
235
    return wrap_err([&]() {
4✔
236
        // Note: confusing naming.
2✔
237
        dict->remove_all();
4✔
238
        return true;
4✔
239
    });
4✔
240
}
4✔
241

242
RLM_API realm_dictionary_t* realm_dictionary_from_thread_safe_reference(const realm_t* realm,
243
                                                                        realm_thread_safe_reference_t* tsr)
244
{
4✔
245
    return wrap_err([&]() {
4✔
246
        auto stsr = dynamic_cast<realm_dictionary::thread_safe_reference*>(tsr);
4✔
247
        if (!stsr) {
4✔
248
            throw LogicError{ErrorCodes::IllegalOperation, "Thread safe reference type mismatch"};
2✔
249
        }
2✔
250

1✔
251
        auto dict = stsr->resolve<object_store::Dictionary>(*realm);
2✔
252
        return new realm_dictionary_t{std::move(dict)};
2✔
253
    });
2✔
254
}
4✔
255

256
RLM_API bool realm_dictionary_resolve_in(const realm_dictionary_t* from_dictionary, const realm_t* target_realm,
257
                                         realm_dictionary_t** resolved)
258
{
6✔
259
    return wrap_err([&]() {
6✔
260
        try {
6✔
261
            const auto& realm = *target_realm;
6✔
262
            auto frozen_dictionary = from_dictionary->freeze(realm);
6✔
263
            if (frozen_dictionary.is_valid()) {
6✔
264
                *resolved = new realm_dictionary_t{std::move(frozen_dictionary)};
4✔
265
            }
4✔
266
            else {
2✔
267
                *resolved = nullptr;
2✔
268
            }
2✔
269
            return true;
6✔
270
        }
6✔
271
        catch (NoSuchTable&) {
×
272
            *resolved = nullptr;
×
273
            return true;
×
274
        }
×
275
        catch (KeyNotFound&) {
×
276
            *resolved = nullptr;
×
277
            return true;
×
278
        }
×
279
    });
6✔
280
}
6✔
281

282
RLM_API bool realm_dictionary_is_valid(const realm_dictionary_t* dictionary)
283
{
6✔
284
    if (!dictionary)
6✔
285
        return false;
×
286
    return dictionary->is_valid();
6✔
287
}
6✔
288

289
} // namespace realm::c_api
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc