<commit_before>package db

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/db/storage"
	"gopkg.in/mgo.v2"
)

const (
	DefaultRedisHost    = "127.0.0.1:6379"
	DefaultDatabaseHost = "127.0.0.1:27017"
	DefaultDatabaseName = "backstage"
)

var (
	redisPool *redis.Pool
)

type Storage struct {
	*storage.Storage
}

func conn() (*storage.Storage, error) {
	databaseHost, _ := config.GetString("database:host")
	if databaseHost == "" {
		databaseHost = DefaultDatabaseHost
	}

	databaseName, _ := config.GetString("database:name")
	if databaseName == "" {
		databaseName = DefaultDatabaseName
	}

	return storage.Open(databaseHost, databaseName)
}

func Conn() (*Storage, error) {
	var (
		strg Storage
		err  error
	)

	strg.Storage, err = conn()
	return &strg, err
}

func GetRedisPool() *redis.Pool {
	if redisPool != nil {
		return redisPool
	}
	netloc, _ := config.GetString("redis:host")

	if netloc == "" {
		netloc = DefaultRedisHost
	}

	password, _ := config.GetString("redis:password")
	redisNumber, _ := config.GetInt("redis:number")

	pool := &redis.Pool{
		MaxActive:   2,
		MaxIdle:     2,
		IdleTimeout: 0,
		Wait:        true,
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial("tcp", netloc)
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := conn.Do("AUTH", password); err != nil {
					conn.Close()
					return nil, err
				}
			}

			if redisNumber > 0 {
				if _, err := conn.Do("SELECT", redisNumber); err != nil {
					conn.Close()
					return nil, err
				}
			}

			return conn, nil
		},
	}
	redisPool = pool
	return redisPool
}

func GetRedis() redis.Conn {
	pool := GetRedisPool()
	return pool.Get()
}

func (storage *Storage) Services() *storage.Collection {
	subdomainIndex := mgo.Index{Key: []string{"subdomain"}, Unique: true}
	collection := storage.Collection("services")
	collection.EnsureIndex(subdomainIndex)
	return collection
}

func (storage *Storage) Users() *storage.Collection {
	emailIndex := mgo.Index{Key: []string{"email"}, Unique: true}
	collection := storage.Collection("users")
	collection.EnsureIndex(emailIndex)
	return collection
}

func (storage *Storage) Teams() *storage.Collection {
	nameIndex := mgo.Index{Key: []string{"name"}, Unique: true}
	aliasIndex := mgo.Index{Key: []string{"alias"}, Unique: true}
	collection := storage.Collection("teams")
	collection.EnsureIndex(nameIndex)
	collection.EnsureIndex(aliasIndex)
	return collection
}

func (storage *Storage) Tokens(key string, expires int, data map[string]interface{}) {
	addHCache(key, expires, data)
}

func (storage *Storage) GetTokenValue(key string) ([]interface{}, error) {
	return getHCache(key)
}

func (storage *Storage) DeleteToken(key string) (interface{}, error) {
	return delCache(key)
}

func delCache(key string) (interface{}, error) {<commit_msg>Block the session while creating the indexes.<commit_after>package db

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/db/storage"
	"gopkg.in/mgo.v2"
)

const (
	DefaultRedisHost    = "127.0.0.1:6379"
	DefaultDatabaseHost = "127.0.0.1:27017"
	DefaultDatabaseName = "backstage"
)

var (
	redisPool *redis.Pool
)

type Storage struct {
	*storage.Storage
}

func conn() (*storage.Storage, error) {
	databaseHost, _ := config.GetString("database:host")
	if databaseHost == "" {
		databaseHost = DefaultDatabaseHost
	}

	databaseName, _ := config.GetString("database:name")
	if databaseName == "" {
		databaseName = DefaultDatabaseName
	}

	return storage.Open(databaseHost, databaseName)
}

func Conn() (*Storage, error) {
	var (
		strg Storage
		err  error
	)

	strg.Storage, err = conn()
	return &strg, err
}

func GetRedisPool() *redis.Pool {
	if redisPool != nil {
		return redisPool
	}
	netloc, _ := config.GetString("redis:host")

	if netloc == "" {
		netloc = DefaultRedisHost
	}

	password, _ := config.GetString("redis:password")
	redisNumber, _ := config.GetInt("redis:number")

	pool := &redis.Pool{
		MaxActive:   2,
		MaxIdle:     2,
		IdleTimeout: 0,
		Wait:        true,
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial("tcp", netloc)
			if err != nil {
				return nil, err
			}
			if password != "" {
				if _, err := conn.Do("AUTH", password); err != nil {
					conn.Close()
					return nil, err
				}
			}

			if redisNumber > 0 {
				if _, err := conn.Do("SELECT", redisNumber); err != nil {
					conn.Close()
					return nil, err
				}
			}

			return conn, nil
		},
	}
	redisPool = pool
	return redisPool
}

func GetRedis() redis.Conn {
	pool := GetRedisPool()
	return pool.Get()
}

func (storage *Storage) Services() *storage.Collection {
	subdomainIndex := mgo.Index{Key: []string{"subdomain"}, Unique: true, Background: false}
	collection := storage.Collection("services")
	collection.EnsureIndex(subdomainIndex)
	return collection
}

func (storage *Storage) Users() *storage.Collection {
	emailIndex := mgo.Index{Key: []string{"email"}, Unique: true, Background: false}
	collection := storage.Collection("users")
	collection.EnsureIndex(emailIndex)
	return collection
}

func (storage *Storage) Teams() *storage.Collection {
	nameIndex := mgo.Index{Key: []string{"name"}, Unique: true, Background: false}
	collection := storage.Collection("teams")
	collection.EnsureIndex(nameIndex)
	return collection
}

func (storage *Storage) Tokens(key string, expires int, data map[string]interface{}) {
	addHCache(key, expires, data)
}

func (storage *Storage) GetTokenValue(key string) ([]interface{}, error) {
	return getHCache(key)
}

func (storage *Storage) DeleteToken(key string) (interface{}, error) {
	return delCache(key)
}

func delCache(key string) (interface{}, error) {
{'fraction_non_alphanumeric': 0.14848379226211222, 'fraction_numerical': 0.009062391077030324, 'mean_word_length': 4.165616561656166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7434640', 'n_tokens_mistral': 2147, 'n_tokens_neox': 1985, 'n_words': 452}