Esempio n. 1
0
func (mc *Connection) store(command, key string, flags uint16, timeout uint64, value []byte, cas uint64) (stored bool) {
	if len(value) > 1000000 {
		return false
	}

	mc.setDeadline()
	// <command name> <key> <flags> <exptime> <bytes> [noreply]\r\n
	mc.writestrings(command, " ", key, " ")
	mc.write(strconv.AppendUint(nil, uint64(flags), 10))
	mc.writestring(" ")
	mc.write(strconv.AppendUint(nil, timeout, 10))
	mc.writestring(" ")
	mc.write(strconv.AppendInt(nil, int64(len(value)), 10))
	if cas != 0 {
		mc.writestring(" ")
		mc.write(strconv.AppendUint(nil, cas, 10))
	}
	mc.writestring("\r\n")
	// <data block>\r\n
	mc.write(value)
	mc.writestring("\r\n")
	reply := mc.readline()
	if strings.Contains(reply, "ERROR") {
		panic(NewError("Server error"))
	}
	return strings.HasPrefix(reply, "STORED")
}
Esempio n. 2
0
func formatTextValue(value interface{}) ([]byte, error) {
	switch v := value.(type) {
	case int8:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int16:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int32:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int64:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case uint8:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint16:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint32:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint64:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case float32:
		return strconv.AppendFloat(nil, float64(v), 'f', -1, 64), nil
	case float64:
		return strconv.AppendFloat(nil, float64(v), 'f', -1, 64), nil
	case []byte:
		return v, nil
	case string:
		return hack.Slice(v), nil
	default:
		return nil, errors.Errorf("invalid type %T", value)
	}
}
Esempio n. 3
0
func write_cursor(x, y int) {
	outbuf.WriteString("\033[")
	outbuf.Write(strconv.AppendUint(intbuf, uint64(y+1), 10))
	outbuf.WriteString(";")
	outbuf.Write(strconv.AppendUint(intbuf, uint64(x+1), 10))
	outbuf.WriteString("H")
}
Esempio n. 4
0
// Version to string
func (v Version) String() string {
	b := make([]byte, 0, 5)
	b = strconv.AppendUint(b, v.Major, 10)
	b = append(b, '.')
	b = strconv.AppendUint(b, v.Minor, 10)
	b = append(b, '.')
	b = strconv.AppendUint(b, v.Patch, 10)

	if len(v.Pre) > 0 {
		b = append(b, '-')
		b = append(b, v.Pre[0].String()...)

		for _, pre := range v.Pre[1:] {
			b = append(b, '.')
			b = append(b, pre.String()...)
		}
	}

	if len(v.Build) > 0 {
		b = append(b, '+')
		b = append(b, v.Build[0]...)

		for _, build := range v.Build[1:] {
			b = append(b, '.')
			b = append(b, build...)
		}
	}

	return string(b)
}
Esempio n. 5
0
func write_sgr(fg, bg Attribute) {
	outbuf.WriteString("\033[3")
	outbuf.Write(strconv.AppendUint(intbuf, uint64(fg-1), 10))
	outbuf.WriteString(";4")
	outbuf.Write(strconv.AppendUint(intbuf, uint64(bg-1), 10))
	outbuf.WriteString("m")
}
Esempio n. 6
0
func (s *testFlusherSink) appendNumber(v interface{}) {
	switch n := v.(type) {
	case int:
		s.buf = strconv.AppendInt(s.buf, int64(n), 10)
	case uint:
		s.buf = strconv.AppendUint(s.buf, uint64(n), 10)
	case int64:
		s.buf = strconv.AppendInt(s.buf, n, 10)
	case uint64:
		s.buf = strconv.AppendUint(s.buf, n, 10)
	case int32:
		s.buf = strconv.AppendInt(s.buf, int64(n), 10)
	case uint32:
		s.buf = strconv.AppendUint(s.buf, uint64(n), 10)
	case int16:
		s.buf = strconv.AppendInt(s.buf, int64(n), 10)
	case uint16:
		s.buf = strconv.AppendUint(s.buf, uint64(n), 10)
	case int8:
		s.buf = strconv.AppendInt(s.buf, int64(n), 10)
	case uint8:
		s.buf = strconv.AppendUint(s.buf, uint64(n), 10)
	case float64:
		s.buf = strconv.AppendFloat(s.buf, n, 'f', -1, 64)
	case float32:
		s.buf = strconv.AppendFloat(s.buf, float64(n), 'f', -1, 32)
	}
}
Esempio n. 7
0
// Bytes formats the message in a RFC5424 format.
func (msg *Message) Bytes() []byte {
	var b []byte

	// Format priority: <pri>, e.g. <0>, <191>
	b = append(b, priorityStart)
	b = strconv.AppendUint(b, uint64(msg.Priority), 10)
	b = append(b, priorityEnd)

	// Add optional version and a space, e.g. 1, 10
	if msg.Version != 0 {
		b = strconv.AppendUint(b, uint64(msg.Version), 10)
	}
	b = append(b, spaceByte)

	// Add values, with a nil value for a zero value.
	b = addTimestamp(b, msg.Timestamp)
	b = addValue(b, msg.Hostname)
	b = addValue(b, msg.Appname)
	b = addValue(b, msg.ProcessID)
	b = addValue(b, msg.MessageID)

	b = addData(b, msg.Data)

	if msg.Message != "" {
		b = append(b, spaceByte)
		b = append(b, msg.Message...)
	}

	return b
}
Esempio n. 8
0
func (c *conn) appendNumber(v interface{}) {
	switch n := v.(type) {
	case int:
		c.buf = strconv.AppendInt(c.buf, int64(n), 10)
	case uint:
		c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
	case int64:
		c.buf = strconv.AppendInt(c.buf, n, 10)
	case uint64:
		c.buf = strconv.AppendUint(c.buf, n, 10)
	case int32:
		c.buf = strconv.AppendInt(c.buf, int64(n), 10)
	case uint32:
		c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
	case int16:
		c.buf = strconv.AppendInt(c.buf, int64(n), 10)
	case uint16:
		c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
	case int8:
		c.buf = strconv.AppendInt(c.buf, int64(n), 10)
	case uint8:
		c.buf = strconv.AppendUint(c.buf, uint64(n), 10)
	case float64:
		c.buf = strconv.AppendFloat(c.buf, n, 'f', -1, 64)
	case float32:
		c.buf = strconv.AppendFloat(c.buf, float64(n), 'f', -1, 32)
	}
}
Esempio n. 9
0
func BuildValue(goval interface{}) (v Value, err error) {
	switch bindVal := goval.(type) {
	case nil:
		// no op
	case int:
		v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
	case int32:
		v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
	case int64:
		v = Value{Numeric(strconv.AppendInt(nil, int64(bindVal), 10))}
	case uint:
		v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
	case uint32:
		v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
	case uint64:
		v = Value{Numeric(strconv.AppendUint(nil, uint64(bindVal), 10))}
	case float64:
		v = Value{Fractional(strconv.AppendFloat(nil, bindVal, 'f', -1, 64))}
	case string:
		v = Value{String([]byte(bindVal))}
	case []byte:
		v = Value{String(bindVal)}
	case time.Time:
		v = Value{String([]byte(bindVal.Format("'2006-01-02 15:04:05'")))}
	case Numeric, Fractional, String:
		v = Value{bindVal.(InnerValue)}
	case Value:
		v = bindVal
	default:
		return Value{}, fmt.Errorf("Unsupported bind variable type %T: %v", goval, goval)
	}
	return v, nil
}
Esempio n. 10
0
func (c *conn) writeStoreCommand(cmd, key string, value []byte, flags uint32, timeout int32, cas uint64) (err error) {
	if len(value) > 1000000 {
		return protocolError("max value size, greate than 1mb")
	}
	// <command name> <key> <flags> <exptime> <bytes> [noreply]\r\n
	c.bw.WriteString(cmd)
	c.bw.WriteByte(space)

	c.bw.WriteString(key)
	c.bw.WriteByte(space)

	c.bw.Write(strconv.AppendUint(c.numScratch[:0], uint64(flags), 10))
	c.bw.WriteByte(space)

	c.bw.Write(strconv.AppendInt(c.numScratch[:0], int64(timeout), 10))
	c.bw.WriteByte(space)

	c.bw.Write(strconv.AppendInt(c.numScratch[:0], int64(len(value)), 10))
	if cas != 0 {
		c.bw.WriteByte(space)
		c.bw.Write(strconv.AppendUint(c.numScratch[:0], cas, 10))
	}
	c.bw.Write(crlf)
	// <data block>\r\n
	c.bw.Write(value)
	_, err = c.bw.Write(crlf)
	return
}
Esempio n. 11
0
func encodeDefault(object interface{}) (buffer []byte, err error) {
	switch object.(type) {
	case bool:
		buffer = strconv.AppendBool(buffer, object.(bool))
	case int:
		buffer = strconv.AppendInt(buffer, int64(object.(int)), _NUMERIC_BASE)
	case int8:
		buffer = strconv.AppendInt(buffer, int64(object.(int8)), _NUMERIC_BASE)
	case int16:
		buffer = strconv.AppendInt(buffer, int64(object.(int16)), _NUMERIC_BASE)
	case int32:
		buffer = strconv.AppendInt(buffer, int64(object.(int32)), _NUMERIC_BASE)
	case int64:
		buffer = strconv.AppendInt(buffer, object.(int64), _NUMERIC_BASE)
	case uint:
		buffer = strconv.AppendUint(buffer, uint64(object.(uint)), _NUMERIC_BASE)
	case uint8:
		buffer = strconv.AppendUint(buffer, uint64(object.(uint8)), _NUMERIC_BASE)
	case uint16:
		buffer = strconv.AppendUint(buffer, uint64(object.(uint16)), _NUMERIC_BASE)
	case uint32:
		buffer = strconv.AppendUint(buffer, uint64(object.(uint32)), _NUMERIC_BASE)
	case uint64:
		buffer = strconv.AppendUint(buffer, object.(uint64), _NUMERIC_BASE)
	case string:
		buffer = []byte(object.(string))
	case []byte:
		buffer = object.([]byte)
	default:
		err = errors.New("Invalid object for default encode")
	}
	return
}
Esempio n. 12
0
File: utils.go Progetto: xtfly/gofd
func (p Percent) String() string {
	var buf [12]byte
	b := strconv.AppendUint(buf[:0], uint64(p)/1000, 10)
	n := len(b)
	b = strconv.AppendUint(b, 1000+uint64(p)%1000, 10)
	b[n] = '.'
	return string(append(b, '%'))
}
Esempio n. 13
0
func (t TermVT100) Pos(b []byte, x, y int) []byte {
	b = b[:0]
	b = append(b, 27, '[')
	b = strconv.AppendUint(b, uint64(y), 10)
	b = append(b, ';')
	b = strconv.AppendUint(b, uint64(x), 10)
	b = append(b, 'f')
	return b
}
Esempio n. 14
0
func main() {
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))
}
Esempio n. 15
0
func (e *jsonEncDriver) EncodeUint(v uint64) {
	if x := e.h.IntegerAsString; x == 'A' || x == 'L' && v > 1<<53 {
		e.w.writen1('"')
		e.w.writeb(strconv.AppendUint(e.b[:0], v, 10))
		e.w.writen1('"')
		return
	}
	e.w.writeb(strconv.AppendUint(e.b[:0], v, 10))
}
Esempio n. 16
0
func (v *Version) String() string {
	b := make([]byte, 0, 5)
	b = strconv.AppendUint(b, v.Major, 10)
	b = append(b, '.')
	b = strconv.AppendUint(b, v.Minor, 10)
	b = append(b, '.')
	b = strconv.AppendUint(b, v.Patch, 10)

	return string(b)
}
Esempio n. 17
0
File: append.go Progetto: anmic/pg
func appendIface(b []byte, vi interface{}, quote bool) []byte {
	switch v := vi.(type) {
	case nil:
		return appendNull(b, quote)
	case bool:
		return appendBool(b, v)
	case int8:
		return strconv.AppendInt(b, int64(v), 10)
	case int16:
		return strconv.AppendInt(b, int64(v), 10)
	case int32:
		return strconv.AppendInt(b, int64(v), 10)
	case int64:
		return strconv.AppendInt(b, int64(v), 10)
	case int:
		return strconv.AppendInt(b, int64(v), 10)
	case uint8:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint16:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint32:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint64:
		return strconv.AppendUint(b, v, 10)
	case uint:
		return strconv.AppendUint(b, uint64(v), 10)
	case float32:
		return appendFloat(b, float64(v))
	case float64:
		return appendFloat(b, v)
	case string:
		return appendString(b, v, quote)
	case time.Time:
		return appendTime(b, v, quote)
	case []byte:
		return appendBytes(b, v, quote)
	case []string:
		return appendStringSlice(b, v, quote)
	case []int:
		return appendIntSlice(b, v, quote)
	case []int64:
		return appendInt64Slice(b, v, quote)
	case []float64:
		return appendFloat64Slice(b, v, quote)
	case map[string]string:
		return appendStringStringMap(b, v, quote)
	case QueryAppender:
		return v.AppendQuery(b)
	case driver.Valuer:
		return appendDriverValuer(b, v, quote)
	default:
		return appendValue(b, reflect.ValueOf(vi), quote)
	}
}
Esempio n. 18
0
// AppendRecord appends the (key,value) record to data and returns the
// appended slice.
func AppendRecord(data, key, value []byte) []byte {
	data = append(data, '+')
	data = strconv.AppendUint(data, uint64(len(key)), 10)
	data = append(data, ',')
	data = strconv.AppendUint(data, uint64(len(value)), 10)
	data = append(data, ':')
	data = append(data, key...)
	data = append(data, []byte("->")...)
	data = append(data, value...)
	return append(data, '\n')
}
Esempio n. 19
0
func write_sgr_fg(a Attribute) {
	switch output_mode {
	case Output256, Output216, OutputGrayscale:
		outbuf.WriteString("\033[38;5;")
		outbuf.Write(strconv.AppendUint(intbuf, uint64(a-1), 10))
		outbuf.WriteString("m")
	default:
		outbuf.WriteString("\033[3")
		outbuf.Write(strconv.AppendUint(intbuf, uint64(a-1), 10))
		outbuf.WriteString("m")
	}
}
Esempio n. 20
0
func encodeReq(buf []byte, args []string) []byte {
	buf = append(buf, '*')
	buf = strconv.AppendUint(buf, uint64(len(args)), 10)
	buf = append(buf, '\r', '\n')
	for _, arg := range args {
		buf = append(buf, '$')
		buf = strconv.AppendUint(buf, uint64(len(arg)), 10)
		buf = append(buf, '\r', '\n')
		buf = append(buf, []byte(arg)...)
		buf = append(buf, '\r', '\n')
	}
	return buf
}
Esempio n. 21
0
func (v vector) String() string {
	buf := []byte{'{'}
	for i, x := range v.list {
		if i > 0 {
			buf = append(buf, ' ')
		}
		buf = strconv.AppendUint(buf, uint64(x.id), 10)
		buf = append(buf, ':')
		buf = strconv.AppendUint(buf, uint64(x.t), 10)
	}
	buf = append(buf, '}')
	return string(buf)
}
Esempio n. 22
0
// String returns sequence value s as a seq-number or seq-range string.
func (s seq) String() string {
	if s.start == s.stop {
		if s.start == 0 {
			return "*"
		}
		return strconv.FormatUint(uint64(s.start), 10)
	}
	b := strconv.AppendUint(make([]byte, 0, 24), uint64(s.start), 10)
	if s.stop == 0 {
		return string(append(b, ':', '*'))
	}
	return string(strconv.AppendUint(append(b, ':'), uint64(s.stop), 10))
}
Esempio n. 23
0
func ExampleAppendUint() {
	b10 := []byte("uint (base 10):")
	b10 = strconv.AppendUint(b10, 42, 10)
	fmt.Println(string(b10))

	b16 := []byte("uint (base 16):")
	b16 = strconv.AppendUint(b16, 42, 16)
	fmt.Println(string(b16))

	// Output:
	// uint (base 10):42
	// uint (base 16):2a
}
Esempio n. 24
0
func (s SID) String() string {
	if len(s) < 8 || s[0] != sidRevision || len(s) != (int(s[1])*4)+8 {
		return ""
	}

	ret := []byte("S-1-")
	ret = strconv.AppendUint(ret, binary.BigEndian.Uint64(s[:8])&0xFFFFFFFFFFFF, 10)

	for i := 0; i < int(s[1]); i++ {
		ret = append(ret, "-"...)
		ret = strconv.AppendUint(ret, uint64(binary.LittleEndian.Uint32(s[8+i*4:])), 10)
	}

	return string(ret)
}
Esempio n. 25
0
func write_sgr(fg, bg uint) {
	outbuf.WriteString("\033[3")
	if fg > 7 {
		// xterm-256 color
		outbuf.WriteString("8;5;")
	}
	outbuf.Write(strconv.AppendUint(intbuf, uint64(fg), 10))
	outbuf.WriteString(";4")
	if bg > 7 {
		// xterm-256 color
		outbuf.WriteString("8;5;")
	}
	outbuf.Write(strconv.AppendUint(intbuf, uint64(bg), 10))
	outbuf.WriteString("m")
}
Esempio n. 26
0
func Append(b []byte, v interface{}, quote bool) []byte {
	switch v := v.(type) {
	case nil:
		return AppendNull(b, quote)
	case bool:
		return appendBool(b, v)
	case int8:
		return strconv.AppendInt(b, int64(v), 10)
	case int16:
		return strconv.AppendInt(b, int64(v), 10)
	case int32:
		return strconv.AppendInt(b, int64(v), 10)
	case int64:
		return strconv.AppendInt(b, int64(v), 10)
	case int:
		return strconv.AppendInt(b, int64(v), 10)
	case uint8:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint16:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint32:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint64:
		return strconv.AppendUint(b, v, 10)
	case uint:
		return strconv.AppendUint(b, uint64(v), 10)
	case float32:
		return appendFloat(b, float64(v))
	case float64:
		return appendFloat(b, v)
	case string:
		return AppendString(b, v, quote)
	case time.Time:
		return AppendTime(b, v, quote)
	case []byte:
		return appendBytes(b, v, quote)
	case ValueAppender:
		b, err := v.AppendValue(b, quote)
		if err != nil {
			panic(err)
		}
		return b
	case driver.Valuer:
		return appendDriverValuer(b, v, quote)
	default:
		return appendValue(b, reflect.ValueOf(v), quote)
	}
}
Esempio n. 27
0
func roleCommand(c *client) error {
	if len(c.args) != 0 {
		return ErrCmdParams
	}

	c.app.m.Lock()
	slaveof := c.app.cfg.SlaveOf
	c.app.m.Unlock()

	isMaster := len(slaveof) == 0

	ay := make([]interface{}, 0, 5)

	var lastId int64 = 0

	stat, _ := c.app.ldb.ReplicationStat()
	if stat != nil {
		lastId = int64(stat.LastID)
	}

	if isMaster {
		ay = append(ay, []byte("master"))
		ay = append(ay, lastId)

		items := make([]interface{}, 0, 3)

		c.app.slock.Lock()
		for addr, slave := range c.app.slaves {
			host, port, _ := splitHostPort(addr)

			items = append(items, []interface{}{[]byte(host),
				strconv.AppendUint(nil, uint64(port), 10),
				strconv.AppendUint(nil, slave.lastLogID.Get(), 10)})
		}
		c.app.slock.Unlock()
		ay = append(ay, items)
	} else {
		host, port, _ := splitHostPort(slaveof)
		ay = append(ay, []byte("slave"))
		ay = append(ay, []byte(host))
		ay = append(ay, int64(port))
		ay = append(ay, []byte(replStatetring(c.app.m.state.Get())))
		ay = append(ay, lastId)
	}

	c.resp.writeArray(ay)
	return nil
}
Esempio n. 28
0
// ValueFromBytes builds a Value using typ and val. It ensures that val
// matches the requested type. If type is an integral it's converted to
// a cannonical form. Otherwise, the original representation is preserved.
func ValueFromBytes(typ querypb.Type, val []byte) (v Value, err error) {
	switch {
	case IsSigned(typ):
		signed, err := strconv.ParseInt(string(val), 0, 64)
		if err != nil {
			return NULL, err
		}
		v = MakeTrusted(typ, strconv.AppendInt(nil, signed, 10))
	case IsUnsigned(typ):
		unsigned, err := strconv.ParseUint(string(val), 0, 64)
		if err != nil {
			return NULL, err
		}
		v = MakeTrusted(typ, strconv.AppendUint(nil, unsigned, 10))
	case typ == Tuple:
		return NULL, errors.New("tuple not allowed for ValueFromBytes")
	case IsFloat(typ) || typ == Decimal:
		_, err := strconv.ParseFloat(string(val), 64)
		if err != nil {
			return NULL, err
		}
		// After verification, we preserve the original representation.
		fallthrough
	default:
		v = MakeTrusted(typ, val)
	}
	return v, nil
}
Esempio n. 29
0
func (w *WriteBuffer) AppendString(s string) {
	w.b = append(w.b, StringReply)
	w.b = strconv.AppendUint(w.b, uint64(len(s)), 10)
	w.b = append(w.b, '\r', '\n')
	w.b = append(w.b, s...)
	w.b = append(w.b, '\r', '\n')
}
Esempio n. 30
0
func (w *WriteBuffer) AppendBytes(p []byte) {
	w.b = append(w.b, StringReply)
	w.b = strconv.AppendUint(w.b, uint64(len(p)), 10)
	w.b = append(w.b, '\r', '\n')
	w.b = append(w.b, p...)
	w.b = append(w.b, '\r', '\n')
}