func (ms *master) convertMigPkg(pkg []byte, head *proto.PkgHead) ([]byte, error) { _, err := head.Decode(pkg) if err != nil { return nil, err } if !ms.migration { return pkg, nil } switch head.Cmd { case proto.CmdIncr: fallthrough case proto.CmdDel: fallthrough case proto.CmdSet: var p proto.PkgOneOp _, err = p.Decode(pkg) if err != nil { return nil, err } if ms.slotId == ctrl.GetSlotId(p.DbId, p.TableId, p.RowKey) { return pkg, nil } else { return nil, nil } case proto.CmdSync: fallthrough case proto.CmdMIncr: fallthrough case proto.CmdMDel: fallthrough case proto.CmdMSet: var p proto.PkgMultiOp _, err = p.Decode(pkg) if err != nil { return nil, err } var kvs []proto.KeyValue for i := 0; i < len(p.Kvs); i++ { if ms.slotId == ctrl.GetSlotId(p.DbId, p.Kvs[i].TableId, p.Kvs[i].RowKey) { kvs = append(kvs, p.Kvs[i]) } } if len(kvs) == 0 { return nil, nil } else { p.Kvs = kvs pkg = make([]byte, p.Length()) _, err = p.Encode(pkg) if err != nil { return nil, err } return pkg, nil } } return nil, nil }
func Decode(pkg []byte, head *proto.PkgHead, v interface{}) error { if len(pkg) < proto.HeadSize { return proto.ErrPkgLen } var err error if head != nil { _, err = head.Decode(pkg) if err != nil { return err } } return json.Unmarshal(pkg[proto.HeadSize:], v) }
func Encode(cmd, dbId uint8, seq uint64, v interface{}) ([]byte, error) { jsonPkg, err := json.Marshal(v) if err != nil { return nil, err } var pkgLen = proto.HeadSize + len(jsonPkg) var head proto.PkgHead head.Cmd = cmd head.DbId = dbId head.Seq = seq head.PkgLen = uint32(pkgLen) var pkg = make([]byte, pkgLen) _, err = head.Encode(pkg) if err != nil { return nil, err } copy(pkg[proto.HeadSize:], jsonPkg) return pkg, nil }