func (self *mongoDriver) Start(rawcfg interface{}) error { self.Config = rawcfg.(*Config) if self.ConnCount == 0 { self.ConnCount = 1 } if self.ShowLog { log.Debugf("[mgo] db connection %d", self.ConnCount) } self.dbChan = make(chan *mgo.Database, self.ConnCount) for i := 0; i < self.ConnCount; i++ { ses, err := mgo.Dial(self.URL) if err != nil { if self.ShowLog { log.Errorf("[mgo] %s", err) } return err } ses.SetMode(mgo.Monotonic, true) // 默认db self.dbChan <- ses.DB("") } return nil }
func (self *mongoDriver) Update(evq cellnet.EventQueue, collName string, selector interface{}, doc interface{}, callback func(error)) { self.writeTask(evq, callback, func(db *mgo.Database) error { if self.ShowLog { log.Debugf("[mgo] update '%s' sel: %s update: %v", collName, selector, doc) } err := db.C(collName).Update(selector, doc) if err != nil && self.ShowLog { log.Errorf("[mgo] update failed, %s", err) } return err }) }
func (self *mongoDriver) FindOne(evq cellnet.EventQueue, collName string, query interface{}, callback interface{}) { self.readTask(evq, callback, func(db *mgo.Database, result interface{}) error { if self.ShowLog { log.Debugf("[mgo] findone '%s' query: %s", collName, query) } err := db.C(collName).Find(query).One(result) if err != nil && self.ShowLog { log.Errorf("[mgo] findone failed, %s", err) } return err }) }
func (self *mongoDriver) Insert(evq cellnet.EventQueue, collName string, doc interface{}, callback func(error)) { self.writeTask(evq, callback, func(db *mgo.Database) error { if self.ShowLog { log.Debugf("[mgo] insert '%s' %v", collName, doc) } err := db.C(collName).Insert(doc) if err != nil && self.ShowLog { log.Errorf("[mgo] insert failed, %s", err) } return err }) }