示例#1
0
// SLOTSINFO [start [count]]
func (h *Handler) SlotsInfo(arg0 interface{}, args [][]byte) (redis.Resp, error) {
	if len(args) > 2 {
		return toRespErrorf("len(args) = %d, expect <= 2", len(args))
	}

	s, err := session(arg0, args)
	if err != nil {
		return toRespError(err)
	}

	if m, err := s.Rpdb().SlotsInfo(s.DB(), iconvert(args)...); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		for i := uint32(0); i < rpdb.MaxSlotNum; i++ {
			v, ok := m[i]
			if ok {
				s := redis.NewArray()
				s.AppendInt(int64(i))
				s.AppendInt(v)
				resp.Append(s)
			}
		}
		return resp, nil
	}
}
示例#2
0
func request(cmd string, args ...interface{}) redis.Resp {
	resp := redis.NewArray()
	resp.AppendBulkBytes([]byte(cmd))
	for _, v := range args {
		resp.AppendBulkBytes([]byte(fmt.Sprintf("%v", v)))
	}
	return resp
}
示例#3
0
func doMigrate(addr string, timeout time.Duration, db uint32, bins []*rdb.BinEntry) error {
	c, err := getSockConn(addr, timeout)
	if err != nil {
		log.WarnErrorf(err, "connect to %s failed, timeout = %d", addr, timeout)
		return err
	}
	defer putSockConn(addr, c)

	cmd1 := redis.NewArray()
	cmd1.AppendBulkBytes([]byte("select"))
	cmd1.AppendBulkBytes([]byte(FormatUint(uint64(db))))

	if err := c.DoMustOK(cmd1, timeout); err != nil {
		log.WarnErrorf(err, "command select failed, addr = %s, db = %d", addr, db)
		return err
	}
	log.Debugf("command select ok, addr = %s, db = %d", addr, db)

	cmd2 := redis.NewArray()
	cmd2.AppendBulkBytes([]byte("slotsrestore"))
	for _, bin := range bins {
		cmd2.AppendBulkBytes(bin.Key)
		ttlms := int64(0)
		if bin.ExpireAt != 0 {
			if v, ok := ExpireAtToTTLms(bin.ExpireAt); ok && v > 0 {
				ttlms = v
			} else {
				ttlms = 1
			}
		}
		cmd2.AppendBulkBytes([]byte(FormatInt(ttlms)))
		cmd2.AppendBulkBytes(bin.Value)
	}

	if err := c.DoMustOK(cmd2, timeout); err != nil {
		log.WarnErrorf(err, "command restore failed, addr = %s, db = %d, len(bins) = %d", addr, db, len(bins))
		return err
	} else {
		log.Debugf("command restore ok, addr = %s, db = %d, len(bins) = %d", addr, db, len(bins))
		return nil
	}
}
示例#4
0
// SLOTSHASHKEY key [key...]
func (h *Handler) SlotsHashKey(arg0 interface{}, args [][]byte) (redis.Resp, error) {
	if len(args) == 0 {
		return toRespErrorf("len(args) = %d, expect != 1", len(args))
	}

	_, err := session(arg0, args)
	if err != nil {
		return toRespError(err)
	}

	resp := redis.NewArray()
	for _, key := range args {
		_, slot := rpdb.HashKeyToSlot(key)
		resp.AppendInt(int64(slot))
	}
	return resp, nil
}
示例#5
0
文件: set.go 项目: fengshao0907/rpdb
// SMEMBERS key
func (h *Handler) SMembers(arg0 interface{}, args [][]byte) (redis.Resp, error) {
	if len(args) != 1 {
		return toRespErrorf("len(args) = %d, expect = 1", len(args))
	}

	s, err := session(arg0, args)
	if err != nil {
		return toRespError(err)
	}

	if a, err := s.Rpdb().SMembers(s.DB(), iconvert(args)...); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		for _, v := range a {
			resp.AppendBulkBytes(v)
		}
		return resp, nil
	}
}
示例#6
0
// SLOTSMGRTTAGSLOT host port timeout slot
func (h *Handler) SlotsMgrtTagSlot(arg0 interface{}, args [][]byte) (redis.Resp, error) {
	if len(args) != 4 {
		return toRespErrorf("len(args) = %d, expect = 4", len(args))
	}

	s, err := session(arg0, args)
	if err != nil {
		return toRespError(err)
	}

	if n, err := s.Rpdb().SlotsMgrtTagSlot(s.DB(), iconvert(args)...); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		resp.AppendInt(n)
		if n != 0 {
			resp.AppendInt(1)
		} else {
			resp.AppendInt(0)
		}
		return resp, nil
	}
}