<commit_before>package storage

import (
	"encoding/json"
	"github.com/MerlinDMC/dsapid"
	"io/ioutil"
	"os"
)

type UserStorage interface {
	Save() error
	Add(string, *dsapid.UserResource)
	Delete(string)
	EnsureExists(string, string) *dsapid.UserResource
	Get(string) *dsapid.UserResource
	GetOK(string) (*dsapid.UserResource, bool)
	FindByName(string) (*dsapid.UserResource, error)
	FindByEmail(string) (*dsapid.UserResource, error)
	FindByToken(string) (*dsapid.UserResource, error)
	GuestUser() *dsapid.UserResource
}

func NewUserStorage(filename string) UserStorage {
	store := new(jsonUserStorage)

	store.filename = filename
	store.loaded = false
	store.users = make(map[string]*dsapid.UserResource, 0)
	store.map_name_id = make(map[string]string)
	store.map_email_id = make(map[string]string)
	store.map_token_id = make(map[string]string)

	return store
}

type jsonUserStorage struct {
	filename string
	loaded   bool
	users    map[string]*dsapid.UserResource

	map_name_id  map[string]string
	map_email_id map[string]string
	map_token_id map[string]string
}

func (me *jsonUserStorage) Save() error {
	return me.save()
}

func (me *jsonUserStorage) Add(id string, user *dsapid.UserResource) {
	me.add(id, *user)
	me.save()
}

func (me *jsonUserStorage) Delete(id string) {
	me.delete(id)
	me.save()
}

func (me *jsonUserStorage) EnsureExists(id, name string) *dsapid.UserResource {
	if v, ok := me.users[id]; ok {
		return v
	}

	user := new(dsapid.UserResource)
	user.Uuid = id
	user.Name = name

	me.Add(id, user)

	return user
}

func (me *jsonUserStorage) Get(id string) *dsapid.UserResource {
	me.load()

	if v, ok := me.users[id]; ok {
		return v
	}

	return nil
}

func (me *jsonUserStorage) GetOK(id string) (*dsapid.UserResource, bool) {
	me.load()

	v, ok := me.users[id]

	return v, ok
}

func (me *jsonUserStorage) FindByName(name string) (*dsapid.UserResource, error) {
	me.load()

	if v, ok := me.map_name_id[name]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) FindByEmail(email string) (*dsapid.UserResource, error) {
	me.load()

	if v, ok := me.map_email_id[email]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) FindByToken(token string) (*dsapid.UserResource, error) {
	me.load()

	if v, ok := me.map_token_id[token]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) GuestUser() *dsapid.UserResource {
	return &dsapid.UserResource{
		Name:  dsapid.DefaultUserGuestName,
		Type:  dsapid.UserTypeSystem,
		Roles: []dsapid.UserRoleName{dsapid.UserRoleGuest},
	}
}

func (me *jsonUserStorage) add(id string, user dsapid.UserResource) {
	me.users[id] = &user

	if _, ok := me.map_name_id[user.Name]; !ok && user.Name != "" {
		me.map_name_id[user.Name] = id
	}

	if _, ok := me.map_email_id[user.Email]; !ok && user.Email != "" {<commit_msg>preload users on store creation<commit_after>package storage

import (
	"encoding/json"
	"github.com/MerlinDMC/dsapid"
	"io/ioutil"
	"os"
)

type UserStorage interface {
	Save() error
	Add(string, *dsapid.UserResource)
	Delete(string)
	EnsureExists(string, string) *dsapid.UserResource
	Get(string) *dsapid.UserResource
	GetOK(string) (*dsapid.UserResource, bool)
	FindByName(string) (*dsapid.UserResource, error)
	FindByEmail(string) (*dsapid.UserResource, error)
	FindByToken(string) (*dsapid.UserResource, error)
	GuestUser() *dsapid.UserResource
}

func NewUserStorage(filename string) UserStorage {
	store := new(jsonUserStorage)

	store.filename = filename
	store.loaded = false
	store.users = make(map[string]*dsapid.UserResource, 0)
	store.map_name_id = make(map[string]string)
	store.map_email_id = make(map[string]string)
	store.map_token_id = make(map[string]string)

	store.load()

	return store
}

type jsonUserStorage struct {
	filename string
	loaded   bool
	users    map[string]*dsapid.UserResource

	map_name_id  map[string]string
	map_email_id map[string]string
	map_token_id map[string]string
}

func (me *jsonUserStorage) Save() error {
	return me.save()
}

func (me *jsonUserStorage) Add(id string, user *dsapid.UserResource) {
	me.add(id, *user)
	me.save()
}

func (me *jsonUserStorage) Delete(id string) {
	me.delete(id)
	me.save()
}

func (me *jsonUserStorage) EnsureExists(id, name string) *dsapid.UserResource {
	if v, ok := me.users[id]; ok {
		return v
	}

	user := new(dsapid.UserResource)
	user.Uuid = id
	user.Name = name

	me.Add(id, user)

	return user
}

func (me *jsonUserStorage) Get(id string) *dsapid.UserResource {
	if v, ok := me.users[id]; ok {
		return v
	}

	return nil
}

func (me *jsonUserStorage) GetOK(id string) (*dsapid.UserResource, bool) {
	v, ok := me.users[id]

	return v, ok
}

func (me *jsonUserStorage) FindByName(name string) (*dsapid.UserResource, error) {
	if v, ok := me.map_name_id[name]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) FindByEmail(email string) (*dsapid.UserResource, error) {
	if v, ok := me.map_email_id[email]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) FindByToken(token string) (*dsapid.UserResource, error) {
	if v, ok := me.map_token_id[token]; ok {
		return me.users[v], nil
	}

	return nil, ErrStorageItemNotFound
}

func (me *jsonUserStorage) GuestUser() *dsapid.UserResource {
	return &dsapid.UserResource{
		Name:  dsapid.DefaultUserGuestName,
		Type:  dsapid.UserTypeSystem,
		Roles: []dsapid.UserRoleName{dsapid.UserRoleGuest},
	}
}

func (me *jsonUserStorage) add(id string, user dsapid.UserResource) {
	me.users[id] = &user

	if _, ok := me.map_name_id[user.Name]; !ok && user.Name != "" {
		me.map_name_id[user.Name] = id
	}

	if _, ok := me.map_email_id[user.Email]; !ok && user.Email != "" {
{'fraction_non_alphanumeric': 0.1366054573536983, 'fraction_numerical': 0.00034322979234597563, 'mean_word_length': 4.892821031344793, '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': '6402848', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2161, 'n_words': 478}