improve server-list performance (#193)

* improve server-list performance

FetchCustomizeColumns that fetch extra fields for all rows 3. separate
read and write http request session

* update vendor

* update: optimize multi-column indexes
This commit is contained in:
Jian Qiu
2019-04-03 20:24:28 +08:00
committed by yunion-ci-robot
parent 72f721bc0c
commit 316335267a
22 changed files with 952 additions and 235 deletions

View File

@@ -0,0 +1,113 @@
package stringutils2
import (
"sort"
)
type SSortedStrings []string
func NewSortedStrings(strs []string) SSortedStrings {
if strs == nil {
return nil
}
sort.Strings(strs)
return SSortedStrings(strs)
}
func (ss SSortedStrings) Index(needle string) (int, bool) {
i := 0
j := len(ss) - 1
for i <= j {
m := (i + j) / 2
if ss[m] < needle {
i = m + 1
} else if ss[m] > needle {
j = m - 1
} else {
return m, true
}
}
return j + 1, false
}
func (ss SSortedStrings) Contains(needle string) bool {
_, find := ss.Index(needle)
return find
}
func (ss SSortedStrings) ContainsAny(needles ...string) bool {
for i := range needles {
_, find := ss.Index(needles[i])
if find {
return true
}
}
return false
}
func (ss SSortedStrings) ContainsAll(needles ...string) bool {
for i := range needles {
_, find := ss.Index(needles[i])
if !find {
return false
}
}
return true
}
func Split(a, b SSortedStrings) (aNoB SSortedStrings, aAndB SSortedStrings, bNoA SSortedStrings) {
a_b := make([]string, 0)
b_a := make([]string, 0)
anb := make([]string, 0)
i := 0
j := 0
for i < len(a) && j < len(b) {
if a[i] == b[j] {
anb = append(anb, a[i])
i += 1
j += 1
} else if a[i] < b[j] {
a_b = append(a_b, a[i])
i += 1
} else if a[i] > b[j] {
b_a = append(b_a, b[j])
j += 1
}
}
if i < len(a) {
a_b = append(a_b, a[i:]...)
}
if j < len(b) {
b_a = append(b_a, b[j:]...)
}
aNoB = SSortedStrings(a_b)
aAndB = SSortedStrings(anb)
bNoA = SSortedStrings(b_a)
return
}
func Merge(a, b SSortedStrings) SSortedStrings {
ret := make([]string, 0)
i := 0
j := 0
for i < len(a) && j < len(b) {
if a[i] == b[j] {
ret = append(ret, a[i])
i += 1
j += 1
} else if a[i] < b[j] {
ret = append(ret, a[i])
i += 1
} else if a[i] > b[j] {
ret = append(ret, b[j])
j += 1
}
}
if i < len(a) {
ret = append(ret, a[i:]...)
}
if j < len(b) {
ret = append(ret, b[j:]...)
}
return SSortedStrings(ret)
}

View File

@@ -0,0 +1,62 @@
package stringutils2
import (
"testing"
)
func TestSortedString(t *testing.T) {
input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
// Alpha Bravo Delta Go Gopher Grin
// 0 1 2 3 4 5
ss := NewSortedStrings(input)
cases := []struct {
Needle string
Index int
Want bool
}{
{"Go", 3, true},
{"Bravo", 1, true},
{"Gopher", 4, true},
{"Alpha", 0, true},
{"Grin", 5, true},
{"Delta", 2, true},
{"Go1", 4, false},
{"G", 3, false},
{"A", 0, false},
{"T", 6, false},
}
for _, c := range cases {
idx, find := ss.Index(c.Needle)
if idx != c.Index || find != c.Want {
t.Errorf("%s: want: %d %v got: %d %v", c.Needle, c.Index, c.Want, idx, find)
}
}
}
func TestSplitStrings(t *testing.T) {
input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
input2 := []string{"Go2", "Bravo", "Gopher", "Alpha1", "Grin", "Delt"}
ss1 := NewSortedStrings(input)
ss2 := NewSortedStrings(input2)
a, b, c := Split(ss1, ss2)
t.Logf("A: %s", ss1)
t.Logf("B: %s", ss2)
t.Logf("A-B: %s", a)
t.Logf("AnB: %s", b)
t.Logf("B-A: %s", c)
}
func TestMergeStrings(t *testing.T) {
input := []string{"Go", "Bravo", "Gopher", "Alpha", "Grin", "Delta"}
input2 := []string{"Go2", "Bravo", "Gopher", "Alpha1", "Grin", "Delt"}
ss1 := NewSortedStrings(input)
ss2 := NewSortedStrings(input2)
m := Merge(ss1, ss2)
t.Logf("A: %s", ss1)
t.Logf("B: %s", ss2)
t.Logf("%s", m)
}