2014-05-29 11:07:14 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2014-06-04 10:42:02 +04:00
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errBinLogDeleteType = errors.New("invalid bin log delete type")
|
|
|
|
errBinLogPutType = errors.New("invalid bin log put type")
|
|
|
|
errBinLogCommandType = errors.New("invalid bin log command type")
|
2014-05-29 11:07:14 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func encodeBinLogDelete(key []byte) []byte {
|
2014-06-04 10:42:02 +04:00
|
|
|
buf := make([]byte, 1+len(key))
|
2014-05-29 11:07:14 +04:00
|
|
|
buf[0] = BinLogTypeDeletion
|
2014-06-04 10:42:02 +04:00
|
|
|
copy(buf[1:], key)
|
2014-05-29 11:07:14 +04:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:42:02 +04:00
|
|
|
func decodeBinLogDelete(sz []byte) ([]byte, error) {
|
|
|
|
if len(sz) < 1 || sz[0] != BinLogTypeDeletion {
|
|
|
|
return nil, errBinLogDeleteType
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz[1:], nil
|
|
|
|
}
|
|
|
|
|
2014-05-29 11:07:14 +04:00
|
|
|
func encodeBinLogPut(key []byte, value []byte) []byte {
|
2014-06-04 10:42:02 +04:00
|
|
|
buf := make([]byte, 3+len(key)+len(value))
|
2014-05-29 11:07:14 +04:00
|
|
|
buf[0] = BinLogTypePut
|
|
|
|
pos := 1
|
|
|
|
binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
|
|
|
|
pos += 2
|
|
|
|
copy(buf[pos:], key)
|
|
|
|
pos += len(key)
|
|
|
|
copy(buf[pos:], value)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-05-29 11:07:14 +04:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:42:02 +04:00
|
|
|
func decodeBinLogPut(sz []byte) ([]byte, []byte, error) {
|
|
|
|
if len(sz) < 3 || sz[0] != BinLogTypePut {
|
|
|
|
return nil, nil, errBinLogPutType
|
|
|
|
}
|
|
|
|
|
|
|
|
keyLen := int(binary.BigEndian.Uint16(sz[1:]))
|
|
|
|
if 3+keyLen > len(sz) {
|
|
|
|
return nil, nil, errBinLogPutType
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz[3 : 3+keyLen], sz[3+keyLen:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeBinLogCommand(commandType uint8, args ...[]byte) []byte {
|
2014-05-29 11:07:14 +04:00
|
|
|
//to do
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
|
|
|
func decodeBinLogCommand(sz []byte) (uint8, [][]byte, error) {
|
|
|
|
return 0, nil, errBinLogCommandType
|
|
|
|
}
|