<commit_before>package facade

import (
	"firempq/common"
	"firempq/db"
	"firempq/svcerr"
	"github.com/op/go-logging"
	"strings"
	"sync"
)

var log = logging.MustGetLogger("firempq")

type ServiceFacade struct {
	allSvcs  map[string]common.ISvc
	lock     sync.Mutex
	database *db.DataStorage
}

func NewFacade(database *db.DataStorage) *ServiceFacade {
	f := ServiceFacade{
		database: database,
		allSvcs:  make(map[string]common.ISvc),
	}
	f.loadAllServices()
	return &f
}

func (s *ServiceFacade) loadAllServices() {
	for _, sm := range s.database.GetAllServiceMeta() {
		log.Info("Loading service data for: %s", sm.Name)
		objLoader, ok := SVC_LOADER[sm.Stype]
		if !ok {
			log.Error("Unknown service '%s' type: %s", sm.Name, sm.Stype)
			continue
		}
		svcInstance, err := objLoader(s.database, sm.Name)
		if err != nil {
			log.Error("Service '%s' was not loaded because of: %s", sm.Name, err)
		} else {
			s.allSvcs[sm.Name] = svcInstance
		}
	}
}

func (s *ServiceFacade) CreateService(svcType string, svcName string, params map[string]string) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	if _, ok := s.allSvcs[svcName]; ok {
		return svcerr.ERR_SVC_ALREADY_EXISTS
	}
	svcCrt, ok := SVC_CREATOR[svcType]
	if !ok {
		return svcerr.ERR_SVC_UNKNOWN_TYPE
	}
	smeta := common.NewServiceMetaInfo(svcType, 0, svcName)
	s.database.SaveServiceMeta(smeta)

	s.allSvcs[svcName] = svcCrt(svcName, params)

	return nil
}

func (s *ServiceFacade) DropService(svcName string) error {
	s.lock.Lock()
	defer s.lock.Unlock()
	_, ok := s.allSvcs[svcName]
	if !ok {
		return svcerr.ERR_NO_SVC
	}
	delete(s.allSvcs, svcName)
	s.database.DeleteServiceData(svcName)
	return nil
}

func (s *ServiceFacade) ListServices(svcPrefix string, svcType string) ([]string, error) {
	s.lock.Lock()
	defer s.lock.Unlock()

	if svcType != "" {
		_, ok := SVC_CREATOR[svcType]
		if !ok {
			return nil, svcerr.ERR_SVC_UNKNOWN_TYPE
		}
	}

	services := make([]string, 0)
	for svcName, svc := range s.allSvcs {
		if svcType != "" && svcType != svc.GetTypeName() {
			continue
		}
		if svcPrefix == "?" || strings.HasPrefix(svcName, svcPrefix) {
			services = append(services, svcName+" "+svc.GetTypeName())
		}
	}
	return services, nil
}

func (s *ServiceFacade) GetService(name string) (common.ISvc, bool) {<commit_msg>Added service Close() call if case if dropped.<commit_after>package facade

import (
	"firempq/common"
	"firempq/db"
	"firempq/svcerr"
	"github.com/op/go-logging"
	"strings"
	"sync"
)

var log = logging.MustGetLogger("firempq")

type ServiceFacade struct {
	allSvcs  map[string]common.ISvc
	lock     sync.Mutex
	database *db.DataStorage
}

func NewFacade(database *db.DataStorage) *ServiceFacade {
	f := ServiceFacade{
		database: database,
		allSvcs:  make(map[string]common.ISvc),
	}
	f.loadAllServices()
	return &f
}

func (s *ServiceFacade) loadAllServices() {
	for _, sm := range s.database.GetAllServiceMeta() {
		log.Info("Loading service data for: %s", sm.Name)
		objLoader, ok := SVC_LOADER[sm.Stype]
		if !ok {
			log.Error("Unknown service '%s' type: %s", sm.Name, sm.Stype)
			continue
		}
		svcInstance, err := objLoader(s.database, sm.Name)
		if err != nil {
			log.Error("Service '%s' was not loaded because of: %s", sm.Name, err)
		} else {
			s.allSvcs[sm.Name] = svcInstance
		}
	}
}

func (s *ServiceFacade) CreateService(svcType string, svcName string, params map[string]string) error {
	s.lock.Lock()
	defer s.lock.Unlock()

	if _, ok := s.allSvcs[svcName]; ok {
		return svcerr.ERR_SVC_ALREADY_EXISTS
	}
	svcCrt, ok := SVC_CREATOR[svcType]
	if !ok {
		return svcerr.ERR_SVC_UNKNOWN_TYPE
	}

	metaInfo := common.NewServiceMetaInfo(svcType, 0, svcName)
	s.database.SaveServiceMeta(metaInfo)

	s.allSvcs[svcName] = svcCrt(svcName, params)

	return nil
}

func (s *ServiceFacade) DropService(svcName string) error {
	s.lock.Lock()
	defer s.lock.Unlock()
	svc, ok := s.allSvcs[svcName]
	if !ok {
		return svcerr.ERR_NO_SVC
	}
	svc.Close()
	delete(s.allSvcs, svcName)
	s.database.DeleteServiceData(svcName)
	return nil
}

func (s *ServiceFacade) ListServices(svcPrefix string, svcType string) ([]string, error) {
	s.lock.Lock()
	defer s.lock.Unlock()

	if svcType != "" {
		_, ok := SVC_CREATOR[svcType]
		if !ok {
			return nil, svcerr.ERR_SVC_UNKNOWN_TYPE
		}
	}

	services := make([]string, 0)
	for svcName, svc := range s.allSvcs {
		if svcType != "" && svcType != svc.GetTypeName() {
			continue
		}
		if svcPrefix == "?" || strings.HasPrefix(svcName, svcPrefix) {
			services = append(services, svcName+" "+svc.GetTypeName())
		}
	}
	return services, nil
}

func (s *ServiceFacade) GetService(name string) (common.ISvc, bool) {
{'fraction_non_alphanumeric': 0.14301098427740686, 'fraction_numerical': 0.0008615119534783546, 'mean_word_length': 4.223847019122609, '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': '1844263', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1875, 'n_words': 387}