Exemplo n.º 1
0
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
}
Exemplo n.º 2
0
// Do we have right to write this key?
func (m *WriteAccess) CheckKey(dbId, tableId uint8, rowKey []byte) bool {
	if m.replication {
		return true // Accept all replication data
	}

	if !m.hasMaster {
		return true
	}

	if m.migration {
		return m.slotId != ctrl.GetSlotId(dbId, tableId, rowKey)
	} else {
		return false
	}
}
Exemplo n.º 3
0
func getRawKey(dbId, tableId, colSpace uint8, rowKey, colKey []byte) []byte {
	var slotId = ctrl.GetSlotId(dbId, tableId, rowKey)

	// wSlotId+cDbId+cTableId+cKeyLen+sRowKey+colSpace+sColKey
	var rowKeyLen = len(rowKey)
	var rawLen = 6 + rowKeyLen + len(colKey)
	var rawKey = make([]byte, rawLen)
	binary.BigEndian.PutUint16(rawKey, slotId)
	rawKey[2] = dbId
	rawKey[3] = tableId
	rawKey[4] = uint8(rowKeyLen)
	copy(rawKey[5:], rowKey)
	rawKey[5+rowKeyLen] = colSpace
	copy(rawKey[6+rowKeyLen:], colKey)

	return rawKey
}