-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathAttributesDictionary.swift
147 lines (129 loc) · 3.82 KB
/
AttributesDictionary.swift
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import Foundation
import OpenTelemetryApi
// A dictionary implementation with a fixed capacity that drops events when the dictionary gets full. Eviction
// is based on the access order.
public struct AttributesDictionary {
var attributes: [String: AttributeValue]
var keys: [String]
private var capacity: Int
private var valueLengthLimit: Int
private var recordedAttributes: Int
public init(capacity: Int, valueLengthLimit: Int = Int.max) {
attributes = [String: AttributeValue]()
keys = [String]()
recordedAttributes = 0
self.valueLengthLimit = valueLengthLimit
self.capacity = capacity
}
public subscript(key: String) -> AttributeValue? {
get {
attributes[key]
}
set {
if newValue == nil {
_ = removeValueForKey(key: key)
} else {
_ = updateValue(value: newValue!, forKey: key)
}
}
}
@discardableResult public mutating func updateValue(
value: AttributeValue, forKey key: String
) -> AttributeValue? {
var attributedValue = value
switch value {
case let .string(v):
if v.count > valueLengthLimit {
attributedValue = AttributeValue.string(
String(v.prefix(valueLengthLimit)))
}
case let .array(v):
var array = [AttributeValue]()
v.values.forEach { value in
switch value {
case let .string(value):
array.append(
AttributeValue.string(String(value.prefix(valueLengthLimit))))
default:
array.append(value)
}
attributedValue = AttributeValue.array(AttributeArray(values: array))
}
default:
break
}
let oldValue = attributes.updateValue(attributedValue, forKey: key)
if oldValue == nil {
recordedAttributes += 1
keys.append(key)
} else {
keys.remove(at: keys.firstIndex(of: key)!)
keys.append(key)
}
if attributes.count > capacity {
let key = keys.removeFirst()
attributes[key] = nil
}
return oldValue
}
public mutating func updateValues(attributes: [String: AttributeValue]) {
_ = attributes.keys.map {
updateValue(value: attributes[$0]!, forKey: $0)
}
}
public mutating func updateValues(attributes: AttributesDictionary) {
_ = attributes.keys.map {
updateValue(value: attributes[$0]!, forKey: $0)
}
}
public mutating func removeValueForKey(key: String) -> AttributeValue? {
keys = keys.filter {
$0 != key
}
return attributes.removeValue(forKey: key)
}
public mutating func removeAll(keepCapacity: Int) {
keys = []
attributes = [String: AttributeValue](minimumCapacity: keepCapacity)
}
public var count: Int {
attributes.count
}
public var numberOfDroppedAttributes: Int {
recordedAttributes - attributes.count
}
public var values: [AttributeValue] {
keys.map { attributes[$0]! }
}
static func == (lhs: AttributesDictionary, rhs: AttributesDictionary) -> Bool {
lhs.keys == rhs.keys && lhs.attributes == rhs.attributes
}
static func != (lhs: AttributesDictionary, rhs: AttributesDictionary) -> Bool {
lhs.keys != rhs.keys || lhs.attributes != rhs.attributes
}
}
extension AttributesDictionary: Sequence {
public func makeIterator() -> AttributesWithCapacityIterator {
AttributesWithCapacityIterator(sequence: attributes, keys: keys, current: 0)
}
}
public struct AttributesWithCapacityIterator: IteratorProtocol {
let sequence: [String: AttributeValue]
let keys: [String]
var current = 0
public mutating func next() -> (String, AttributeValue)? {
defer { current += 1 }
guard sequence.count > current else {
return nil
}
let key = keys[current]
guard let value = sequence[key] else {
return nil
}
return (key, value)
}
}