Example #1
0
File: db.go Project: MG-RAST/AWE
func Initialize() (err error) {
	c := connection{}

	// test connection
	canDial := false
	for i := 0; i < DialAttempts; i++ {
		s, err := mgo.DialWithTimeout(conf.MONGODB_HOST, DialTimeout)
		if err == nil {
			s.Close()
			canDial = true
			break
		}
	}
	if !canDial {
		return errors.New(fmt.Sprintf("no reachable mongodb server(s) at %s", conf.MONGODB_HOST))
	}

	// get handle
	s, err := mgo.DialWithTimeout(conf.MONGODB_HOST, DbTimeout)
	if err != nil {
		return errors.New(fmt.Sprintf("no reachable mongodb server(s) at %s", conf.MONGODB_HOST))
	}
	c.Session = s
	c.DB = c.Session.DB(conf.MONGODB_DATABASE)
	if conf.MONGODB_USER != "" && conf.MONGODB_PASSWD != "" {
		c.DB.Login(conf.MONGODB_USER, conf.MONGODB_PASSWD)
	}
	Connection = c
	return
}
func setupDatabase() {
	session, _ := mgo.DialWithTimeout("localhost", time.Millisecond*500)
	fetcher.ConfigurationSettings = &mgoconfig.Configuration{Database: "steam_test", Key: "steam", Session: session}
	session.DB("steam_test").DropDatabase()

	session.DB("steam_test").C("configuration").Insert(bson.M{"id": "steam", "steam_api_key": "API KEY", "steam_id": "ID"})
	mongoDB = &database.MongoDB{Collection: session.DB("steam_test").C("games")}
	mongoDB.SetSession(session)
}
func getSession(databaseHost string) (*mgo.Session, error) {
	var session *mgo.Session
	var err error

	if session, err = mgo.DialWithTimeout(databaseHost, time.Second); err != nil {
		return nil, err
	}
	session.SetSocketTimeout(time.Second)
	session.SetSyncTimeout(time.Second)
	session.SetMode(mgo.Monotonic, true)
	return session, nil
}
Example #4
0
// Connect to mongo using addr, which must contain db name (mongodb://localhost/test_NAME)
// Be careful, database will be dropped.
func PrepareTestDb(addr string) (*mgo.Session, error) {
	s, err := mgo.DialWithTimeout(addr, 2*time.Second)
	if err != nil {
		return nil, err
	}
	// check if the default db has prefix "test_"
	if !strings.HasPrefix(s.DB("").Name, TestDbPrefix) {
		s.Close()
		return nil, fmt.Errorf("Default db name for test should be with prefix %s", TestDbPrefix)
	}
	// clean test db
	s.DB("").DropDatabase()
	return s, nil
}
Example #5
0
// GetDB returns a database connection.
func GetDB() (*mgo.Database, error) {
	if db == nil && session == nil {
		goserv.Logger.Info("Creating new session: ", conf.Config.MongoURI)
		ses, err := mgo.DialWithTimeout(conf.Config.MongoURI, 3*time.Second)
		if err != nil {
			goserv.Logger.Error("Could not connect to MongoDB: ", err)
			return nil, err
		}
		session := *ses
		newdb := session.DB(conf.Config.MongoDatabase)
		goserv.Logger.Infof("Connection made %s", newdb.Name)
		if err != nil {
			goserv.Logger.Fatal(err)
		}
		db = newdb
	}
	return db, nil
}
Example #6
0
func main() {
	session, err := mgo.DialWithTimeout("db", time.Minute)
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// Optional. Switch the session to a monotonic behavior
	session.SetMode(mgo.Monotonic, true)

	// Retrieve lines from STIF website.
	c := session.DB("dispotrains").C("stations")
	lines, err := client.GetAllLines()
	if err != nil {
		panic(err)
	}

	// Build the station database collection.
	stations := make(map[string]*storage.Station)
	for _, line := range lines {
		for _, station := range line.GetStations() {
			if _, ok := stations[strings.ToLower(station.Name)]; ok == true {
				for _, sLine := range station.Lines {
					stations[strings.ToLower(station.Name)].AttachLine(sLine)
				}
			} else {
				stations[strings.ToLower(station.Name)] = station
			}
		}
	}
	AddPositionToStations(stations)
	stationIndex := mgo.Index{
		Key:        []string{"name"},
		Background: true,
		DropDups:   true,
		Unique:     true,
	}
	err = c.EnsureIndex(stationIndex)
	if err != nil {
		panic(err)
	}
	for _, station := range stations {
		_, err = c.Upsert(bson.M{"name": station.Name}, &station)
		if err != nil {
			panic(err)
		}
	}

	// Build the lines database collection.
	job := &mgo.MapReduce{
		Map:    mapStationsToLines,
		Reduce: reduceLines,
		Out:    bson.M{"replace": "lines"},
	}
	_, err = c.Find(bson.M{}).MapReduce(job, nil)
	if err != nil {
		panic(err)
	}

	// Append the new statuses to the database log.
	c = session.DB("dispotrains").C("statuses")
	index := mgo.Index{
		Key:        []string{"state", "lastupdate", "elevator"},
		Background: true,
		DropDups:   true,
		Sparse:     true,
		Unique:     true,
	}
	err = c.EnsureIndex(index)
	if err != nil {
		panic(err)
	}
	for _, station := range stations {
		for _, elevator := range station.GetElevators() {
			bsonStatus := bson.M{
				"state":      elevator.Status.State,
				"lastupdate": elevator.Status.LastUpdate,
				"elevator":   elevator.ID,
			}
			err = c.Insert(bsonStatus)
			if err != nil && !mgo.IsDup(err) {
				panic(err)
			}
		}
	}
}