<commit_before>package main

import (
	"math/rand"
)

const (
	// ValidChars chars to consists of random string
	ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	// DefaultLen default random string length
	DefaultLen = 4
)

// URLServiceImpl main service of shortpath
type URLServiceImpl struct {
	dbs    *DBService
	length int
}

// NewURLServiceImpl return new UrlServiceImpl instance
func NewURLServiceImpl(dbs *DBService) *URLServiceImpl {
	usi := &URLServiceImpl{dbs: dbs, length: DefaultLen}
	return usi
}

// Shorten generate a short, non-repeat path maps to longurl
func (u *URLServiceImpl) Shorten(longurl string, length int) string {
	if length == 0 {
		u.SetLen(DefaultLen)
	} else {
		u.SetLen(length)
	}

	shortpath := generateRandomStr(u.GetLen())
	for u.Seek(shortpath) {
		shortpath = generateRandomStr(u.GetLen())
	}
	u.Put(longurl, shortpath)

	return shortpath
}

// Put save url and shortpath to db
func (u *URLServiceImpl) Put(longurl, shortpath string) {
	u.dbs.InsertShortpath(longurl, shortpath)
}

// GetShortpath get shortpath and url by shortpath from db
func (u *URLServiceImpl) GetShortpath(shortpath string) string {
	return u.dbs.QueryUrlRecord(shortpath)
}

// SetLen set current random string length
func (u *URLServiceImpl) SetLen(length int) {
	u.length = length
}

// GetLen return current random string length
func (u *URLServiceImpl) GetLen() int {
	return u.length
}

// Seek check if path has existed in db
func (u *URLServiceImpl) Seek(path string) bool {
	return u.dbs.CheckPath(path)
}

// generateRandomStr
func generateRandomStr(length int) string {
	shortURL := make([]byte, length)
	seed := make([]byte, length)
	charLength := len(ValidChars)

	_, err := rand.Read(seed)
	if err != nil {
		logq.Fatal(err)
	}

	for k, v := range seed {
		i := int(v)<commit_msg>logq -> logrus<commit_after>package main

import (
	"math/rand"

	"github.com/Sirupsen/logrus"
)

const (
	// ValidChars chars to consists of random string
	ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	// DefaultLen default random string length
	DefaultLen = 4
)

// URLServiceImpl main service of shortpath
type URLServiceImpl struct {
	dbs    *DBService
	length int
}

// NewURLServiceImpl return new UrlServiceImpl instance
func NewURLServiceImpl(dbs *DBService) *URLServiceImpl {
	usi := &URLServiceImpl{dbs: dbs, length: DefaultLen}
	return usi
}

// Shorten generate a short, non-repeat path maps to longurl
func (u *URLServiceImpl) Shorten(longurl string, length int) string {
	if length == 0 {
		u.SetLen(DefaultLen)
	} else {
		u.SetLen(length)
	}

	shortpath := generateRandomStr(u.GetLen())
	for u.Seek(shortpath) {
		shortpath = generateRandomStr(u.GetLen())
	}
	u.Put(longurl, shortpath)

	return shortpath
}

// Put save url and shortpath to db
func (u *URLServiceImpl) Put(longurl, shortpath string) {
	u.dbs.InsertShortpath(longurl, shortpath)
}

// GetShortpath get shortpath and url by shortpath from db
func (u *URLServiceImpl) GetShortpath(shortpath string) string {
	return u.dbs.QueryUrlRecord(shortpath)
}

// SetLen set current random string length
func (u *URLServiceImpl) SetLen(length int) {
	u.length = length
}

// GetLen return current random string length
func (u *URLServiceImpl) GetLen() int {
	return u.length
}

// Seek check if path has existed in db
func (u *URLServiceImpl) Seek(path string) bool {
	return u.dbs.CheckPath(path)
}

// generateRandomStr
func generateRandomStr(length int) string {
	shortURL := make([]byte, length)
	seed := make([]byte, length)
	charLength := len(ValidChars)

	_, err := rand.Read(seed)
	if err != nil {
		logrus.Fatal(err)
	}

	for k, v := range seed {
		i := int(v)
{'fraction_non_alphanumeric': 0.10485175202156334, 'fraction_numerical': 0.00646900269541779, 'mean_word_length': 4.4734513274336285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3037741', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1269, 'n_words': 365}