<commit_before>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package infoGatherer

import (
	"github.com/getgauge/common"
	"github.com/getgauge/gauge/config"
	"github.com/getgauge/gauge/conn"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/parser"
	"github.com/getgauge/gauge/runner"
	"github.com/getgauge/gauge/util"
	"github.com/golang/protobuf/proto"
	fsnotify "gopkg.in/fsnotify.v1"
	"path/filepath"
	"sync"
)

type SpecInfoGatherer struct {
	mutex             sync.Mutex
	conceptDictionary *parser.ConceptDictionary
	specsCache        map[string][]*parser.Specification
	conceptsCache     map[string][]*parser.Concept
	stepsCache        map[string]*parser.StepValue
}

func (s *SpecInfoGatherer) MakeListOfAvailableSteps(runner *runner.TestRunner) {
	s.specsCache = make(map[string][]*parser.Specification, 0)
	s.conceptsCache = make(map[string][]*parser.Concept, 0)
	s.stepsCache = make(map[string]*parser.StepValue, 0)

	// Concepts parsed first because we need to create a concept dictionary that spec parsing can use
	s.initConceptsCache()
	s.initSpecsCache()
	s.initStepsCache(runner)

	go s.watchForFileChanges()
}

func (s *SpecInfoGatherer) initSpecsCache() {
	specFiles := util.FindSpecFilesIn(filepath.Join(config.ProjectRoot, common.SpecsDirectoryName))
	parsedSpecs := s.getParsedSpecs(specFiles)

	logger.ApiLog.Debug("Initializing specs cache with %d specs", len(parsedSpecs))
	for _, spec := range parsedSpecs {
		s.addToSpecsCache(spec.FileName, spec)
	}
}

func (s *SpecInfoGatherer) initConceptsCache() {
	parsedConcepts := s.getParsedConcepts()

	logger.ApiLog.Debug("Initializing concepts cache with %d concepts", len(parsedConcepts))
	for _, concept := range parsedConcepts {
		s.addToConceptsCache(concept.FileName, concept)
	}
}

func (s *SpecInfoGatherer) initStepsCache(runner *runner.TestRunner) {
	stepsFromSpecs := s.getStepsFromCachedSpecs()
	stepsFromConcepts := s.getStepsFromCachedConcepts()
	implementedSteps := s.getImplementedSteps(runner)

	allSteps := append(stepsFromSpecs, stepsFromConcepts...)
	allSteps = append(allSteps, implementedSteps...)

	logger.ApiLog.Debug("Initializing steps cache with %d steps", len(allSteps))
	s.addToStepsCache(allSteps)
}

func (s *SpecInfoGatherer) addToSpecsCache(key string, value *parser.Specification) {
	s.mutex.Lock()
	if s.specsCache[key] == nil {
		s.specsCache[key] = make([]*parser.Specification, 0)
	}
	s.specsCache[key] = append(s.specsCache[key], value)
	s.mutex.Unlock()
}
<commit_msg>refactoring cache init in specInfoGatherer<commit_after>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package infoGatherer

import (
	"github.com/getgauge/common"
	"github.com/getgauge/gauge/config"
	"github.com/getgauge/gauge/conn"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/parser"
	"github.com/getgauge/gauge/runner"
	"github.com/getgauge/gauge/util"
	"github.com/golang/protobuf/proto"
	fsnotify "gopkg.in/fsnotify.v1"
	"path/filepath"
	"sync"
)

type SpecInfoGatherer struct {
	mutex             sync.Mutex
	conceptDictionary *parser.ConceptDictionary
	specsCache        map[string][]*parser.Specification
	conceptsCache     map[string][]*parser.Concept
	stepsCache        map[string]*parser.StepValue
}

func (s *SpecInfoGatherer) MakeListOfAvailableSteps(runner *runner.TestRunner) {
	// Concepts parsed first because we need to create a concept dictionary that spec parsing can use
	s.initConceptsCache()
	s.initSpecsCache()
	s.initStepsCache(runner)

	go s.watchForFileChanges()
}

func (s *SpecInfoGatherer) initSpecsCache() {
	s.specsCache = make(map[string][]*parser.Specification, 0)
	specFiles := util.FindSpecFilesIn(filepath.Join(config.ProjectRoot, common.SpecsDirectoryName))
	parsedSpecs := s.getParsedSpecs(specFiles)

	logger.ApiLog.Debug("Initializing specs cache with %d specs", len(parsedSpecs))
	for _, spec := range parsedSpecs {
		s.addToSpecsCache(spec.FileName, spec)
	}
}

func (s *SpecInfoGatherer) initConceptsCache() {
	s.conceptsCache = make(map[string][]*parser.Concept, 0)
	parsedConcepts := s.getParsedConcepts()

	logger.ApiLog.Debug("Initializing concepts cache with %d concepts", len(parsedConcepts))
	for _, concept := range parsedConcepts {
		s.addToConceptsCache(concept.FileName, concept)
	}
}

func (s *SpecInfoGatherer) initStepsCache(runner *runner.TestRunner) {
	s.stepsCache = make(map[string]*parser.StepValue, 0)
	stepsFromSpecs := s.getStepsFromCachedSpecs()
	stepsFromConcepts := s.getStepsFromCachedConcepts()
	implementedSteps := s.getImplementedSteps(runner)

	allSteps := append(stepsFromSpecs, stepsFromConcepts...)
	allSteps = append(allSteps, implementedSteps...)

	logger.ApiLog.Debug("Initializing steps cache with %d steps", len(allSteps))
	s.addToStepsCache(allSteps)
}

func (s *SpecInfoGatherer) addToSpecsCache(key string, value *parser.Specification) {
	s.mutex.Lock()
	if s.specsCache[key] == nil {
		s.specsCache[key] = make([]*parser.Specification, 0)
	}
	s.specsCache[key] = append(s.specsCache[key], value)
	s.mutex.Unlock()
}

{'fraction_non_alphanumeric': 0.11448232718173287, 'fraction_numerical': 0.0031279324366593683, 'mean_word_length': 4.910351201478743, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2466291', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2135, 'n_words': 514}