<commit_before>package influxdb

import (
	"sync"
)

type Int struct {
	mu sync.RWMutex
	i  int64
}

func NewInt(v int64) *Int {
	return &Int{i: v}
}

// Add atomically adds the given delta to the Int.
func (i *Int) Add(delta int64) {
	i.mu.Lock()
	defer i.mu.Unlock()
	i.i += delta

}

type Stats struct {
	name string
	m    map[string]*Int
	mu   sync.RWMutex
}

func NewStats(name string) *Stats {
	return &Stats{
		name: name,
		m:    make(map[string]*Int),
	}
}

// Add adds delta to the stat indiciated by key.
func (s *Stats) Add(key string, delta int64) {
	s.mu.RLock()
	i, ok := s.m[key]
	s.mu.RUnlock()
	if !ok {
		// check again under the write lock
		s.mu.Lock()
		i, ok = s.m[key]
		if !ok {
			i = new(Int)
			s.m[key] = i
		}
		s.mu.Unlock()
	}

	i.Add(delta)
}

// Inc simply increments the given key by 1.
func (s *Stats) Inc(key string) {
	s.Add(key, 1)
}

func (s *Stats) Get(key string) int64 {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.m[key].i
}

func (s *Stats) Set(key string, v int64) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.m[key] = NewInt(v)
}

func (s *Stats) Name() string {
	return s.name
}

// Walk calls f for each entry in the stats. The stats are locked<commit_msg>Add GoDoc comments to Stats type<commit_after>package influxdb

import (
	"sync"
)

// Int representes a 64-bit signed integer which can be updated atomically.
type Int struct {
	mu sync.RWMutex
	i  int64
}

// NewInt returns a new Int
func NewInt(v int64) *Int {
	return &Int{i: v}
}

// Add atomically adds the given delta to the Int.
func (i *Int) Add(delta int64) {
	i.mu.Lock()
	defer i.mu.Unlock()
	i.i += delta

}

// Stats represents a collection of metrics, as key-value pairs.
type Stats struct {
	name string
	m    map[string]*Int
	mu   sync.RWMutex
}

// NewStats returns a Stats object with the given name.
func NewStats(name string) *Stats {
	return &Stats{
		name: name,
		m:    make(map[string]*Int),
	}
}

// Add adds delta to the stat indiciated by key.
func (s *Stats) Add(key string, delta int64) {
	s.mu.RLock()
	i, ok := s.m[key]
	s.mu.RUnlock()
	if !ok {
		// check again under the write lock
		s.mu.Lock()
		i, ok = s.m[key]
		if !ok {
			i = new(Int)
			s.m[key] = i
		}
		s.mu.Unlock()
	}

	i.Add(delta)
}

// Inc simply increments the given key by 1.
func (s *Stats) Inc(key string) {
	s.Add(key, 1)
}

// Get returns a value for a given key.
func (s *Stats) Get(key string) int64 {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.m[key].i
}

// Set sets a value for the given key.
func (s *Stats) Set(key string, v int64) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.m[key] = NewInt(v)
}

// Name returns the name of the Stats object.
func (s *Stats) Name() string {
	return s.name
}

// Walk calls f for each entry in the stats. The stats are locked
{'fraction_non_alphanumeric': 0.1418260555756045, 'fraction_numerical': 0.010826416456153013, 'mean_word_length': 2.8987341772151898, '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': '6374456', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1121, 'n_words': 372}