-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.cpp
More file actions
499 lines (409 loc) · 12.7 KB
/
Copy pathattribute.cpp
File metadata and controls
499 lines (409 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/attribute.hpp"
#include <cstring>
#include <iostream>
#include "datadog/impl/core/attribute/cow.hpp"
#include "datadog/impl/core/util/assert.hpp"
namespace datadog {
// Defensively clamp the maximum array/object capacity to a reasonable upper limit at
// the API boundary, so that e.g. a call to `datadog::Attribute::Array(-1)` (which would
// interpret -1 as SIZE_MAX on implicit conversion to size_t) wouldn't cause a crash
static const size_t MAX_INITIAL_CAPACITY = 1024;
static size_t _clamp_initial_capacity(size_t capacity) {
return std::min(MAX_INITIAL_CAPACITY, capacity);
}
static bool _is_primitive_type(ValueType type) {
switch (type) {
case ValueType::Null:
case ValueType::Bool:
case ValueType::Int:
case ValueType::UInt:
case ValueType::Timestamp:
case ValueType::Double:
return true;
case ValueType::UUID:
case ValueType::String:
case ValueType::Array:
case ValueType::Object:
return false;
}
DATADOG_ASSERT(false, "unhandled ValueType enum value");
return false;
}
Attribute::Attribute(ValueType in_type, int64_t in_i64) : type(in_type), value() {
value.i64 = in_i64;
}
Attribute::Attribute(ValueType in_type, uint64_t in_u64) : type(in_type), value() {
value.u64 = in_u64;
}
Attribute::Attribute(ValueType in_type, double in_f64) : type(in_type), value() {
value.f64 = in_f64;
}
Attribute::Attribute(ValueType in_type, impl::CowValue* in_ptr)
: type(in_type), value() {
value.ptr = in_ptr;
}
Attribute::Attribute() : type(ValueType::Null), value() { value.ptr = nullptr; }
Attribute::Attribute(const Attribute& other)
: type(other.type),
value(other.value) // Bit-level copy of union data
{
// If we've copied a pointer to a non-primitive value, increment its reference count
if (!_is_primitive_type(type)) {
value.ptr->AddRef();
}
}
Attribute::~Attribute() {
// If we hold a reference to a CowValue, release it
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
}
Attribute& Attribute::operator=(const Attribute& other) {
// Guard against self-assignment
if (this == &other) {
return *this;
}
// If our current value is non-primitive, release our reference to it
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
// Adopt the type of the other attribute, then copy its value, incrementing its
// reference count if it's non-primitive
type = other.type;
value = other.value; // Bit-level copy of union data
if (!_is_primitive_type(type)) {
value.ptr->AddRef();
}
return *this;
}
Attribute::Attribute(Attribute&& other) noexcept
: type(other.type),
value(other.value) // Bit-level copy of union data
{
// If the value is non-primitive, ownership has been transferred simply by virtue of
// the pointer copy: reference count remains as-is. Just leave the original object in
// a valid but empty state to ensure that it retains no references.
other.type = ValueType::Null;
other.value.i64 = 0;
}
Attribute& Attribute::operator=(Attribute&& other) noexcept {
// Guard against self-assignment
if (this == &other) {
return *this;
}
// Release our current value if non-primitive
if (!_is_primitive_type(type) && value.ptr) {
value.ptr->Release();
}
// Transfer ownership from other
type = other.type;
value = other.value; // Bit-level copy of union data
// Leave other in valid but empty state
other.type = ValueType::Null;
other.value.i64 = 0;
return *this;
}
Attribute Attribute::Null() { return Attribute(); }
Attribute Attribute::Bool(bool value) {
const int64_t int_value = value ? 1 : 0;
return Attribute(ValueType::Bool, int_value);
}
Attribute Attribute::Int(int64_t value) { return Attribute(ValueType::Int, value); }
Attribute Attribute::UInt(uint64_t value) { return Attribute(ValueType::UInt, value); }
Attribute Attribute::Timestamp(datadog::Timestamp value) {
return Attribute(
ValueType::Timestamp, static_cast<int64_t>(value.time_since_epoch().count())
);
}
Attribute Attribute::Double(double value) {
return Attribute(ValueType::Double, value);
}
Attribute Attribute::UUID(const uint8_t value[16]) {
return Attribute(ValueType::UUID, impl::CowValue::UUID(value));
}
Attribute Attribute::String(std::string_view value) {
return Attribute(ValueType::String, impl::CowValue::String(value));
}
Attribute Attribute::Array(size_t initial_capacity) {
return Attribute(
ValueType::Array, impl::CowValue::Array(_clamp_initial_capacity(initial_capacity))
);
}
Attribute Attribute::Object(size_t initial_capacity) {
return Attribute(
ValueType::Object,
impl::CowValue::Object(_clamp_initial_capacity(initial_capacity))
);
}
void Attribute::SetNull() {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Null;
value.i64 = 0;
}
void Attribute::SetBool(bool new_value) {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Bool;
value.i64 = new_value ? 1 : 0;
}
void Attribute::SetInt(int64_t new_value) {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Int;
value.i64 = new_value;
}
void Attribute::SetUInt(uint64_t new_value) {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::UInt;
value.u64 = new_value;
}
void Attribute::SetTimestamp(datadog::Timestamp new_value) {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Timestamp;
value.i64 = new_value.time_since_epoch().count();
}
void Attribute::SetDouble(double new_value) {
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Double;
value.f64 = new_value;
}
void Attribute::SetUUID(const datadog::UUID& new_value) {
// If already a UUID, avoid allocation: simply overwrite UUID value
const uint8_t* bytes = new_value.bytes.data();
if (type == ValueType::UUID) {
GetCowValueForWrite()->SetUUID(bytes);
return;
}
// Type is not UUID: release if needed, then allocate new UUID CowValue
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::UUID;
value.ptr = impl::CowValue::UUID(bytes);
}
void Attribute::SetString(std::string_view new_value) {
// Early-out if already a string: write new string value directly to string storage,
// cloning a copy if our reference is shared with other attributes
if (type == ValueType::String) {
GetCowValueForWrite()->SetString(new_value);
return;
}
// Type is not string: release if needed, then allocate new string CowValue
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::String;
value.ptr = impl::CowValue::String(new_value);
}
void Attribute::InitArray(size_t initial_capacity) {
// Early-out if already an array: clear the array, reserving the desired storage
// capacity if applicable
if (type == ValueType::Array) {
GetCowValueForWrite()->Clear(_clamp_initial_capacity(initial_capacity));
return;
}
// Type is not array: release if needed, then allocate new array CowValue
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Array;
value.ptr = impl::CowValue::Array(_clamp_initial_capacity(initial_capacity));
}
void Attribute::InitObject(size_t initial_capacity) {
// Early-out if already an object: clear the object of all properties, reserving the
// desired storage capacity if applicable
if (type == ValueType::Object) {
GetCowValueForWrite()->Clear(_clamp_initial_capacity(initial_capacity));
return;
}
// Type is not object: release if needed, then allocate new object CowValue
if (!_is_primitive_type(type)) {
value.ptr->Release();
}
type = ValueType::Object;
value.ptr = impl::CowValue::Object(_clamp_initial_capacity(initial_capacity));
}
ValueType Attribute::GetType() const { return type; }
bool Attribute::GetBoolValue() const {
if (type != ValueType::Bool) {
return false;
}
return value.i64 != 0;
}
int64_t Attribute::GetIntValue() const {
if (type != ValueType::Int) {
return 0;
}
return value.i64;
}
uint64_t Attribute::GetUIntValue() const {
if (type != ValueType::UInt) {
return 0;
}
return value.u64;
}
Timestamp Attribute::GetTimestampValue() const {
if (type != ValueType::Timestamp) {
return datadog::Timestamp{std::chrono::nanoseconds(0)};
}
return datadog::Timestamp{std::chrono::nanoseconds(value.i64)};
}
double Attribute::GetDoubleValue() const {
if (type != ValueType::Double) {
return 0.0;
}
return value.f64;
}
datadog::UUID Attribute::GetUUIDValue() const {
if (type != ValueType::UUID) {
return datadog::UUID::Zero;
}
return value.ptr->GetUUID();
}
std::string_view Attribute::GetStringValue() const {
if (type != ValueType::String) {
return std::string_view{};
}
return value.ptr->CStr();
}
size_t Attribute::GetArrayLen() const {
if (type != ValueType::Array) {
return 0;
}
return value.ptr->Size();
}
Attribute Attribute::GetArrayItem(int index) const {
if (type != ValueType::Array) {
return Attribute();
}
return value.ptr->GetAt(index);
}
void Attribute::ArrayClear() {
if (type != ValueType::Array) {
return;
}
GetCowValueForWrite()->Clear();
}
void Attribute::ArrayPush(const Attribute& item) {
if (type != ValueType::Array) {
return;
}
// If the caller wants to add an array as an item of itself, create a clone so as not
// to create an infinitely-recursive data structure
if (&item == this) {
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
const Attribute copy = item;
ArrayPush(copy);
return;
}
// Otherwise, push normally
GetCowValueForWrite()->Push(item);
}
void Attribute::ArrayReserve(size_t capacity) {
if (type != ValueType::Array) {
return;
}
// Even though Reserve() is idempotent, GetCowValueForWrite() is not: inspect current
// capacity so we don't clone needlessly
if (value.ptr->Capacity() >= capacity) {
return;
}
GetCowValueForWrite()->Reserve(capacity);
}
size_t Attribute::GetObjectPropertyCount() const {
if (type != ValueType::Object) {
return 0;
}
return value.ptr->Size();
}
int Attribute::FindObjectProperty(std::string_view name) const {
if (type != ValueType::Object) {
return -1;
}
return value.ptr->FindPropertyIndex(name);
}
std::string_view Attribute::GetObjectPropertyNameAt(int index) const {
if (type != ValueType::Object) {
return std::string_view{};
}
return value.ptr->GetPropertyNameCStr(index);
}
Attribute Attribute::GetObjectPropertyValueAt(int index) const {
if (type != ValueType::Object) {
return Attribute();
}
return value.ptr->GetAt(index);
}
Attribute Attribute::GetObjectProperty(std::string_view name) const {
if (type != ValueType::Object) {
return Attribute();
}
const int index = value.ptr->FindPropertyIndex(name);
if (index < 0) {
return Attribute();
}
return value.ptr->GetAt(index);
}
void Attribute::SetObjectProperty(std::string_view name, const Attribute& attribute) {
if (type != ValueType::Object) {
return;
}
// If the caller wants to add an object as a property of itself, create a clone so as
// not to create an infinitely-recursive data structure
if (&attribute == this) {
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
const Attribute copy = attribute;
SetObjectProperty(name, copy);
return;
}
// Otherwise, set the property normally
GetCowValueForWrite()->SetProperty(name, attribute);
}
void Attribute::DeleteObjectProperty(std::string_view name) {
if (type != ValueType::Object) {
return;
}
GetCowValueForWrite()->DeleteProperty(name);
}
void Attribute::ReserveObjectPropertyCapacity(size_t capacity) {
if (type != ValueType::Object) {
return;
}
// Inspect current capacity so we don't clone needlessly
if (value.ptr->Capacity() >= capacity) {
return;
}
GetCowValueForWrite()->Reserve(capacity);
}
impl::CowValue* Attribute::GetCowValueForWrite() {
// This is a private helper function; it should only be called if we've already
// checked that our value is a COW type
DATADOG_ASSERT(
!_is_primitive_type(type),
"GetCowValueForWrite called on attribute of primitive type"
);
// Note that IsShared and Clone/Release are not atomic; we assume that the same
// Attribute will not be used concurrently by multiple threads
if (value.ptr->IsShared()) {
impl::CowValue* new_value = value.ptr->Clone();
value.ptr->Release();
value.ptr = new_value;
}
return value.ptr;
}
} // namespace datadog