Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Search rules from the end of list
Browse files Browse the repository at this point in the history
Rules are stored by cutover time ascending, searching backwards should
be faster
  • Loading branch information
Chao Wang committed Apr 6, 2017
1 parent 83aed51 commit dbba223
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
7 changes: 3 additions & 4 deletions rules/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ func (mc *mappingRule) ActiveRule(timeNs int64) *mappingRule {
}

func (mc *mappingRule) activeIndex(timeNs int64) int {
idx := 0
for idx < len(mc.snapshots) && mc.snapshots[idx].cutoverNs <= timeNs {
idx++
idx := len(mc.snapshots) - 1
for idx >= 0 && mc.snapshots[idx].cutoverNs > timeNs {
idx--
}
idx--
return idx
}
20 changes: 18 additions & 2 deletions rules/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,25 @@ func TestMappingRuleActiveSnapshotNotFound(t *testing.T) {
require.Nil(t, mr.ActiveSnapshot(0))
}

func TestMappingRuleActiveSnapshotFound(t *testing.T) {
func TestMappingRuleActiveSnapshotFound_Second(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, nil)
require.NoError(t, err)
require.Equal(t, mr.snapshots[1], mr.ActiveSnapshot(100000))
}

func TestMappingRuleActiveSnapshotFound_First(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, nil)
require.NoError(t, err)
require.Equal(t, mr.snapshots[0], mr.ActiveSnapshot(20000))
}

func TestMappingRuleActiveRuleNotFound(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, nil)
require.NoError(t, err)
require.Equal(t, mr, mr.ActiveRule(0))
}

func TestMappingRuleActiveRuleFound(t *testing.T) {
func TestMappingRuleActiveRuleFound_Second(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, nil)
require.NoError(t, err)
expected := &mappingRule{
Expand All @@ -161,3 +167,13 @@ func TestMappingRuleActiveRuleFound(t *testing.T) {
}
require.Equal(t, expected, mr.ActiveRule(100000))
}

func TestMappingRuleActiveRuleFound_First(t *testing.T) {
mr, err := newMappingRule(testMappingRuleSchema, nil)
require.NoError(t, err)
expected := &mappingRule{
uuid: mr.uuid,
snapshots: mr.snapshots,
}
require.Equal(t, expected, mr.ActiveRule(20000))
}

0 comments on commit dbba223

Please sign in to comment.