<commit_before>package registry

import (
	"encoding/json"
	"fmt"
	"os"
	"path"
	"strings"

	"code.google.com/p/go-uuid/uuid"
	"github.com/coreos/go-etcd/etcd"

	"github.com/wantedly/risu/schema"
)

const (
	DefaultMachines  = "http://172.17.8.101:4001"
	DefaultKeyPrefix = "/risu/"
)

type EtcdRegistry struct {
	etcd      etcd.Client
	keyPrefix string
}

func NewEtcdRegistry(machines string) Registry {
	if os.Getenv("RISU_ETCD_MACHINES") != "" {
		machines = os.Getenv("RISU_ETCD_MACHINES")
	}

	if machines == "" {
		machines = DefaultMachines
	}

	m := strings.Split(machines, ",")
	etcdClient := *etcd.NewClient(m)
	return &EtcdRegistry{etcdClient, DefaultKeyPrefix}
}

func (r *EtcdRegistry) Create(opts schema.BuildCreateOpts) (schema.Build, error) {
	build := schema.NewBuild(&opts)
	j, err := marshal(build)
	if err != nil {
		return build, err
	}

	key := path.Join(r.keyPrefix, build.ID.String())
	_, err = r.etcd.Create(key, string(j), 0)
	if err != nil {
		return build, err
	}

	return build, nil
}

func (r *EtcdRegistry) Set(build schema.Build, opts schema.BuildUpdateOpts) error {
	build = schema.UpdateBuild(build, &opts)
	j, err := marshal(build)
	if err != nil {
		return err
	}

	key := path.Join(r.keyPrefix, build.ID.String())
	_, err = r.etcd.Set(key, string(j), 0)
	if err != nil {
		return err
	}

	return nil
}

func (r *EtcdRegistry) Get(id uuid.UUID) (schema.Build, error) {
	key := path.Join(r.keyPrefix, id.String())<commit_msg>registry: Delete expired build data at etcd backend registry<commit_after>package registry

import (
	"encoding/json"
	"fmt"
	"os"
	"path"
	"strings"

	"code.google.com/p/go-uuid/uuid"
	"github.com/coreos/go-etcd/etcd"

	"github.com/wantedly/risu/schema"
)

const (
	DefaultMachines  = "http://172.17.8.101:4001"
	DefaultKeyPrefix = "/risu/"
)

type EtcdRegistry struct {
	etcd      etcd.Client
	keyPrefix string
}

func NewEtcdRegistry(machines string) Registry {
	if os.Getenv("RISU_ETCD_MACHINES") != "" {
		machines = os.Getenv("RISU_ETCD_MACHINES")
	}

	if machines == "" {
		machines = DefaultMachines
	}

	m := strings.Split(machines, ",")
	etcdClient := *etcd.NewClient(m)
	return &EtcdRegistry{etcdClient, DefaultKeyPrefix}
}

func (r *EtcdRegistry) Create(opts schema.BuildCreateOpts) (schema.Build, error) {
	build := schema.NewBuild(&opts)
	j, err := marshal(build)
	if err != nil {
		return build, err
	}

	key := path.Join(r.keyPrefix, build.ID.String())
	_, err = r.etcd.Create(key, string(j), DefaultExpireSeconds)
	if err != nil {
		return build, err
	}

	return build, nil
}

func (r *EtcdRegistry) Set(build schema.Build, opts schema.BuildUpdateOpts) error {
	build = schema.UpdateBuild(build, &opts)
	j, err := marshal(build)
	if err != nil {
		return err
	}

	key := path.Join(r.keyPrefix, build.ID.String())
	_, err = r.etcd.Set(key, string(j), DefaultExpireSeconds)
	if err != nil {
		return err
	}

	return nil
}

func (r *EtcdRegistry) Get(id uuid.UUID) (schema.Build, error) {
	key := path.Join(r.keyPrefix, id.String())
{'fraction_non_alphanumeric': 0.15330894579314933, 'fraction_numerical': 0.009311606252078483, 'mean_word_length': 4.089678510998308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4178816', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1172, 'n_words': 245}