Exemplo n.º 1
0
func (c *client) writeError(err error) {
	c.wb.Write(ledis.Slice("-ERR"))
	if err != nil {
		c.wb.WriteByte(' ')
		c.wb.Write(ledis.Slice(err.Error()))
	}
	c.wb.Write(Delims)
}
Exemplo n.º 2
0
func (w *respWriter) writeError(err error) {
	w.buff.Write(ledis.Slice("-ERR"))
	if err != nil {
		w.buff.WriteByte(' ')
		w.buff.Write(ledis.Slice(err.Error()))
	}
	w.buff.Write(Delims)
}
Exemplo n.º 3
0
func (w *respWriter) writeArray(lst []interface{}) {
	w.buff.WriteByte('*')
	if lst == nil {
		w.buff.Write(NullArray)
		w.buff.Write(Delims)
	} else {
		w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
		w.buff.Write(Delims)

		for i := 0; i < len(lst); i++ {
			switch v := lst[i].(type) {
			case []interface{}:
				w.writeArray(v)
			case [][]byte:
				w.writeSliceArray(v)
			case []byte:
				w.writeBulk(v)
			case nil:
				w.writeBulk(nil)
			case int64:
				w.writeInteger(v)
			default:
				panic("invalid array type")
			}
		}
	}
}
Exemplo n.º 4
0
func scriptLoadCommand(c *client) error {
	s := c.app.s
	l := s.l

	if len(c.args) != 2 {
		return ErrCmdParams
	}

	h := sha1.Sum(c.args[1])
	key := hex.EncodeToString(h[0:20])

	if r := l.LoadString(ledis.String(c.args[1])); r != 0 {
		err := fmt.Errorf("%s", l.ToString(-1))
		l.Pop(1)
		return err
	} else {
		l.PushValue(-1)
		l.SetGlobal(key)

		s.chunks[key] = struct{}{}
	}

	c.resp.writeBulk(ledis.Slice(key))
	return nil
}
Exemplo n.º 5
0
func (c *client) writeArray(ay []interface{}) {
	c.wb.WriteByte('*')
	if ay == nil {
		c.wb.Write(NullArray)
		c.wb.Write(Delims)
	} else {
		c.wb.Write(ledis.Slice(strconv.Itoa(len(ay))))
		c.wb.Write(Delims)

		for i := 0; i < len(ay); i++ {
			switch v := ay[i].(type) {
			case []interface{}:
				c.writeArray(v)
			case []byte:
				c.writeBulk(v)
			case nil:
				c.writeBulk(nil)
			case int64:
				c.writeInteger(v)
			default:
				panic("invalid array type")
			}
		}
	}
}
Exemplo n.º 6
0
func (c *client) writeBulkFrom(n int64, rb io.Reader) {
	c.wb.WriteByte('$')
	c.wb.Write(ledis.Slice(strconv.FormatInt(n, 10)))
	c.wb.Write(Delims)

	io.Copy(c.wb, rb)
	c.wb.Write(Delims)
}
Exemplo n.º 7
0
func (w *respWriter) writeBulkFrom(n int64, rb io.Reader) {
	w.buff.WriteByte('$')
	w.buff.Write(ledis.Slice(strconv.FormatInt(n, 10)))
	w.buff.Write(Delims)

	io.Copy(w.buff, rb)
	w.buff.Write(Delims)
}
Exemplo n.º 8
0
func (c *client) writeBulk(b []byte) {
	c.wb.WriteByte('$')
	if b == nil {
		c.wb.Write(NullBulk)
	} else {
		c.wb.Write(ledis.Slice(strconv.Itoa(len(b))))
		c.wb.Write(Delims)
		c.wb.Write(b)
	}

	c.wb.Write(Delims)
}
Exemplo n.º 9
0
func (w *respWriter) writeBulk(b []byte) {
	w.buff.WriteByte('$')
	if b == nil {
		w.buff.Write(NullBulk)
	} else {
		w.buff.Write(ledis.Slice(strconv.Itoa(len(b))))
		w.buff.Write(Delims)
		w.buff.Write(b)
	}

	w.buff.Write(Delims)
}
Exemplo n.º 10
0
func luaSha1Hex(l *lua.State) int {
	argc := l.GetTop()
	if argc != 1 {
		luaPushError(l, "wrong number of arguments")
		return 1
	}

	s := l.ToString(1)
	s = hex.EncodeToString(ledis.Slice(s))

	l.PushString(s)
	return 1
}
Exemplo n.º 11
0
func luaReplyToLedisReply(l *lua.State) interface{} {
	base := l.GetTop()
	defer func() {
		l.SetTop(base - 1)
	}()

	switch l.Type(-1) {
	case lua.LUA_TSTRING:
		return ledis.Slice(l.ToString(-1))
	case lua.LUA_TBOOLEAN:
		if l.ToBoolean(-1) {
			return int64(1)
		} else {
			return nil
		}
	case lua.LUA_TNUMBER:
		return int64(l.ToInteger(-1))
	case lua.LUA_TTABLE:
		l.PushString("err")
		l.GetTable(-2)
		if l.Type(-1) == lua.LUA_TSTRING {
			return fmt.Errorf("%s", l.ToString(-1))
		}

		l.Pop(1)
		l.PushString("ok")
		l.GetTable(-2)
		if l.Type(-1) == lua.LUA_TSTRING {
			return l.ToString(-1)
		} else {
			l.Pop(1)

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

			for i := 1; ; i++ {
				l.PushInteger(int64(i))
				l.GetTable(-2)
				if l.Type(-1) == lua.LUA_TNIL {
					l.Pop(1)
					break
				}

				ay = append(ay, luaReplyToLedisReply(l))
			}
			return ay

		}
	default:
		return nil
	}
}
Exemplo n.º 12
0
func (m *master) sync() error {
	logIndexStr := strconv.FormatInt(m.info.LogFileIndex, 10)
	logPosStr := strconv.FormatInt(m.info.LogPos, 10)

	cmd := ledis.Slice(fmt.Sprintf(syncCmdFormat, len(logIndexStr),
		logIndexStr, len(logPosStr), logPosStr))

	if _, err := m.conn.Write(cmd); err != nil {
		return err
	}

	m.syncBuf.Reset()

	err := ReadBulkTo(m.rb, &m.syncBuf)
	if err != nil {
		return err
	}

	var buf []byte
	buf, err = snappy.Decode(m.compressBuf, m.syncBuf.Bytes())
	if err != nil {
		return err
	} else if len(buf) > len(m.compressBuf) {
		m.compressBuf = buf
	}

	if len(buf) < 16 {
		return fmt.Errorf("invalid sync data len %d", len(buf))
	}

	m.info.LogFileIndex = int64(binary.BigEndian.Uint64(buf[0:8]))
	m.info.LogPos = int64(binary.BigEndian.Uint64(buf[8:16]))

	if m.info.LogFileIndex == 0 {
		//master now not support binlog, stop replication
		m.stopReplication()
		return nil
	} else if m.info.LogFileIndex == -1 {
		//-1 means than binlog index and pos are lost, we must start a full sync instead
		return m.fullSync()
	}

	err = m.app.ldb.ReplicateFromData(buf[16:])
	if err != nil {
		return err
	}

	return m.saveInfo()

}
Exemplo n.º 13
0
func (w *respWriter) writeSliceArray(lst [][]byte) {
	w.buff.WriteByte('*')
	if lst == nil {
		w.buff.Write(NullArray)
		w.buff.Write(Delims)
	} else {
		w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
		w.buff.Write(Delims)

		for i := 0; i < len(lst); i++ {
			w.writeBulk(lst[i])
		}
	}
}
Exemplo n.º 14
0
func (c *client) writeSliceArray(ay [][]byte) {
	c.wb.WriteByte('*')
	if ay == nil {
		c.wb.Write(NullArray)
		c.wb.Write(Delims)
	} else {
		c.wb.Write(ledis.Slice(strconv.Itoa(len(ay))))
		c.wb.Write(Delims)

		for i := 0; i < len(ay); i++ {
			c.writeBulk(ay[i])
		}
	}
}
Exemplo n.º 15
0
func (c *client) writeFVPairArray(ay []ledis.FVPair) {
	c.wb.WriteByte('*')
	if ay == nil {
		c.wb.Write(NullArray)
		c.wb.Write(Delims)
	} else {
		c.wb.Write(ledis.Slice(strconv.Itoa(len(ay) * 2)))
		c.wb.Write(Delims)

		for i := 0; i < len(ay); i++ {
			c.writeBulk(ay[i].Field)
			c.writeBulk(ay[i].Value)
		}
	}
}
Exemplo n.º 16
0
func (w *respWriter) writeScorePairArray(lst []ledis.ScorePair, withScores bool) {
	w.buff.WriteByte('*')
	if lst == nil {
		w.buff.Write(NullArray)
		w.buff.Write(Delims)
	} else {
		if withScores {
			w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
			w.buff.Write(Delims)
		} else {
			w.buff.Write(ledis.Slice(strconv.Itoa(len(lst))))
			w.buff.Write(Delims)

		}

		for i := 0; i < len(lst); i++ {
			w.writeBulk(lst[i].Member)

			if withScores {
				w.writeBulk(ledis.StrPutInt64(lst[i].Score))
			}
		}
	}
}
Exemplo n.º 17
0
func (w *respWriter) writeFVPairArray(lst []ledis.FVPair) {
	w.buff.WriteByte('*')
	if lst == nil {
		w.buff.Write(NullArray)
		w.buff.Write(Delims)
	} else {
		w.buff.Write(ledis.Slice(strconv.Itoa(len(lst) * 2)))
		w.buff.Write(Delims)

		for i := 0; i < len(lst); i++ {
			w.writeBulk(lst[i].Field)
			w.writeBulk(lst[i].Value)
		}
	}
}
Exemplo n.º 18
0
func (c *client) writeScorePairArray(ay []ledis.ScorePair, withScores bool) {
	c.wb.WriteByte('*')
	if ay == nil {
		c.wb.Write(NullArray)
		c.wb.Write(Delims)
	} else {
		if withScores {
			c.wb.Write(ledis.Slice(strconv.Itoa(len(ay) * 2)))
			c.wb.Write(Delims)
		} else {
			c.wb.Write(ledis.Slice(strconv.Itoa(len(ay))))
			c.wb.Write(Delims)

		}

		for i := 0; i < len(ay); i++ {
			c.writeBulk(ay[i].Member)

			if withScores {
				c.writeBulk(ledis.StrPutInt64(ay[i].Score))
			}
		}
	}
}
Exemplo n.º 19
0
func (m *master) sync() error {
	logIndexStr := strconv.FormatInt(m.logFileIndex, 10)
	logPosStr := strconv.FormatInt(m.logPos, 10)

	cmd := ledis.Slice(fmt.Sprintf(syncCmdFormat, len(logIndexStr),
		logIndexStr, len(logPosStr), logPosStr))
	if _, err := m.c.Write(cmd); err != nil {
		return err
	}

	m.syncBuf.Reset()

	err := readBulkTo(m.rb, &m.syncBuf)
	if err != nil {
		return err
	}

	err = binary.Read(&m.syncBuf, binary.BigEndian, &m.logFileIndex)
	if err != nil {
		return err
	}

	err = binary.Read(&m.syncBuf, binary.BigEndian, &m.logPos)
	if err != nil {
		return err
	}

	if m.logFileIndex == 0 {
		//master now not support binlog, stop replication
		m.stopReplication()
		return nil
	} else if m.logFileIndex == -1 {
		//-1 means than binlog index and pos are lost, we must start a full sync instead
		return m.fullSync()
	}

	err = m.app.ldb.ReplicateFromReader(&m.syncBuf)
	if err != nil {
		return err
	}

	return m.saveInfo()

}
Exemplo n.º 20
0
func (c *client) writeStatus(status string) {
	c.wb.WriteByte('+')
	c.wb.Write(ledis.Slice(status))
	c.wb.Write(Delims)
}
Exemplo n.º 21
0
func (w *respWriter) writeStatus(status string) {
	w.buff.WriteByte('+')
	w.buff.Write(ledis.Slice(status))
	w.buff.Write(Delims)
}