Пример #1
0
func syncCommand(req *requestContext) error {
	args := req.args
	if len(args) != 2 {
		return ErrCmdParams
	}

	var logIndex int64
	var logPos int64
	var err error
	logIndex, err = ledis.StrInt64(args[0], nil)
	if err != nil {
		return ErrCmdParams
	}

	logPos, err = ledis.StrInt64(args[1], nil)
	if err != nil {
		return ErrCmdParams
	}

	req.syncBuf.Reset()

	//reserve space to write master info
	if _, err := req.syncBuf.Write(reserveInfoSpace); err != nil {
		return err
	}

	m := &ledis.MasterInfo{logIndex, logPos}

	if _, err := req.app.ldb.ReadEventsTo(m, &req.syncBuf); err != nil {
		return err
	} else {
		buf := req.syncBuf.Bytes()

		binary.BigEndian.PutUint64(buf[0:], uint64(m.LogFileIndex))
		binary.BigEndian.PutUint64(buf[8:], uint64(m.LogPos))

		if len(req.compressBuf) < snappy.MaxEncodedLen(len(buf)) {
			req.compressBuf = make([]byte, snappy.MaxEncodedLen(len(buf)))
		}

		if buf, err = snappy.Encode(req.compressBuf, buf); err != nil {
			return err
		}

		req.resp.writeBulk(buf)
	}

	return nil
}
Пример #2
0
func syncCommand(c *client) error {
	args := c.args
	if len(args) != 2 {
		return ErrCmdParams
	}

	var logIndex int64
	var logPos int64
	var err error
	logIndex, err = ledis.StrInt64(args[0], nil)
	if err != nil {
		return ErrCmdParams
	}

	logPos, err = ledis.StrInt64(args[1], nil)
	if err != nil {
		return ErrCmdParams
	}

	c.syncBuf.Reset()

	//reserve space to write binlog anchor
	if _, err := c.syncBuf.Write(reserveInfoSpace); err != nil {
		return err
	}

	m := &ledis.BinLogAnchor{logIndex, logPos}

	if _, err := c.app.ldb.ReadEventsToTimeout(m, &c.syncBuf, 5); err != nil {
		return err
	} else {
		buf := c.syncBuf.Bytes()

		binary.BigEndian.PutUint64(buf[0:], uint64(m.LogFileIndex))
		binary.BigEndian.PutUint64(buf[8:], uint64(m.LogPos))

		if len(c.compressBuf) < snappy.MaxEncodedLen(len(buf)) {
			c.compressBuf = make([]byte, snappy.MaxEncodedLen(len(buf)))
		}

		if buf, err = snappy.Encode(c.compressBuf, buf); err != nil {
			return err
		}

		c.resp.writeBulk(buf)
	}

	return nil
}
Пример #3
0
func (l *Ledis) Dump(w io.Writer) error {
	var m *MasterInfo = new(MasterInfo)
	l.Lock()
	defer l.Unlock()

	if l.binlog != nil {
		m.LogFileIndex = l.binlog.LogFileIndex()
		m.LogPos = l.binlog.LogFilePos()
	}

	var err error

	wb := bufio.NewWriterSize(w, 4096)
	if err = m.WriteTo(wb); err != nil {
		return err
	}

	it := l.ldb.NewIterator()
	it.SeekToFirst()

	compressBuf := make([]byte, 4096)

	var key []byte
	var value []byte
	for ; it.Valid(); it.Next() {
		key = it.Key()
		value = it.Value()

		if key, err = snappy.Encode(compressBuf, key); err != nil {
			return err
		}

		if err = binary.Write(wb, binary.BigEndian, uint16(len(key))); err != nil {
			return err
		}

		if _, err = wb.Write(key); err != nil {
			return err
		}

		if value, err = snappy.Encode(compressBuf, value); err != nil {
			return err
		}

		if err = binary.Write(wb, binary.BigEndian, uint32(len(value))); err != nil {
			return err
		}

		if _, err = wb.Write(value); err != nil {
			return err
		}
	}

	if err = wb.Flush(); err != nil {
		return err
	}

	compressBuf = nil

	return nil
}