Skip to content

Commit

Permalink
Add configurable filter size
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesduncombe committed Nov 16, 2020
1 parent a440293 commit 61331a4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config

config :petal,
filter_size: 64,
hashers: [
Petal.Hasher.Adler32,
Petal.Hasher.CRC32
Expand Down
12 changes: 11 additions & 1 deletion lib/petal/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ defmodule Petal.Application do

use Application

import Petal.Bytes, only: [ceil_bits: 1]

@impl true
def start(_type, _args) do
children = [
{Petal.Worker, size: 64}
{Petal.Worker, size: default_filter_size()}
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Petal.Supervisor]
Supervisor.start_link(children, opts)
end

# Default size of the filter to use
defp default_filter_size() do
size = Application.get_env(:petal, :filter_size, _bits = 64)

# Make sure this is divisible by 8
ceil_bits(size)
end
end
6 changes: 6 additions & 0 deletions lib/petal/bytes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ defmodule Petal.Bytes do
@spec byte_size_of_field(size :: pos_integer()) :: pos_integer()
def byte_size_of_field(size), do: div(size, 8)

@doc """
Round up the bits so that they are divisible by 8.
"""
@spec ceil_bits(size :: pos_integer()) :: pos_integer()
def ceil_bits(size), do: ceil(size / 8) * 8

@doc ~S"""
Pad the `encoded` payload to `n` bytes.
Expand Down
2 changes: 1 addition & 1 deletion lib/petal/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Petal.Worker do

def init(args) do
# Init the bitfield
size_of_field = Keyword.get(args, :size, 64)
size_of_field = Keyword.get(args, :size, _bits = 64)
state = Bitfield.new(size_of_field)

{:ok, state}
Expand Down
4 changes: 4 additions & 0 deletions test/petal/bytes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule Petal.BytesTest do
assert Bytes.byte_size_of_field(64) == 8
end

test "ceil_bits/1" do
assert Bytes.ceil_bits(100) == 104
end

test "pad_encoded_payload/2" do
payload = <<1, 2, 3>>

Expand Down

0 comments on commit 61331a4

Please sign in to comment.