Example #1
0
func (krval bvcKeyRange) eval(bv interface{}, op Operator, onMismatch bool) bool {
	switch op {
	case QR_IN:
		switch num, status := getuint64(bv); status {
		case QR_OK:
			k := key.Uint64Key(num).KeyspaceId()
			return key.KeyRange(krval).Contains(k)
		case QR_OUT_OF_RANGE:
			return false
		}
		// Not a number. Check string.
		switch str, status := getstring(bv); status {
		case QR_OK:
			return key.KeyRange(krval).Contains(key.KeyspaceId(str))
		}
	case QR_NOTIN:
		switch num, status := getuint64(bv); status {
		case QR_OK:
			k := key.Uint64Key(num).KeyspaceId()
			return !key.KeyRange(krval).Contains(k)
		case QR_OUT_OF_RANGE:
			return true
		}
		// Not a number. Check string.
		switch str, status := getstring(bv); status {
		case QR_OK:
			return !key.KeyRange(krval).Contains(key.KeyspaceId(str))
		}
	default:
		panic("unexpected:")
	}
	return onMismatch
}
Example #2
0
func buildBindVarCondition(bvc interface{}) (name string, onAbsent, onMismatch bool, op Operator, value interface{}, err error) {
	bvcinfo, ok := bvc.(map[string]interface{})
	if !ok {
		err = NewTabletError(FAIL, "Expecting json object for bind var conditions")
		return
	}

	var v interface{}
	v, ok = bvcinfo["Name"]
	if !ok {
		err = NewTabletError(FAIL, "Name missing in BindVarConds")
		return
	}
	name, ok = v.(string)
	if !ok {
		err = NewTabletError(FAIL, "Expecting string for Name in BindVarConds")
		return
	}

	v, ok = bvcinfo["OnAbsent"]
	if !ok {
		err = NewTabletError(FAIL, "OnAbsent missing in BindVarConds")
		return
	}
	onAbsent, ok = v.(bool)
	if !ok {
		err = NewTabletError(FAIL, "Expecting bool for OnAbsent")
		return
	}

	v, ok = bvcinfo["Operator"]
	if !ok {
		err = NewTabletError(FAIL, "Operator missing in BindVarConds")
		return
	}
	strop, ok := v.(string)
	if !ok {
		err = NewTabletError(FAIL, "Expecting string for Operator")
		return
	}
	op, ok = opmap[strop]
	if !ok {
		err = NewTabletError(FAIL, "Invalid Operator %s", strop)
		return
	}
	if op == QR_NOOP {
		return
	}
	v, ok = bvcinfo["Value"]
	if !ok {
		err = NewTabletError(FAIL, "Value missing in BindVarConds")
		return
	}
	if op >= QR_EQ && op <= QR_LE {
		strvalue, ok := v.(string)
		if !ok {
			err = NewTabletError(FAIL, "Expecting string: %v", v)
			return
		}
		if strop[0] == 'U' {
			value, err = strconv.ParseUint(strvalue, 0, 64)
			if err != nil {
				err = NewTabletError(FAIL, "Expecting uint64: %s", strvalue)
				return
			}
		} else if strop[0] == 'I' {
			value, err = strconv.ParseInt(strvalue, 0, 64)
			if err != nil {
				err = NewTabletError(FAIL, "Expecting int64: %s", strvalue)
				return
			}
		} else if strop[0] == 'S' {
			value = strvalue
		} else {
			panic("unexpected")
		}
	} else if op == QR_MATCH || op == QR_NOMATCH {
		strvalue, ok := v.(string)
		if !ok {
			err = NewTabletError(FAIL, "Expecting string: %v", v)
			return
		}
		value = strvalue
	} else if op == QR_IN || op == QR_NOTIN {
		kr, ok := v.(map[string]interface{})
		if !ok {
			err = NewTabletError(FAIL, "Expecting keyrange for Value")
			return
		}
		var keyrange key.KeyRange
		strstart, ok := kr["Start"]
		if !ok {
			err = NewTabletError(FAIL, "Start missing in KeyRange")
			return
		}
		start, ok := strstart.(string)
		if !ok {
			err = NewTabletError(FAIL, "Expecting string for Start")
			return
		}
		keyrange.Start = key.KeyspaceId(start)

		strend, ok := kr["End"]
		if !ok {
			err = NewTabletError(FAIL, "End missing in KeyRange")
			return
		}
		end, ok := strend.(string)
		if !ok {
			err = NewTabletError(FAIL, "Expecting string for End")
			return
		}
		keyrange.End = key.KeyspaceId(end)
		value = keyrange
	}

	v, ok = bvcinfo["OnMismatch"]
	if !ok {
		err = NewTabletError(FAIL, "OnMismatch missing in BindVarConds")
		return
	}
	onMismatch, ok = v.(bool)
	if !ok {
		err = NewTabletError(FAIL, "Expecting bool for OnAbsent")
		return
	}
	return
}