Ejemplo n.º 1
0
func Flush() string {
	GetConnectionFromKey("Flush")
	iType := protos.Type(protos.Type_value["Flush"])
	command := &protos.Command{
		Type: &iType,
	}
	commandBytes, _ := proto.Marshal(command)
	conn.Write(commandBytes)
	//Create a data buffer of type byte slice with capacity of 4096
	data := make([]byte, 4096)
	n, _ := conn.Read(data)
	response := new(protos.Response)
	proto.Unmarshal(data[0:n], response)
	//fmt.Println("receive response:  "+response.String())
	return response.String()
}
Ejemplo n.º 2
0
func Add(key string, value string) string {
	GetConnectionFromKey(key)
	iType := protos.Type(protos.Type_value["Add"])
	command := &protos.Command{
		Type:  &iType,
		Key:   &key,
		Value: &value,
	}
	commandBytes, _ := proto.Marshal(command)
	conn.Write(commandBytes)
	//Create a data buffer of type byte slice with capacity of 4096
	data := make([]byte, 4096)
	//Read the data waiting on the connection and put it in the data buffer
	n, _ := conn.Read(data)
	response := new(protos.Response)
	proto.Unmarshal(data[0:n], response)
	//fmt.Println("receive response:  "+response.String())
	return response.String()
}
func Execute(req *servlet.Servlet) {
	request := req.Command
	if request.Type.String() == "Get" {
		value := []string{kv[*request.Key]}
		response := protos.Response{
			Status: proto.Bool(true),
			Value:  value,
		}
		req.Response = &response
		responseChannel <- req
	}
	if request.Type.String() == "Set" {
		partitionKey := req.PartitionKey
		keys[partitionKey] = 1
		kv[*request.Key] = *request.Value
		response := protos.Response{
			Status: proto.Bool(true),
		}
		req.Response = &response
		responseChannel <- req
	}
	if request.Type.String() == "Add" {
		partitionKey := req.PartitionKey
		keys[partitionKey] = 1
		if kv[*request.Key] != "" && kv[*request.Key] != "0" {
			oper1, _ := strconv.ParseFloat(kv[*request.Key], 32)
			oper2, _ := strconv.ParseFloat(*request.Value, 32)
			sum := oper1 + oper2
			kv[*request.Key] = strconv.FormatFloat(sum, 'f', -1, 32)
		} else {
			kv[*request.Key] = *request.Value
		}
		response := protos.Response{
			Status: proto.Bool(true),
		}
		req.Response = &response
		responseChannel <- req
	}
	if request.Type.String() == "Iterate" {
		//一次取出所有的key吗?只取partitionKey
		response := protos.Response{
			Status: proto.Bool(true),
		}
		var iterate []string
		for k, _ := range keys {
			//			fmt.Println("key: "+k)
			iterate = append(iterate, k)
		}
		response.Value = iterate
		//fmt.Println("result:"+result)
		req.Response = &response
		responseChannel <- req
	}
	if request.Type.String() == "Flush" {
		if kv["Flush"] != "" && kv["Flush"] != "0" {
			sum, _ := strconv.Atoi(kv["Flush"])
			kv["Flush"] = strconv.Itoa(sum + 1)
		} else {
			kv["Flush"] = "1"
		}
		if kv["Flush"] == strconv.Itoa(len(servers)) {
			Accumulator()
			kv["Flush"] = "0"
		}
		response := protos.Response{
			Status: proto.Bool(true),
		}
		req.Response = &response
		responseChannel <- req
	}
}