Coverage Report

Created: 2025-05-16 06:24

/src/ntopng/include/Bloom.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-25 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _BLOOM_H_
23
#define _BLOOM_H_
24
25
/**
26
 * @file Bloom.h
27
 *
28
 * @brief      Bloom class implementation.
29
 * @details    A Bloom instance represents a bitmask that can be used as bloom
30
 * filter for strings.
31
 */
32
33
#include "ntop_includes.h"
34
35
class Bloom {
36
 private:
37
  Bitmask *bitmask;         /**< The bitmask */
38
  u_int32_t num_bloom_bits; /**< The bitmask size */
39
  u_int32_t mask;           /**< The mask to be used for the hash */
40
41
  u_int32_t ntophash(char *str);
42
43
 public:
44
  Bloom(u_int32_t _num_bloom_bits);
45
  ~Bloom();
46
47
  /**
48
   * Adds a new value to the bloom setting the relative bit in the bitmask.
49
   * @param str The value to set.
50
   */
51
0
  inline void setBit(char *str) { bitmask->set_bit(ntophash(str)); }
52
0
  inline void setBit(u_int32_t value) { bitmask->set_bit(value & mask); }
53
54
  /**
55
   * Removes a value to the bloom unsetting the relative bit in the bitmask.
56
   * This is not the best thing that we could do with a bloom even though
57
   * if the user is aware of the limitation it can be used safely
58
   * @param str The value to set.
59
   */
60
0
  inline void unsetBit(char *str) { bitmask->clear_bit(ntophash(str)); }
61
0
  inline void unsetBit(u_int32_t value) { bitmask->clear_bit(value & mask); }
62
63
  /**
64
   * Checks if a value is set in the bloom filter.
65
   * @param str The value to check.
66
   * @return True is the hash for the provided value is set, false otherwise.
67
   */
68
4.18k
  inline bool isSetBit(char *str) {
69
4.18k
    return (bitmask->is_set_bit(ntophash(str)));
70
4.18k
  }
71
};
72
73
#endif /* _BLOOM_H_ */