nsave 0.1.0

capturing and saving packets
Documentation
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use super::iter::*;
use core::fmt;
use hashbrown::HashMap;
use std::{
    borrow::Borrow,
    hash::Hash,
    mem,
    ptr::{self, NonNull},
};

struct KeyRef<K> {
    k: *const K,
}

impl<K: Hash> Hash for KeyRef<K> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        unsafe { (*self.k).hash(state) }
    }
}

impl<K: PartialEq> PartialEq for KeyRef<K> {
    fn eq(&self, other: &Self) -> bool {
        unsafe { (*self.k).eq(&*other.k) }
    }
}

impl<K: Eq> Eq for KeyRef<K> {}

impl<K> Borrow<K> for KeyRef<K> {
    fn borrow(&self) -> &K {
        unsafe { &*self.k }
    }
}

#[derive(Debug)]
pub(crate) struct Node<K, V> {
    pub(crate) key: mem::MaybeUninit<K>,
    pub(crate) value: mem::MaybeUninit<V>,
    pub(crate) prev: *mut Node<K, V>,
    pub(crate) next: *mut Node<K, V>,
}

impl<K, V> Node<K, V> {
    fn new_dummy() -> Self {
        Node {
            key: mem::MaybeUninit::uninit(),
            value: mem::MaybeUninit::uninit(),
            prev: ptr::null_mut(),
            next: ptr::null_mut(),
        }
    }

    fn init_dummy(&mut self, key: K, val: V) {
        unsafe {
            self.key.as_mut_ptr().write(key);
            self.value.as_mut_ptr().write(val);
        }
    }
}

pub struct TmoHash<K, V>
where
    K: Eq + Hash,
{
    hash: HashMap<KeyRef<K>, NonNull<Node<K, V>>>,
    capacity: usize,
    pool: *mut Node<K, V>,
    pool_num: usize,
    head: *mut Node<K, V>, // 可能最老的节点,新的节点都在tail,那老的节点会逐渐剩下到head上
    tail: *mut Node<K, V>,
}

impl<K, V> TmoHash<K, V>
where
    K: Eq + Hash,
{
    pub fn new(capacity: usize) -> TmoHash<K, V> {
        let mut tmo = TmoHash {
            hash: HashMap::with_capacity(capacity),
            capacity,
            pool: Box::into_raw(Box::new(Node::new_dummy())),
            pool_num: 0,
            head: Box::into_raw(Box::new(Node::new_dummy())),
            tail: Box::into_raw(Box::new(Node::new_dummy())),
        };

        unsafe {
            (*tmo.head).next = tmo.tail;
            (*tmo.tail).prev = tmo.head;
        }
        for _ in 0..capacity {
            let dummy = Box::new(Node::<K, V>::new_dummy());
            tmo.pool_put(dummy);
        }
        tmo
    }

    fn pool_put(&mut self, node: Box<Node<K, V>>) {
        if !self.pool_is_full() {
            let node = Box::into_raw(node);
            unsafe {
                (*node).next = (*self.pool).next;
                (*self.pool).next = node;
            };
            self.pool_num += 1;
        }
    }

    fn pool_get(&mut self) -> Option<Box<Node<K, V>>> {
        if self.pool_is_empty() {
            return None;
        }

        unsafe {
            let node_ptr = (*self.pool).next;
            (*self.pool).next = (*node_ptr).next;
            self.pool_num -= 1;
            Some(Box::from_raw(node_ptr))
        }
    }

    /// 返回内存池中当前剩余node的数量
    pub fn pool_len(&self) -> usize {
        self.pool_num
    }

    fn pool_is_empty(&self) -> bool {
        self.pool_num == 0
    }

    fn pool_is_full(&self) -> bool {
        self.pool_num >= self.capacity
    }

    /// 释放内存池中所有node
    fn pool_clear(&mut self) {
        unsafe {
            let mut p = (*self.pool).next;
            while !p.is_null() {
                let next = (*p).next;
                let _ = Box::from_raw(p);
                p = next;
            }
            let _ = Box::from_raw(self.pool);
        }
        self.pool_num = 0;
    }

    /// 新结点添加到尾部,头部是老的,尾部是新的
    fn list_attach(&mut self, node: *mut Node<K, V>) {
        unsafe {
            let prev = (*self.tail).prev;
            (*node).prev = prev;
            (*node).next = self.tail;
            (*prev).next = node;
            (*self.tail).prev = node;
        }
    }

    fn list_detach(&mut self, node: *mut Node<K, V>) {
        unsafe {
            (*(*node).prev).next = (*node).next;
            (*(*node).next).prev = (*node).prev;
        }
    }

    fn insert_inner(&mut self, key: K, val: V) -> Option<*mut Node<K, V>> {
        if self.capacity == 0 || self.is_full() || self.pool_is_empty() {
            return None;
        }

        if let Some(mut node) = self.pool_get() {
            node.init_dummy(key, val);
            unsafe {
                let new = NonNull::new_unchecked(Box::into_raw(node));
                let new_ptr = new.as_ptr();
                let keyref = (*new_ptr).key.as_ptr();
                self.list_attach(new_ptr);
                self.hash.insert(KeyRef { k: keyref }, new);
                return Some(new_ptr);
            }
        }
        None
    }

    /// 插入一个k v对儿,如果已经存在,会替代。
    /// 返回插入value的引用
    /// 因为限于在流表场景下使用,所以在insert之前,用户需要确保节点不存在,也就是不要产生替代的情况
    /// # Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// assert_eq!(Some(&"a"), tmo.insert(1, "a"));
    /// assert!(tmo.contains_key(&1));
    /// assert!(!tmo.contains_key(&2));
    /// ```
    pub fn insert(&mut self, key: K, val: V) -> Option<&V> {
        if let Some(new_ptr) = self.insert_inner(key, val) {
            unsafe {
                return Some(&*(*new_ptr).value.as_mut_ptr());
            }
        }
        None
    }

    /// 插入一个k v对儿,如果已经存在,会替代。
    /// 返回插入value的可变引用
    /// 因为限于在流表场景下使用,所以在insert之前,用户需要确保节点不存在,也就是不要产生替代的情况
    /// # Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// assert_eq!(Some(&mut "a"), tmo.insert_mut(1, "a"));
    /// assert!(tmo.contains_key(&1));
    /// assert!(!tmo.contains_key(&2));
    /// ```
    pub fn insert_mut(&mut self, key: K, val: V) -> Option<&mut V> {
        if let Some(new_ptr) = self.insert_inner(key, val) {
            unsafe {
                return Some(&mut *(*new_ptr).value.as_mut_ptr());
            }
        }
        None
    }

    /// 删除一个key
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert(1, "a");
    /// tmo.insert(2, "b");
    /// tmo.insert(3, "c");
    /// tmo.remove(&2);
    /// assert!(tmo.contains_key(&1));
    /// assert!(tmo.contains_key(&3));
    /// assert!(!tmo.contains_key(&2));
    /// ```
    pub fn remove(&mut self, k: &K) {
        if let Some(node) = self.hash.remove(k) {
            let mut node = unsafe { Box::from_raw(node.as_ptr()) };
            self.list_detach(&mut *node);
            self.pool_put(node);
        }
    }

    /// 根据key,判断是否存在节点
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert(1, "a");
    /// tmo.insert(2, "b");
    /// assert!(tmo.contains_key(&1));
    /// assert!(tmo.contains_key(&2));
    /// assert!(!tmo.contains_key(&3));
    /// ```
    pub fn contains_key(&self, key: &K) -> bool {
        self.hash.contains_key(key)
    }

    /// 返回最大容量
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo: TmoHash<usize, String> = TmoHash::new(10);
    /// assert_eq!(tmo.capacity(), 10);
    /// ```
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// 返回已有数据的个数
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// assert_eq!(tmo.len(), 0);
    /// tmo.insert(1, "a");
    /// tmo.insert(2, "b");
    /// assert_eq!(tmo.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.hash.len()
    }

    /// 返回是否为空
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// assert!(tmo.is_empty());
    /// tmo.insert(1, "a");
    /// assert!(!tmo.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.hash.is_empty()
    }

    /// 返回是否已满
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo: TmoHash<usize, usize> = TmoHash::new(10);
    /// assert!(!tmo.is_full());
    /// for i in 0..10 {
    ///     tmo.insert(i, i);
    /// }
    /// assert!(tmo.is_full());
    /// ```
    pub fn is_full(&self) -> bool {
        self.hash.len() >= self.capacity
    }

    /// 根据key,查询返回value的引用。因为是不变引用,说明没有更新,所以不需要重新移动到链表尾部
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert(1, "a");
    /// tmo.insert(2, "b");
    /// tmo.insert(3, "c");
    /// assert_eq!(Some((&"a")), tmo.get(&1));
    /// assert_eq!(Some((&"b")), tmo.get(&2));
    /// assert_eq!(Some((&"c")), tmo.get(&3));
    /// assert_ne!(Some((&"d")), tmo.get(&4));
    /// ```
    pub fn get(&self, k: &K) -> Option<&V> {
        if let Some(node) = self.hash.get(k) {
            let node_ptr = node.as_ptr();
            Some(unsafe { &*(*node_ptr).value.as_ptr() })
        } else {
            None
        }
    }

    /// 根据key,查询返回value的可变引用
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert(1, "a");
    /// tmo.insert(2, "b");
    /// tmo.insert(3, "c");
    /// let node = tmo.get_mut(&1).unwrap();
    /// *node = &"d";
    /// assert_eq!(Some((&"d")), tmo.get(&1));
    /// ```
    pub fn get_mut(&mut self, k: &K) -> Option<&mut V> {
        if let Some(node) = self.hash.get(k) {
            let node_ptr = node.as_ptr();
            self.list_detach(node_ptr);
            self.list_attach(node_ptr);
            Some(unsafe { &mut *(*node_ptr).value.as_mut_ptr() })
        } else {
            None
        }
    }

    /// 从最老的node开始迭代
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert("a", 1);
    /// tmo.insert("b", 2);
    /// tmo.insert("c", 3);
    /// let sum = tmo.iter().map(|x| x.1).sum();
    /// assert_eq!(6, sum);
    /// ```
    pub fn iter(&self) -> Iter<'_, K, V> {
        Iter {
            done: 0,
            len: self.len(),
            next: unsafe { (*self.head).next },
            phantom: std::marker::PhantomData,
        }
    }

    /// 从最老的一端开始遍历,根据闭包返回确定是否保留此节点
    /// 闭包为true 删除,为fals 不删除,保留
    ///
    /// 遇到第一个不满足条件的就返回。用于老化tmo场景。
    /// 从最老开始,一直删除到不满足闭包条件的第一个node为止。并不遍历所有节点。只从最老开始遍历到第一个不需要老化为止。
    /// 这样,既能尽快删除了所有需要老化的节点,也不会遍历过多
    ///
    /// #Example
    ///
    /// ```
    /// use libnsave::tmohash::TmoHash;
    ///
    /// let mut tmo = TmoHash::new(10);
    /// tmo.insert("a", 1);
    /// tmo.insert("b", 2);
    /// tmo.insert("c", 3);
    /// tmo.insert("d", 10);
    /// tmo.insert("e", 11);
    /// tmo.insert("f", 12);
    /// tmo.insert("g", 4);
    /// tmo.timeout(|&_k, &v| v < 10);
    /// assert_eq!(4, tmo.len());
    /// assert!(!tmo.contains_key(&"a"));
    /// assert!(!tmo.contains_key(&"b"));
    /// assert!(!tmo.contains_key(&"c"));
    /// assert!(tmo.contains_key(&"g"));
    /// ```
    pub fn timeout<F>(&mut self, fun: F)
    where
        F: Fn(&K, &V) -> bool,
    {
        if self.is_empty() {
            return;
        }

        unsafe {
            let mut p = (*self.head).next;
            while p != self.tail {
                let next = (*p).next;

                let key_ref = &(*(*p).key.as_ptr());
                let val_ref = &(*(*p).value.as_ptr());
                if fun(key_ref, val_ref) {
                    self.remove(key_ref);
                } else {
                    return;
                }

                p = next;
            }
        }
    }

    fn clear(&mut self) {
        self.timeout(|_k, _v| true);
        self.pool_clear();
        unsafe {
            let _ = Box::from_raw(self.head);
            let _ = Box::from_raw(self.tail);
        }
    }
}

impl<K, V> Drop for TmoHash<K, V>
where
    K: Hash + Eq,
{
    fn drop(&mut self) {
        self.clear();
    }
}

unsafe impl<K: Send, V: Send> Send for TmoHash<K, V> where K: Eq + Hash {}
unsafe impl<K: Sync, V: Sync> Sync for TmoHash<K, V> where K: Eq + Hash {}

impl<K, V> fmt::Debug for TmoHash<K, V>
where
    K: fmt::Debug + Hash + Eq,
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TmoHash [")?;
        if self.is_empty() {
            return write!(f, "]");
        } else {
            write!(f, " ")?;
        }
        let mut comma = false;
        let iter = self.iter();
        for kv in iter {
            if comma {
                write!(f, ", ")?;
            } else {
                comma = true;
            }
            write!(f, "({:?}, {:?})", kv.0, kv.1)?;
        }
        write!(f, " ]")
    }
}

/// display
///
/// # Examples
///
/// ```
/// use libnsave::tmohash::TmoHash;
///
/// let mut tmo = TmoHash::new(10);
/// tmo.insert(1, "a");
/// tmo.insert(2, "b");
/// tmo.insert(3, "c");
/// assert_eq!("[1: a, 2: b, 3: c]", format!("{}", tmo));
/// ```
impl<K, V> fmt::Display for TmoHash<K, V>
where
    K: fmt::Display + Hash + Eq + Clone,
    V: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let items = self
            .iter()
            .map(|x| format!("{}: {}", x.0, x.1))
            .collect::<Vec<String>>();
        write!(f, "[{}]", items.join(", "))
    }
}

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

    #[test]
    fn test_new() {
        let tmo: tmo::TmoHash<i32, &str> = TmoHash::new(10);
        assert_eq!(10, tmo.pool_len());
        assert_eq!(10, tmo.capacity());
        assert_eq!(0, tmo.len());
        assert!(tmo.is_empty());
        assert!(!tmo.is_full());
    }

    #[test]
    fn test_insert() {
        let mut tmo = TmoHash::new(10);
        assert_eq!(10, tmo.pool_len());

        tmo.insert("a", 1);
        assert_eq!(9, tmo.pool_len());
        assert_eq!(1, tmo.len());

        tmo.insert("b", 2);
        assert_eq!(8, tmo.pool_len());
        assert_eq!(2, tmo.len());
    }

    #[test]
    fn test_debug() {
        let mut tmo = TmoHash::new(10);
        tmo.insert(1, "a");
        tmo.insert(2, "b");
        tmo.insert(3, "c");
        assert_eq!(
            "TmoHash [ (1, \"a\"), (2, \"b\"), (3, \"c\") ]",
            format!("{:?}", tmo)
        );
    }

    #[test]
    fn test_display() {
        let mut tmo: TmoHash<usize, &str> = TmoHash::new(10);
        assert_eq!("[]", format!("{}", tmo));

        tmo.insert(1, "a");
        tmo.insert(2, "b");
        tmo.insert(3, "c");
        assert_eq!("[1: a, 2: b, 3: c]", format!("{}", tmo));
    }

    #[test]
    fn test_iter_debug() {
        let mut tmo = TmoHash::new(10);
        tmo.insert(1, "a");
        tmo.insert(2, "b");
        tmo.insert(3, "c");

        let mut iter = tmo.iter();
        iter.next();
        assert_eq!("Iter [1/3 next: 2]", format!("{:?}", iter));

        iter.next();
        iter.next();
        assert_eq!("Iter [3/3]", format!("{:?}", iter));
    }

    #[test]
    fn test_iter_display() {
        let mut tmo = TmoHash::new(10);
        tmo.insert(1, "a");
        tmo.insert(2, "b");
        tmo.insert(3, "c");

        let mut iter = tmo.iter();
        iter.next();
        assert_eq!("Iter [1/3 next: 2]", format!("{}", iter));

        iter.next();
        iter.next();
        assert_eq!("Iter [3/3]", format!("{}", iter));
    }

    #[test]
    fn test_pool() {
        let mut tmo = TmoHash::new(10);
        assert_eq!(10, tmo.pool_len());

        tmo.insert(1, "a");
        assert_eq!(9, tmo.pool_len());
        tmo.insert(2, "b");
        assert_eq!(8, tmo.pool_len());
        tmo.insert(3, "c");
        assert_eq!(7, tmo.pool_len());

        tmo.remove(&1);
        assert_eq!(8, tmo.pool_len());
        tmo.remove(&2);
        assert_eq!(9, tmo.pool_len());
        tmo.remove(&3);
        assert_eq!(10, tmo.pool_len());
    }
}