otel-arrow-dfe-engine 0.61.0

Async pipeline engine
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//! A generic indexed binary min-heap with O(1) key lookup and O(log n)
//! insert, remove, and in-place priority update.
//!
//! The heap stores `(K, P)` pairs where `K` is a unique key and `P` is a
//! priority value.  A companion `HashMap<K, usize>` tracks each key's
//! current position in the underlying `Vec`, enabling efficient keyed
//! operations that a standard `BinaryHeap` cannot provide.

use std::collections::HashMap;
use std::hash::Hash;

/// Outcome of an [`IndexedMinHeap::insert`] call.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum InsertOutcome {
    /// A new entry was added to the heap.
    Inserted,
    /// An existing entry's priority was replaced.
    Replaced,
}

/// A binary min-heap that supports O(1) key lookup and O(log n) keyed
/// insertion, removal, and in-place priority updates.
///
/// Entries are `(K, P)` pairs.  The heap is ordered by `P` where the
/// *smallest* priority sits at the root.  Keys must be unique -- inserting
/// a key that already exists replaces its priority in place.
///
/// # Key cloning
///
/// Keys are cloned during insert, swap, pop, and removal operations.
/// Callers should prefer cheap-to-clone key types (e.g. integers).
//  Using an expensive-to-clone key will add overhead proportional to
//  tree depth on every heap operation.
pub(crate) struct IndexedMinHeap<K, P> {
    entries: Vec<Entry<K, P>>,
    indices: HashMap<K, usize>,
}

#[derive(Clone, Debug)]
struct Entry<K, P> {
    key: K,
    priority: P,
}

impl<K, P> IndexedMinHeap<K, P>
where
    K: Eq + Hash + Clone + std::fmt::Debug,
    P: Ord,
{
    /// Creates an empty heap.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            indices: HashMap::new(),
        }
    }

    /// Returns the number of entries in the heap.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` if the heap contains no entries.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Returns a reference to the `(key, priority)` pair with the smallest
    /// priority, or `None` if the heap is empty.
    #[must_use]
    pub fn peek(&self) -> Option<(&K, &P)> {
        self.entries.first().map(|e| (&e.key, &e.priority))
    }

    /// Returns `true` if the heap contains an entry for `key`.
    pub fn contains_key(&self, key: &K) -> bool {
        self.indices.contains_key(key)
    }

    /// Inserts `key` with the given `priority`.
    ///
    /// If `key` already exists its priority is replaced in place and the
    /// heap property is restored.  Returns whether the key was newly inserted
    /// or replaced.
    pub fn insert(&mut self, key: K, priority: P) -> InsertOutcome {
        if let Some(&index) = self.indices.get(&key) {
            self.entries[index].priority = priority;
            self.repair_at(index);
            InsertOutcome::Replaced
        } else {
            let index = self.entries.len();
            self.entries.push(Entry {
                key: key.clone(),
                priority,
            });
            assert!(
                self.indices.insert(key, index).is_none(),
                "new key should not already exist in index map"
            );
            self.sift_up(index);
            InsertOutcome::Inserted
        }
    }

    /// Removes the entry with the smallest priority and returns its
    /// `(key, priority)` pair, or `None` if the heap is empty.
    pub fn pop(&mut self) -> Option<(K, P)> {
        if self.entries.is_empty() {
            return None;
        }
        let root_key = self.entries[0].key.clone();
        let removed = self
            .indices
            .remove(&root_key)
            .expect("root key should exist in index map");
        debug_assert_eq!(removed, 0);
        let entry = self.remove_at(0);
        Some((entry.key, entry.priority))
    }

    /// Removes the entry for `key` and returns its `(key, priority)` pair,
    /// or `None` if the key is not present.
    pub fn remove(&mut self, key: &K) -> Option<(K, P)> {
        let index = self.indices.remove(key)?;
        let entry = self.remove_at(index);
        debug_assert_eq!(&entry.key, key);
        Some((entry.key, entry.priority))
    }

    /// Removes all entries from the heap.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.indices.clear();
    }

    // -- internal heap machinery ------------------------------------------

    fn swap(&mut self, a: usize, b: usize) {
        if a == b {
            return;
        }
        self.entries.swap(a, b);
        let key_a = self.entries[a].key.clone();
        let key_b = self.entries[b].key.clone();
        let _ = self
            .indices
            .insert(key_a, a)
            .expect("swapped key a should exist in index map");
        let _ = self
            .indices
            .insert(key_b, b)
            .expect("swapped key b should exist in index map");
    }

    fn sift_up(&mut self, mut index: usize) {
        while index > 0 {
            let parent = (index - 1) / 2;
            if self.entries[index].priority >= self.entries[parent].priority {
                break;
            }
            self.swap(index, parent);
            index = parent;
        }
    }

    fn sift_down(&mut self, mut index: usize) {
        let len = self.entries.len();
        loop {
            let left = index * 2 + 1;
            if left >= len {
                break;
            }
            let right = left + 1;
            let mut smallest = left;
            if right < len && self.entries[right].priority < self.entries[left].priority {
                smallest = right;
            }
            if self.entries[smallest].priority >= self.entries[index].priority {
                break;
            }
            self.swap(index, smallest);
            index = smallest;
        }
    }

    fn repair_at(&mut self, index: usize) {
        if index > 0 {
            let parent = (index - 1) / 2;
            if self.entries[index].priority < self.entries[parent].priority {
                self.sift_up(index);
                return;
            }
        }
        self.sift_down(index);
    }

    /// Removes the entry at `index`, restoring the heap property.
    ///
    /// The caller must have already removed the key from `self.indices`.
    fn remove_at(&mut self, index: usize) -> Entry<K, P> {
        let last = self
            .entries
            .len()
            .checked_sub(1)
            .expect("remove_at requires a non-empty heap");

        if index == last {
            return self.entries.pop().expect("last entry should exist");
        }

        self.entries.swap(index, last);
        let removed = self.entries.pop().expect("removed entry should exist");

        // Update the index of the entry that was moved into `index`.
        let moved_key = self.entries[index].key.clone();
        let _ = self
            .indices
            .insert(moved_key, index)
            .expect("moved key should exist in index map");
        self.repair_at(index);
        removed
    }

    /// Asserts that the heap invariant and the index map are consistent.
    ///
    /// This is intended for use in tests and debug builds.
    #[cfg(debug_assertions)]
    pub fn assert_consistent(&self) {
        assert_eq!(self.entries.len(), self.indices.len());

        for (i, entry) in self.entries.iter().enumerate() {
            assert_eq!(
                self.indices.get(&entry.key).copied(),
                Some(i),
                "heap index must match map entry"
            );
            if i > 0 {
                let parent = (i - 1) / 2;
                assert!(
                    self.entries[i].priority >= self.entries[parent].priority,
                    "heap child must not have smaller priority than parent"
                );
            }
        }
    }
}

impl<K, P> Default for IndexedMinHeap<K, P>
where
    K: Eq + Hash + Clone + std::fmt::Debug,
    P: Ord,
{
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_heap() {
        let heap: IndexedMinHeap<u32, u32> = IndexedMinHeap::new();
        assert!(heap.is_empty());
        assert_eq!(heap.len(), 0);
        assert_eq!(heap.peek(), None);
    }

    #[test]
    fn insert_and_peek() {
        let mut heap = IndexedMinHeap::new();
        assert_eq!(heap.insert(1u32, 10u32), InsertOutcome::Inserted);
        assert_eq!(heap.peek(), Some((&1, &10)));
        assert_eq!(heap.len(), 1);
        #[cfg(debug_assertions)]
        heap.assert_consistent();
    }

    #[test]
    fn insert_maintains_min_order() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 30u32);
        let _ = heap.insert(2, 10);
        let _ = heap.insert(3, 20);
        #[cfg(debug_assertions)]
        heap.assert_consistent();

        assert_eq!(heap.peek(), Some((&2, &10)));
    }

    #[test]
    fn pop_returns_entries_in_priority_order() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 30u32);
        let _ = heap.insert(2, 10);
        let _ = heap.insert(3, 20);

        assert_eq!(heap.pop(), Some((2, 10)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.pop(), Some((3, 20)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.pop(), Some((1, 30)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.pop(), None);
    }

    #[test]
    fn insert_replaces_existing_key() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 30u32);
        let _ = heap.insert(2, 10);
        assert_eq!(heap.insert(1, 5), InsertOutcome::Replaced);
        #[cfg(debug_assertions)]
        heap.assert_consistent();

        // Key 1 now has priority 5, should be the new root.
        assert_eq!(heap.peek(), Some((&1, &5)));
        assert_eq!(heap.len(), 2);
    }

    #[test]
    fn replace_priority_upward() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        let _ = heap.insert(2, 20);
        let _ = heap.insert(3, 30);

        // Make key 3 the highest priority.
        let _ = heap.insert(3, 1);
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.peek(), Some((&3, &1)));
    }

    #[test]
    fn replace_priority_downward() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        let _ = heap.insert(2, 20);
        let _ = heap.insert(3, 30);

        // Make key 1 the lowest priority.
        let _ = heap.insert(1, 100);
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.peek(), Some((&2, &20)));
    }

    #[test]
    fn remove_by_key() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        let _ = heap.insert(2, 20);
        let _ = heap.insert(3, 30);

        assert_eq!(heap.remove(&2), Some((2, 20)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.len(), 2);
        assert!(!heap.contains_key(&2));

        // Remaining entries still ordered.
        assert_eq!(heap.pop(), Some((1, 10)));
        assert_eq!(heap.pop(), Some((3, 30)));
    }

    #[test]
    fn remove_nonexistent_key_returns_none() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        assert_eq!(heap.remove(&99), None);
        assert_eq!(heap.len(), 1);
    }

    #[test]
    fn remove_root() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        let _ = heap.insert(2, 20);
        let _ = heap.insert(3, 30);

        assert_eq!(heap.remove(&1), Some((1, 10)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();
        assert_eq!(heap.peek(), Some((&2, &20)));
    }

    #[test]
    fn remove_last_entry() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        assert_eq!(heap.remove(&1), Some((1, 10)));
        assert!(heap.is_empty());
    }

    #[test]
    fn clear_empties_the_heap() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        let _ = heap.insert(2, 20);
        heap.clear();
        assert!(heap.is_empty());
        assert_eq!(heap.peek(), None);
    }

    #[test]
    fn contains_key_works() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 10u32);
        assert!(heap.contains_key(&1));
        assert!(!heap.contains_key(&2));
    }

    #[test]
    fn many_inserts_maintain_heap_order() {
        let mut heap = IndexedMinHeap::new();
        // Insert in reverse order.
        for i in (0u32..100).rev() {
            let _ = heap.insert(i, i);
        }
        #[cfg(debug_assertions)]
        heap.assert_consistent();

        for expected in 0u32..100 {
            assert_eq!(heap.pop(), Some((expected, expected)));
        }
        assert!(heap.is_empty());
    }

    #[test]
    fn equal_priorities_are_stable_by_insertion_order() {
        // With equal priorities, the heap only guarantees they all come out,
        // not a specific order among equals. Verify all are returned.
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 0u32);
        let _ = heap.insert(2, 0);
        let _ = heap.insert(3, 0);
        #[cfg(debug_assertions)]
        heap.assert_consistent();

        let mut keys = Vec::new();
        while let Some((k, _)) = heap.pop() {
            keys.push(k);
        }
        keys.sort();
        assert_eq!(keys, vec![1, 2, 3]);
    }

    #[test]
    fn remove_middle_entry_preserves_heap() {
        let mut heap = IndexedMinHeap::new();
        for i in 0u32..10 {
            let _ = heap.insert(i, i * 10);
        }

        // Remove entry in the middle of the heap.
        assert_eq!(heap.remove(&5), Some((5, 50)));
        #[cfg(debug_assertions)]
        heap.assert_consistent();

        let mut prev = 0u32;
        while let Some((_, p)) = heap.pop() {
            assert!(p >= prev);
            prev = p;
        }
    }

    #[test]
    fn repeated_replace_same_key() {
        let mut heap = IndexedMinHeap::new();
        let _ = heap.insert(1u32, 100u32);
        for p in (1u32..=50).rev() {
            assert_eq!(heap.insert(1, p), InsertOutcome::Replaced);
            #[cfg(debug_assertions)]
            heap.assert_consistent();
            assert_eq!(heap.len(), 1);
            assert_eq!(heap.peek(), Some((&1, &p)));
        }
        assert_eq!(heap.pop(), Some((1, 1)));
    }
}