<commit_before>package handler

import (
	"context"
	"io"
	"sync"
	"time"

	"github.com/micro/go-micro/errors"
	"github.com/micro/go-micro/metadata"
	"github.com/micro/go-micro/store"
	pb "github.com/micro/go-micro/store/service/proto"
)

type Store struct {
	// The default store
	Default store.Store

	// Store initialiser
	New func(string, string) (store.Store, error)

	// Store map
	sync.RWMutex
	Stores map[string]store.Store
}

func (s *Store) get(ctx context.Context) (store.Store, error) {
	md, ok := metadata.FromContext(ctx)
	if !ok {
		return s.Default, nil
	}

	namespace := md["Micro-Namespace"]
	prefix := md["Micro-Prefix"]

	if len(namespace) == 0 && len(prefix) == 0 {
		return s.Default, nil
	}

	s.RLock()
	str, ok := s.Stores[namespace+prefix]
	// got it
	if ok {
		s.RUnlock()
		return str, nil
	}
	s.RUnlock()

	// lock (might be a race)
	s.Lock()
	defer s.Unlock()

	// create a new store

	// either namespace is not blank or prefix is not blank
	st, err := s.New(namespace, prefix)
	if err != nil {
		return nil, errors.InternalServerError("go.micro.store", err.Error())
	}

	// save store
	s.Stores[namespace+prefix] = st

	return st, nil<commit_msg>use global lock, use colon delimiter<commit_after>package handler

import (
	"context"
	"io"
	"sync"
	"time"

	"github.com/micro/go-micro/errors"
	"github.com/micro/go-micro/metadata"
	"github.com/micro/go-micro/store"
	pb "github.com/micro/go-micro/store/service/proto"
)

type Store struct {
	// The default store
	Default store.Store

	// Store initialiser
	New func(string, string) (store.Store, error)

	// Store map
	sync.RWMutex
	Stores map[string]store.Store
}

func (s *Store) get(ctx context.Context) (store.Store, error) {
	// lock (might be a race)
	s.Lock()
	defer s.Unlock()

	md, ok := metadata.FromContext(ctx)
	if !ok {
		return s.Default, nil
	}

	namespace := md["Micro-Namespace"]
	prefix := md["Micro-Prefix"]

	if len(namespace) == 0 && len(prefix) == 0 {
		return s.Default, nil
	}

	str, ok := s.Stores[namespace+":"+prefix]
	// got it
	if ok {
		return str, nil
	}

	// create a new store

	// either namespace is not blank or prefix is not blank
	st, err := s.New(namespace, prefix)
	if err != nil {
		return nil, errors.InternalServerError("go.micro.store", err.Error())
	}

	// save store
	s.Stores[namespace+":"+prefix] = st

	return st, nil
{'fraction_non_alphanumeric': 0.15253515125692374, 'fraction_numerical': 0.0017043033659991478, 'mean_word_length': 3.446969696969697, 'pattern_counts': {'":': 2, '<': 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': '7215411', 'n_tokens_mistral': 922, 'n_tokens_neox': 860, 'n_words': 225}