Skip to content

Commit 5dafda7

Browse files
committed
moved common logic of changesItem
1 parent 53b12d5 commit 5dafda7

4 files changed

Lines changed: 91 additions & 31 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package persistent
2+
3+
// ChangesItem represents an changesItem service response
4+
type ChangesItem struct {
5+
ChangeNumber int64 `json:"changeNumber"`
6+
Name string `json:"name"`
7+
Status string `json:"status"`
8+
JSON string
9+
}
10+
11+
// ChangesItem Sortable list
12+
type ChangesItems struct {
13+
items []ChangesItem
14+
}
15+
16+
func NewChangesItems(size int) ChangesItems {
17+
return ChangesItems{
18+
items: make([]ChangesItem, 0, size),
19+
}
20+
}
21+
22+
func (c *ChangesItems) Append(item ChangesItem) {
23+
c.items = append(c.items, item)
24+
}
25+
26+
func (c *ChangesItems) Len() int {
27+
return len(c.items)
28+
}
29+
30+
func (c *ChangesItems) Less(i, j int) bool {
31+
return c.items[i].ChangeNumber > c.items[j].ChangeNumber
32+
}
33+
34+
func (c *ChangesItems) Swap(i, j int) {
35+
c.items[i], c.items[j] = c.items[j], c.items[i]
36+
}
37+
38+
//----------------------------------------------------
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package persistent
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestChangesItem(t *testing.T) {
10+
item := ChangesItem{
11+
ChangeNumber: 123,
12+
Name: "test_split",
13+
Status: "ACTIVE",
14+
JSON: `{"name":"test_split","status":"ACTIVE"}`,
15+
}
16+
17+
items := NewChangesItems(2)
18+
items.Append(item)
19+
items.Append(ChangesItem{
20+
ChangeNumber: 124,
21+
Name: "another_split",
22+
Status: "ARCHIVED",
23+
JSON: `{"name":"another_split","status":"ARCHIVED"}`,
24+
})
25+
26+
assert.Equal(t, 2, items.Len(), "Expected length to be 2")
27+
assert.Equal(t, "test_split", items.items[0].Name)
28+
assert.Equal(t, int64(123), items.items[0].ChangeNumber)
29+
assert.Equal(t, "ACTIVE", items.items[0].Status)
30+
assert.Equal(t, `{"name":"test_split","status":"ACTIVE"}`, items.items[0].JSON)
31+
assert.Equal(t, "another_split", items.items[1].Name)
32+
assert.Equal(t, int64(124), items.items[1].ChangeNumber)
33+
assert.Equal(t, "ARCHIVED", items.items[1].Status)
34+
assert.Equal(t, `{"name":"another_split","status":"ARCHIVED"}`, items.items[1].JSON)
35+
assert.True(t, items.Less(1, 0), "Expected item at index 1 to be less than item at index 0")
36+
items.Swap(0, 1)
37+
assert.Equal(t, "another_split", items.items[0].Name)
38+
assert.Equal(t, int64(124), items.items[0].ChangeNumber)
39+
assert.Equal(t, "ARCHIVED", items.items[0].Status)
40+
assert.Equal(t, `{"name":"another_split","status":"ARCHIVED"}`, items.items[0].JSON)
41+
assert.Equal(t, "test_split", items.items[1].Name)
42+
assert.Equal(t, int64(123), items.items[1].ChangeNumber)
43+
assert.Equal(t, "ACTIVE", items.items[1].Status)
44+
assert.Equal(t, `{"name":"test_split","status":"ACTIVE"}`, items.items[1].JSON)
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package persistent
2+
3+
const ruleBasedSegmentsChangesCollectionName = "RULE_BASED_SEGMENTS_CHANGES_COLLECTION"

splitio/proxy/storage/persistent/splits.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,6 @@ import (
1212

1313
const splitChangesCollectionName = "SPLIT_CHANGES_COLLECTION"
1414

15-
// SplitChangesItem represents an SplitChanges service response
16-
type SplitChangesItem struct {
17-
ChangeNumber int64 `json:"changeNumber"`
18-
Name string `json:"name"`
19-
Status string `json:"status"`
20-
JSON string
21-
}
22-
23-
// SplitsChangesItems Sortable list
24-
type SplitsChangesItems []SplitChangesItem
25-
26-
func (slice SplitsChangesItems) Len() int {
27-
return len(slice)
28-
}
29-
30-
func (slice SplitsChangesItems) Less(i, j int) bool {
31-
return slice[i].ChangeNumber > slice[j].ChangeNumber
32-
}
33-
34-
func (slice SplitsChangesItems) Swap(i, j int) {
35-
slice[i], slice[j] = slice[j], slice[i]
36-
}
37-
38-
//----------------------------------------------------
39-
4015
// SplitChangesCollection represents a collection of SplitChangesItem
4116
type SplitChangesCollection struct {
4217
collection CollectionWrapper
@@ -54,15 +29,14 @@ func NewSplitChangesCollection(db DBWrapper, logger logging.LoggerInterface) *Sp
5429

5530
// Update processes a set of feature flag changes items + a changeNumber bump atomically
5631
func (c *SplitChangesCollection) Update(toAdd []dtos.SplitDTO, toRemove []dtos.SplitDTO, cn int64) {
57-
58-
items := make(SplitsChangesItems, 0, len(toAdd)+len(toRemove))
32+
items := NewChangesItems(len(toAdd) + len(toRemove))
5933
process := func(split *dtos.SplitDTO) {
6034
asJSON, err := json.Marshal(split)
6135
if err != nil {
6236
// This should not happen unless the DTO class is broken
6337
return
6438
}
65-
items = append(items, SplitChangesItem{
39+
items.Append(ChangesItem{
6640
ChangeNumber: split.ChangeNumber,
6741
Name: split.Name,
6842
Status: split.Status,
@@ -80,8 +54,8 @@ func (c *SplitChangesCollection) Update(toAdd []dtos.SplitDTO, toRemove []dtos.S
8054

8155
c.mutex.Lock()
8256
defer c.mutex.Unlock()
83-
for idx := range items {
84-
err := c.collection.SaveAs([]byte(items[idx].Name), items[idx])
57+
for idx := range items.items {
58+
err := c.collection.SaveAs([]byte(items.items[idx].Name), items.items[idx])
8559
if err != nil {
8660
// TODO(mredolatti): log
8761
}
@@ -102,7 +76,7 @@ func (c *SplitChangesCollection) FetchAll() ([]dtos.SplitDTO, error) {
10276

10377
var decodeBuffer bytes.Buffer
10478
for _, v := range items {
105-
var q SplitChangesItem
79+
var q ChangesItem
10680
// resets buffer data
10781
decodeBuffer.Reset()
10882
decodeBuffer.Write(v)

0 commit comments

Comments
 (0)