Skip to content

Commit

Permalink
修复bloomfiler 扩容bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris-code committed Jun 7, 2022
1 parent e1b47a9 commit c5d2a68
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion feapder/dedup/bitarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ def count(self, value=True):
if count:
return int(count)
else:
count = self.redis_db.bitcount(self.name)
count = self.redis_db.bitcount(self.name) # 被设置为 1 的比特位的数量
self.redis_db.strset(self.count_cached_name, count, ex=1800) # 半小时过期
return count
28 changes: 11 additions & 17 deletions feapder/dedup/bloomfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,18 @@ def is_at_capacity(self):
比较耗时 半小时检查一次
@return:
"""
# if self._is_at_capacity:
# return self._is_at_capacity
#
# if not self._check_capacity_time or time.time() - self._check_capacity_time > 1800:
# bit_count = self.bitarray.count()
# if bit_count and bit_count / self.num_bits > 0.5:
# self._is_at_capacity = True
#
# self._check_capacity_time = time.time()
#
# return self._is_at_capacity

if self._is_at_capacity:
return self._is_at_capacity

bit_count = self.bitarray.count()
if bit_count and bit_count / self.num_bits > 0.5:
self._is_at_capacity = True
if (
not self._check_capacity_time
or time.time() - self._check_capacity_time > 1800
):
bit_count = self.bitarray.count()
if bit_count and bit_count / self.num_bits > 0.5:
self._is_at_capacity = True

self._check_capacity_time = time.time()

return self._is_at_capacity

Expand All @@ -173,8 +167,8 @@ def add(self, keys):
@param keys: list or one key
@return:
"""
if self.is_at_capacity:
raise IndexError("BloomFilter is at capacity")
# if self.is_at_capacity:
# raise IndexError("BloomFilter is at capacity")

is_list = isinstance(keys, list)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_dedup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ def test_filter():
datas = ["xxx", "bbb", "ccc"]
dedup.filter_exist_data(datas)
assert datas == ["ccc"]

def test_ScalableBloomFilter():
dedup = Dedup(Dedup.BloomFilter, redis_url="redis://@localhost:6379/0", initial_capacity=10)
for i in range(1000):
print(dedup.add(i))

test_ScalableBloomFilter()

0 comments on commit c5d2a68

Please sign in to comment.