Esempio n. 1
0
File: zset.go Progetto: CowLeo/qdb
// ZSCORE key member
func ZScoreCmd(s Session, args [][]byte) (redis.Resp, error) {
	if v, ok, err := s.Store().ZScore(s.DB(), args); err != nil {
		return toRespError(err)
	} else if !ok {
		return redis.NewBulkBytes(nil), nil
	} else {
		return redis.NewBulkBytes(store.FormatFloat(v)), nil
	}
}
Esempio n. 2
0
File: hash.go Progetto: cuiwm/reborn
// HGET key field
func HGetCmd(s Session, args [][]byte) (redis.Resp, error) {
	if b, err := s.Store().HGet(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		return redis.NewBulkBytes(b), nil
	}
}
Esempio n. 3
0
File: misc.go Progetto: vebin/reborn
// ECHO text
func EchoCmd(s Session, args [][]byte) (redis.Resp, error) {
	if len(args) != 1 {
		return toRespErrorf("len(args) = %d, expect = 1", len(args))
	}

	return redis.NewBulkBytes(args[0]), nil
}
Esempio n. 4
0
File: misc.go Progetto: vebin/reborn
// INFO [section]
func InfoCmd(s Session, args [][]byte) (redis.Resp, error) {
	if len(args) != 0 && len(args) != 1 {
		return toRespErrorf("len(args) = %d, expect = 0|1", len(args))
	}

	c, _ := s.(*conn)
	if c == nil {
		return nil, errors.New("invalid connection")
	}

	section := "all"
	if len(args) == 1 {
		section = strings.ToLower(string(args[0]))
	}

	var b bytes.Buffer

	switch section {
	case "database":
		c.h.infoDataBase(&b)
	case "config":
		c.h.infoConfig(&b)
	case "clients":
		c.h.infoClients(&b)
	case "replication":
		c.h.infoReplication(&b)
	default:
		// all
		c.h.infoAll(&b)
	}

	fmt.Fprintf(&b, "\r\n")

	return redis.NewBulkBytes(b.Bytes()), nil
}
Esempio n. 5
0
File: zset.go Progetto: CowLeo/qdb
// ZINCRBY key delta member
func ZIncrByCmd(s Session, args [][]byte) (redis.Resp, error) {
	if v, err := s.Store().ZIncrBy(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		return redis.NewBulkBytes(store.FormatFloat(v)), nil
	}
}
Esempio n. 6
0
File: list.go Progetto: cuiwm/reborn
// LINDEX key index
func LIndexCmd(s Session, args [][]byte) (redis.Resp, error) {
	if v, err := s.Store().LIndex(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		return redis.NewBulkBytes(v), nil
	}
}
Esempio n. 7
0
File: string.go Progetto: CowLeo/qdb
// SET key value [EX seconds] [PX milliseconds] [NX|XX]
func SetCmd(s Session, args [][]byte) (redis.Resp, error) {
	if err := s.Store().Set(s.DB(), args); err != nil && errors2.ErrorNotEqual(err, store.ErrSetAborted) {
		return toRespError(err)
	} else if errors2.ErrorEqual(err, store.ErrSetAborted) {
		return redis.NewBulkBytes(nil), nil
	} else {
		return redis.NewString("OK"), nil
	}
}
Esempio n. 8
0
File: zset.go Progetto: CowLeo/qdb
// ZREVRANK key member
func ZRevRankCmd(s Session, args [][]byte) (redis.Resp, error) {
	if v, err := s.Store().ZRevRank(s.DB(), args); err != nil {
		return toRespError(err)
	} else if v >= 0 {
		return redis.NewInt(v), nil
	} else {
		return redis.NewBulkBytes(nil), nil
	}
}
Esempio n. 9
0
File: keys.go Progetto: CowLeo/qdb
// DUMP key
func DumpCmd(s Session, args [][]byte) (redis.Resp, error) {
	if x, err := s.Store().Dump(s.DB(), args); err != nil {
		return toRespError(err)
	} else if dump, err := rdb.EncodeDump(x); err != nil {
		return toRespError(err)
	} else {
		return redis.NewBulkBytes(dump), nil
	}
}
Esempio n. 10
0
func RegisterBulkReply(name string, f CommandBulkStringFunc, flag CommandFlag) {
	v := func(s Session, args [][]byte) (redis.Resp, error) {
		r, err := f(s, args)
		if err != nil {
			return toRespError(err)
		}
		return redis.NewBulkBytes(r), nil
	}

	register(name, v, flag)
}
Esempio n. 11
0
func (s *testReplSuite) testReplication(c *C, master testReplNode, slave testReplNode) {
	// first let both stop replication
	s.doCmdMustOK(c, master.Port(), "SLAVEOF", "NO", "ONE")
	s.doCmdMustOK(c, slave.Port(), "SLAVEOF", "NO", "ONE")

	s.doCmdMustOK(c, master.Port(), "SET", "a", "100")

	offset := int64(-1)
	// slaveof, will do full sync first, must support psync
	nc := slave.Slaveof(c, master.Port())
	defer nc.Close(c)

	s.waitAndCheckSyncOffset(c, slave, offset)

	resp := s.doCmd(c, slave.Port(), "GET", "a")
	c.Assert(slave.SyncOffset(c), Not(Equals), int64(-1))
	c.Assert(resp, DeepEquals, redis.NewBulkBytesWithString("100"))

	// write to a slave, must error
	resp = s.doCmd(c, slave.Port(), "SET", "e", "1")
	c.Assert(resp, FitsTypeOf, (*redis.Error)(nil))
	c.Assert(resp.(*redis.Error).Value, Matches, "READONLY.*")

	s.doCmdMustOK(c, master.Port(), "SET", "b", "100")

	time.Sleep(500 * time.Millisecond)
	resp = s.doCmd(c, slave.Port(), "GET", "b")
	c.Assert(resp, DeepEquals, redis.NewBulkBytesWithString("100"))

	s.doCmdMustOK(c, master.Port(), "SET", "c", "")

	// set another value with TTL
	s.doCmdMustOK(c, master.Port(), "SET", "ttl_key", "1", "PX", 100)

	time.Sleep(500 * time.Millisecond)
	resp = s.doCmd(c, slave.Port(), "GET", "c")
	c.Assert(resp, DeepEquals, redis.NewBulkBytesWithString(""))

	// get expired key to let master force delete it
	resp = s.doCmd(c, master.Port(), "GET", "ttl_key")
	c.Assert(resp, DeepEquals, redis.NewBulkBytes(nil))

	time.Sleep(500 * time.Millisecond)

	// the key must be deleted from master repliction
	resp = s.doCmd(c, slave.Port(), "GET", "ttl_key")
	c.Assert(resp, DeepEquals, redis.NewBulkBytes(nil))

	offset = slave.SyncOffset(c)
	// now close replication connection
	nc.Close(c)
	s.doCmdMustOK(c, master.Port(), "SET", "b", "1000")

	s.doCmdMustOK(c, master.Port(), "SET", "c", "123")

	s.waitAndCheckSyncOffset(c, slave, offset)

	resp = s.doCmd(c, slave.Port(), "GET", "b")
	c.Assert(resp, DeepEquals, redis.NewBulkBytesWithString("1000"))

	resp = s.doCmd(c, slave.Port(), "GET", "c")
	c.Assert(resp, DeepEquals, redis.NewBulkBytesWithString("123"))

	s.checkRole(c, master.Port(), "master")
	s.checkRole(c, slave.Port(), "slave")

	s.doCmdMustOK(c, slave.Port(), "SLAVEOF", "NO", "ONE")
	s.doCmdMustOK(c, master.Port(), "SLAVEOF", "NO", "ONE")

	s.checkRole(c, master.Port(), "master")
	s.checkRole(c, slave.Port(), "master")
}