<commit_before>// Copyright 2016 IBM Corporation
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package store

import (
	"fmt"
	"strings"
	"sync"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/garyburd/redigo/redis"
	"github.com/rcrowley/go-metrics"

	"github.com/amalgam8/amalgam8/pkg/auth"
	"github.com/amalgam8/amalgam8/registry/utils/database"
	"github.com/amalgam8/amalgam8/registry/utils/logging"
)

type externalConfig struct {
	defaultTTL time.Duration
	minimumTTL time.Duration
	maximumTTL time.Duration

	namespaceCapacity int

	store    string
	address  string
	password string
	database database.Database
}

type externalFactory struct {
	conf *externalConfig
	pool *redis.Pool
}

func newExternalFactory(conf *externalConfig) CatalogFactory {
	if conf.store == "redis" {
		pool := &redis.Pool{
			MaxIdle:     10,
			IdleTimeout: 240 * time.Second,
			Dial: func() (redis.Conn, error) {
				c, err := redis.Dial("tcp", conf.address)
				if err != nil {
					return nil, err
				}
				if _, err := c.Do("AUTH", conf.password); err != nil {
					c.Close()
					return nil, err
				}
				return c, err
			},
			TestOnBorrow: func(c redis.Conn, t time.Time) error {
				if time.Since(t) < time.Minute {
					return nil
				}
				_, err := c.Do("PING")
				return err
			},
		}
		return &externalFactory{conf: conf, pool: pool}
	}
	return &externalFactory{conf: conf}
}

func (f *externalFactory) CreateCatalog(namespace auth.Namespace) (Catalog, error) {
	return newExternalCatalog(f.conf, namespace, f.pool)
}

type externalCatalog struct {
	conf      *externalConfig
	logger    *log.Entry<commit_msg>Modified Redis to only send AUTH when a password has been provided<commit_after>// Copyright 2016 IBM Corporation
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package store

import (
	"fmt"
	"strings"
	"sync"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/garyburd/redigo/redis"
	"github.com/rcrowley/go-metrics"

	"github.com/amalgam8/amalgam8/pkg/auth"
	"github.com/amalgam8/amalgam8/registry/utils/database"
	"github.com/amalgam8/amalgam8/registry/utils/logging"
)

type externalConfig struct {
	defaultTTL time.Duration
	minimumTTL time.Duration
	maximumTTL time.Duration

	namespaceCapacity int

	store    string
	address  string
	password string
	database database.Database
}

type externalFactory struct {
	conf *externalConfig
	pool *redis.Pool
}

func newExternalFactory(conf *externalConfig) CatalogFactory {
	if conf.store == "redis" {
		pool := &redis.Pool{
			MaxIdle:     10,
			IdleTimeout: 240 * time.Second,
			Dial: func() (redis.Conn, error) {
				c, err := redis.Dial("tcp", conf.address)
				if err != nil {
					return nil, err
				}
				if conf.password != "" {
					if _, err := c.Do("AUTH", conf.password); err != nil {
						c.Close()
						return nil, err
					}
				}
				return c, nil
			},
			TestOnBorrow: func(c redis.Conn, t time.Time) error {
				if time.Since(t) < time.Minute {
					return nil
				}
				_, err := c.Do("PING")
				return err
			},
		}
		return &externalFactory{conf: conf, pool: pool}
	}
	return &externalFactory{conf: conf}
}

func (f *externalFactory) CreateCatalog(namespace auth.Namespace) (Catalog, error) {
	return newExternalCatalog(f.conf, namespace, f.pool)
}

type externalCatalog struct {
	conf      *externalConfig
	logger    *log.Entry
{'fraction_non_alphanumeric': 0.10997267759562841, 'fraction_numerical': 0.008652094717668488, 'mean_word_length': 3.2691933916423714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3727169', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1416, 'n_words': 422}