<commit_before>package infrastructure

import (
	"log"
	"os"

	"github.com/marcusolsson/goddd/domain/location"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type locationRepositoryMongoDB struct {
}

func (r *locationRepositoryMongoDB) Store(l location.Location) {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}
	defer session.Close()

	c := session.DB("app30695645").C("locations")
	err = c.Insert(&l)

	if err != nil {
		log.Fatal(err)
	}
}

func (r *locationRepositoryMongoDB) Find(locode location.UNLocode) (location.Location, error) {
	return location.Location{}, nil
}

func (r *locationRepositoryMongoDB) FindAll() []location.Location {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}
	defer session.Close()

	c := session.DB("app30695645").C("locations")

	var result []location.Location
	err = c.Find(bson.M{}).All(&result)

	if err != nil {
		log.Fatal(err)
	}

	return result
}

func ensureUNLocodeIndex() {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}

	defer session.Close()

	index := mgo.Index{
		Key:    []string{"unlocode"},
		Unique: true,
	}

	c := session.DB("app30695645").C("locations")
	err = c.EnsureIndex(index)

	if err != nil {
		panic(err)
	}
}

func NewLocationRepositoryMongoDB() location.LocationRepository {

	r := &locationRepositoryMongoDB{}

	ensureUNLocodeIndex()

	r.Store(location.Stockholm)
	r.Store(location.Hamburg)
	r.Store(location.Chicago)

	return r
}<commit_msg>Drop duplicate locations in mongodb.<commit_after>package infrastructure

import (
	"log"
	"os"

	"github.com/marcusolsson/goddd/domain/location"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

type locationRepositoryMongoDB struct {
}

func (r *locationRepositoryMongoDB) Store(l location.Location) {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}
	defer session.Close()

	c := session.DB("app30695645").C("locations")
	err = c.Insert(&l)

	if err != nil {
		log.Fatal(err)
	}
}

func (r *locationRepositoryMongoDB) Find(locode location.UNLocode) (location.Location, error) {
	return location.Location{}, nil
}

func (r *locationRepositoryMongoDB) FindAll() []location.Location {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}
	defer session.Close()

	c := session.DB("app30695645").C("locations")

	var result []location.Location
	err = c.Find(bson.M{}).All(&result)

	if err != nil {
		log.Fatal(err)
	}

	return result
}

func ensureUNLocodeIndex() {
	session, err := mgo.Dial(os.Getenv("MONGOHQ_URL"))

	if err != nil {
		panic(err)
	}

	defer session.Close()

	index := mgo.Index{
		Key:      []string{"unlocode"},
		Unique:   true,
		DropDups: true,
	}

	c := session.DB("app30695645").C("locations")
	err = c.EnsureIndex(index)

	if err != nil {
		panic(err)
	}
}

func NewLocationRepositoryMongoDB() location.LocationRepository {

	r := &locationRepositoryMongoDB{}

	ensureUNLocodeIndex()

	r.Store(location.Stockholm)
	r.Store(location.Hamburg)
	r.Store(location.Chicago)

	return r
}
{'fraction_non_alphanumeric': 0.15918630933161124, 'fraction_numerical': 0.016790442363577657, 'mean_word_length': 4.378472222222222, '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': '6241994', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1187, 'n_words': 218}