Exemplo n.º 1
0
func Iterate() []string {
	GetLocalConnection()
	iType := protos.Type(protos.Type_value["Iterate"])
	command := &protos.Command{
		Type: &iType,
	}
	commandBytes, _ := proto.Marshal(command)
	conn.Write(commandBytes)
	//Create a data buffer of type byte slice with capacity of 4096
	var buf []byte
	data := make([]byte, 4096)
	n, _ := conn.Read(data)
	buf = append(buf, data[:n]...)
	for n == 4096 {
		n, _ = conn.Read(data)
		buf = append(buf, data[:n]...)
	}
	//println("iterate read bytes length:"+strconv.Itoa(n))
	//var buf bytes.Buffer
	//io.Copy(&buf, conn)
	//fmt.Println("total size:", buf.Len())
	response := new(protos.Response)
	proto.Unmarshal(buf, response)
	//fmt.Println("receive response:  "+response.String())
	return response.Value
}
Exemplo n.º 2
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()
}
Exemplo n.º 3
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()
}
Exemplo n.º 4
0
func Get(key string) string {
	GetConnectionFromKey(key)
	iType := protos.Type(protos.Type_value["Get"])
	command := &protos.Command{
		Type: &iType,
		Key:  &key,
	}
	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)
	//	fmt.Println("Decoding Protobuf message")
	response := new(protos.Response)
	proto.Unmarshal(data[0:n], response)
	//fmt.Println("receive response:  "+response.String())
	if response.Value == nil {
		return ""
	} else {
		return response.Value[0]
	}
}