Skip to content

Commit

Permalink
Use sync.Map approach: no behavoir changes, memorizing the existing f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
paulquerna-okta committed Nov 1, 2019
1 parent 308580d commit da98f55
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ type DbMap struct {
tablesDynamic map[string]*TableMap // tables that use same go-struct and different db table names
logger GorpLogger
logPrefix string

Cache Cache
}

type Cache interface {
Load(key interface{}) (value interface{}, ok bool)
Store(key, value interface{})
}

func (m *DbMap) dynamicTableAdd(tableName string, tbl *TableMap) {
Expand Down
38 changes: 36 additions & 2 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,32 @@ func expandNamedQuery(m *DbMap, query string, keyGetter func(key string) reflect
}), args
}

type fieldCacheKey struct {
t reflect.Type
name string
cols string
}

type fieldCacheEntry struct {
mapping [][]int
err error
}

func columnToFieldIndex(m *DbMap, t reflect.Type, name string, cols []string) ([][]int, error) {
var ck fieldCacheKey
var err error
if m.Cache != nil {
ck.t = t
ck.name = name
ck.cols = strings.Join(cols, ",")

rv, ok := m.Cache.Load(ck)
if ok {
entry := rv.(*fieldCacheEntry)
return entry.mapping, entry.err
}
}

colToFieldIndex := make([][]int, len(cols))

// check if type t is a mapped table - if so we'll
Expand Down Expand Up @@ -298,13 +323,22 @@ func columnToFieldIndex(m *DbMap, t reflect.Type, name string, cols []string) ([
missingColNames = append(missingColNames, colName)
}
}

if len(missingColNames) > 0 {
return colToFieldIndex, &NoFieldInTypeError{
err = &NoFieldInTypeError{
TypeName: t.Name(),
MissingColNames: missingColNames,
}
}
return colToFieldIndex, nil

if m.Cache != nil {
entry := &fieldCacheEntry{
mapping: colToFieldIndex,
err: err,
}
m.Cache.Store(ck, entry)
}
return colToFieldIndex, err
}

func fieldByName(val reflect.Value, fieldName string) *reflect.Value {
Expand Down
3 changes: 2 additions & 1 deletion mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gorp

import (
"reflect"
"sync"
"testing"
"time"
)
Expand All @@ -23,7 +24,7 @@ type testCoolUser struct {

func BenchmarkCcolumnToFieldIndex(b *testing.B) {
structType := reflect.TypeOf(testUser{})
dbmap := &DbMap{}
dbmap := &DbMap{Cache: &sync.Map{}}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := columnToFieldIndex(dbmap,
Expand Down

0 comments on commit da98f55

Please sign in to comment.