<commit_before>package tmongo

import (
	"errors"
	"fmt"
	"github.com/tolexo/aero/conf"
	"gopkg.in/mgo.v2"
)

// create the mongo connection string
func getMongoConnStr(container string) (string, string) {
	username := conf.String(container+".username", "")
	password := conf.String(container+".password", "")
	host := conf.String(container+".host", "")
	port := conf.String(container+".port", "")
	db := conf.String(container+".db", "")
	replicas := conf.String(container+".replicas", "")
	options := conf.String(container+".options", "")

	if port != "" {
		port = ":" + port
	}
	if replicas != "" {
		replicas = "," + replicas
	}
	if options != "" {
		options = "?" + options
	}
	auth := ""
	if username != "" || password != "" {
		auth = username + ":" + password + "@"
	}

	return fmt.Sprintf("mongodb://%s%s%s%s/%s%s", auth, host, port, replicas, db, options), db
}

// validate the container string
func validateContainer(container ...string) (db string, err error) {
	cLen := len(container)
	if cLen == 0 {
		container = append(container, "database.mongo")
	} else if cLen > 1 {
		err = errors.New("More than one container not supported")
		return
	}
	if conf.Exists(container[0]) == false {
		err = errors.New("mongo configuration missing")
		return
	}
	db = container[0]
	return
}

// create mongo connection
// TODO introduce parameter for connection additional settings like socket timeout
func GetMongoConn(container ...string) (sess *mgo.Session, mdb string, err error) {
	var db string
	if db, err = validateContainer(container...); err == nil {
		var conn string
		conn, mdb = getMongoConnStr(db)
		if mdb == "" {
			err = errors.New("mongo database name missing")
		}
		sess, err = mgo.Dial(conn)
		if err == nil {
			sess.SetMode(mgo.Monotonic, true)
		}
	}
	return
}<commit_msg>mgoTimeout changed Dial() to DialWithTimeout()<commit_after>package tmongo

import (
	"errors"
	"fmt"
	"github.com/tolexo/aero/conf"
	"gopkg.in/mgo.v2"
	"time"
)

// create the mongo connection string
func getMongoConnStr(container string) (string, string) {
	username := conf.String(container+".username", "")
	password := conf.String(container+".password", "")
	host := conf.String(container+".host", "")
	port := conf.String(container+".port", "")
	db := conf.String(container+".db", "")
	replicas := conf.String(container+".replicas", "")
	options := conf.String(container+".options", "")

	if port != "" {
		port = ":" + port
	}
	if replicas != "" {
		replicas = "," + replicas
	}
	if options != "" {
		options = "?" + options
	}
	auth := ""
	if username != "" || password != "" {
		auth = username + ":" + password + "@"
	}

	return fmt.Sprintf("mongodb://%s%s%s%s/%s%s", auth, host, port, replicas, db, options), db
}

// validate the container string
func validateContainer(container ...string) (db string, err error) {
	cLen := len(container)
	if cLen == 0 {
		container = append(container, "database.mongo")
	} else if cLen > 1 {
		err = errors.New("More than one container not supported")
		return
	}
	if conf.Exists(container[0]) == false {
		err = errors.New("mongo configuration missing")
		return
	}
	db = container[0]
	return
}

// create mongo connection
// TODO introduce parameter for connection additional settings like socket timeout
func GetMongoConn(container ...string) (sess *mgo.Session, mdb string, err error) {
	var db string
	if db, err = validateContainer(container...); err == nil {
		var conn string
		conn, mdb = getMongoConnStr(db)
		if mdb == "" {
			err = errors.New("mongo database name missing")
		}
		sess, err = mgo.DialWithTimeout(conn, 20*time.Minute)
		if err == nil {
			sess.SetMode(mgo.Monotonic, true)
		}
	}
	return
}
{'fraction_non_alphanumeric': 0.17012561441835064, 'fraction_numerical': 0.0032768978700163844, 'mean_word_length': 3.877496671105193, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4849980', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1247, 'n_words': 314}