<commit_before>package main

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
)

type Server struct {
	BindAddress string
	DataPath    string
	Commands    map[string]Command
}

func (s *Server) HandleConnection(conn net.Conn) {
	defer conn.Close()
	var data []byte
	var dataB []byte
	var readSize int
	var totalLength int

	totalLength = 0
	readSize = ReadBufferSize
	data = make([]byte, readSize)
	for {
		length, err := conn.Read(data)
		if err != nil {
			break
		}
		totalLength += length
		if length == readSize {
			readSize *= 2
			dataB = make([]byte, readSize)
			dataB = data
			data = make([]byte, readSize)
			data = dataB
		} else {
			break
		}
	}
	data = data[0:totalLength]
	s.HandleRequest(conn, data)
}

func (s *Server) HandleRequest(conn net.Conn, data []byte) {
	var paramMsgs []*Message
	var paramCount int
	var collection *Collection
	var parameters []interface{}
	var message *Message

	message, err := NewMessage(data)
	if err != nil {
		conn.Write([]byte("-Bad RESP message received\r\n"))
		return
	}

	msgs, err := message.Array()
	if err != nil {
		conn.Write([]byte("-Command error\r\n"))
		return
	}

	commandName, err := msgs[0].Str()
	if err != nil {
		conn.Write([]byte("-Server error\r\n"))
		return
	}

	if command, ok := s.Commands[strings.ToLower(commandName)]; ok {

		paramMsgs = msgs[1:]
		paramCount = len(paramMsgs)

		if len(paramMsgs) > len(command.Parameters) {
			fmt.Fprint(conn, "-Incorrect command parameters\r\n")
			return
		}

		for i, param := range command.Parameters {

			if param.Required && i >= paramCount {
				fmt.Fprint(conn, "-Incorrect command parameters\r\n")
				return
			}
			message = paramMsgs[i]
			switch param.Type {
			case "collection":
				value, err := message.Str()
				if err != nil || collection != nil {
					fmt.Fprint(conn, "-Incorrect command parameters\r\n")
					return
				}
				collection = CreateCollection(value, s.DataPath)
			case "string":
				value, err := message.Str()
				if err != nil {
					fmt.Fprint(conn, "-Incorrect command parameters\r\n")
					return
				}
				parameters = append(parameters, value)
			case "integer":
				value, err := message.Str()<commit_msg>optional params no longer cause out of index panics<commit_after>package main

import (
	"fmt"
	"net"
	"os"
	"strconv"
	"strings"
)

type Server struct {
	BindAddress string
	DataPath    string
	Commands    map[string]Command
}

func (s *Server) HandleConnection(conn net.Conn) {
	defer conn.Close()
	var data []byte
	var dataB []byte
	var readSize int
	var totalLength int

	totalLength = 0
	readSize = ReadBufferSize
	data = make([]byte, readSize)
	for {
		length, err := conn.Read(data)
		if err != nil {
			break
		}
		totalLength += length
		if length == readSize {
			readSize *= 2
			dataB = make([]byte, readSize)
			dataB = data
			data = make([]byte, readSize)
			data = dataB
		} else {
			break
		}
	}
	data = data[0:totalLength]
	s.HandleRequest(conn, data)
}

func (s *Server) HandleRequest(conn net.Conn, data []byte) {
	var paramMsgs []*Message
	var paramCount int
	var collection *Collection
	var parameters []interface{}
	var message *Message

	message, err := NewMessage(data)
	if err != nil {
		conn.Write([]byte("-Bad RESP message received\r\n"))
		return
	}

	msgs, err := message.Array()
	if err != nil {
		conn.Write([]byte("-Command error\r\n"))
		return
	}

	commandName, err := msgs[0].Str()
	if err != nil {
		conn.Write([]byte("-Server error\r\n"))
		return
	}

	if command, ok := s.Commands[strings.ToLower(commandName)]; ok {

		paramMsgs = msgs[1:]
		paramCount = len(paramMsgs)

		if len(paramMsgs) > len(command.Parameters) {
			fmt.Fprint(conn, "-Incorrect command parameters\r\n")
			return
		}

		for i, param := range command.Parameters {

			if param.Required && i >= paramCount {
				fmt.Fprint(conn, "-Incorrect command parameters\r\n")
				return
			}
			if !param.Required && i >= paramCount {
				continue
			}
			message = paramMsgs[i]
			switch param.Type {
			case "collection":
				value, err := message.Str()
				if err != nil || collection != nil {
					fmt.Fprint(conn, "-Incorrect command parameters\r\n")
					return
				}
				collection = CreateCollection(value, s.DataPath)
			case "string":
				value, err := message.Str()
				if err != nil {
					fmt.Fprint(conn, "-Incorrect command parameters\r\n")
					return
				}
				parameters = append(parameters, value)
			case "integer":
				value, err := message.Str()
{'fraction_non_alphanumeric': 0.13651723361117368, 'fraction_numerical': 0.0022527596305474207, 'mean_word_length': 3.2085308056872037, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 8, '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': '5961571', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1508, 'n_words': 410}