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

[SDKS-8965][Proxy] Adding Large Segments storage implementation. #287

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
applying feedback
  • Loading branch information
sanzmauro committed Oct 25, 2024
commit 38ca0faebbbdd39915748872e05b0fafc1293b5b
40 changes: 10 additions & 30 deletions splitio/proxy/storage/large_segments.go
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import (
// LargeSegmentsStorage defines the interface for a per-user large segments storage
type LargeSegmentsStorage interface {
Count() int
SegmentsForUser(key string) []string
LargeSegmentsForUser(userKey string) []string
Update(lsName string, userKeys []string)
}

@@ -38,16 +38,18 @@ func (s *LargeSegmentsStorageImpl) Count() int {
}

// SegmentsForUser returns the list of segments a certain user belongs to
func (s *LargeSegmentsStorageImpl) SegmentsForUser(key string) []string {
func (s *LargeSegmentsStorageImpl) LargeSegmentsForUser(userKey string) []string {
s.mutex.RLock()
defer s.mutex.RUnlock()

toReturn := make([]string, 0)
lsNames := s.names()
toReturn := make([]string, 0, len(s.largeSegments))
for lsName, data := range s.largeSegments {
i := sort.Search(len(data), func(i int) bool {
return data[i] >= userKey
})

for _, name := range lsNames {
if s.exists(name, key) {
toReturn = append(toReturn, name)
if i < len(data) && data[i] == userKey {
toReturn = append(toReturn, lsName)
}
}

@@ -62,26 +64,4 @@ func (s *LargeSegmentsStorageImpl) Update(lsName string, userKeys []string) {
s.largeSegments[lsName] = userKeys
}

// names returns the list with Large Segment Names
func (s *LargeSegmentsStorageImpl) names() []string {
toReturn := make([]string, 0, len(s.largeSegments))
for key := range s.largeSegments {
toReturn = append(toReturn, key)
}

return toReturn
}

// exists returns true if a userKey is part of a large segment, else returns false
func (s *LargeSegmentsStorageImpl) exists(lsName string, userKey string) bool {
data, ok := s.largeSegments[lsName]
if !ok {
return false
}

i := sort.Search(len(data), func(i int) bool {
return data[i] >= userKey
})

return i < len(data) && data[i] == userKey
}
var _ LargeSegmentsStorage = (*LargeSegmentsStorageImpl)(nil)
10 changes: 5 additions & 5 deletions splitio/proxy/storage/large_segments_test.go
Original file line number Diff line number Diff line change
@@ -38,19 +38,19 @@ func TestLatgeSegmentStorage(t *testing.T) {

assert.Equal(t, 3, storage.Count())

result := storage.SegmentsForUser(*sharedKey)
result := storage.LargeSegmentsForUser(*sharedKey)
sort.Strings(result)
assert.Equal(t, []string{"ls_test_1", "ls_test_2", "ls_test_3"}, result)

result = storage.SegmentsForUser(keys1[100])
result = storage.LargeSegmentsForUser(keys1[100])
assert.Equal(t, []string{"ls_test_1"}, result)

result = storage.SegmentsForUser(keys2[100])
result = storage.LargeSegmentsForUser(keys2[100])
assert.Equal(t, []string{"ls_test_2"}, result)

result = storage.SegmentsForUser(keys3[100])
result = storage.LargeSegmentsForUser(keys3[100])
assert.Equal(t, []string{"ls_test_3"}, result)

result = storage.SegmentsForUser("mauro-test")
result = storage.LargeSegmentsForUser("mauro-test")
assert.Equal(t, []string{}, result)
}