-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package reddit | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// OrderedMaxSet is intended to be able to check if things have been seen while | ||
// expiring older entries that are unlikely to be seen again. | ||
// This is to avoid memory issues in long-running streams. | ||
type OrderedMaxSet struct { | ||
MaxSize int | ||
set map[string]struct{} | ||
keys []string | ||
mutex *sync.Mutex | ||
} | ||
|
||
func (s OrderedMaxSet) updateKeys(newKeys []string) { | ||
s.keys = newKeys | ||
} | ||
|
||
// NewOrderedMaxSet instantiates an OrderedMaxSet and returns it for downstream use. | ||
func NewOrderedMaxSet(maxSize int) OrderedMaxSet { | ||
var mutex = &sync.Mutex{} | ||
orderedMaxSet := OrderedMaxSet{ | ||
MaxSize: maxSize, | ||
set: map[string]struct{}{}, | ||
keys: []string{}, | ||
mutex: mutex, | ||
} | ||
|
||
return orderedMaxSet | ||
} | ||
|
||
// Add accepts a string and inserts it into an OrderedMaxSet | ||
func (s *OrderedMaxSet) Add(v string) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
_, ok := s.set[v] | ||
if !ok { | ||
s.keys = append(s.keys, v) | ||
s.set[v] = struct{}{} | ||
} | ||
if len(s.keys) > s.MaxSize { | ||
for _, id := range s.keys[:len(s.keys)-s.MaxSize] { | ||
delete(s.set, id) | ||
} | ||
s.keys = s.keys[(len(s.keys) - s.MaxSize):] | ||
|
||
} | ||
} | ||
|
||
// Delete accepts a string and deletes it from OrderedMaxSet | ||
func (s *OrderedMaxSet) Delete(v string) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
delete(s.set, v) | ||
} | ||
|
||
// Len returns the number of elements in OrderedMaxSet | ||
func (s *OrderedMaxSet) Len() int { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
return len(s.set) | ||
} | ||
|
||
// Exists accepts a string and determines if it is present in OrderedMaxSet | ||
func (s *OrderedMaxSet) Exists(v string) bool { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
_, ok := s.set[v] | ||
return ok | ||
} |
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,34 @@ | ||
package reddit | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewOrderedMaxSet(t *testing.T) { | ||
set := NewOrderedMaxSet(1) | ||
set.Add("foo") | ||
set.Add("bar") | ||
println(len(set.keys)) | ||
require.Equal(t, set.Len(), 1) | ||
} | ||
|
||
func TestOrderedMaxSetCollision(t *testing.T) { | ||
set := NewOrderedMaxSet(2) | ||
set.Add("foo") | ||
set.Add("foo") | ||
|
||
require.Equal(t, set.Len(), 1) | ||
} | ||
|
||
func TestOrderedMaxSet_Delete(t *testing.T) { | ||
set := NewOrderedMaxSet(1) | ||
set.Add("foo") | ||
|
||
require.Equal(t, set.Len(), 1) | ||
|
||
set.Delete("foo") | ||
require.Equal(t, set.Len(), 0) | ||
require.False(t, set.Exists("foo")) | ||
} |
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