<commit_before>package mgo

import (
	"fmt"
	"labix.org/v2/mgo"
)

// DbHelperInitOptions contain initialization options for DbHelper constructor.
type DbHelperInitOptions struct {
	Safe *mgo.Safe // Safe mode set for DbHelper session.
}

// DbHelper is a convenience helper for systems that work with a single mongo DB.
// It creates a 'master' session, given the mgo url/db and init options and then
// uses its copies for db operations.
type DbHelper struct {
	dbName        string // used mongodb database name
	masterSession *mgo.Session
}

func NewDbHelper(mgoUrl string, dbName string, opts *DbHelperInitOptions) (d *DbHelper, err error) {
	d = new(DbHelper)
	d.masterSession, err = mgo.Dial(mgoUrl)
	if err != nil {
		return nil, fmt.Errorf("Cannot connect to mongo: '%s'", err)
	}
	d.masterSession.SetSafe(opts.Safe)
	d.dbName = dbName
	return d, nil
}

// c is a shortcut for getting a collection by name inside current database
func (db *DbHelper) c(collectionName string) (*mgo.Collection, *mgo.Session) {
	session := db.masterSession.New()
	return session.DB(db.dbName).C(collectionName), session
}

// s is a shortcut for creating a session for current database
func (db *DbHelper) s() *mgo.Session {
	return db.masterSession.New()
}

// cs is a shortcut for creating a collection using an existing session
func (db *DbHelper) cs(collectionName string, session *mgo.Session) *mgo.Collection {
	return session.DB(db.dbName).C(collectionName)
}<commit_msg>Changed mgo util func signature.<commit_after>package mgo

import (
	"fmt"
	"labix.org/v2/mgo"
)

// DbHelperInitOptions contain initialization options for DbHelper constructor.
type DbHelperInitOptions struct {
	Safe *mgo.Safe // Safe mode set for DbHelper session.
}

// DbHelper is a convenience helper for systems that work with a single mongo DB.
// It creates a 'master' session, given the mgo url/db and init options and then
// uses its copies for db operations.
type DbHelper struct {
	masterSession *mgo.Session
}

// Dial connects to the db using specified options and returns helper or error if dial fails.
func Dial(dialInfo *mgo.DialInfo, opts *DbHelperInitOptions) (d *DbHelper, err error) {
	d = new(DbHelper)
	d.masterSession, err = mgo.DialWithInfo(dialInfo)
	if err != nil {
		return nil, fmt.Errorf("Cannot connect to mongo: '%s'", err)
	}
	d.masterSession.SetSafe(opts.Safe)
	return d, nil
}

// C is a shortcut for getting a collection+session (master session copy) by name inside current database
func (db *DbHelper) C(collectionName string) (*mgo.Collection, *mgo.Session) {
	session := db.masterSession.New()
	return session.DB("").C(collectionName), session
}

// S is a shortcut for creating a session for current database
func (db *DbHelper) S() *mgo.Session {
	return db.masterSession.New()
}

// Cs is a shortcut for creating a collection using an existing session
func (db *DbHelper) Cs(collectionName string, session *mgo.Session) *mgo.Collection {
	return session.DB("").C(collectionName)
}
{'fraction_non_alphanumeric': 0.10163824807756604, 'fraction_numerical': 0.0006686726847208291, 'mean_word_length': 4.362007168458781, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '561472', 'n_tokens_mistral': 945, 'n_tokens_neox': 919, 'n_words': 345}