Example #1
0
func (r *rdbReader) readLength() (uint32, error) {
	length, encoded, err := r.readEncodedLength()
	if err == nil && encoded {
		err = errors.New("encoded-length")
	}
	return length, err
}
Example #2
0
func (e *Encoder) EncodeObject(db uint32, key []byte, expireat uint64, obj interface{}) error {
	o, ok := obj.(objectEncoder)
	if !ok {
		return errors.New("unsupported object type")
	}
	if e.db == -1 || uint32(e.db) != db {
		e.db = int64(db)
		if err := e.enc.EncodeDatabase(int(db)); err != nil {
			return errors.Trace(err)
		}
	}
	if expireat != 0 {
		if err := e.enc.EncodeExpiry(expireat); err != nil {
			return errors.Trace(err)
		}
	}
	if err := o.encodeType(e.enc); err != nil {
		return err
	}
	if err := e.enc.EncodeString(key); err != nil {
		return errors.Trace(err)
	}
	if err := o.encodeValue(e.enc); err != nil {
		return err
	}
	return nil
}
Example #3
0
func (l *Loader) Footer() error {
	crc1 := l.crc.Sum64()
	if crc2, err := l.readUint64(); err != nil {
		return err
	} else if crc1 != crc2 {
		return errors.New("checksum validation failed")
	}
	return nil
}
Example #4
0
func (d *decoder) initObject(obj interface{}) {
	if d.err != nil {
		return
	}
	if d.obj != nil {
		d.err = errors.New("invalid object, init again")
	} else {
		d.obj = obj
	}
}
Example #5
0
func (d *decoder) Rpush(key, value []byte) {
	if d.err != nil {
		return
	}
	switch l := d.obj.(type) {
	default:
		d.err = errors.New("invalid object, not a list")
	case List:
		d.obj = append(l, value)
	}
}
Example #6
0
func (d *decoder) Sadd(key, member []byte) {
	if d.err != nil {
		return
	}
	switch s := d.obj.(type) {
	default:
		d.err = errors.New("invalid object, not a set")
	case Set:
		d.obj = append(s, member)
	}
}
Example #7
0
func (d *decoder) Zadd(key []byte, score float64, member []byte) {
	if d.err != nil {
		return
	}
	switch z := d.obj.(type) {
	default:
		d.err = errors.New("invalid object, not a zset")
	case ZSet:
		v := &ZSetElement{Member: member, Score: score}
		d.obj = append(z, v)
	}
}
Example #8
0
func (d *decoder) Hset(key, field, value []byte) {
	if d.err != nil {
		return
	}
	switch h := d.obj.(type) {
	default:
		d.err = errors.New("invalid object, not a hashmap")
	case Hash:
		v := &HashElement{Field: field, Value: value}
		d.obj = append(h, v)
	}
}
Example #9
0
func ParseArgs(resp Resp) (cmd string, args [][]byte, err error) {
	var array []Resp
	if o, ok := resp.(*Array); !ok {
		return "", nil, errors.Errorf("expect array, but got type = '%s'", resp.Type())
	} else if o == nil || len(o.Value) == 0 {
		return "", nil, errors.New("request is an empty array")
	} else {
		array = o.Value
	}
	slices := make([][]byte, 0, len(array))
	for i, resp := range array {
		if o, ok := resp.(*BulkBytes); !ok {
			return "", nil, errors.Errorf("args[%d], expect bulkbytes, but got '%s'", i, resp.Type())
		} else if i == 0 && len(o.Value) == 0 {
			return "", nil, errors.New("command is empty")
		} else {
			slices = append(slices, o.Value)
		}
	}
	return strings.ToLower(string(slices[0])), slices[1:], nil
}
Example #10
0
func (l *Loader) Header() error {
	header := make([]byte, 9)
	if err := l.readFull(header); err != nil {
		return err
	}
	if !bytes.Equal(header[:5], []byte("REDIS")) {
		return errors.New("verify magic string, invalid file format")
	}
	if version, err := strconv.ParseInt(string(header[5:]), 10, 64); err != nil {
		return errors.Trace(err)
	} else if version <= 0 || version > Version {
		return errors.Errorf("verify version, invalid RDB version number %d", version)
	}
	return nil
}
Example #11
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
}