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

fix golint reports #3

Merged
merged 1 commit into from
Feb 15, 2014
Merged
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
3 changes: 2 additions & 1 deletion githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
set -e -x

go vet ./...
go test ./...
gofmt -w .
golint .
go test ./...
41 changes: 20 additions & 21 deletions gossip/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"math"
)

// Filters are counting bloom filters, used to approximate the number
// Filter is a counting bloom filter, used to approximate the number
// of differences between InfoStores from different nodes, with
// minimal network overhead.
type Filter struct {
Expand All @@ -34,15 +34,15 @@ type Filter struct {
hasher *Hasher // Provides independent hashes
}

// Computes the probability of a false positive.
// probFalsePositive computes the probability of a false positive.
func probFalsePositive(N uint32, K uint32, M uint32) float64 {
pSet := 1.0 - math.Pow(float64(M-1)/float64(M), float64(N*K))
return math.Pow(pSet, float64(K))
}

// Computes minimum number of slots such that the maximum false
// positive probability (maxFP) is guaranteed. Returns the number
// of slots (M) as well as optimal number of hashes (K).
// computeOptimalValues computes minimum number of slots such that
// the maximum false positive probability (maxFP) is guaranteed.
// Returns the number of slots (M) as well as optimal number of hashes (K).
//
// Math from: http://en.wikipedia.org/wiki/Bloom_filter
func computeOptimalValues(N uint32, maxFP float64) (uint32, uint32) {
Expand All @@ -52,13 +52,12 @@ func computeOptimalValues(N uint32, maxFP float64) (uint32, uint32) {
K2 := uint32(math.Floor((float64(M) / float64(N)) * logN2))
if probFalsePositive(N, K1, M) < probFalsePositive(N, K2, M) {
return M, K1
} else {
return M, K2
}
return M, K2
}

// Creates a new filter with expected number of insertions N,
// Number of bits per slot B, and expected value of a false
// NewFilter allocates and returns a new filter with expected number of
// insertions N, Number of bits per slot B, and expected value of a false
// positive < maxFP.
func NewFilter(N uint32, B uint32, maxFP float64) (*Filter, error) {
// TODO(spencer): we probably would be well-served using a 3-bit
Expand All @@ -78,7 +77,7 @@ func NewFilter(N uint32, B uint32, maxFP float64) (*Filter, error) {
return &Filter{K, 0, 0, B, M, maxCount, bytes, NewHasher()}, nil
}

// Increments slot value by the specified amount, bounding at
// incrementSlot increments slot value by the specified amount, bounding at
// maximum slot value.
func (f *Filter) incrementSlot(slot uint32, incr int32) {
val := int32(f.getSlot(slot)) + incr
Expand All @@ -94,15 +93,15 @@ func (f *Filter) incrementSlot(slot uint32, incr int32) {
f.Data[byteIndex] = byte(uint32(f.Data[byteIndex]) | uint32(val)<<byteOffset)
}

// Returns the slot value.
// getSlot returns the slot value.
func (f *Filter) getSlot(slot uint32) uint32 {
bitIndex := slot * f.B
byteIndex := bitIndex / 8
byteOffset := bitIndex % 8
return (uint32(f.Data[byteIndex]) & (f.MaxCount << byteOffset)) >> byteOffset
}

// Adds the key to the filter.
// AddKey adds the key to the filter.
func (f *Filter) AddKey(key string) {
f.hasher.HashKey(key)
for i := uint32(0); i < f.K; i++ {
Expand All @@ -112,7 +111,7 @@ func (f *Filter) AddKey(key string) {
f.N++
}

// Checks whether key has been added to the filter. The chance this
// HasKey checks whether key has been added to the filter. The chance this
// method returns an incorrect value is given by ProbFalsePositive().
func (f *Filter) HasKey(key string) bool {
f.hasher.HashKey(key)
Expand All @@ -125,9 +124,9 @@ func (f *Filter) HasKey(key string) bool {
return true
}

// Removes a key by first verifying it's likely been seen and then
// decrementing each of the slots it hashes to. Returns true if
// the key was "removed"; false otherwise.
// RemoveKey removes a key by first verifying it's likely been seen and then
// decrementing each of the slots it hashes to. Returns true if the key was
// "removed"; false otherwise.
func (f *Filter) RemoveKey(key string) bool {
if f.HasKey(key) {
f.hasher.HashKey(key)
Expand All @@ -141,17 +140,17 @@ func (f *Filter) RemoveKey(key string) bool {
return false
}

// Returns the probability the filter returns a false positive.
// ProbFalsePositive returns the probability the filter returns a false
// positive.
func (f *Filter) ProbFalsePositive() float64 {
if f.R != 0 {
return probFalsePositive(f.ApproximateInsertions(), f.K, f.M)
} else {
return probFalsePositive(f.N, f.K, f.M)
}
return probFalsePositive(f.N, f.K, f.M)
}

// Determines the approximate number of items inserted into the
// Filter after removals.
// ApproximateInsertions determines the approximate number of items
// inserted into the Filter after removals.
func (f *Filter) ApproximateInsertions() uint32 {
count := uint32(0)
for i := uint32(0); i < f.M; i++ {
Expand Down
18 changes: 9 additions & 9 deletions gossip/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"testing"
)

// Verify some basic expectations of false positive
// computations.
// TestProbFalsePositive verifies some basic expectations
// of false positive computations.
func TestProbFalsePositive(t *testing.T) {
// Simple cases.
if probFalsePositive(0, 1, 1) != 0 {
Expand All @@ -39,7 +39,7 @@ func TestProbFalsePositive(t *testing.T) {
}
}

// Verify optimal values make sense.
// TestOptimalValues verifies optimal values make sense.
func TestOptimalValues(t *testing.T) {
// Compute optimal values for 1000 insertions and various probabilities.
M10, _ := computeOptimalValues(1000, 0.10)
Expand All @@ -51,7 +51,7 @@ func TestOptimalValues(t *testing.T) {
}
}

// Verify bad inputs, optimal values, size of slots data.
// TestNewFilter verifies bad inputs, optimal values, size of slots data.
func TestNewFilter(t *testing.T) {
if _, err := NewFilter(1, 3, 0.10); err == nil {
t.Error("NewFilter should not accept bits B which are non-divisor of 8")
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestNewFilter(t *testing.T) {
}
}

// Test slot increment and slot count fetching.
// TestSlots tests slot increment and slot count fetching.
func TestSlots(t *testing.T) {
f, err := NewFilter(1000, 4, 0.01)
if err != nil {
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestSlots(t *testing.T) {
}
}

// Add keys, test existence, and remove keys.
// TestKeys adds keys, tests existence, and removes keys.
func TestKeys(t *testing.T) {
f, err := NewFilter(1000, 4, 0.01)
if err != nil {
Expand All @@ -150,7 +150,7 @@ func TestKeys(t *testing.T) {
}
}

// Add many keys, verify false positive probability.
// TestFalsePositives adds many keys and verifies false positive probability.
func TestFalsePositives(t *testing.T) {
f, err := NewFilter(1000, 4, 0.01)
if err != nil {
Expand Down Expand Up @@ -185,8 +185,8 @@ func TestFalsePositives(t *testing.T) {
}
}

// Add many keys with overloaded filter and verify approximation
// degrades gracefully.
// TestApproximateInsertions adds many keys with an overloaded filter and
// verifies that approximation degrades gracefully.
func TestApproximateInsertions(t *testing.T) {
f, err := NewFilter(10, 4, 0.10)
if err != nil {
Expand Down
Loading