<commit_before>package chunkserver

import (
	_"time"
	"io"
	"fmt"
	"encoding/binary"
	"bytes"
	"github.com/jcloudpub/speedy/imageserver/meta"
	"github.com/jcloudpub/speedy/imageserver/util/log"
)

const (
	RW_STATUS = 1
	RO_STATUS = 2
	ERR_STATUS = 3

	GLOBAL_NORMAL_STATUS = 0
	GLOBAL_READ_STAUS = 8
)

type ChunkServer struct {
	GroupId int32
	Ip string
	Port int64
	Status int8
	GlobalStatus   int8
	TotalFreeSpace int64
	MaxFreeSpace int64
	PendingWrites	int
	WritingCount   int
}

type ChunkServerGroups struct {
	GroupMap map[string][]ChunkServer //groupId <> []ChunkServer
}

var (
	PUT uint8 = 0x00
	GET uint8 = 0x01
	DELETE uint8 = 0x02
)

func (csgs *ChunkServerGroups) GetChunkServerGroup(groupId string) ([]ChunkServer, bool) {
	group, ok := csgs.GroupMap[groupId]
	return group, ok
}

func (csi *ChunkServer) HostInfoEqual(another *ChunkServer) bool {
	return csi.Ip == another.Ip && csi.Port == another.Port
}

func (cs *ChunkServer) PutData(data []byte, conn *PooledConn, fileId uint64) error {
	output := new(bytes.Buffer)
	header := make([]byte, HEADERSIZE)

	binary.Write(output, binary.BigEndian, PUT)
	binary.Write(output, binary.BigEndian, uint32(len(data) + 2 + 8))
	binary.Write(output, binary.BigEndian, uint16(cs.GroupId))
	binary.Write(output, binary.BigEndian, uint64(fileId))

	log.Debugf("groupId: %d, fileId: %d", cs.GroupId, fileId)

	output.Write(data)
	_, err := conn.Write(output.Bytes())
	if err != nil {
		log.Errorf("write conn error: %s", err)
		return err
	}

	if _, err := io.ReadFull(conn.br, header); err != nil {
		log.Errorf("read header error: %s", err)
		return err
	}

	if header[0] == PUT && header[1] == 0 {
		log.Debugf("upload success")
		return nil
	}

	log.Errorf("fileId: %d, upload failed, header[0] = %d, header[1] = %d", fileId, header[0], header[1])
	return fmt.Errorf("upload error, code: %d", header[1])
}

func (cs *ChunkServer) GetData(miv *meta.MetaInfoValue, conn *PooledConn) ([]byte, error) {
	//conn.SetReadDeadline(time.Now().Add(60*time.Second))
	output := new(bytes.Buffer)
	header := make([]byte, HEADERSIZE)

	binary.Write(output, binary.BigEndian, GET)
	binary.Write(output, binary.BigEndian, uint32(2+8))
	binary.Write(output, binary.BigEndian, uint16(miv.GroupId))
	binary.Write(output, binary.BigEndian, uint64(miv.FileId))<commit_msg>delete useless code<commit_after>package chunkserver

import (
	_"time"
	"io"
	"fmt"
	"encoding/binary"
	"bytes"
	"github.com/jcloudpub/speedy/imageserver/meta"
	"github.com/jcloudpub/speedy/imageserver/util/log"
)

const (
	RW_STATUS = 1
	RO_STATUS = 2
	ERR_STATUS = 3

	GLOBAL_NORMAL_STATUS = 0
	GLOBAL_READ_STAUS = 8
)

type ChunkServer struct {
	GroupId int32
	Ip string
	Port int64
	Status int8
	GlobalStatus   int8
	TotalFreeSpace int64
	MaxFreeSpace int64
	PendingWrites	int
	WritingCount   int
}

type ChunkServerGroups struct {
	GroupMap map[string][]ChunkServer //groupId <> []ChunkServer
}

var (
	PUT uint8 = 0x00
	GET uint8 = 0x01
	DELETE uint8 = 0x02
)

func (csgs *ChunkServerGroups) GetChunkServerGroup(groupId string) ([]ChunkServer, bool) {
	group, ok := csgs.GroupMap[groupId]
	return group, ok
}

func (csi *ChunkServer) HostInfoEqual(another *ChunkServer) bool {
	return csi.Ip == another.Ip && csi.Port == another.Port
}

func (cs *ChunkServer) PutData(data []byte, conn *PooledConn, fileId uint64) error {
	output := new(bytes.Buffer)
	header := make([]byte, HEADERSIZE)

	binary.Write(output, binary.BigEndian, PUT)
	binary.Write(output, binary.BigEndian, uint32(len(data) + 2 + 8))
	binary.Write(output, binary.BigEndian, uint16(cs.GroupId))
	binary.Write(output, binary.BigEndian, uint64(fileId))

	log.Debugf("groupId: %d, fileId: %d", cs.GroupId, fileId)

	output.Write(data)
	_, err := conn.Write(output.Bytes())
	if err != nil {
		log.Errorf("write conn error: %s", err)
		return err
	}

	if _, err := io.ReadFull(conn.br, header); err != nil {
		log.Errorf("read header error: %s", err)
		return err
	}

	if header[0] == PUT && header[1] == 0 {
		log.Debugf("upload success")
		return nil
	}

	log.Errorf("fileId: %d, upload failed, header[0] = %d, header[1] = %d", fileId, header[0], header[1])
	return fmt.Errorf("upload error, code: %d", header[1])
}

func (cs *ChunkServer) GetData(miv *meta.MetaInfoValue, conn *PooledConn) ([]byte, error) {
	output := new(bytes.Buffer)
	header := make([]byte, HEADERSIZE)

	binary.Write(output, binary.BigEndian, GET)
	binary.Write(output, binary.BigEndian, uint32(2+8))
	binary.Write(output, binary.BigEndian, uint16(miv.GroupId))
	binary.Write(output, binary.BigEndian, uint64(miv.FileId))
{'fraction_non_alphanumeric': 0.1445124617402711, 'fraction_numerical': 0.023611718408395276, 'mean_word_length': 4.857874519846351, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1352244', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1710, 'n_words': 394}