<commit_before>package models

import (
	"fmt"
	"koding/db/mongodb/modelhelper"
	"strconv"
	"time"

	"github.com/koding/cache"
)

var (
	channelCache        map[int64]*Channel
	channelMessageCache map[int64]*ChannelMessage
	accountCache        map[int64]*Account
	oldAccountCache     map[string]int64
	secretNamesCache    cache.Cache
)

func init() {
	// those are not thread safe!!!!
	channelCache = make(map[int64]*Channel)
	channelMessageCache = make(map[int64]*ChannelMessage)
	accountCache = make(map[int64]*Account)
	oldAccountCache = make(map[string]int64)
	secretNamesCache = cache.NewMemoryWithTTL(time.Second * 10)
}

/////////// ChannelMessage

func ChannelMessageById(id int64) (*ChannelMessage, error) {
	if channelMessage, ok := channelMessageCache[id]; ok {
		return channelMessage, nil
	}

	// todo add caching here
	c := NewChannelMessage()
	if err := c.ById(id); err != nil {
		return nil, err
	}

	// add channel to cache
	channelMessageCache[c.Id] = c

	return c, nil
}

/////////// Channel

// todo fix!!
// this will fail when a channel marked as troll
func ChannelById(id int64) (*Channel, error) {
	if channel, ok := channelCache[id]; ok {
		return channel, nil
	}

	// todo add caching here
	c := NewChannel()
	if err := c.ById(id); err != nil {
		return nil, err
	}

	// add channel to cache
	channelCache[c.Id] = c

	return c, nil
}

func ChannelsByIds(ids []int64) ([]*Channel, error) {
	channels := make([]*Channel, len(ids))
	if len(channels) == 0 {
		return channels, nil
	}

	for i, id := range ids {
		channel, err := ChannelById(id)
		if err != nil {
			return channels, err
		}
		channels[i] = channel
	}

	return channels, nil
}

///// Account

func FetchAccountOldIdByIdFromCache(id int64) (string, error) {
	if a, ok := accountCache[id]; ok && a != nil {
		return a.OldId, nil
	}

	account, err := ResetAccountCache(id)
	if err != nil {
		return "", err
	}

	return account.OldId, nil
}

func FetchAccountFromCache(id int64) (*Account, error) {
	if a, ok := accountCache[id]; ok && a != nil {
		return a, nil
	}

	return ResetAccountCache(id)
}

func ResetAccountCache(id int64) (*Account, error) {
	account, err := FetchAccountById(id)
	if err != nil {
		return nil, err
	}

	SetAccountToCache(account)

	return account, nil
}

func FetchAccountOldsIdByIdsFromCache(ids []int64) ([]string, error) {
	oldIds := make([]string, len(ids))
	if len(oldIds) == 0 {
		return oldIds, nil
	}

	for i, id := range ids {
		oldId, err := FetchAccountOldIdByIdFromCache(id)
		if err != nil {
			return oldIds, err
		}
		oldIds[i] = oldId
	}

	return oldIds, nil
}

func SetAccountToCache(a *Account) {
	if a == nil {
		return
	}

	if a.Id == 0 {
		fmt.Println("account id is empty")
		return
	}

	accountCache[a.Id] = a
}

func AccountIdByOldId(oldId, nick string) (int64, error) {
	if id, ok := oldAccountCache[oldId]; ok {
		return id, nil
	}

	a := NewAccount()
	a.OldId = oldId
	a.Nick = nick
	if err := a.FetchOrCreate(); err != nil {
		return 0, err
	}

	oldAccountCache[oldId] = a.Id

	return a.Id, nil
}

////// SecretNames /////

func SecretNamesByChannelId(channelId int64) ([]string, error) {
	key := strconv.FormatInt(channelId, 10)<commit_msg>Social: Add FetchAccountIdByOldId function just for querying account id existence in cache<commit_after>package models

import (
	"fmt"
	"koding/db/mongodb/modelhelper"
	"strconv"
	"time"

	"github.com/koding/cache"
)

var (
	channelCache        map[int64]*Channel
	channelMessageCache map[int64]*ChannelMessage
	accountCache        map[int64]*Account
	oldAccountCache     map[string]int64
	secretNamesCache    cache.Cache
)

func init() {
	// those are not thread safe!!!!
	channelCache = make(map[int64]*Channel)
	channelMessageCache = make(map[int64]*ChannelMessage)
	accountCache = make(map[int64]*Account)
	oldAccountCache = make(map[string]int64)
	secretNamesCache = cache.NewMemoryWithTTL(time.Second * 10)
}

/////////// ChannelMessage

func ChannelMessageById(id int64) (*ChannelMessage, error) {
	if channelMessage, ok := channelMessageCache[id]; ok {
		return channelMessage, nil
	}

	// todo add caching here
	c := NewChannelMessage()
	if err := c.ById(id); err != nil {
		return nil, err
	}

	// add channel to cache
	channelMessageCache[c.Id] = c

	return c, nil
}

/////////// Channel

// todo fix!!
// this will fail when a channel marked as troll
func ChannelById(id int64) (*Channel, error) {
	if channel, ok := channelCache[id]; ok {
		return channel, nil
	}

	// todo add caching here
	c := NewChannel()
	if err := c.ById(id); err != nil {
		return nil, err
	}

	// add channel to cache
	channelCache[c.Id] = c

	return c, nil
}

func ChannelsByIds(ids []int64) ([]*Channel, error) {
	channels := make([]*Channel, len(ids))
	if len(channels) == 0 {
		return channels, nil
	}

	for i, id := range ids {
		channel, err := ChannelById(id)
		if err != nil {
			return channels, err
		}
		channels[i] = channel
	}

	return channels, nil
}

///// Account

func FetchAccountOldIdByIdFromCache(id int64) (string, error) {
	if a, ok := accountCache[id]; ok && a != nil {
		return a.OldId, nil
	}

	account, err := ResetAccountCache(id)
	if err != nil {
		return "", err
	}

	return account.OldId, nil
}

func FetchAccountFromCache(id int64) (*Account, error) {
	if a, ok := accountCache[id]; ok && a != nil {
		return a, nil
	}

	return ResetAccountCache(id)
}

func ResetAccountCache(id int64) (*Account, error) {
	account, err := FetchAccountById(id)
	if err != nil {
		return nil, err
	}

	SetAccountToCache(account)

	return account, nil
}

func FetchAccountOldsIdByIdsFromCache(ids []int64) ([]string, error) {
	oldIds := make([]string, len(ids))
	if len(oldIds) == 0 {
		return oldIds, nil
	}

	for i, id := range ids {
		oldId, err := FetchAccountOldIdByIdFromCache(id)
		if err != nil {
			return oldIds, err
		}
		oldIds[i] = oldId
	}

	return oldIds, nil
}

func SetAccountToCache(a *Account) {
	if a == nil {
		return
	}

	if a.Id == 0 {
		fmt.Println("account id is empty")
		return
	}

	accountCache[a.Id] = a
}

func AccountIdByOldId(oldId, nick string) (int64, error) {
	if id, ok := oldAccountCache[oldId]; ok {
		return id, nil
	}

	a := NewAccount()
	a.OldId = oldId
	a.Nick = nick
	if err := a.FetchOrCreate(); err != nil {
		return 0, err
	}

	oldAccountCache[oldId] = a.Id

	return a.Id, nil
}

func FetchAccountIdByOldId(oldId string) int64 {
	id, ok := oldAccountCache[oldId]
	if !ok {
		return 0
	}

	return id
}

////// SecretNames /////

func SecretNamesByChannelId(channelId int64) ([]string, error) {
	key := strconv.FormatInt(channelId, 10)
{'fraction_non_alphanumeric': 0.13976527968297517, 'fraction_numerical': 0.013260173754000914, 'mean_word_length': 3.6081460674157304, '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': '4656095', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2328, 'n_words': 641}