Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameterizable Init funciton #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var (
errElements = errors.New("error: elements must be greater than 0")
errFalsePositive = errors.New("error: falsePositive must be greater than 0 and less than 1")
errHash = errors.New("error: Hash functions must be greater than zero")
)

// Ring contains the information for a ring data store.
Expand Down Expand Up @@ -49,6 +50,25 @@ func Init(elements int, falsePositive float64) (*Ring, error) {
return &r, nil
}

// InitByParameters initializes a bloom filter allowing the user to explicitly set
// the size of the bit array and the amount of hash functions
func InitByParameters(size, hashFunctions uint64) (*Ring, error) {
if size <= 0 {
return nil, errElements
}
if hashFunctions <= 0 {
return nil, errHash
}

r := Ring{}

r.mutex = &sync.RWMutex{}
r.size = size
r.hash = hashFunctions
r.bits = make([]uint8, r.size/8+1)
return &r, nil
}

// Add adds the data to the ring.
func (r *Ring) Add(data []byte) {
// generate hashes
Expand Down
8 changes: 8 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ func TestBadParameters(t *testing.T) {
if err == nil {
t.Fatal("element <= 0 not captured")
}

// InitByParameters tests

_, err = ring.InitByParameters(1, 0)
if err != nil {
t.Fatal("Hash function cannot be <=0")
}

}

// TestReset ensures the Ring is cleared on Reset().
Expand Down