Skip to content

Commit fdbeb23

Browse files
committed
fix test
1 parent 7f0ef9a commit fdbeb23

File tree

3 files changed

+110
-61
lines changed

3 files changed

+110
-61
lines changed

opentelemetry-sdk/src/growable_array.rs

+84-32
Original file line numberDiff line numberDiff line change
@@ -87,40 +87,91 @@ impl<
8787
self.count + self.overflow.as_ref().map_or(0, Vec::len)
8888
}
8989

90-
/// Deletes the element matching the given value from the array while preserving the order.
90+
/// Removes the first element matching the given value from the array.
91+
///
92+
/// This function searches both the internal array (`inline`) and the heap-allocated
93+
/// vector (`overflow`) for the specified value. If found, it calls `delete_at` to
94+
/// perform the deletion and preserve the order of the remaining elements.
95+
///
96+
/// # Arguments
97+
///
98+
/// - `item`: A reference to the value to be deleted.
99+
///
100+
/// # Returns
101+
///
102+
/// - `Some(T)`: The deleted value, if found.
103+
/// - `None`: If the value was not found in either the array or the vector.
104+
///
105+
#[allow(dead_code)]
106+
pub(crate) fn remove_first(&mut self, item: &T) -> Option<T> {
107+
// Search for the item using `iter()` and get its index
108+
let index = self.iter().position(|v| v == item)?;
109+
110+
// Use `delete_at()` to remove the item by index
111+
self.remove_at(index)
112+
}
113+
114+
/// Remove all elements matching the given value from the array.
115+
///
116+
/// This function searches both the internal array (`inline`) and the heap-allocated
117+
/// vector (`overflow`) for the specified value. If found, it removes all occurrences.
118+
///
119+
/// # Arguments
120+
///
121+
/// - `item`: A reference to the value to be deleted.
122+
///
123+
/// # Returns
124+
///
125+
/// - The number of deleted occurrences of the value.
126+
#[allow(dead_code)]
127+
/// Remove all elements matching the given value from the array.
128+
pub(crate) fn remove(&mut self, item: &T) -> usize {
129+
let mut deleted_count = 0;
130+
131+
// Loop to find and delete all occurrences
132+
while let Some(index) = {
133+
let position = self.iter().position(|v| v == item);
134+
position
135+
} {
136+
self.remove_at(index);
137+
deleted_count += 1;
138+
}
139+
140+
deleted_count
141+
}
142+
143+
/// Removes the element at a specific position (index) while preserving the order.
91144
///
92145
/// This function performs the following operations:
93146
///
94-
/// - Searches the internal array (`inline`) for the specified value.
95-
/// - If the value is found in the internal array:
96-
/// - Removes the value.
97-
/// - Shifts the remaining elements in the array to the left to fill the gap, preserving the order.
147+
/// - If the index points to an element in the internal array (`inline`):
148+
/// - Removes the element at the specified index.
149+
/// - Shifts the remaining elements in the internal array to the left to fill the gap, preserving the order.
98150
/// - If an overflow vector exists:
99-
/// - Moves the first element from the overflow vector into the last position of the internal array.
100-
/// - If the value is not found in the internal array, searches the heap-allocated vector (`overflow`).
101-
/// - If the value is found in the overflow vector, it is removed, and the remaining elements in the vector are shifted left to maintain order.
151+
/// - Moves the first element from the overflow vector into the last position of the internal array, if available.
152+
/// - If the index points to an element in the overflow vector:
153+
/// - The element is removed directly from the overflow vector.
102154
///
103155
/// # Arguments
104156
///
105-
/// - `value`: A reference to the value to be deleted.
157+
/// - `index`: The index of the element to be deleted.
106158
///
107159
/// # Returns
108160
///
109-
/// - `Some(T)`: The deleted value, if found.
110-
/// - `None`: If the value was found in neither the array nor the vector
161+
/// - `Some(T)`: The deleted element, if found.
162+
/// - `None`: If the index is out of bounds for both the internal array and the overflow vector.
111163
///
112164
#[allow(dead_code)]
113-
pub(crate) fn delete_item(&mut self, item: &T) -> Option<T> {
114-
// Search and remove from inline array
115-
if let Some(index) = self.inline[..self.count].iter().position(|v| v == item) {
165+
pub(crate) fn remove_at(&mut self, index: usize) -> Option<T> {
166+
if index < self.count {
116167
let removed_value = self.inline[index].clone();
117168

118-
// Shift elements to the left to fill the gap
169+
// Shift elements in inline array to the left
119170
for i in index..self.count - 1 {
120171
self.inline[i] = self.inline[i + 1].clone();
121172
}
122173

123-
// Check if we can move an element from the overflow into the inline array
174+
// Handle moving an overflow element to inline, if available
124175
let moved_from_overflow = if let Some(ref mut overflow) = self.overflow {
125176
if let Some(first_from_overflow) = overflow.first().cloned() {
126177
self.inline[self.count - 1] = first_from_overflow;
@@ -135,21 +186,22 @@ impl<
135186
false
136187
};
137188

138-
// Only decrement count if no item was moved from the overflow
189+
// Only decrement count if no item was moved from overflow
139190
if !moved_from_overflow {
140191
self.count -= 1;
141192
}
142193
return Some(removed_value);
143194
}
144195

145-
// Search and remove from overflow vector
196+
// Handle removing from the overflow vector
146197
if let Some(ref mut overflow) = self.overflow {
147-
if let Some(index) = overflow.iter().position(|v| v == item) {
148-
return Some(overflow.remove(index));
198+
let overflow_index = index - MAX_INLINE_CAPACITY;
199+
if overflow_index < overflow.len() {
200+
return Some(overflow.remove(overflow_index));
149201
}
150202
}
151203

152-
// Value not found
204+
// Index out of bounds
153205
None
154206
}
155207

@@ -497,7 +549,7 @@ mod tests {
497549
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY);
498550

499551
// Delete a value from the inline array
500-
let removed = collection.delete_item(&3);
552+
let removed = collection.remove_first(&3);
501553
assert_eq!(removed, Some(3));
502554
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY - 1);
503555

@@ -510,7 +562,7 @@ mod tests {
510562
}
511563

512564
// Try to delete a value not in the array
513-
let non_existent = collection.delete_item(&99);
565+
let non_existent = collection.remove_first(&99);
514566
assert_eq!(non_existent, None);
515567
}
516568

@@ -528,7 +580,7 @@ mod tests {
528580
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY + 5);
529581

530582
// Delete a value from the overflow vector
531-
let removed = collection.delete_item(&12);
583+
let removed = collection.remove_first(&12);
532584
assert_eq!(removed, Some(12));
533585
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY + 4);
534586

@@ -548,7 +600,7 @@ mod tests {
548600
assert_eq!(collection.len(), 1);
549601

550602
// Delete the only element in the collection
551-
let removed = collection.delete_item(&10);
603+
let removed = collection.remove_first(&10);
552604
assert_eq!(removed, Some(10));
553605
assert_eq!(collection.len(), 0);
554606

@@ -564,9 +616,9 @@ mod tests {
564616
}
565617

566618
// Delete multiple values
567-
assert_eq!(collection.delete_item(&2), Some(2));
619+
assert_eq!(collection.remove(&2), 1);
568620
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY - 1);
569-
assert_eq!(collection.delete_item(&4), Some(4));
621+
assert_eq!(collection.remove(&4), 1);
570622
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY - 2);
571623

572624
// Ensure the elements are still correct
@@ -579,7 +631,7 @@ mod tests {
579631
let mut collection = GrowableArray::<i32>::new();
580632

581633
// Try to delete from an empty array
582-
let removed = collection.delete_item(&5);
634+
let removed = collection.remove_first(&5);
583635
assert_eq!(removed, None);
584636
assert_eq!(collection.len(), 0);
585637
}
@@ -592,8 +644,8 @@ mod tests {
592644
collection.push(3);
593645

594646
// Try to delete a value not present
595-
let removed = collection.delete_item(&10);
596-
assert_eq!(removed, None);
647+
let removed = collection.remove(&10);
648+
assert_eq!(removed, 0);
597649
assert_eq!(collection.len(), 3);
598650
}
599651

@@ -614,8 +666,8 @@ mod tests {
614666
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY + 3);
615667

616668
// Delete an inline value and ensure that an overflow value takes its place
617-
let removed = collection.delete_item(&5); // Deleting from inline
618-
assert_eq!(removed, Some(5));
669+
let removed = collection.remove(&5); // Deleting from inline
670+
assert_eq!(removed, 1);
619671
// [0,1,2,3,4,6,7,8,9,10,11,12]
620672
assert_eq!(collection.len(), DEFAULT_MAX_INLINE_CAPACITY + 2);
621673

opentelemetry-sdk/src/logs/log_processor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ mod tests {
862862
&Key::from_static_str("processed_by"),
863863
AnyValue::from("SecondProcessor"),
864864
);
865-
let _ = record.delete_attribute(&Key::from_static_str("key1"));
865+
let _ = record.remove_attribute(&Key::from_static_str("key1"));
866866
assert!(
867867
record.body.clone().unwrap()
868868
== AnyValue::String("Updated by FirstProcessor".into())
@@ -902,7 +902,7 @@ mod tests {
902902
&AnyValue::String("value1".into())
903903
));
904904

905-
let _ = record.delete_attribute(&Key::from_static_str("key1"));
905+
let _ = record.remove_attribute(&Key::from_static_str("key1"));
906906
assert!(
907907
record.body.clone().unwrap()
908908
== AnyValue::String("Updated by FirstProcessor".into())

opentelemetry-sdk/src/logs/record.rs

+24-27
Original file line numberDiff line numberDiff line change
@@ -160,40 +160,37 @@ impl LogRecord {
160160
None
161161
}
162162

163-
/// Deletes the first occurrence of an attribute with the specified key.
163+
/// Removes all occurrences of an attribute with the specified key.
164164
///
165-
/// This method first searches for the attribute with the given key in the `attributes`
166-
/// collection. If the attribute is found, it then calls the `delete_item` method from
167-
/// the `GrowableArray` to remove it.
165+
/// This method searches for all occurrences of the attribute with the given key
166+
/// in the `attributes` collection and removes them.
168167
///
169168
/// # Arguments
170169
///
171-
/// - `key`: A reference to the key of the attribute to delete.
170+
/// - `key`: A reference to the key of the attribute to remove.
172171
///
173172
/// # Returns
174173
///
175-
/// - `Some((Key, AnyValue))`: The deleted key-value pair if found.
176-
/// - `None`: If the attribute was not found.
174+
/// - The number of removed occurrences of the key.
177175
///
178-
pub fn delete_attribute(&mut self, key: &Key) -> Option<(Key, AnyValue)> {
179-
// First, find the position of the attribute (this is an immutable borrow).
180-
let position = self
181-
.attributes
182-
.iter()
183-
.position(|opt| opt.as_ref().map(|(k, _)| k == key).unwrap_or(false));
184-
185-
// Now drop the immutable borrow before we proceed with the mutable borrow.
186-
if let Some(position) = position {
187-
// Mutably borrow attributes and remove the item
188-
if let Some(attribute) = self.attributes.get(position).and_then(|opt| opt.clone()) {
189-
// Clone the attribute before passing it to delete_item
190-
let cloned_attribute = attribute.clone();
191-
// Use delete_item to remove the found attribute
192-
self.attributes.delete_item(&Some(attribute));
193-
return Some(cloned_attribute);
194-
}
176+
pub fn remove_attribute(&mut self, key: &Key) -> usize {
177+
let mut deleted_count = 0;
178+
179+
// Loop to find and remove all occurrences
180+
while let Some(index) = {
181+
// Isolate the immutable borrow in a block scope
182+
let position = self
183+
.attributes
184+
.iter()
185+
.position(|opt| opt.as_ref().map(|(k, _)| k == key).unwrap_or(false));
186+
position
187+
} {
188+
// Now proceed with the mutable borrow and remove the item
189+
self.attributes.remove_at(index);
190+
deleted_count += 1;
195191
}
196-
None
192+
193+
deleted_count
197194
}
198195
}
199196

@@ -394,8 +391,8 @@ mod tests {
394391
assert!(log_record.attributes_contains(&key, &value));
395392

396393
// Delete the attribute
397-
let deleted_attribute = log_record.delete_attribute(&key);
398-
assert_eq!(deleted_attribute, Some((key.clone(), value.clone())));
394+
let del_count = log_record.remove_attribute(&key);
395+
assert_eq!(del_count, 1);
399396

400397
// Ensure it is deleted
401398
assert!(!log_record.attributes_contains(&key, &value));

0 commit comments

Comments
 (0)