good morning!!!!

Skip to content
Snippets Groups Projects
Commit 52fba8ac authored by Maciej Lisiewski's avatar Maciej Lisiewski
Browse files

Cache version info for mongo

parent 08cfe53e
Branches
Tags
No related merge requests found
...@@ -209,12 +209,7 @@ func (self *Collection) Append(item interface{}) (interface{}, error) { ...@@ -209,12 +209,7 @@ func (self *Collection) Append(item interface{}) (interface{}, error) {
id := getId(item) id := getId(item)
buildInfo, err := self.collection.Database.Session.BuildInfo() if self.parent.VersionAtLeast(2, 6, 0, 0) {
if err != nil {
return nil, err
}
if buildInfo.VersionAtLeast(2, 6, 0, 0) {
// this breaks MongoDb older than 2.6 // this breaks MongoDb older than 2.6
if _, err = self.collection.Upsert(bson.M{"_id": id}, item); err != nil { if _, err = self.collection.Upsert(bson.M{"_id": id}, item); err != nil {
return nil, err return nil, err
......
...@@ -41,6 +41,7 @@ type Source struct { ...@@ -41,6 +41,7 @@ type Source struct {
connURL db.ConnectionURL connURL db.ConnectionURL
session *mgo.Session session *mgo.Session
database *mgo.Database database *mgo.Database
version []int
} }
func debugEnabled() bool { func debugEnabled() bool {
...@@ -75,6 +76,7 @@ func (s *Source) Clone() (db.Database, error) { ...@@ -75,6 +76,7 @@ func (s *Source) Clone() (db.Database, error) {
connURL: s.connURL, connURL: s.connURL,
session: s.session.Copy(), session: s.session.Copy(),
database: s.database, database: s.database,
version: s.version,
} }
return clone, nil return clone, nil
} }
...@@ -201,3 +203,28 @@ func (s *Source) Collection(names ...string) (db.Collection, error) { ...@@ -201,3 +203,28 @@ func (s *Source) Collection(names ...string) (db.Collection, error) {
return col, err return col, err
} }
func (s *Source) VersionAtLeast(version ...int) bool {
// only fetch this once - it makes a db call
if len(s.version) == 0 {
buildInfo, err := s.database.Session.BuildInfo()
if err != nil {
return false
}
s.version = buildInfo.VersionArray
}
for i := range version {
if i == len(s.version) {
return false
}
if s.version[i] < version[i] {
return false
}
if s.version[i] > version[i] {
return true
}
}
return true
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment