// Creates an index and waits for it to become active func CreateSecondaryIndex( indexName, bucketName, server, whereExpr string, indexFields []string, isPrimary bool, with []byte, skipIfExists bool, indexActiveTimeoutSeconds int64, client *qc.GsiClient) error { if client == nil { c, e := CreateClient(server, "2itest") if e != nil { return e } client = c defer client.Close() } indexExists := IndexExistsWithClient(indexName, bucketName, server, client) if skipIfExists == true && indexExists == true { return nil } var secExprs []string if isPrimary == false { for _, indexField := range indexFields { expr, err := n1ql.ParseExpression(indexField) if err != nil { log.Printf("Creating index %v. Error while parsing the expression (%v) : %v", indexName, indexField, err) } secExprs = append(secExprs, expression.NewStringer().Visit(expr)) } } using := "gsi" exprType := "N1QL" partnExp := "" start := time.Now() defnID, err := client.CreateIndex(indexName, bucketName, using, exprType, partnExp, whereExpr, secExprs, isPrimary, with) if err == nil { log.Printf("Created the secondary index %v. Waiting for it become active", indexName) e := WaitTillIndexActive(defnID, client, indexActiveTimeoutSeconds) if e != nil { return e } else { elapsed := time.Since(start) tc.LogPerfStat("CreateAndBuildIndex", elapsed) return nil } } return err }
// Creates an index and DOES NOT wait for it to become active func CreateSecondaryIndexAsync( indexName, bucketName, server, whereExpr string, indexFields []string, isPrimary bool, with []byte, skipIfExists bool, client *qc.GsiClient) error { if client == nil { c, e := CreateClient(server, "2itest") if e != nil { return e } client = c defer client.Close() } indexExists := IndexExistsWithClient(indexName, bucketName, server, client) if skipIfExists == true && indexExists == true { return nil } var secExprs []string if isPrimary == false { for _, indexField := range indexFields { expr, err := n1ql.ParseExpression(indexField) if err != nil { log.Printf("Creating index %v. Error while parsing the expression (%v) : %v", indexName, indexField, err) } secExprs = append(secExprs, expression.NewStringer().Visit(expr)) } } using := "gsi" exprType := "N1QL" partnExp := "" _, err := client.CreateIndex(indexName, bucketName, using, exprType, partnExp, whereExpr, secExprs, isPrimary, with) if err == nil { log.Printf("Created the secondary index %v", indexName) return nil } return err }
func main() { argParse() expr, err := n1ql.ParseExpression(options.expr) if err != nil { log.Fatal(err) } if options.json { exprb, err := json.Marshal(expr) if err != nil { log.Fatal(err) } exprstr := string(exprb) fmt.Printf("input: %v\n", options.expr) fmt.Printf("expr: %T %v\n", expr, expr) fmt.Printf("json: %T %v\n", exprstr, exprstr) } else { exprstr := expression.NewStringer().Visit(expr) fmt.Printf("input: %v\n", options.expr) fmt.Printf("expr: %T %v\n", expr, expr) fmt.Printf("output: %T %v\n", exprstr, exprstr) } }
// ParseArgs into Command object, return the list of arguments, // flagset used for parseing and error if any. func ParseArgs(arguments []string) (*Command, []string, *flag.FlagSet, error) { var fields, bindexes string var inclusion uint var equal, low, high string var useSessionCons bool cmdOptions := &Command{Consistency: c.AnyConsistency} fset := flag.NewFlagSet("cmd", flag.ExitOnError) // basic options fset.StringVar(&cmdOptions.Server, "server", "", "Cluster server address") fset.StringVar(&cmdOptions.Auth, "auth", "", "Auth user and password") fset.StringVar(&cmdOptions.Bucket, "bucket", "", "Bucket name") fset.StringVar(&cmdOptions.OpType, "type", "", "Command: scan|stats|scanAll|count|nodes|create|build|drop|list|config") fset.StringVar(&cmdOptions.IndexName, "index", "", "Index name") // options for create-index fset.StringVar(&cmdOptions.WhereStr, "where", "", "where clause for create index") fset.StringVar(&fields, "fields", "", "Comma separated on-index fields") // secStrs fset.BoolVar(&cmdOptions.IsPrimary, "primary", false, "Is primary index") fset.StringVar(&cmdOptions.With, "with", "", "index specific properties") // options for build-indexes, drop-indexes fset.StringVar(&bindexes, "indexes", "", "csv list of bucket.index to build") // options for Range, Statistics, Count fset.StringVar(&low, "low", "[]", "Span.Range: [low]") fset.StringVar(&high, "high", "[]", "Span.Range: [high]") fset.StringVar(&equal, "equal", "", "Span.Lookup: [key]") fset.UintVar(&inclusion, "incl", 0, "Range: 0|1|2|3") fset.Int64Var(&cmdOptions.Limit, "limit", 10, "Row limit") fset.BoolVar(&cmdOptions.Help, "h", false, "print help") fset.BoolVar(&useSessionCons, "consistency", false, "Use session consistency") // options for setting configuration fset.StringVar(&cmdOptions.ConfigKey, "ckey", "", "Config key") fset.StringVar(&cmdOptions.ConfigVal, "cval", "", "Config value") fset.StringVar(&cmdOptions.Using, "using", c.ForestDB, "storate type to use") // not useful to expose in sherlock cmdOptions.ExprType = "N1QL" cmdOptions.PartnStr = "partn" if err := fset.Parse(arguments); err != nil { return nil, nil, fset, err } if useSessionCons { cmdOptions.Consistency = c.SessionConsistency } // if server is not specified, try guessing if cmdOptions.Server == "" { if guess := guessServer(); guess != "" { cmdOptions.Server = guess fset.Set("server", guess) } } // if server is not specified, try guessing if cmdOptions.Auth == "" { if guess := guessAuth(cmdOptions.Server); guess != "" { cmdOptions.Auth = guess fset.Set("auth", guess) } } // validate combinations err := validate(cmdOptions, fset) if err != nil { return nil, nil, fset, err } // bindexes if len(bindexes) > 0 { cmdOptions.Bindexes = strings.Split(bindexes, ",") } // inclusion, secStrs, equal, low, high cmdOptions.Inclusion = qclient.Inclusion(inclusion) cmdOptions.SecStrs = make([]string, 0) if fields != "" { for _, field := range strings.Split(fields, ",") { expr, err := n1ql.ParseExpression(field) if err != nil { msgf := "Error occured: Invalid field (%v) %v\n" return nil, nil, fset, fmt.Errorf(msgf, field, err) } secStr := expression.NewStringer().Visit(expr) cmdOptions.SecStrs = append(cmdOptions.SecStrs, secStr) } } if equal != "" { cmdOptions.Equal = c.SecondaryKey(Arg2Key([]byte(equal))) } cmdOptions.Low = c.SecondaryKey(Arg2Key([]byte(low))) cmdOptions.High = c.SecondaryKey(Arg2Key([]byte(high))) // with if len(cmdOptions.With) > 0 { err := json.Unmarshal([]byte(cmdOptions.With), &cmdOptions.WithPlan) if err != nil { logging.Fatalf("%v\n", err) os.Exit(1) } } // setup cbauth if cmdOptions.Auth != "" { up := strings.Split(cmdOptions.Auth, ":") _, err := cbauth.InternalRetryDefaultInit(cmdOptions.Server, up[0], up[1]) if err != nil { logging.Fatalf("Failed to initialize cbauth: %s\n", err) os.Exit(1) } } return cmdOptions, fset.Args(), fset, err }
func Parse(s string) (expression.Expression, error) { return n1ql.ParseExpression(s) }