tabletserver: lint & vet fixes

This commit is contained in:
Sugu Sougoumarane 2015-10-17 22:27:45 -07:00
Родитель 29c7d9fdf0
Коммит 3a9476137b
10 изменённых файлов: 372 добавлений и 366 удалений

Просмотреть файл

@ -78,10 +78,10 @@ func (agent *ActionAgent) loadKeyspaceAndBlacklistRules(tablet *pbt.Tablet, blac
qr := tabletserver.NewQueryRule(
fmt.Sprintf("enforce keyspace_id range for %v", plan.planID),
fmt.Sprintf("keyspace_id_not_in_range_%v", plan.planID),
tabletserver.QR_FAIL,
tabletserver.QRFail,
)
qr.AddPlanCond(plan.planID)
err := qr.AddBindVarCond("keyspace_id", plan.onAbsent, true, tabletserver.QR_NOTIN, tablet.KeyRange)
err := qr.AddBindVarCond("keyspace_id", plan.onAbsent, true, tabletserver.QRNotIn, tablet.KeyRange)
if err != nil {
return fmt.Errorf("Unable to add keyspace rule: %v", err)
}
@ -98,7 +98,7 @@ func (agent *ActionAgent) loadKeyspaceAndBlacklistRules(tablet *pbt.Tablet, blac
return err
}
log.Infof("Blacklisting tables %v", strings.Join(tables, ", "))
qr := tabletserver.NewQueryRule("enforce blacklisted tables", "blacklisted_table", tabletserver.QR_FAIL_RETRY)
qr := tabletserver.NewQueryRule("enforce blacklisted tables", "blacklisted_table", tabletserver.QRFailRetry)
for _, t := range tables {
qr.AddTableCond(t)
}

Просмотреть файл

@ -8,7 +8,7 @@ import (
"github.com/youtube/vitess/go/vt/tabletserver/proto"
)
const TRAILING_COMMENT = "_trailingComment"
const trailingComment = "_trailingComment"
type matchtracker struct {
query string
@ -26,13 +26,13 @@ func stripTrailing(query *proto.Query) {
pos := tracker.matchComments()
if pos >= 0 {
query.Sql = tracker.query[:pos]
query.BindVariables[TRAILING_COMMENT] = tracker.query[pos:]
query.BindVariables[trailingComment] = tracker.query[pos:]
}
}
// restoreTrailing undoes work done by stripTrailing
func restoreTrailing(sql []byte, bindVars map[string]interface{}) []byte {
if ytcomment, ok := bindVars[TRAILING_COMMENT]; ok {
if ytcomment, ok := bindVars[trailingComment]; ok {
sql = append(sql, ytcomment.(string)...)
}
return sql

Просмотреть файл

@ -13,7 +13,7 @@ import (
var testCases = []struct {
input string
outSql, outVar string
outSQL, outVar string
}{{
"/",
"/", "",
@ -74,16 +74,16 @@ func TestComments(t *testing.T) {
stripTrailing(&query)
want := proto.Query{
Sql: testCase.outSql,
Sql: testCase.outSQL,
}
want.BindVariables = make(map[string]interface{})
if testCase.outVar != "" {
want.BindVariables[TRAILING_COMMENT] = testCase.outVar
want.BindVariables[trailingComment] = testCase.outVar
}
if !reflect.DeepEqual(query, want) {
t.Errorf("test input: '%s', got\n%+v, want\n%+v", testCase.input, query, want)
}
sql := string(restoreTrailing([]byte(testCase.outSql), want.BindVariables))
sql := string(restoreTrailing([]byte(testCase.outSQL), want.BindVariables))
if !reflect.DeepEqual(testCase.input, sql) {
t.Fatalf("failed to restore to original sql, got: %s, want: %s", sql, testCase.input)
}

Просмотреть файл

@ -227,9 +227,9 @@ func (qre *QueryExecutor) checkPermissions() error {
}
action, desc := qre.plan.Rules.getAction(remoteAddr, username, qre.bindVars)
switch action {
case QR_FAIL:
case QRFail:
return NewTabletError(ErrFail, vtrpc.ErrorCode_BAD_INPUT, "Query disallowed due to rule: %s", desc)
case QR_FAIL_RETRY:
case QRFailRetry:
return NewTabletError(ErrRetry, vtrpc.ErrorCode_QUERY_NOT_SERVED, "Query disallowed due to rule: %s", desc)
}

Просмотреть файл

@ -856,7 +856,7 @@ func TestQueryExecutorBlacklistQRFail(t *testing.T) {
bannedAddr := "127.0.0.1"
bannedUser := "u2"
alterRule := NewQueryRule("disable update", "disable update", QR_FAIL)
alterRule := NewQueryRule("disable update", "disable update", QRFail)
alterRule.SetIPCond(bannedAddr)
alterRule.SetUserCond(bannedUser)
alterRule.SetQueryCond("select.*")
@ -916,7 +916,7 @@ func TestQueryExecutorBlacklistQRRetry(t *testing.T) {
bannedAddr := "127.0.0.1"
bannedUser := "x"
alterRule := NewQueryRule("disable update", "disable update", QR_FAIL_RETRY)
alterRule := NewQueryRule("disable update", "disable update", QRFailRetry)
alterRule.SetIPCond(bannedAddr)
alterRule.SetUserCond(bannedUser)
alterRule.SetQueryCond("select.*")

Просмотреть файл

@ -47,17 +47,17 @@ func setupQueryRules() {
qr = NewQueryRule(
fmt.Sprintf("enforce keyspace_id range for %v", plan.planID),
fmt.Sprintf("keyspace_id_not_in_range_%v", plan.planID),
QR_FAIL,
QRFail,
)
qr.AddPlanCond(plan.planID)
qr.AddBindVarCond("keyspace_id", plan.onAbsent, true, QR_NOTIN, key.KeyRange{Start: "aa", End: "zz"})
qr.AddBindVarCond("keyspace_id", plan.onAbsent, true, QRNotIn, key.KeyRange{Start: "aa", End: "zz"})
keyrangeRules.Add(qr)
}
// mock blacklisted tables
blacklistRules = NewQueryRules()
blacklistedTables := []string{"bannedtable1", "bannedtable2", "bannedtable3"}
qr = NewQueryRule("enforce blacklisted tables", "blacklisted_table", QR_FAIL_RETRY)
qr = NewQueryRule("enforce blacklisted tables", "blacklisted_table", QRFailRetry)
for _, t := range blacklistedTables {
qr.AddTableCond(t)
}
@ -65,9 +65,9 @@ func setupQueryRules() {
// mock custom rules
otherRules = NewQueryRules()
qr = NewQueryRule("sample custom rule", "customrule_ban_bindvar", QR_FAIL)
qr = NewQueryRule("sample custom rule", "customrule_ban_bindvar", QRFail)
qr.AddTableCond("t_customer")
qr.AddBindVarCond("bindvar1", true, false, QR_NOOP, nil)
qr.AddBindVarCond("bindvar1", true, false, QRNoOp, nil)
otherRules.Add(qr)
}
@ -220,8 +220,8 @@ func TestQueryRuleInfoFilterByPlan(t *testing.T) {
// Test match two rules: both keyrange rule and custom rule will be matched
otherRules = NewQueryRules()
qr := NewQueryRule("sample custom rule", "customrule_ban_bindvar", QR_FAIL)
qr.AddBindVarCond("bindvar1", true, false, QR_NOOP, nil)
qr := NewQueryRule("sample custom rule", "customrule_ban_bindvar", QRFail)
qr.AddBindVarCond("bindvar1", true, false, QRNoOp, nil)
otherRules.Add(qr)
qri.SetRules(customQueryRules, otherRules)
qrs = qri.filterByPlan("insert into t_test values (:bindvar1, 123, 'test')", planbuilder.PlanInsertPK, "t_test")

Просмотреть файл

@ -81,6 +81,7 @@ func (qrs *QueryRules) Delete(name string) (qr *QueryRule) {
return nil
}
// UnmarshalJSON unmarshals QueryRules.
func (qrs *QueryRules) UnmarshalJSON(data []byte) (err error) {
var rulesInfo []map[string]interface{}
err = json.Unmarshal(data, &rulesInfo)
@ -117,11 +118,11 @@ func (qrs *QueryRules) filterByPlan(query string, planid planbuilder.PlanType, t
func (qrs *QueryRules) getAction(ip, user string, bindVars map[string]interface{}) (action Action, desc string) {
for _, qr := range qrs.rules {
if act := qr.getAction(ip, user, bindVars); act != QR_CONTINUE {
if act := qr.getAction(ip, user, bindVars); act != QRContinue {
return act, qr.Description
}
}
return QR_CONTINUE, ""
return QRContinue, ""
}
//-----------------------------------------------
@ -246,25 +247,25 @@ func makeExact(pattern string) string {
// whole numbers can be: int, int8, int16, int32, int64, uint64
func (qr *QueryRule) AddBindVarCond(name string, onAbsent, onMismatch bool, op Operator, value interface{}) error {
var converted bvcValue
if op == QR_NOOP {
if op == QRNoOp {
qr.bindVarConds = append(qr.bindVarConds, BindVarCond{name, onAbsent, onMismatch, op, nil})
return nil
}
switch v := value.(type) {
case uint64:
if op < QR_EQ || op > QR_LE {
if op < QREqual || op > QRLessEqual {
goto Error
}
converted = bvcuint64(v)
case int64:
if op < QR_EQ || op > QR_LE {
if op < QREqual || op > QRLessEqual {
goto Error
}
converted = bvcint64(v)
case string:
if op >= QR_EQ && op <= QR_LE {
if op >= QREqual && op <= QRLessEqual {
converted = bvcstring(v)
} else if op >= QR_MATCH && op <= QR_NOMATCH {
} else if op >= QRMatch && op <= QRNoMatch {
var err error
// Change the value to compiled regexp
re, err := regexp.Compile(makeExact(v))
@ -276,7 +277,7 @@ func (qr *QueryRule) AddBindVarCond(name string, onAbsent, onMismatch bool, op O
goto Error
}
case *pb.KeyRange:
if op < QR_IN || op > QR_NOTIN {
if op < QRIn || op > QRNotIn {
goto Error
}
b := bvcKeyRange(*v)
@ -314,14 +315,14 @@ func (qr *QueryRule) filterByPlan(query string, planid planbuilder.PlanType, tab
func (qr *QueryRule) getAction(ip, user string, bindVars map[string]interface{}) Action {
if !reMatch(qr.requestIP, ip) {
return QR_CONTINUE
return QRContinue
}
if !reMatch(qr.user, user) {
return QR_CONTINUE
return QRContinue
}
for _, bvcond := range qr.bindVarConds {
if !bvMatch(bvcond, bindVars) {
return QR_CONTINUE
return QRContinue
}
}
return qr.act
@ -360,7 +361,7 @@ func bvMatch(bvcond BindVarCond, bindVars map[string]interface{}) bool {
if !ok {
return bvcond.onAbsent
}
if bvcond.op == QR_NOOP {
if bvcond.op == QRNoOp {
return !bvcond.onAbsent
}
return bvcond.value.eval(bv, bvcond.op, bvcond.onMismatch)
@ -373,10 +374,11 @@ func bvMatch(bvcond BindVarCond, bindVars map[string]interface{}) bool {
// when a QueryRule is triggered.
type Action int
// These are actions.
const (
QR_CONTINUE = Action(iota)
QR_FAIL
QR_FAIL_RETRY
QRContinue = Action(iota)
QRFail
QRFailRetry
)
// BindVarCond represents a bind var condition.
@ -391,50 +393,52 @@ type BindVarCond struct {
// Operator represents the list of operators.
type Operator int
// These are comparison operators.
const (
QR_NOOP = Operator(iota)
QR_EQ
QR_NE
QR_LT
QR_GE
QR_GT
QR_LE
QR_MATCH
QR_NOMATCH
QR_IN
QR_NOTIN
QRNoOp = Operator(iota)
QREqual
QRNotEqual
QRLessThan
QRGreaterEqual
QRGreaterThan
QRLessEqual
QRMatch
QRNoMatch
QRIn
QRNotIn
)
var opmap = map[string]Operator{
"NOOP": QR_NOOP,
"UEQ": QR_EQ,
"UNE": QR_NE,
"ULT": QR_LT,
"UGE": QR_GE,
"UGT": QR_GT,
"ULE": QR_LE,
"IEQ": QR_EQ,
"INE": QR_NE,
"ILT": QR_LT,
"IGE": QR_GE,
"IGT": QR_GT,
"ILE": QR_LE,
"SEQ": QR_EQ,
"SNE": QR_NE,
"SLT": QR_LT,
"SGE": QR_GE,
"SGT": QR_GT,
"SLE": QR_LE,
"MATCH": QR_MATCH,
"NOMATCH": QR_NOMATCH,
"IN": QR_IN,
"NOTIN": QR_NOTIN,
"NOOP": QRNoOp,
"UEQ": QREqual,
"UNE": QRNotEqual,
"ULT": QRLessThan,
"UGE": QRGreaterEqual,
"UGT": QRGreaterThan,
"ULE": QRLessEqual,
"IEQ": QREqual,
"INE": QRNotEqual,
"ILT": QRLessThan,
"IGE": QRGreaterEqual,
"IGT": QRGreaterThan,
"ILE": QRLessEqual,
"SEQ": QREqual,
"SNE": QRNotEqual,
"SLT": QRLessThan,
"SGE": QRGreaterEqual,
"SGT": QRGreaterThan,
"SLE": QRLessEqual,
"MATCH": QRMatch,
"NOMATCH": QRNoMatch,
"IN": QRIn,
"NOTIN": QRNotIn,
}
// These are return statii.
const (
QR_OK = iota
QR_MISMATCH
QR_OUT_OF_RANGE
QROK = iota
QRMismatch
QROutOfRange
)
// bvcValue defines the common interface
@ -448,46 +452,46 @@ type bvcuint64 uint64
func (uval bvcuint64) eval(bv interface{}, op Operator, onMismatch bool) bool {
num, status := getuint64(bv)
switch op {
case QR_EQ:
case QREqual:
switch status {
case QR_OK:
case QROK:
return num == uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
case QR_NE:
case QRNotEqual:
switch status {
case QR_OK:
case QROK:
return num != uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
case QR_LT:
case QRLessThan:
switch status {
case QR_OK:
case QROK:
return num < uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
case QR_GE:
case QRGreaterEqual:
switch status {
case QR_OK:
case QROK:
return num >= uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
case QR_GT:
case QRGreaterThan:
switch status {
case QR_OK:
case QROK:
return num > uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
case QR_LE:
case QRLessEqual:
switch status {
case QR_OK:
case QROK:
return num <= uint64(uval)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
default:
@ -502,46 +506,46 @@ type bvcint64 int64
func (ival bvcint64) eval(bv interface{}, op Operator, onMismatch bool) bool {
num, status := getint64(bv)
switch op {
case QR_EQ:
case QREqual:
switch status {
case QR_OK:
case QROK:
return num == int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
case QR_NE:
case QRNotEqual:
switch status {
case QR_OK:
case QROK:
return num != int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
case QR_LT:
case QRLessThan:
switch status {
case QR_OK:
case QROK:
return num < int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
case QR_GE:
case QRGreaterEqual:
switch status {
case QR_OK:
case QROK:
return num >= int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
case QR_GT:
case QRGreaterThan:
switch status {
case QR_OK:
case QROK:
return num > int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
case QR_LE:
case QRLessEqual:
switch status {
case QR_OK:
case QROK:
return num <= int64(ival)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
default:
@ -555,21 +559,21 @@ type bvcstring string
func (sval bvcstring) eval(bv interface{}, op Operator, onMismatch bool) bool {
str, status := getstring(bv)
if status != QR_OK {
if status != QROK {
return onMismatch
}
switch op {
case QR_EQ:
case QREqual:
return str == string(sval)
case QR_NE:
case QRNotEqual:
return str != string(sval)
case QR_LT:
case QRLessThan:
return str < string(sval)
case QR_GE:
case QRGreaterEqual:
return str >= string(sval)
case QR_GT:
case QRGreaterThan:
return str > string(sval)
case QR_LE:
case QRLessEqual:
return str <= string(sval)
}
panic("unexpected:")
@ -581,13 +585,13 @@ type bvcre struct {
func (reval bvcre) eval(bv interface{}, op Operator, onMismatch bool) bool {
str, status := getstring(bv)
if status != QR_OK {
if status != QROK {
return onMismatch
}
switch op {
case QR_MATCH:
case QRMatch:
return reval.re.MatchString(str)
case QR_NOMATCH:
case QRNoMatch:
return !reval.re.MatchString(str)
}
panic("unexpected:")
@ -597,30 +601,30 @@ type bvcKeyRange pb.KeyRange
func (krval *bvcKeyRange) eval(bv interface{}, op Operator, onMismatch bool) bool {
switch op {
case QR_IN:
case QRIn:
switch num, status := getuint64(bv); status {
case QR_OK:
case QROK:
k := key.Uint64Key(num).Bytes()
return key.KeyRangeContains((*pb.KeyRange)(krval), k)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return false
}
// Not a number. Check string.
switch str, status := getstring(bv); status {
case QR_OK:
case QROK:
return key.KeyRangeContains((*pb.KeyRange)(krval), []byte(str))
}
case QR_NOTIN:
case QRNotIn:
switch num, status := getuint64(bv); status {
case QR_OK:
case QROK:
k := key.Uint64Key(num).Bytes()
return !key.KeyRangeContains((*pb.KeyRange)(krval), k)
case QR_OUT_OF_RANGE:
case QROutOfRange:
return true
}
// Not a number. Check string.
switch str, status := getstring(bv); status {
case QR_OK:
case QROK:
return !key.KeyRangeContains((*pb.KeyRange)(krval), []byte(str))
}
default:
@ -629,84 +633,86 @@ func (krval *bvcKeyRange) eval(bv interface{}, op Operator, onMismatch bool) boo
return onMismatch
}
// getuint64 returns QR_OUT_OF_RANGE for negative values
// getuint64 returns QROutOfRange for negative values
func getuint64(val interface{}) (uv uint64, status int) {
switch v := val.(type) {
case int:
if v < 0 {
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return uint64(v), QR_OK
return uint64(v), QROK
case int8:
if v < 0 {
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return uint64(v), QR_OK
return uint64(v), QROK
case int16:
if v < 0 {
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return uint64(v), QR_OK
return uint64(v), QROK
case int32:
if v < 0 {
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return uint64(v), QR_OK
return uint64(v), QROK
case int64:
if v < 0 {
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return uint64(v), QR_OK
return uint64(v), QROK
case uint64:
return v, QR_OK
return v, QROK
}
return 0, QR_MISMATCH
return 0, QRMismatch
}
// getint64 returns QR_OUT_OF_RANGE if a uint64 is too large
// getint64 returns QROutOfRange if a uint64 is too large
func getint64(val interface{}) (iv int64, status int) {
switch v := val.(type) {
case int:
return int64(v), QR_OK
return int64(v), QROK
case int8:
return int64(v), QR_OK
return int64(v), QROK
case int16:
return int64(v), QR_OK
return int64(v), QROK
case int32:
return int64(v), QR_OK
return int64(v), QROK
case int64:
return int64(v), QR_OK
return int64(v), QROK
case uint64:
if v > 0x7FFFFFFFFFFFFFFF { // largest int64
return 0, QR_OUT_OF_RANGE
return 0, QROutOfRange
}
return int64(v), QR_OK
return int64(v), QROK
}
return 0, QR_MISMATCH
return 0, QRMismatch
}
func getstring(val interface{}) (sv string, status int) {
switch v := val.(type) {
case []byte:
return string(v), QR_OK
return string(v), QROK
case string:
return v, QR_OK
return v, QROK
}
return "", QR_MISMATCH
return "", QRMismatch
}
//-----------------------------------------------
// Support functions for JSON
// MapStrOperator maps a string representation to an Operator.
func MapStrOperator(strop string) (op Operator, err error) {
if op, ok := opmap[strop]; ok {
return op, nil
}
return QR_NOOP, NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "invalid Operator %s", strop)
return QRNoOp, NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "invalid Operator %s", strop)
}
// BuildQueryRule builds a query rule from a ruleInfo.
func BuildQueryRule(ruleInfo map[string]interface{}) (qr *QueryRule, err error) {
qr = NewQueryRule("", "", QR_FAIL)
qr = NewQueryRule("", "", QRFail)
for k, v := range ruleInfo {
var sv string
var lv []interface{}
@ -780,9 +786,9 @@ func BuildQueryRule(ruleInfo map[string]interface{}) (qr *QueryRule, err error)
case "Action":
switch sv {
case "FAIL":
qr.act = QR_FAIL
qr.act = QRFail
case "FAIL_RETRY":
qr.act = QR_FAIL_RETRY
qr.act = QRFailRetry
default:
return nil, NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "invalid Action %s", sv)
}
@ -835,7 +841,7 @@ func buildBindVarCondition(bvc interface{}) (name string, onAbsent, onMismatch b
if err != nil {
return
}
if op == QR_NOOP {
if op == QRNoOp {
return
}
v, ok = bvcinfo["Value"]
@ -843,7 +849,7 @@ func buildBindVarCondition(bvc interface{}) (name string, onAbsent, onMismatch b
err = NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "Value missing in BindVarConds")
return
}
if op >= QR_EQ && op <= QR_LE {
if op >= QREqual && op <= QRLessEqual {
strvalue, ok := v.(string)
if !ok {
err = NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "want string: %v", v)
@ -866,14 +872,14 @@ func buildBindVarCondition(bvc interface{}) (name string, onAbsent, onMismatch b
} else {
panic("unexpected")
}
} else if op == QR_MATCH || op == QR_NOMATCH {
} else if op == QRMatch || op == QRNoMatch {
strvalue, ok := v.(string)
if !ok {
err = NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "want string: %v", v)
return
}
value = strvalue
} else if op == QR_IN || op == QR_NOTIN {
} else if op == QRIn || op == QRNotIn {
kr, ok := v.(map[string]interface{})
if !ok {
err = NewTabletError(ErrFail, vtrpc.ErrorCode_INTERNAL_ERROR, "want keyrange for Value")

Просмотреть файл

@ -18,8 +18,8 @@ import (
func TestQueryRules(t *testing.T) {
qrs := NewQueryRules()
qr1 := NewQueryRule("rule 1", "r1", QR_FAIL)
qr2 := NewQueryRule("rule 2", "r2", QR_FAIL)
qr1 := NewQueryRule("rule 1", "r1", QRFail)
qr2 := NewQueryRule("rule 2", "r2", QRFail)
qrs.Add(qr1)
qrs.Add(qr2)
@ -64,12 +64,12 @@ func TestQueryRules(t *testing.T) {
// TestCopy tests for deep copy
func TestCopy(t *testing.T) {
qrs := NewQueryRules()
qr1 := NewQueryRule("rule 1", "r1", QR_FAIL)
qr1 := NewQueryRule("rule 1", "r1", QRFail)
qr1.AddPlanCond(planbuilder.PlanPassSelect)
qr1.AddTableCond("aa")
qr1.AddBindVarCond("a", true, false, QR_NOOP, nil)
qr1.AddBindVarCond("a", true, false, QRNoOp, nil)
qr2 := NewQueryRule("rule 2", "r2", QR_FAIL)
qr2 := NewQueryRule("rule 2", "r2", QRFail)
qrs.Add(qr1)
qrs.Add(qr2)
@ -105,22 +105,22 @@ func TestCopy(t *testing.T) {
func TestFilterByPlan(t *testing.T) {
qrs := NewQueryRules()
qr1 := NewQueryRule("rule 1", "r1", QR_FAIL)
qr1 := NewQueryRule("rule 1", "r1", QRFail)
qr1.SetIPCond("123")
qr1.SetQueryCond("select")
qr1.AddPlanCond(planbuilder.PlanPassSelect)
qr1.AddBindVarCond("a", true, false, QR_NOOP, nil)
qr1.AddBindVarCond("a", true, false, QRNoOp, nil)
qr2 := NewQueryRule("rule 2", "r2", QR_FAIL)
qr2 := NewQueryRule("rule 2", "r2", QRFail)
qr2.AddPlanCond(planbuilder.PlanPassSelect)
qr2.AddPlanCond(planbuilder.PlanPKIn)
qr2.AddBindVarCond("a", true, false, QR_NOOP, nil)
qr2.AddBindVarCond("a", true, false, QRNoOp, nil)
qr3 := NewQueryRule("rule 3", "r3", QR_FAIL)
qr3 := NewQueryRule("rule 3", "r3", QRFail)
qr3.SetQueryCond("sele.*")
qr3.AddBindVarCond("a", true, false, QR_NOOP, nil)
qr3.AddBindVarCond("a", true, false, QRNoOp, nil)
qr4 := NewQueryRule("rule 4", "r4", QR_FAIL)
qr4 := NewQueryRule("rule 4", "r4", QRFail)
qr4.AddTableCond("b")
qr4.AddTableCond("c")
@ -180,7 +180,7 @@ func TestFilterByPlan(t *testing.T) {
t.Errorf("want r4, got %s", qrs1.rules[0].Name)
}
qr5 := NewQueryRule("rule 5", "r5", QR_FAIL)
qr5 := NewQueryRule("rule 5", "r5", QRFail)
qrs.Add(qr5)
qrs1 = qrs.filterByPlan("sel", planbuilder.PlanInsertPK, "a")
@ -198,7 +198,7 @@ func TestFilterByPlan(t *testing.T) {
}
func TestQueryRule(t *testing.T) {
qr := NewQueryRule("rule 1", "r1", QR_FAIL)
qr := NewQueryRule("rule 1", "r1", QRFail)
err := qr.SetIPCond("123")
if err != nil {
t.Errorf("unexpected: %v", err)
@ -234,11 +234,11 @@ func TestQueryRule(t *testing.T) {
}
func TestBindVarStruct(t *testing.T) {
qr := NewQueryRule("rule 1", "r1", QR_FAIL)
qr := NewQueryRule("rule 1", "r1", QRFail)
var err error
err = qr.AddBindVarCond("b", false, true, QR_NOOP, nil)
err = qr.AddBindVarCond("a", true, false, QR_NOOP, nil)
err = qr.AddBindVarCond("b", false, true, QRNoOp, nil)
err = qr.AddBindVarCond("a", true, false, QRNoOp, nil)
if err != nil {
t.Errorf("unexpected: %v", err)
}
@ -251,7 +251,7 @@ func TestBindVarStruct(t *testing.T) {
if qr.bindVarConds[1].onMismatch {
t.Errorf("want false, got true")
}
if qr.bindVarConds[1].op != QR_NOOP {
if qr.bindVarConds[1].op != QRNoOp {
t.Errorf("exepecting NOOP, got %v", qr.bindVarConds[1])
}
if qr.bindVarConds[1].value != nil {
@ -269,45 +269,45 @@ type BVCreation struct {
}
var creationCases = []BVCreation{
{"a", true, true, QR_EQ, uint64(1), false},
{"a", true, true, QR_NE, uint64(1), false},
{"a", true, true, QR_LT, uint64(1), false},
{"a", true, true, QR_GE, uint64(1), false},
{"a", true, true, QR_GT, uint64(1), false},
{"a", true, true, QR_LE, uint64(1), false},
{"a", true, true, QREqual, uint64(1), false},
{"a", true, true, QRNotEqual, uint64(1), false},
{"a", true, true, QRLessThan, uint64(1), false},
{"a", true, true, QRGreaterEqual, uint64(1), false},
{"a", true, true, QRGreaterThan, uint64(1), false},
{"a", true, true, QRLessEqual, uint64(1), false},
{"a", true, true, QR_EQ, int64(1), false},
{"a", true, true, QR_NE, int64(1), false},
{"a", true, true, QR_LT, int64(1), false},
{"a", true, true, QR_GE, int64(1), false},
{"a", true, true, QR_GT, int64(1), false},
{"a", true, true, QR_LE, int64(1), false},
{"a", true, true, QREqual, int64(1), false},
{"a", true, true, QRNotEqual, int64(1), false},
{"a", true, true, QRLessThan, int64(1), false},
{"a", true, true, QRGreaterEqual, int64(1), false},
{"a", true, true, QRGreaterThan, int64(1), false},
{"a", true, true, QRLessEqual, int64(1), false},
{"a", true, true, QR_EQ, "a", false},
{"a", true, true, QR_NE, "a", false},
{"a", true, true, QR_LT, "a", false},
{"a", true, true, QR_GE, "a", false},
{"a", true, true, QR_GT, "a", false},
{"a", true, true, QR_LE, "a", false},
{"a", true, true, QR_MATCH, "a", false},
{"a", true, true, QR_NOMATCH, "a", false},
{"a", true, true, QREqual, "a", false},
{"a", true, true, QRNotEqual, "a", false},
{"a", true, true, QRLessThan, "a", false},
{"a", true, true, QRGreaterEqual, "a", false},
{"a", true, true, QRGreaterThan, "a", false},
{"a", true, true, QRLessEqual, "a", false},
{"a", true, true, QRMatch, "a", false},
{"a", true, true, QRNoMatch, "a", false},
{"a", true, true, QR_IN, &pb.KeyRange{}, false},
{"a", true, true, QR_NOTIN, &pb.KeyRange{}, false},
{"a", true, true, QRIn, &pb.KeyRange{}, false},
{"a", true, true, QRNotIn, &pb.KeyRange{}, false},
{"a", true, true, QR_MATCH, int64(1), true},
{"a", true, true, QR_NOMATCH, int64(1), true},
{"a", true, true, QR_MATCH, "[", true},
{"a", true, true, QR_NOMATCH, "[", true},
{"a", true, true, QRMatch, int64(1), true},
{"a", true, true, QRNoMatch, int64(1), true},
{"a", true, true, QRMatch, "[", true},
{"a", true, true, QRNoMatch, "[", true},
{"a", true, true, QR_IN, int64(1), true},
{"a", true, true, QR_NOTIN, int64(1), true},
{"a", true, true, QRIn, int64(1), true},
{"a", true, true, QRNotIn, int64(1), true},
{"a", true, true, QR_EQ, int32(1), true},
{"a", true, true, QREqual, int32(1), true},
}
func TestBVCreation(t *testing.T) {
qr := NewQueryRule("rule 1", "r1", QR_FAIL)
qr := NewQueryRule("rule 1", "r1", QRFail)
for i, tcase := range creationCases {
err := qr.AddBindVarCond(tcase.name, tcase.onAbsent, tcase.onMismatch, tcase.op, tcase.value)
haserr := (err != nil)
@ -324,140 +324,140 @@ type BindVarTestCase struct {
}
var bvtestcases = []BindVarTestCase{
{BindVarCond{"b", true, true, QR_NOOP, nil}, 1, true},
{BindVarCond{"b", false, true, QR_NOOP, nil}, 1, false},
{BindVarCond{"a", true, true, QR_NOOP, nil}, 1, false},
{BindVarCond{"a", false, true, QR_NOOP, nil}, 1, true},
{BindVarCond{"b", true, true, QRNoOp, nil}, 1, true},
{BindVarCond{"b", false, true, QRNoOp, nil}, 1, false},
{BindVarCond{"a", true, true, QRNoOp, nil}, 1, false},
{BindVarCond{"a", false, true, QRNoOp, nil}, 1, true},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int16(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int32(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int64(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, uint64(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcuint64(10)}, "abc", true},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int16(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int32(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int64(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, uint64(1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QREqual, bvcuint64(10)}, "abc", true},
{BindVarCond{"a", true, true, QR_NE, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_NE, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_NE, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_NE, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRNotEqual, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QR_LT, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_LT, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_LT, bvcuint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QR_LT, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QRLessThan, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRLessThan, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRLessThan, bvcuint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QRLessThan, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QR_GE, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_GE, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_GE, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_GE, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QR_GT, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_GT, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_GT, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_GT, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcuint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcuint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcuint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRGreaterThan, bvcuint64(10)}, int8(-1), false},
{BindVarCond{"a", true, true, QR_LE, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_LE, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_LE, bvcuint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QR_LE, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QRLessEqual, bvcuint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRLessEqual, bvcuint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QRLessEqual, bvcuint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QRLessEqual, bvcuint64(10)}, int8(-1), true},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int16(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int32(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, int64(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, uint64(1), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QR_EQ, bvcint64(10)}, "abc", true},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int16(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int32(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, int64(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, uint64(1), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QREqual, bvcint64(10)}, "abc", true},
{BindVarCond{"a", true, true, QR_NE, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_NE, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_NE, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_NE, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRNotEqual, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRNotEqual, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QR_LT, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_LT, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_LT, bvcint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QR_LT, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QRLessThan, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRLessThan, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRLessThan, bvcint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QRLessThan, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QR_GE, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_GE, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_GE, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_GE, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QR_GT, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QR_GT, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QR_GT, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QR_GT, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QRGreaterThan, bvcint64(10)}, int8(1), false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcint64(10)}, int8(10), false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcint64(10)}, int8(11), true},
{BindVarCond{"a", true, true, QRGreaterThan, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), true},
{BindVarCond{"a", true, true, QR_LE, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QR_LE, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QR_LE, bvcint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QR_LE, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QRLessEqual, bvcint64(10)}, int8(1), true},
{BindVarCond{"a", true, true, QRLessEqual, bvcint64(10)}, int8(10), true},
{BindVarCond{"a", true, true, QRLessEqual, bvcint64(10)}, int8(11), false},
{BindVarCond{"a", true, true, QRLessEqual, bvcint64(10)}, uint64(0xFFFFFFFFFFFFFFFF), false},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, []byte("a"), false},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, []byte("b"), true},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, []byte("c"), false},
{BindVarCond{"a", true, true, QR_EQ, bvcstring("b")}, int8(1), true},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, []byte("a"), false},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, []byte("b"), true},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, []byte("c"), false},
{BindVarCond{"a", true, true, QREqual, bvcstring("b")}, int8(1), true},
{BindVarCond{"a", true, true, QR_NE, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QR_NE, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QR_NE, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QRNotEqual, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QRNotEqual, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QRNotEqual, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QR_LT, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QR_LT, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QR_LT, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QRLessThan, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QRLessThan, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QRLessThan, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QR_GE, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QR_GE, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QR_GE, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QRGreaterEqual, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QR_GT, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QR_GT, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QR_GT, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QRGreaterThan, bvcstring("b")}, "a", false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcstring("b")}, "b", false},
{BindVarCond{"a", true, true, QRGreaterThan, bvcstring("b")}, "c", true},
{BindVarCond{"a", true, true, QR_LE, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QR_LE, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QR_LE, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QRLessEqual, bvcstring("b")}, "a", true},
{BindVarCond{"a", true, true, QRLessEqual, bvcstring("b")}, "b", true},
{BindVarCond{"a", true, true, QRLessEqual, bvcstring("b")}, "c", false},
{BindVarCond{"a", true, true, QR_MATCH, makere("a.*")}, "c", false},
{BindVarCond{"a", true, true, QR_MATCH, makere("a.*")}, "a", true},
{BindVarCond{"a", true, true, QR_MATCH, makere("a.*")}, int8(1), true},
{BindVarCond{"a", true, true, QRMatch, makere("a.*")}, "c", false},
{BindVarCond{"a", true, true, QRMatch, makere("a.*")}, "a", true},
{BindVarCond{"a", true, true, QRMatch, makere("a.*")}, int8(1), true},
{BindVarCond{"a", true, true, QR_NOMATCH, makere("a.*")}, "c", true},
{BindVarCond{"a", true, true, QR_NOMATCH, makere("a.*")}, "a", false},
{BindVarCond{"a", true, true, QR_NOMATCH, makere("a.*")}, int8(1), true},
{BindVarCond{"a", true, true, QRNoMatch, makere("a.*")}, "c", true},
{BindVarCond{"a", true, true, QRNoMatch, makere("a.*")}, "a", false},
{BindVarCond{"a", true, true, QRNoMatch, makere("a.*")}, int8(1), true},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0), false},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x5000000000000000), true},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x7000000000000000), false},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int(-1), false},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int16(-1), false},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int32(-1), false},
{BindVarCond{"a", true, true, QR_IN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int64(-1), false},
{BindVarCond{"a", true, true, QR_IN, strKeyRange("b", "d")}, "a", false},
{BindVarCond{"a", true, true, QR_IN, strKeyRange("b", "d")}, "c", true},
{BindVarCond{"a", true, true, QR_IN, strKeyRange("b", "d")}, "e", false},
{BindVarCond{"a", true, true, QR_IN, strKeyRange("b", "d")}, float64(1.0), true},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0), false},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x5000000000000000), true},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x7000000000000000), false},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int(-1), false},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int16(-1), false},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int32(-1), false},
{BindVarCond{"a", true, true, QRIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int64(-1), false},
{BindVarCond{"a", true, true, QRIn, strKeyRange("b", "d")}, "a", false},
{BindVarCond{"a", true, true, QRIn, strKeyRange("b", "d")}, "c", true},
{BindVarCond{"a", true, true, QRIn, strKeyRange("b", "d")}, "e", false},
{BindVarCond{"a", true, true, QRIn, strKeyRange("b", "d")}, float64(1.0), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x5000000000000000), false},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x7000000000000000), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int(-1), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int16(-1), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int32(-1), true},
{BindVarCond{"a", true, true, QR_NOTIN, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int64(-1), true},
{BindVarCond{"a", true, true, QR_NOTIN, strKeyRange("b", "d")}, "a", true},
{BindVarCond{"a", true, true, QR_NOTIN, strKeyRange("b", "d")}, "c", false},
{BindVarCond{"a", true, true, QR_NOTIN, strKeyRange("b", "d")}, "e", true},
{BindVarCond{"a", true, true, QR_NOTIN, strKeyRange("b", "d")}, float64(1.0), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x5000000000000000), false},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, uint64(0x7000000000000000), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int(-1), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int16(-1), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int32(-1), true},
{BindVarCond{"a", true, true, QRNotIn, numKeyRange(0x4000000000000000, 0x6000000000000000)}, int64(-1), true},
{BindVarCond{"a", true, true, QRNotIn, strKeyRange("b", "d")}, "a", true},
{BindVarCond{"a", true, true, QRNotIn, strKeyRange("b", "d")}, "c", false},
{BindVarCond{"a", true, true, QRNotIn, strKeyRange("b", "d")}, "e", true},
{BindVarCond{"a", true, true, QRNotIn, strKeyRange("b", "d")}, float64(1.0), true},
}
func makere(s string) bvcre {
@ -496,14 +496,14 @@ func TestBVConditions(t *testing.T) {
func TestAction(t *testing.T) {
qrs := NewQueryRules()
qr1 := NewQueryRule("rule 1", "r1", QR_FAIL)
qr1 := NewQueryRule("rule 1", "r1", QRFail)
qr1.SetIPCond("123")
qr2 := NewQueryRule("rule 2", "r2", QR_FAIL_RETRY)
qr2 := NewQueryRule("rule 2", "r2", QRFailRetry)
qr2.SetUserCond("user")
qr3 := NewQueryRule("rule 3", "r3", QR_FAIL)
qr3.AddBindVarCond("a", true, true, QR_EQ, uint64(1))
qr3 := NewQueryRule("rule 3", "r3", QRFail)
qr3.AddBindVarCond("a", true, true, QREqual, uint64(1))
qrs.Add(qr1)
qrs.Add(qr2)
@ -512,26 +512,26 @@ func TestAction(t *testing.T) {
bv := make(map[string]interface{})
bv["a"] = uint64(0)
action, desc := qrs.getAction("123", "user1", bv)
if action != QR_FAIL {
if action != QRFail {
t.Errorf("want fail")
}
if desc != "rule 1" {
t.Errorf("want rule 1, got %s", desc)
}
action, desc = qrs.getAction("1234", "user", bv)
if action != QR_FAIL_RETRY {
if action != QRFailRetry {
t.Errorf("want fail_retry")
}
if desc != "rule 2" {
t.Errorf("want rule 2, got %s", desc)
}
action, desc = qrs.getAction("1234", "user1", bv)
if action != QR_CONTINUE {
if action != QRContinue {
t.Errorf("want continue")
}
bv["a"] = uint64(1)
action, desc = qrs.getAction("1234", "user1", bv)
if action != QR_FAIL {
if action != QRFail {
t.Errorf("want fail")
}
if desc != "rule 3" {
@ -605,7 +605,7 @@ func TestImport(t *testing.T) {
if !bvc.onAbsent {
t.Errorf("want true")
}
if bvc.op != QR_NOOP {
if bvc.op != QRNoOp {
t.Errorf("want NOOP, got %v", bvc.op)
}
bvc = qrs.rules[0].bindVarConds[1]
@ -618,11 +618,11 @@ func TestImport(t *testing.T) {
if !bvc.onMismatch {
t.Errorf("want true")
}
if qrs.rules[0].act != QR_FAIL_RETRY {
if qrs.rules[0].act != QRFailRetry {
t.Errorf("want FAIL_RETRY")
}
if bvc.op != QR_EQ {
t.Errorf("want %v, got %v", QR_EQ, bvc.op)
if bvc.op != QREqual {
t.Errorf("want %v, got %v", QREqual, bvc.op)
}
if bvc.value.(bvcuint64) != 123 {
t.Errorf("want 123, got %v", bvc.value.(bvcuint64))
@ -648,7 +648,7 @@ func TestImport(t *testing.T) {
if qrs.rules[1].bindVarConds != nil {
t.Errorf("want nil")
}
if qrs.rules[1].act != QR_FAIL {
if qrs.rules[1].act != QRFail {
t.Errorf("want FAIL")
}
}
@ -668,32 +668,32 @@ const (
)
var validjsons = []ValidJSONCase{
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UEQ", "Value": "123"}]}]`, QR_EQ, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UNE", "Value": "123"}]}]`, QR_NE, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ULT", "Value": "123"}]}]`, QR_LT, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UGE", "Value": "123"}]}]`, QR_GE, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UGT", "Value": "123"}]}]`, QR_GT, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ULE", "Value": "123"}]}]`, QR_LE, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UEQ", "Value": "123"}]}]`, QREqual, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UNE", "Value": "123"}]}]`, QRNotEqual, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ULT", "Value": "123"}]}]`, QRLessThan, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UGE", "Value": "123"}]}]`, QRGreaterEqual, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "UGT", "Value": "123"}]}]`, QRGreaterThan, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ULE", "Value": "123"}]}]`, QRLessEqual, UINT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IEQ", "Value": "123"}]}]`, QR_EQ, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "INE", "Value": "123"}]}]`, QR_NE, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ILT", "Value": "123"}]}]`, QR_LT, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IGE", "Value": "123"}]}]`, QR_GE, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IGT", "Value": "123"}]}]`, QR_GT, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ILE", "Value": "123"}]}]`, QR_LE, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IEQ", "Value": "123"}]}]`, QREqual, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "INE", "Value": "123"}]}]`, QRNotEqual, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ILT", "Value": "123"}]}]`, QRLessThan, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IGE", "Value": "123"}]}]`, QRGreaterEqual, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IGT", "Value": "123"}]}]`, QRGreaterThan, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "ILE", "Value": "123"}]}]`, QRLessEqual, INT},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SEQ", "Value": "123"}]}]`, QR_EQ, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SNE", "Value": "123"}]}]`, QR_NE, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SLT", "Value": "123"}]}]`, QR_LT, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SGE", "Value": "123"}]}]`, QR_GE, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SGT", "Value": "123"}]}]`, QR_GT, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SLE", "Value": "123"}]}]`, QR_LE, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SEQ", "Value": "123"}]}]`, QREqual, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SNE", "Value": "123"}]}]`, QRNotEqual, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SLT", "Value": "123"}]}]`, QRLessThan, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SGE", "Value": "123"}]}]`, QRGreaterEqual, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SGT", "Value": "123"}]}]`, QRGreaterThan, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "SLE", "Value": "123"}]}]`, QRLessEqual, STR},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "MATCH", "Value": "123"}]}]`, QR_MATCH, REGEXP},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "NOMATCH", "Value": "123"}]}]`, QR_NOMATCH, REGEXP},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "MATCH", "Value": "123"}]}]`, QRMatch, REGEXP},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "NOMATCH", "Value": "123"}]}]`, QRNoMatch, REGEXP},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IN", "Value": {"Start": "1", "End": "2"}}]}]`, QR_IN, KEYRANGE},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "NOTIN", "Value": {"Start": "1", "End": "2"}}]}]`, QR_NOTIN, KEYRANGE},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "IN", "Value": {"Start": "1", "End": "2"}}]}]`, QRIn, KEYRANGE},
{`[{"BindVarConds": [{"Name": "bvname1", "OnAbsent": true, "OnMismatch": true, "Operator": "NOTIN", "Value": {"Start": "1", "End": "2"}}]}]`, QRNotIn, KEYRANGE},
}
func TestValidJSON(t *testing.T) {
@ -830,7 +830,7 @@ func TestBuildQueryRuleActionFail(t *testing.T) {
if err != nil {
t.Fatalf("build query rule should succeed")
}
if qr.act != QR_FAIL {
if qr.act != QRFail {
t.Fatalf("action should fail")
}
}
@ -840,7 +840,7 @@ func TestBuildQueryRuleFailureModes(t *testing.T) {
var errStr string
_, err = BuildQueryRule(map[string]interface{}{
"BindVarConds": []interface{}{map[string]interface{}{"Name": "a", "OnAbsent": true, "Operator": QR_IN, "Value": &pb.KeyRange{}}},
"BindVarConds": []interface{}{map[string]interface{}{"Name": "a", "OnAbsent": true, "Operator": QRIn, "Value": &pb.KeyRange{}}},
})
if err == nil {
t.Fatalf("should get an error")
@ -918,17 +918,17 @@ func TestBuildQueryRuleFailureModes(t *testing.T) {
}
func TestBadAddBindVarCond(t *testing.T) {
qr1 := NewQueryRule("rule 1", "r1", QR_FAIL)
err := qr1.AddBindVarCond("a", true, false, QR_MATCH, uint64(1))
qr1 := NewQueryRule("rule 1", "r1", QRFail)
err := qr1.AddBindVarCond("a", true, false, QRMatch, uint64(1))
if err == nil {
t.Fatalf("invalid op: QR_MATCH for value type: uint64")
t.Fatalf("invalid op: QRMatch for value type: uint64")
}
err = qr1.AddBindVarCond("a", true, false, QR_NOTIN, "test")
err = qr1.AddBindVarCond("a", true, false, QRNotIn, "test")
if err == nil {
t.Fatalf("invalid op: QR_NOTIN for value type: string")
t.Fatalf("invalid op: QRNotIn for value type: string")
}
err = qr1.AddBindVarCond("a", true, false, QR_LT, &pb.KeyRange{})
err = qr1.AddBindVarCond("a", true, false, QRLessThan, &pb.KeyRange{})
if err == nil {
t.Fatalf("invalid op: QR_LT for value type: key.KeyRange")
t.Fatalf("invalid op: QRLessThan for value type: key.KeyRange")
}
}

Просмотреть файл

@ -43,7 +43,7 @@ func TestTabletErrorMsgTooLong(t *testing.T) {
t.Fatalf("tablet error should have error type ErrFatal and error code %v", vtrpc.ErrorCode_INTEGRITY_ERROR)
}
if tabletErr.Message != string(buf[:maxErrLen]) {
t.Fatalf("message should be capped, only %s character will be shown", maxErrLen)
t.Fatalf("message should be capped, only %d character will be shown", maxErrLen)
}
}

Просмотреть файл

@ -339,7 +339,7 @@ func TestTabletServerCheckMysqlInUnintialized(t *testing.T) {
t.Fatalf("invalid state reported by expvar, should be a valid state code, but got: %s", tabletState.String())
}
if varzState != StateNotConnected {
t.Fatalf("queryservice should be in %d state, but exposed varz reports: %s", StateNotConnected, varzState)
t.Fatalf("queryservice should be in %d state, but exposed varz reports: %d", StateNotConnected, varzState)
}
}