Esempio n. 1
0
File: prot.go Progetto: Netflix/rend
func (t TextProt) Prepend(rw *bufio.ReadWriter, key []byte, value []byte) error {
	if _, err := fmt.Fprintf(rw, "prepend %s 0 0 %v\r\n%s\r\n", string(key), len(value), string(value)); err != nil {
		return err
	}

	rw.Flush()

	_, err := rw.ReadString('\n')
	return err
}
Esempio n. 2
0
func (t TextProt) BatchGet(rw *bufio.ReadWriter, keys [][]byte) ([][]byte, error) {
	if VERBOSE {
		fmt.Printf("Getting keys %v\n", keys)
	}

	cmd := []byte("get")
	space := byte(' ')
	end := []byte("\r\n")

	for _, key := range keys {
		cmd = append(cmd, space)
		cmd = append(cmd, key...)
	}

	cmd = append(cmd, end...)

	if _, err := fmt.Fprint(rw, string(cmd)); err != nil {
		return nil, err
	}

	rw.Flush()

	var ret [][]byte

	for {
		// read the header line
		response, err := rw.ReadString('\n')
		if err != nil {
			return nil, err
		}
		if VERBOSE {
			fmt.Println(response)
		}

		if strings.TrimSpace(response) == "END" {
			if VERBOSE {
				fmt.Println("End of batch response")
			}
			return ret, nil
		}

		// then read the value
		response, err = rw.ReadString('\n')
		if err != nil {
			return nil, err
		}
		if VERBOSE {
			fmt.Println(response)
		}

		ret = append(ret, []byte(response))
	}
}
Esempio n. 3
0
/* read client data, if client closed,then close server conn
 *
 */
func read(rwr *bufio.ReadWriter, userId int64) {
	var p byte = '\n'
	var err error
	status := "Ok"
	if _, err = rwr.ReadString(p); err != nil {
		conn := us.conns[userId]
		if closeErr := conn.Close(); closeErr != nil {
			status = "Fail"
		}
		delete(us.conns, userId)
		del(userId)
		log.Debug("close%s:\t%d", status, userId)
	}
}
Esempio n. 4
0
// Run performs a single synchronous command on the GarageFeed.
func (g *GarageFeed) run(req garageReq, rw *bufio.ReadWriter) (result string, err error) {
	if rw.Reader.Buffered() > 0 {
		panic("should be no bytes pending")
	}

	// Write the command, flush the buffer.
	_, err = rw.WriteString(fmt.Sprintf("%s\n", req.command))
	if err != nil {
		return "", err
	}
	rw.Flush()

	// Now, wait for a single line respnse.
	return rw.ReadString('\n')
}
Esempio n. 5
0
func (t TextProt) Touch(rw *bufio.ReadWriter, key []byte) error {
	strKey := string(key)
	if VERBOSE {
		fmt.Printf("Touching key %s\n", strKey)
	}

	if _, err := fmt.Fprintf(rw, "touch %s %v\r\n", strKey, common.Exp()); err != nil {
		return err
	}

	rw.Flush()

	response, err := rw.ReadString('\n')
	if err != nil {
		return err
	}
	if VERBOSE {
		fmt.Println(response)
		fmt.Printf("Touched key %s\n", strKey)
	}
	return nil
}
Esempio n. 6
0
func (t TextProt) Delete(rw *bufio.ReadWriter, key []byte) error {
	strKey := string(key)
	if VERBOSE {
		fmt.Printf("Deleting key %s\n", strKey)
	}

	if _, err := fmt.Fprintf(rw, "delete %s\r\n", strKey); err != nil {
		return err
	}

	rw.Flush()

	response, err := rw.ReadString('\n')
	if err != nil {
		return err
	}
	if VERBOSE {
		fmt.Println(response)
		fmt.Printf("Deleted key %s\r\n", strKey)
	}
	return nil
}
Esempio n. 7
0
func (t TextProt) Replace(rw *bufio.ReadWriter, key []byte, value []byte) error {
	strKey := string(key)
	if VERBOSE {
		fmt.Printf("Replacing key %s, value of length %v\n", strKey, len(value))
	}

	if _, err := fmt.Fprintf(rw, "replace %s 0 0 %v\r\n%s\r\n", strKey, len(value), string(value)); err != nil {
		return err
	}

	rw.Flush()

	response, err := rw.ReadString('\n')
	if err != nil {
		return err
	}

	if VERBOSE {
		fmt.Println(response)
		fmt.Printf("Replaced key %s\n", strKey)
	}

	return nil
}
Esempio n. 8
0
func (t TextProt) Get(rw *bufio.ReadWriter, key []byte) ([]byte, error) {
	strKey := string(key)
	if VERBOSE {
		fmt.Printf("Getting key %s\n", strKey)
	}

	if _, err := fmt.Fprintf(rw, "get %s\r\n", strKey); err != nil {
		return nil, err
	}

	rw.Flush()

	// read the header line
	response, err := rw.ReadString('\n')
	if err != nil {
		return nil, err
	}
	if VERBOSE {
		fmt.Println(response)
	}

	if strings.TrimSpace(response) == "END" {
		if VERBOSE {
			fmt.Println("Empty response / cache miss")
		}
		return []byte{}, nil
	}

	// then read the value
	response, err = rw.ReadString('\n')
	if err != nil {
		return nil, err
	}
	if VERBOSE {
		fmt.Println(response)
	}

	// then read the END
	response, err = rw.ReadString('\n')
	if err != nil {
		return nil, err
	}
	if VERBOSE {
		fmt.Println(response)
		fmt.Printf("Got key %s\n", key)
	}
	return []byte(response), nil
}