<commit_before>package db

import (
	"fmt"
	"time"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"github.com/DVI-GI-2017/Jira__backend/models"
	"github.com/DVI-GI-2017/Jira__backend/configs"
)

type MongoConnection struct {
	originalSession *mgo.Session
}

func NewDBConnection(mongo *configs.Mongo) (conn *MongoConnection) {
	conn = new(MongoConnection)
	conn.createConnection(mongo)
	return
}

func (c *MongoConnection) createConnection(mongo *configs.Mongo) (err error) {
	fmt.Println("Connecting to local mongo server....")

	c.originalSession, err = mgo.Dial(mongo.URL())

	if err != nil {
		return
	}

	defer c.CloseConnection()

	c.originalSession.SetMode(mgo.Monotonic, true)

	// Drop Database
	if mongo.Drop {
		err = c.originalSession.DB(mongo.Db).DropDatabase()
		if err != nil {
			panic(err)
		}
	}

	// TODO: Init several collections or remove they from config?
	users := c.originalSession.DB(mongo.Db).C(mongo.Collections[0])

	// Index
	index := mgo.Index{
		Key:        []string{"first_name", "updated_at"},
		Unique:     true,
		DropDups:   true,
		Background: true,
		Sparse:     true,
	}

	err = users.EnsureIndex(index)
	if err != nil {
		panic(err)
	}

	// Insert Datas
	err = users.Insert(&FakeUsers[0])

	if err != nil {
		panic(err)
	}

	// Query One
	result := models.User{}<commit_msg>add drop and get<commit_after>package db

import (
	"fmt"
	"time"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"github.com/DVI-GI-2017/Jira__backend/models"
	"github.com/DVI-GI-2017/Jira__backend/configs"
)

type MongoConnection struct {
	originalSession *mgo.Session
}

func NewDBConnection(mongo *configs.Mongo) (conn *MongoConnection) {
	conn = new(MongoConnection)
	conn.createConnection(mongo)
	return
}

func (c *MongoConnection) DropDataBase(mongo *configs.Mongo) (err error) {
	if mongo.Drop {
		err = c.originalSession.DB(mongo.Db).DropDatabase()
		if err != nil {
			return
		}
	}

	return nil
}

func (c *MongoConnection) GetCollection(mongo *configs.Mongo) (collection *mgo.Collection) {
	return c.originalSession.DB(mongo.Db).C(mongo.Collections[0])
}

func (c *MongoConnection) createConnection(mongo *configs.Mongo) (err error) {
	fmt.Println("Connecting to local mongo server....")

	c.originalSession, err = mgo.Dial(mongo.URL())

	if err != nil {
		return
	}

	defer c.CloseConnection()

	c.originalSession.SetMode(mgo.Monotonic, true)

	// TODO: Init several collections or remove they from config?
	users := c.originalSession.DB(mongo.Db).C(mongo.Collections[0])

	// Index
	index := mgo.Index{
		Key:        []string{"first_name", "updated_at"},
		Unique:     true,
		DropDups:   true,
		Background: true,
		Sparse:     true,
	}

	err = users.EnsureIndex(index)
	if err != nil {
		panic(err)
	}

	// Insert Datas
	err = users.Insert(&FakeUsers[0])

	if err != nil {
		panic(err)
	}

	// Query One
	result := models.User{}
{'fraction_non_alphanumeric': 0.14481065918653577, 'fraction_numerical': 0.00876577840112202, 'mean_word_length': 3.7869127516778525, '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': '3768519', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1090, 'n_words': 216}