<commit_before>package record_set

import (
	"fmt"
	"github.com/miekg/dns"
	"log"
	"net"
	"sort"
	"strings"
	"sync"
)

//
// Key/value store used for dns records. keys are unique, but each key has a list of values
type RecordStore interface {
	PutVal(dnsType uint16, key, val string) error        // Put a single key/value type into store.
	GetVal(dnsType uint16, key string) (string, error)   // Gets a single value. Will round robin or randomly select  if multiple values
	GetAll(dnsType uint16, key string) ([]string, error) // Get all key values
	DelKey(dnsType uint16, key string) error             // Deletes key and all values for a
	DelVal(dnsType uint16, key, value string) error      // Deletes a single value from a key. Deletes key ifthere are no more values
	Clear() error                                        // Clear all keys from set
}

type RrIndexes struct {
	sync.Mutex
	indexes map[string]int
}

func (rri *RrIndexes) NextVal(dnsType uint16, key string, vals []string) (val string) {
	vl := len(vals)
	if vl == 0 {
		return
	}
	if vl == 1 {
		val = vals[0]
		return
	}
	sort.Strings(vals)
	rri.Lock()
	defer rri.Unlock()
	kp := fmt.Sprintf("/%d/%s", dnsType, key)
	idx := rri.indexes[kp]
	if idx >= vl {
		idx = 0
	}
	val = vals[idx]
	rri.indexes[kp] = idx + 1
	return
}

func (rri *RrIndexes) Del(dnsType uint16, key string) {
	kp := fmt.Sprintf("/%d/%s", dnsType, key)
	delete(rri.indexes, kp)
}

type RecordSet struct {
	store      RecordStore
	rr_indexes *RrIndexes
}

func Create(store RecordStore) (rs *RecordSet) {
	rs = &RecordSet{store, &RrIndexes{indexes: make(map[string]int)}}
	return
}

func (r *RecordSet) Put(dnsType uint16, host, addr string) {
	log.Printf("Adding/updating  %X %s A %s", dnsType, host, addr)
	err := r.store.PutVal(dnsType, host+".", addr)
	if err != nil {
		log.Printf("Unable to put primary record: %s", err.Error())
		return
	}
	// For A or AAAA records, put in reverse DNS
	if dnsType == dns.TypeA || dnsType == dns.TypeAAAA {
		raddr, _ := ReverseAddr(addr)
		if raddr != "" {
			log.Printf("Adding %s PTR %s", raddr, host)
			err = r.store.PutVal(dns.TypePTR, raddr+".", host)<commit_msg>oops -- lock has on entry delete<commit_after>package record_set

import (
	"fmt"
	"github.com/miekg/dns"
	"log"
	"net"
	"sort"
	"strings"
	"sync"
)

//
// Key/value store used for dns records. keys are unique, but each key has a list of values
type RecordStore interface {
	PutVal(dnsType uint16, key, val string) error        // Put a single key/value type into store.
	GetVal(dnsType uint16, key string) (string, error)   // Gets a single value. Will round robin or randomly select  if multiple values
	GetAll(dnsType uint16, key string) ([]string, error) // Get all key values
	DelKey(dnsType uint16, key string) error             // Deletes key and all values for a
	DelVal(dnsType uint16, key, value string) error      // Deletes a single value from a key. Deletes key ifthere are no more values
	Clear() error                                        // Clear all keys from set
}

type RrIndexes struct {
	sync.Mutex
	indexes map[string]int
}

func (rri *RrIndexes) NextVal(dnsType uint16, key string, vals []string) (val string) {
	vl := len(vals)
	if vl == 0 {
		return
	}
	if vl == 1 {
		val = vals[0]
		return
	}
	sort.Strings(vals)
	rri.Lock()
	defer rri.Unlock()
	kp := fmt.Sprintf("/%d/%s", dnsType, key)
	idx := rri.indexes[kp]
	if idx >= vl {
		idx = 0
	}
	val = vals[idx]
	rri.indexes[kp] = idx + 1
	return
}

func (rri *RrIndexes) Del(dnsType uint16, key string) {
	kp := fmt.Sprintf("/%d/%s", dnsType, key)
	rri.Lock()
	defer rri.Unlock()
	delete(rri.indexes, kp)
}

type RecordSet struct {
	store      RecordStore
	rr_indexes *RrIndexes
}

func Create(store RecordStore) (rs *RecordSet) {
	rs = &RecordSet{store, &RrIndexes{indexes: make(map[string]int)}}
	return
}

func (r *RecordSet) Put(dnsType uint16, host, addr string) {
	log.Printf("Adding/updating  %X %s A %s", dnsType, host, addr)
	err := r.store.PutVal(dnsType, host+".", addr)
	if err != nil {
		log.Printf("Unable to put primary record: %s", err.Error())
		return
	}
	// For A or AAAA records, put in reverse DNS
	if dnsType == dns.TypeA || dnsType == dns.TypeAAAA {
		raddr, _ := ReverseAddr(addr)
		if raddr != "" {
			log.Printf("Adding %s PTR %s", raddr, host)
			err = r.store.PutVal(dns.TypePTR, raddr+".", host)
{'fraction_non_alphanumeric': 0.13290411588871004, 'fraction_numerical': 0.009657392504023914, 'mean_word_length': 3.248046875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4919543', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1619, 'n_words': 507}