forked from mirror/gorm
Use sync.Map for DB.values (#2064)
* Replace the regular map with a sync.Map to avoid fatal concurrent map reads/writes * fix the formatting
This commit is contained in:
parent
d3e666a1e0
commit
73e7561e20
15
main.go
15
main.go
|
@ -22,7 +22,7 @@ type DB struct {
|
||||||
logMode int
|
logMode int
|
||||||
logger logger
|
logger logger
|
||||||
search *search
|
search *search
|
||||||
values map[string]interface{}
|
values sync.Map
|
||||||
|
|
||||||
// global db
|
// global db
|
||||||
parent *DB
|
parent *DB
|
||||||
|
@ -72,7 +72,6 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
|
||||||
db = &DB{
|
db = &DB{
|
||||||
db: dbSQL,
|
db: dbSQL,
|
||||||
logger: defaultLogger,
|
logger: defaultLogger,
|
||||||
values: map[string]interface{}{},
|
|
||||||
callbacks: DefaultCallback,
|
callbacks: DefaultCallback,
|
||||||
dialect: newDialect(dialect, dbSQL),
|
dialect: newDialect(dialect, dbSQL),
|
||||||
}
|
}
|
||||||
|
@ -680,13 +679,13 @@ func (s *DB) Set(name string, value interface{}) *DB {
|
||||||
|
|
||||||
// InstantSet instant set setting, will affect current db
|
// InstantSet instant set setting, will affect current db
|
||||||
func (s *DB) InstantSet(name string, value interface{}) *DB {
|
func (s *DB) InstantSet(name string, value interface{}) *DB {
|
||||||
s.values[name] = value
|
s.values.Store(name, value)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get get setting by name
|
// Get get setting by name
|
||||||
func (s *DB) Get(name string) (value interface{}, ok bool) {
|
func (s *DB) Get(name string) (value interface{}, ok bool) {
|
||||||
value, ok = s.values[name]
|
value, ok = s.values.Load(name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -750,16 +749,16 @@ func (s *DB) clone() *DB {
|
||||||
parent: s.parent,
|
parent: s.parent,
|
||||||
logger: s.logger,
|
logger: s.logger,
|
||||||
logMode: s.logMode,
|
logMode: s.logMode,
|
||||||
values: map[string]interface{}{},
|
|
||||||
Value: s.Value,
|
Value: s.Value,
|
||||||
Error: s.Error,
|
Error: s.Error,
|
||||||
blockGlobalUpdate: s.blockGlobalUpdate,
|
blockGlobalUpdate: s.blockGlobalUpdate,
|
||||||
dialect: newDialect(s.dialect.GetName(), s.db),
|
dialect: newDialect(s.dialect.GetName(), s.db),
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range s.values {
|
s.values.Range(func(k, v interface{}) bool {
|
||||||
db.values[key] = value
|
db.values.Store(k, v)
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
if s.search == nil {
|
if s.search == nil {
|
||||||
db.search = &search{limit: -1, offset: -1}
|
db.search = &search{limit: -1, offset: -1}
|
||||||
|
|
Loading…
Reference in New Issue