// initMongo dials the initial MongoDB connection, setting a
// password for the admin user, and returning the session.
557793
func initMongo(info mongo.Info, dialOpts mongo.DialOpts, password string) (*mgo.Session, error) {
	session, err := mongo.DialWithInfo(mongo.MongoInfo{Info: info}, dialOpts)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if err := mongo.SetAdminMongoPassword(session, mongo.AdminUser, password); err != nil {
		session.Close()
		return nil, errors.Trace(err)
	}
	if err := mongo.Login(session, mongo.AdminUser, password); err != nil {
		session.Close()
		return nil, errors.Trace(err)
	}
	return session, nil
}
928163
func (p LogicalJoin) Init(ctx sessionctx.Context) *LogicalJoin {
	p.baseLogicalPlan = newBaseLogicalPlan(ctx, TypeJoin, &p)
	return &p
}
350019
func (b *Backend) createMongoIndexes(database string) error {

	tasksCollection := b.client.Database(database).Collection("tasks")

	expireIn := int32(b.GetConfig().ResultsExpireIn)

	_, err := tasksCollection.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys:    bson.M{"state": 1},
			Options: options.Index().SetBackground(true).SetExpireAfterSeconds(expireIn),
		},
		mongo.IndexModel{
			Keys:    bson.M{"lock": 1},
			Options: options.Index().SetBackground(true).SetExpireAfterSeconds(expireIn),
		},
	})
	if err != nil {
		return err
	}

	return err
}
1161943
func init() {
	DefaultCallback.Create().Register("gorm:begin_transaction", beginTransactionCallback)
	DefaultCallback.Create().Register("gorm:before_create", beforeCreateCallback)
	DefaultCallback.Create().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
	DefaultCallback.Create().Register("gorm:update_time_stamp", updateTimeStampForCreateCallback)
	DefaultCallback.Create().Register("gorm:create", createCallback)
	DefaultCallback.Create().Register("gorm:force_reload_after_create", forceReloadAfterCreateCallback)
	DefaultCallback.Create().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
	DefaultCallback.Create().Register("gorm:after_create", afterCreateCallback)
	DefaultCallback.Create().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
}
479599
func NewMongodbBackend(mongoURL string, database string) (b MongodbAuthBackend, e error) {
	// Set up connection to database
	b.mongoURL = mongoURL
	b.database = database
	session, err := mgo.Dial(b.mongoURL)
	if err != nil {
		return b, mkmgoerror(err.Error())
	}
	err = session.Ping()
	if err != nil {
		return b, mkmgoerror(err.Error())
	}

	// Ensure that the Username field is unique
	index := mgo.Index{
		Key:    []string{"Username"},
		Unique: true,
	}
	err = session.DB(b.database).C("goauth").EnsureIndex(index)
	if err != nil {
		return b, mkmgoerror(err.Error())
	}
	b.session = session
	return
}
1115140
func (c *configInternal) SetMongoVersion(v mongo.Version) {
	c.mongoVersion = v.String()
}
663725
func init() {
	DefaultCallback.Query().Register("gorm:query", queryCallback)
	DefaultCallback.Query().Register("gorm:preload", preloadCallback)
	DefaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
}
819162
func (sys *ConfigSys) Init(objAPI ObjectLayer) error {
	if objAPI == nil {
		return errInvalidArgument
	}

	doneCh := make(chan struct{})
	defer close(doneCh)

	// Initializing configuration needs a retry mechanism for
	// the following reasons:
	//  - Read quorum is lost just after the initialization
	//    of the object layer.
	//  - Write quorum not met when upgrading configuration
	//    version is needed.
	for range newRetryTimerSimple(doneCh) {
		if err := initConfig(objAPI); err != nil {
			if strings.Contains(err.Error(), InsufficientReadQuorum{}.Error()) ||
				strings.Contains(err.Error(), InsufficientWriteQuorum{}.Error()) {
				logger.Info("Waiting for configuration to be initialized..")
				continue
			}
			return err
		}
		break
	}
	return nil
}
1026556
func (m *MMClient) initUser() error {
	m.Lock()
	defer m.Unlock()
	m.log.Debug("initUser()")
	initLoad, err := m.Client.GetInitialLoad()
	if err != nil {
		return err
	}
	initData := initLoad.Data.(*model.InitialLoad)
	m.User = initData.User
	// we only load all team data on initial login.
	// all other updates are for channels from our (primary) team only.
	m.log.Debug("initUser(): loading all team data")
	for _, v := range initData.Teams {
		m.Client.SetTeamId(v.Id)
		mmusers, _ := m.Client.GetProfiles(v.Id, "")
		t := &Team{Team: v, Users: mmusers.Data.(map[string]*model.User), Id: v.Id}
		mmchannels, _ := m.Client.GetChannels("")
		t.Channels = mmchannels.Data.(*model.ChannelList)
		mmchannels, _ = m.Client.GetMoreChannels("")
		t.MoreChannels = mmchannels.Data.(*model.ChannelList)
		m.OtherTeams = append(m.OtherTeams, t)
		if v.Name == m.Credentials.Team {
			m.Team = t
			m.log.Debugf("initUser(): found our team %s (id: %s)", v.Name, v.Id)
		}
		// add all users
		for k, v := range t.Users {
			m.Users[k] = v
		}
	}
	return nil
}
320980
func (fw *FirewallSSO) init(ctx context.Context) error {
	for _, net := range fw.Networks {
		err := net.init(ctx)
		if err != nil {
			return err
		}
	}
	return nil
}
1271780
func MustMongoDB(ctx context.Context) *mgo.Session {
	db, ok := MongoDB(ctx)
	if !ok {
		panic("not found mongoDB")
	}
	return db
}
1013712
func InitModels() {
	index := mgo.Index{
		Key:      []string{"username"},
		Unique:   true,
		DropDups: true,
	}

	db.EnsureIndex(mongoUsersCollectionName, index)

	// Removes users without valid 2 factor authentication after 3 days
	automaticUserExpiration := mgo.Index{
		Key:         []string{"expire"},
		ExpireAfter: time.Second * 3600 * 24 * 3,
		Background:  true,
	}
	db.EnsureIndex(mongoUsersCollectionName, automaticUserExpiration)

	avatarIndex := mgo.Index{
		Key:      []string{"hash"},
		Unique:   true,
		DropDups: true,
	}
	db.EnsureIndex(mongoAvatarFileCollectionName, avatarIndex)

	emailIndex := mgo.Index{
		Key: []string{"emailaddresses.emailaddress"},
	}
	db.EnsureIndex(mongoUsersCollectionName, emailIndex)
}
958517
func CloseMongoDB(ctx context.Context) context.Context {
	sesh, _ := MongoDB(ctx)
	if sesh == nil {
		fmt.Println("not found mongoDB")
	}
	sesh.Close()
	ctx = context.WithValue(ctx, mongodb(mongoDBName), nil)
	return ctx
}
491291
func (m *Machine) SetMongoPassword(password string) error {
	if !m.IsManager() {
		return errors.NotSupportedf("setting mongo password for non-controller machine %v", m)
	}
	return mongo.SetAdminMongoPassword(m.st.session, m.Tag().String(), password)
}
529266
func (m *Manager) Init() error {
	for _, manager := range m.managers {
		if err := manager.Init(); err != nil {
			return err
		}
	}
	return nil
}
189163
func (m *Module) Init(c *service.Config) {
	c.Start = func() {
		for _, p := range m.oauthConfigs {
			m.register(p)
		}
	}
}
1328854
func (a *Application) init(confFile string) {
	a.InitEBus()
	a.initRouter()
	a.initConf(confFile)

	// neo logger
	lvl, err := parseLogLevel(a.Conf.Neo.Logger.Level)
	if err != nil {
		log.Warn(err)
	} else {
		log.Level = lvl
	}

	// application logger
	lvl, err = parseLogLevel(a.Conf.App.Logger.Level)
	a.Logger = golog.GetLogger(a.Conf.App.Logger.Name)
	if err != nil {
		log.Warn(err)
	} else {
		a.Logger.Level = lvl
	}
}