<commit_before>package pkg

import "github.com/jennal/goplay/log"

type HandShakeImpl struct {
	serverResponse *HandShakeResponse
	routesMap      RouteMap
	rpcRoutesMap   RouteMap
	specRoutesMap  RouteMap
}

type HandShake interface {
	RegistSpecRoute(route string, idx RouteIndex) error

	MergeRpcRoutesMap(data RouteMap)

	AddRoutesMap(data string)
	UpdateHandShakeResponse(resp *HandShakeResponse)

	RoutesMap() RouteMap
	RpcRoutesMap() RouteMap
	SpecRoutesMap() RouteMap

	GetIndexRoute(str string) (RouteIndex, bool)
	GetStringRoute(idx RouteIndex) (string, bool)

	ConvertRouteIndexToRpc(route string) (RouteIndex, bool)
	ConvertRouteIndexFromRpc(route string) (RouteIndex, bool)
}

var _handShakeInstance HandShake

func init() {
	_handShakeInstance = NewHandShakeImpl()
}

func SetHandShakeImpl(hs HandShake) error {
	if hs == nil {
		return log.NewError("handshake can't be nil")
	}

	//restore spec
	if _handShakeInstance != nil {
		for k, v := range _handShakeInstance.SpecRoutesMap() {
			hs.RegistSpecRoute(k, v)
		}
	}

	_handShakeInstance = hs
	return nil
}

func DefaultHandShake() HandShake {
	return _handShakeInstance
}

func NewHandShakeImpl() *HandShakeImpl {
	return &HandShakeImpl{
		serverResponse: nil,
		routesMap:      make(RouteMap),
		rpcRoutesMap:   make(RouteMap),
		specRoutesMap:  make(RouteMap),
	}
}

func (r *HandShakeImpl) RegistSpecRoute(route string, idx RouteIndex) error {
	if _, ok := r.specRoutesMap[route]; ok {
		return log.NewErrorf("route already exists: %v", route)
	}

	r.specRoutesMap[route] = idx
	return nil
}

func (r *HandShakeImpl) MergeRpcRoutesMap(data RouteMap) {
	r.rpcRoutesMap.Merge(data)
}

func (r *HandShakeImpl) AddRoutesMap(route string) {
	count := len(r.routesMap)
	if r.routesMap == nil {
		r.routesMap = RouteMap{}
	}

	log.Tracef("Build route map: %v %v", count, route)
	r.routesMap[route] = RouteIndex(count)
}

func (r *HandShakeImpl) UpdateHandShakeResponse(resp *HandShakeResponse) {
	r.serverResponse = resp

	routesMap := make(RouteMap)<commit_msg>remove log<commit_after>package pkg

import "github.com/jennal/goplay/log"

type HandShakeImpl struct {
	serverResponse *HandShakeResponse
	routesMap      RouteMap
	rpcRoutesMap   RouteMap
	specRoutesMap  RouteMap
}

type HandShake interface {
	RegistSpecRoute(route string, idx RouteIndex) error

	MergeRpcRoutesMap(data RouteMap)

	AddRoutesMap(data string)
	UpdateHandShakeResponse(resp *HandShakeResponse)

	RoutesMap() RouteMap
	RpcRoutesMap() RouteMap
	SpecRoutesMap() RouteMap

	GetIndexRoute(str string) (RouteIndex, bool)
	GetStringRoute(idx RouteIndex) (string, bool)

	ConvertRouteIndexToRpc(route string) (RouteIndex, bool)
	ConvertRouteIndexFromRpc(route string) (RouteIndex, bool)
}

var _handShakeInstance HandShake

func init() {
	_handShakeInstance = NewHandShakeImpl()
}

func SetHandShakeImpl(hs HandShake) error {
	if hs == nil {
		return log.NewError("handshake can't be nil")
	}

	//restore spec
	if _handShakeInstance != nil {
		for k, v := range _handShakeInstance.SpecRoutesMap() {
			hs.RegistSpecRoute(k, v)
		}
	}

	_handShakeInstance = hs
	return nil
}

func DefaultHandShake() HandShake {
	return _handShakeInstance
}

func NewHandShakeImpl() *HandShakeImpl {
	return &HandShakeImpl{
		serverResponse: nil,
		routesMap:      make(RouteMap),
		rpcRoutesMap:   make(RouteMap),
		specRoutesMap:  make(RouteMap),
	}
}

func (r *HandShakeImpl) RegistSpecRoute(route string, idx RouteIndex) error {
	if _, ok := r.specRoutesMap[route]; ok {
		return log.NewErrorf("route already exists: %v", route)
	}

	r.specRoutesMap[route] = idx
	return nil
}

func (r *HandShakeImpl) MergeRpcRoutesMap(data RouteMap) {
	r.rpcRoutesMap.Merge(data)
}

func (r *HandShakeImpl) AddRoutesMap(route string) {
	count := len(r.routesMap)
	if r.routesMap == nil {
		r.routesMap = RouteMap{}
	}

	// log.Tracef("Build route map: %v %v", count, route)
	r.routesMap[route] = RouteIndex(count)
}

func (r *HandShakeImpl) UpdateHandShakeResponse(resp *HandShakeResponse) {
	r.serverResponse = resp

	routesMap := make(RouteMap)
{'fraction_non_alphanumeric': 0.10222222222222223, 'fraction_numerical': 0.0, 'mean_word_length': 5.24191063174114, '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': '3792609', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1457, 'n_words': 324}