<commit_before>package db

import (
	"encoding/json"
	"fmt"

	"gopkg.in/redis.v3"
)

const (
	//Dockyard Data Index
	GLOBAL_REPOSITORY_INDEX = "GLOBAL_REPOSITORY_INDEX"
	GLOBAL_IMAGE_INDEX      = "GLOBAL_IMAGE_INDEX"
	GLOBAL_TARSUM_INDEX     = "GLOBAL_TARSUM_INDEX"
	GLOBAL_TAG_INDEX        = "GLOBAL_TAG_INDEX"
	GLOBAL_COMPOSE_INDEX    = "GLOBAL_COMPOSE_INDEX"
	//Sail Data Index
	GLOBAL_USER_INDEX         = "GLOBAL_USER_INDEX"
	GLOBAL_ORGANIZATION_INDEX = "GLOBAL_ORGANIZATION_INDEX"
	GLOBAL_TEAM_INDEX         = "GLOBAL_TEAM_INDEX"
	//Wharf Data Index
	GLOBAL_ADMIN_INDEX = "GLOBAL_ADMIN_INDEX"
	GLOBAL_LOG_INDEX   = "GLOBAL_LOG_INDEX"
)

/*
  [user] : USER-(username)
	[organization] : ORG-(org)
	[team] : TEAM-(org)-(team)
	[repository] : REPO-(namespace)-(repo)
	[image] : IMAGE-(imageId)
	[tag] : TAG-(namespace)-(repo)-(tag)
	[compose] : COMPOSE-(namespace)-(compose)
	[admin] : ADMIN-(username)
	[log] : LOG-(object)
	[lock] : LOCK-(object)
*/

var (
	Client *redis.Client
)

func Key(object string, keys ...string) string {
	switch object {
	case "USER" || "user":
		return fmt.Sprint("USER-%s", keys[0])
	case "ORG" || "ORGANIZATION" || "org" || "organization":
		return fmt.Sprintf("ORG-%s", keys[0])
	case "TEAM" || "team":
		return fmt.Sprintf("ORG-%s-%s", keys[0], keys[1])
	case "REPO" || "REPOSITORY" || "repo" || "repository":
		return fmt.Sprintf("REPO-%s-%s", keys[0], keys[1])
	case "IMAGE" || "image":
		return fmt.Sprintf("IMAGE-%s", keys[0])
	case "TAG" || "tag":
		return fmt.Sprintf("TAG-%s-%s-%s", keys[0], keys[1], keys[2])
	case "COMPOSE" || "compose":
		return fmt.Sprintf("COMPOSE-%s-%s", keys[0]-kyes[1])
	case "ADMIN" || "admin":
		return fmt.Sprintf("ADMIN-%s", keys[0])
	case "LOG" || "log":
		return fmt.Sprintf("LOG-%s", keys[0])
	case "LOCK" || "lock":
		return fmt.Sprintf("LOCK-%s", keys[0])
	default:
		return ""
	}
}

func InitDB(addr, passwd string, db int64) error {
	Client = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: <PASSWORD>,
		DB:       db,
	})

	if _, err := Client.Ping().Result(); err != nil {
		return err
	} else {
		return nil
	}
}

func Save(obj interface{}, key string) (err error) {
	result, err := json.Marshal(&obj)
	if err != nil {
		return err
	}

	if _, err := Client.Set(key, string(result), 0).Result(); err != nil {
		return err
	}

	return nil
}<commit_msg>Fix the key function<commit_after>package db

import (
	"encoding/json"
	"fmt"

	"gopkg.in/redis.v3"
)

const (
	//Dockyard Data Index
	GLOBAL_REPOSITORY_INDEX = "GLOBAL_REPOSITORY_INDEX"
	GLOBAL_IMAGE_INDEX      = "GLOBAL_IMAGE_INDEX"
	GLOBAL_TARSUM_INDEX     = "GLOBAL_TARSUM_INDEX"
	GLOBAL_TAG_INDEX        = "GLOBAL_TAG_INDEX"
	GLOBAL_COMPOSE_INDEX    = "GLOBAL_COMPOSE_INDEX"
	//Sail Data Index
	GLOBAL_USER_INDEX         = "GLOBAL_USER_INDEX"
	GLOBAL_ORGANIZATION_INDEX = "GLOBAL_ORGANIZATION_INDEX"
	GLOBAL_TEAM_INDEX         = "GLOBAL_TEAM_INDEX"
	//Wharf Data Index
	GLOBAL_ADMIN_INDEX = "GLOBAL_ADMIN_INDEX"
	GLOBAL_LOG_INDEX   = "GLOBAL_LOG_INDEX"
)

/*
  [user] : USER-(username)
	[organization] : ORG-(org)
	[team] : TEAM-(org)-(team)
	[repository] : REPO-(namespace)-(repo)
	[image] : IMAGE-(imageId)
	[tag] : TAG-(namespace)-(repo)-(tag)
	[compose] : COMPOSE-(namespace)-(compose)
	[admin] : ADMIN-(username)
	[log] : LOG-(object)
	[lock] : LOCK-(object)
*/

var (
	Client *redis.Client
)

func Key(object string, keys ...string) (result string) {
	switch object {
	case "USER":
	case "user":
		result = fmt.Sprint("USER-%s", keys[0])
	case "ORG":
	case "ORGANIZATION":
	case "org":
	case "organization":
		result = fmt.Sprintf("ORG-%s", keys[0])
	case "TEAM":
	case "team":
		result = fmt.Sprintf("ORG-%s-%s", keys[0], keys[1])
	case "REPO":
	case "REPOSITORY":
	case "repo":
	case "repository":
		result = fmt.Sprintf("REPO-%s-%s", keys[0], keys[1])
	case "IMAGE":
	case "image":
		result = fmt.Sprintf("IMAGE-%s", keys[0])
	case "TAG":
	case "tag":
		result = fmt.Sprintf("TAG-%s-%s-%s", keys[0], keys[1], keys[2])
	case "COMPOSE":
	case "compose":
		result = fmt.Sprintf("COMPOSE-%s-%s", keys[0], keys[1])
	case "ADMIN":
	case "admin":
		result = fmt.Sprintf("ADMIN-%s", keys[0])
	case "LOG":
	case "log":
		result = fmt.Sprintf("LOG-%s", keys[0])
	case "LOCK":
	case "lock":
		result = fmt.Sprintf("LOCK-%s", keys[0])
	default:
		result = ""
	}

	return result
}

func InitDB(addr, passwd string, db int64) error {
	Client = redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: <PASSWORD>,
		DB:       db,
	})

	if _, err := Client.Ping().Result(); err != nil {
		return err
	} else {
		return nil
	}
}

func Save(obj interface{}, key string) (err error) {
	result, err := json.Marshal(&obj)
	if err != nil {
		return err
	}

	if _, err := Client.Set(key, string(result), 0).Result(); err != nil {
		return err
	}

	return nil
}
{'fraction_non_alphanumeric': 0.1813449927128878, 'fraction_numerical': 0.007911721840516344, 'mean_word_length': 3.9730848861283645, 'pattern_counts': {'":': 34, '<': 5, '<?xml version=': 0, '>': 5, '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': '150240', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1905, 'n_words': 397}