-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated API to support more configuration
Added configuration options to the API to support supplied hashing functions, configurable filter sizes, bucket sizes and fingerprint sizes. Additionally there is the option to specify maximum number of kicks displacing fingerprints from their buckets. They are to be provided to `cfilter.New(opts ...options)` and are as follows: - cfilter.Size(uint) sets the number of buckets in the filter - cfilter.BucketSize(uint8) sets the size of each bucket - cfilter.FingerprintSize(uint8) sets the size of the fingerprint - cfilter.MaximumKicks(uint) sets the maximum number of bucket kicks - cfilter.HashFn(hash.Hash) sets the fingerprinting hashing function NOTE: this commit introduces a change to the API, in order to retrieve the number or items currently in the filter instead of `cf.Size()` you would now use `cf.Count()`.
- Loading branch information
1 parent
c3db6c3
commit 6ad3898
Showing
6 changed files
with
135 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cfilter | ||
|
||
import ( | ||
"hash" | ||
"hash/fnv" | ||
) | ||
|
||
type option func(*CFilter) | ||
|
||
// Size sets the number of buckets in the filter. | ||
// Defaults to ((1 << 18) / BucketSize). | ||
func Size(s uint) option { | ||
return func(cf *CFilter) { | ||
cf.size = s | ||
} | ||
} | ||
|
||
// BucketSize sets the size of each bucket in the filter. Defaults to 4. | ||
func BucketSize(s uint8) option { | ||
return func(cf *CFilter) { | ||
cf.bSize = s | ||
} | ||
} | ||
|
||
// FingerprintSize sets the size of the fingerprint. Defaults to 2. | ||
func FingerprintSize(s uint8) option { | ||
return func(cf *CFilter) { | ||
cf.fpSize = s | ||
} | ||
} | ||
|
||
// MaximumKicks sets the maximum number of times we kick down items/displace | ||
// from their buckets. Defaults to 500. | ||
func MaximumKicks(k uint) option { | ||
return func(cf *CFilter) { | ||
cf.kicks = k | ||
} | ||
} | ||
|
||
// HashFn sets the hashing function to be used for fingerprinting. Defaults to | ||
// a 64-bit FNV-1 hash.Hash. | ||
func HashFn(hashfn hash.Hash) option { | ||
return func(cf *CFilter) { | ||
cf.hashfn = hashfn | ||
} | ||
} | ||
|
||
func configure(cf *CFilter) { | ||
if cf.hashfn == nil { | ||
cf.hashfn = fnv.New64() | ||
} | ||
if cf.bSize == 0 { | ||
cf.bSize = 4 | ||
} | ||
if cf.fpSize == 0 { | ||
cf.fpSize = 2 | ||
} | ||
if cf.kicks == 0 { | ||
cf.kicks = 500 | ||
} | ||
if cf.size == 0 { | ||
cf.size = (1 << 18) / uint(cf.bSize) | ||
} | ||
} |