Remove allocations from GetColumn & GetColumnIdx
This commit is contained in:
parent
5bf745d7d1
commit
5caa28d411
35
table.go
35
table.go
|
@ -47,19 +47,40 @@ func NewTable(name string, t reflect.Type) *Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *Table) GetColumn(name string) *Column {
|
func (table *Table) columnsByName(name string) []*Column {
|
||||||
if c, ok := table.columnsMap[strings.ToLower(name)]; ok {
|
|
||||||
return c[0]
|
n := len(name)
|
||||||
|
|
||||||
|
for k := range table.columnsMap {
|
||||||
|
if len(k) != n {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if strings.EqualFold(k, name) {
|
||||||
|
return table.columnsMap[k]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (table *Table) GetColumn(name string) *Column {
|
||||||
|
|
||||||
|
cols := table.columnsByName(name)
|
||||||
|
|
||||||
|
if cols != nil {
|
||||||
|
return cols[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
func (table *Table) GetColumnIdx(name string, idx int) *Column {
|
||||||
if c, ok := table.columnsMap[strings.ToLower(name)]; ok {
|
|
||||||
if idx < len(c) {
|
cols := table.columnsByName(name)
|
||||||
return c[idx]
|
|
||||||
}
|
if cols != nil && idx < len(cols) {
|
||||||
|
return cols[idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,107 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testsGetColumn = []struct {
|
||||||
|
name string
|
||||||
|
idx int
|
||||||
|
}{
|
||||||
|
{"Id", 0},
|
||||||
|
{"Deleted", 0},
|
||||||
|
{"Caption", 0},
|
||||||
|
{"Code_1", 0},
|
||||||
|
{"Code_2", 0},
|
||||||
|
{"Code_3", 0},
|
||||||
|
{"Parent_Id", 0},
|
||||||
|
{"Latitude", 0},
|
||||||
|
{"Longitude", 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
var table *Table
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
|
||||||
|
table = NewEmptyTable()
|
||||||
|
|
||||||
|
var name string
|
||||||
|
|
||||||
|
for i := 0; i < len(testsGetColumn); i++ {
|
||||||
|
// as in Table.AddColumn func
|
||||||
|
name = strings.ToLower(testsGetColumn[i].name)
|
||||||
|
|
||||||
|
table.columnsMap[name] = append(table.columnsMap[name], &Column{})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetColumn(t *testing.T) {
|
||||||
|
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
if table.GetColumn(test.name) == nil {
|
||||||
|
t.Error("Column not found!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetColumnIdx(t *testing.T) {
|
||||||
|
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
if table.GetColumnIdx(test.name, test.idx) == nil {
|
||||||
|
t.Errorf("Column %s with idx %d not found!", test.name, test.idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGetColumnWithToLower(b *testing.B) {
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
|
||||||
|
if _, ok := table.columnsMap[strings.ToLower(test.name)]; !ok {
|
||||||
|
b.Errorf("Column not found:%s", test.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGetColumnIdxWithToLower(b *testing.B) {
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
|
||||||
|
if c, ok := table.columnsMap[strings.ToLower(test.name)]; ok {
|
||||||
|
if test.idx < len(c) {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
b.Errorf("Bad idx in: %s, %d", test.name, test.idx)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
b.Errorf("Column not found: %s, %d", test.name, test.idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGetColumn(b *testing.B) {
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
if table.GetColumn(test.name) == nil {
|
||||||
|
b.Errorf("Column not found:%s", test.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGetColumnIdx(b *testing.B) {
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, test := range testsGetColumn {
|
||||||
|
if table.GetColumnIdx(test.name, test.idx) == nil {
|
||||||
|
b.Errorf("Column not found:%s, %d", test.name, test.idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue