Exemple #1
0
func dumpKey(key []byte) ([]byte, error) {
	buf := &bytes.Buffer{}
	e := &rdbEncoder{rdb.NewEncoder(buf)}
	err := e.encodeKey(key, true)
	if err != nil {
		return nil, err
	}
	if buf.Len() == 0 {
		return nil, nil
	}
	return buf.Bytes(), nil
}
Exemple #2
0
func (e *EncoderSuite) TestStringEncoding(c *C) {
	buf := &bytes.Buffer{}
	for _, t := range stringEncodingTests {
		e := rdb.NewEncoder(buf)
		e.EncodeType(rdb.TypeString)
		e.EncodeString([]byte(t.str))
		e.EncodeDumpFooter()
		expected, _ := base64.StdEncoding.DecodeString(t.res)
		c.Assert(buf.Bytes(), DeepEquals, expected, Commentf("%s - expected: %x, actual: %x", t.str, expected, buf.Bytes()))
		buf.Reset()
	}
}
Exemple #3
0
func EncodeDump(obj interface{}) ([]byte, error) {
	o, ok := obj.(objectEncoder)
	if !ok {
		return nil, errors.New("unsupported object type")
	}
	var b bytes.Buffer
	enc := rdb.NewEncoder(&b)
	if err := o.encodeType(enc); err != nil {
		return nil, err
	}
	if err := o.encodeValue(enc); err != nil {
		return nil, err
	}
	if err := enc.EncodeDumpFooter(); err != nil {
		return nil, errors.Trace(err)
	}
	return b.Bytes(), nil
}
Exemple #4
0
func Dump(obj interface{}) ([]byte, error) {
	var buf bytes.Buffer

	e := rdb.NewEncoder(&buf)

	switch v := obj.(type) {
	case String:
		e.EncodeType(rdb.TypeString)
		e.EncodeString(v)
	case Hash:
		e.EncodeType(rdb.TypeHash)
		e.EncodeLength(uint32(len(v)))

		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i].Field)
			e.EncodeString(v[i].Value)
		}
	case List:
		e.EncodeType(rdb.TypeList)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i])
		}
	case Set:
		e.EncodeType(rdb.TypeSet)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i])
		}
	case ZSet:
		e.EncodeType(rdb.TypeZSet)
		e.EncodeLength(uint32(len(v)))
		for i := 0; i < len(v); i++ {
			e.EncodeString(v[i].Member)
			e.EncodeFloat(v[i].Score)
		}
	default:
		return nil, fmt.Errorf("invalid dump type %T", obj)
	}

	e.EncodeDumpFooter()

	return buf.Bytes(), nil
}
Exemple #5
0
func NewEncoder(w io.Writer) *Encoder {
	return &Encoder{
		enc: rdb.NewEncoder(w),
		db:  -1,
	}
}