Ejemplo n.º 1
0
/*
Marshals input into byte array.
*/
func (this *SetTerm) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "setTerm"}
	r["path"] = expression.NewStringer().Visit(this.path)
	r["value"] = expression.NewStringer().Visit(this.value)
	r["updateFor"] = this.updateFor
	return json.Marshal(r)
}
Ejemplo n.º 2
0
/*
Marshals input into byte array.
*/
func (this *SetTerm) MarshalJSON() ([]byte, error) {
	r := make(map[string]interface{}, 3)
	r["path"] = expression.NewStringer().Visit(this.path)
	r["value"] = expression.NewStringer().Visit(this.value)
	if this.updateFor != nil {
		r["path_for"] = this.updateFor
	}

	return json.Marshal(r)
}
Ejemplo n.º 3
0
func (this *Set) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Set"}
	s := make([]interface{}, 0, len(this.node.Terms()))
	for _, term := range this.node.Terms() {
		t := make(map[string]interface{})
		t["path"] = expression.NewStringer().Visit(term.Path())
		t["expr"] = expression.NewStringer().Visit(term.Value())
		s = append(s, t)
	}
	r["set_terms"] = s
	return json.Marshal(r)
}
Ejemplo n.º 4
0
/*
Marshals the input keyspace into a byte array.
*/
func (this *KeyspaceTerm) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "keyspaceTerm"}
	r["as"] = this.as
	if this.keys != nil {
		r["keys"] = expression.NewStringer().Visit(this.keys)
	}
	r["namespace"] = this.namespace
	r["keyspace"] = this.keyspace
	if this.projection != nil {
		r["projection"] = expression.NewStringer().Visit(this.projection)
	}
	return json.Marshal(r)
}
Ejemplo n.º 5
0
func (idx *viewIndex) putDesignDoc() error {
	var view cb.ViewDefinition
	view.Map = idx.ddoc.mapfn

	var put ddocJSON
	put.Views = make(map[string]cb.ViewDefinition)
	put.Views[idx.name] = view
	put.IndexChecksum = idx.ddoc.checksum()
	put.PrimaryIndex = idx.IsPrimary()

	// add view update options
	put.Options = map[string]interface{}{"updateMinChanges": 1}

	put.IndexOn = make([]string, len(idx.on))
	for idx, expr := range idx.on {
		put.IndexOn[idx] = expression.NewStringer().Visit(expr)
	}

	if condition := idx.Condition(); condition != nil {
		put.Condition = expression.NewStringer().Visit(condition)
	}

	if err := idx.keyspace.cbbucket.PutDDoc(idx.DDocName(), &put); err != nil {
		return err
	}

	var saved *ddocJSON = nil
	var err error = nil

	// give the PUT some time to register
	for i := 0; i < 3; i++ {
		if i > 1 {
			time.Sleep(time.Duration(i*3) * time.Second)
		}

		saved, err = getDesignDoc(idx.keyspace, idx.DDocName())
		if err == nil {
			break
		}
	}

	if err != nil {
		return errors.New("Creating index '" + idx.name + "' failed: " + err.Error())
	}

	if saved.IndexChecksum != idx.ddoc.checksum() {
		return errors.New("Checksum mismatch after creating index '" + idx.name + "'")
	}

	return nil
}
Ejemplo n.º 6
0
func (this *InitialGroup) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "InitialGroup"}
	keylist := make([]string, 0, len(this.keys))
	for _, key := range this.keys {
		keylist = append(keylist, expression.NewStringer().Visit(key))
	}
	r["group_keys"] = keylist
	s := make([]interface{}, 0, len(this.aggregates))
	for _, agg := range this.aggregates {
		s = append(s, expression.NewStringer().Visit(agg))
	}
	r["aggregates"] = s
	return json.Marshal(r)
}
Ejemplo n.º 7
0
func Test2iCondition(t *testing.T) {
	whereKey := index.Condition()
	v := expression.NewStringer().Visit(whereKey)
	if v != "(30 < `age`)" {
		t.Fatalf("failed Condition() - %v, expected (30 < `age`)", v)
	}
}
Ejemplo n.º 8
0
func (this *IndexNest) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "IndexNest"}
	r["namespace"] = this.term.Namespace()
	r["keyspace"] = this.term.Keyspace()
	r["on_key"] = expression.NewStringer().Visit(this.term.Keys())
	r["for"] = this.keyFor

	if this.outer {
		r["outer"] = this.outer
	}

	if this.term.As() != "" {
		r["as"] = this.term.As()
	}

	scan := map[string]interface{}{
		"index": this.index.Name(),
		"using": this.index.Type(),
	}

	if this.covers != nil {
		scan["covers"] = this.covers
	}

	r["scan"] = scan
	return json.Marshal(r)
}
Ejemplo n.º 9
0
func indexKeyToIndexKeyStringArray(key expression.Expressions) []string {
	rv := make([]string, len(key))
	for i, kp := range key {
		rv[i] = expression.NewStringer().Visit(kp)
	}
	return rv
}
Ejemplo n.º 10
0
func (this *InitialProject) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "InitialProject"}

	if this.projection.Distinct() {
		r["distinct"] = this.projection.Distinct()
	}

	if this.projection.Raw() {
		r["raw"] = this.projection.Raw()
	}

	s := make([]interface{}, 0, len(this.terms))
	for _, term := range this.terms {
		t := make(map[string]interface{})

		if term.Result().Star() {
			t["star"] = term.Result().Star()
		}

		if term.Result().As() != "" {
			t["as"] = term.Result().As()
		}

		expr := term.Result().Expression()
		if expr != nil {
			t["expr"] = expression.NewStringer().Visit(expr)
		}

		s = append(s, t)
	}
	r["result_terms"] = s
	return json.Marshal(r)
}
Ejemplo n.º 11
0
/*
Marshals input into byte array.
*/
func (this *UpdateFor) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "updateFor"}
	r["bindings"] = this.bindings
	if this.when != nil {
		r["when"] = expression.NewStringer().Visit(this.when)
	}
	return json.Marshal(r)
}
Ejemplo n.º 12
0
/*
Marshals input receiver into byte array.
*/
func (this *CreateIndex) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "createIndex"}
	r["keyspaceRef"] = this.keyspace
	r["name"] = this.name
	if this.partition != nil {
		r["partition"] = expression.NewStringer().Visit(this.partition)
	}
	if this.where != nil {
		r["where"] = expression.NewStringer().Visit(this.where)
	}
	r["using"] = this.using
	if this.with != nil {
		r["with"] = this.with
	}

	return json.Marshal(r)
}
Ejemplo n.º 13
0
func Test2iRangeKey(t *testing.T) {
	rangeKey := index.RangeKey()
	if len(rangeKey) != 1 {
		t.Fatalf("failed RangeKey() - %v, expected 1", len(rangeKey))
	} else if v := expression.NewStringer().Visit(rangeKey[0]); v != "`name`" {
		t.Fatalf("failed RangeKey() - %v, expected `name`")
	}
}
Ejemplo n.º 14
0
func Test2iSeekKey(t *testing.T) {
	equalKey := index.SeekKey()
	if len(equalKey) != 1 {
		t.Fatalf("failed SeekKey() - %v, expected 1", len(equalKey))
	} else if v := expression.NewStringer().Visit(equalKey[0]); v != "`gender`" {
		t.Fatalf("failed SeekKey() - %v, expected `gender`", v)
	}
}
Ejemplo n.º 15
0
// CreateIndex implements datastore.Indexer{} interface. Create a secondary
// index on this keyspace
func (gsi *gsiKeyspace) CreateIndex(
	requestId, name string, seekKey, rangeKey expression.Expressions,
	where expression.Expression, with value.Value) (
	datastore.Index, errors.Error) {

	var partnStr string
	if seekKey != nil && len(seekKey) > 0 {
		partnStr = expression.NewStringer().Visit(seekKey[0])
	}

	var whereStr string
	if where != nil {
		whereStr = expression.NewStringer().Visit(where)
	}

	secStrs := make([]string, len(rangeKey))
	for i, key := range rangeKey {
		s := expression.NewStringer().Visit(key)
		secStrs[i] = s
	}

	var withJSON []byte
	var err error
	if with != nil {
		if withJSON, err = with.MarshalJSON(); err != nil {
			return nil, errors.NewError(err, "GSI error marshalling WITH clause")
		}
	}
	defnID, err := gsi.gsiClient.CreateIndex(
		name,
		gsi.keyspace,       /*bucket-name*/
		string(c.ForestDB), /*using, by default always forestdb*/
		"N1QL",             /*exprType*/
		partnStr, whereStr, secStrs,
		false, /*isPrimary*/
		withJSON)
	if err != nil {
		return nil, errors.NewError(err, "GSI CreateIndex()")
	}
	// refresh to get back the newly created index.
	if err := gsi.Refresh(); err != nil {
		return nil, err
	}
	return gsi.IndexById(defnID2String(defnID))
}
Ejemplo n.º 16
0
/*
Marshals input unnest terms into byte array.
*/
func (this *Unnest) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "unnest"}
	r["left"] = this.left
	r["as"] = this.as
	r["outer"] = this.outer
	if this.expr != nil {
		r["expr"] = expression.NewStringer().Visit(this.expr)
	}
	return json.Marshal(r)
}
Ejemplo n.º 17
0
/*
Marshal input ResultTerm into byte array.
*/
func (this *ResultTerm) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"type": "resultTerm"}
	r["alias"] = this.alias
	r["as"] = this.as
	if this.expr != nil {
		r["expr"] = expression.NewStringer().Visit(this.expr)
	}
	r["star"] = this.star
	return json.Marshal(r)
}
Ejemplo n.º 18
0
func (this *Fetch) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Fetch"}
	if this.term.Projection() != nil {
		r["projection"] = expression.NewStringer().Visit(this.term.Projection())
	}
	r["namespace"] = this.term.Namespace()
	r["keyspace"] = this.term.Keyspace()
	if this.term.As() != "" {
		r["as"] = this.term.As()
	}
	return json.Marshal(r)
}
Ejemplo n.º 19
0
func (this *Unnest) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Unnest"}

	if this.term.Outer() {
		r["outer"] = this.term.Outer()
	}

	r["expr"] = expression.NewStringer().Visit(this.term.Expression())
	if this.alias != "" {
		r["as"] = this.alias
	}
	return json.Marshal(r)
}
Ejemplo n.º 20
0
func (this *PrimaryScan) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "PrimaryScan"}
	r["index"] = this.index.Name()
	r["namespace"] = this.term.Namespace()
	r["keyspace"] = this.term.Keyspace()
	r["using"] = this.index.Type()

	if this.limit != nil {
		r["limit"] = expression.NewStringer().Visit(this.limit)
	}

	return json.Marshal(r)
}
Ejemplo n.º 21
0
func (this *Join) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Join"}
	r["namespace"] = this.term.Namespace()
	r["keyspace"] = this.term.Keyspace()
	r["on_keys"] = expression.NewStringer().Visit(this.term.Keys())

	if this.outer {
		r["outer"] = this.outer
	}

	if this.term.As() != "" {
		r["as"] = this.term.As()
	}
	return json.Marshal(r)
}
Ejemplo n.º 22
0
func (this *Order) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Order"}

	/* generate sort terms */
	s := make([]interface{}, 0, len(this.terms))
	for _, term := range this.terms {
		q := make(map[string]interface{})
		q["expr"] = expression.NewStringer().Visit(term.Expression())

		if term.Descending() {
			q["desc"] = term.Descending()
		}

		s = append(s, q)
	}
	r["sort_terms"] = s
	if this.offset != nil {
		r["offset"] = expression.NewStringer().Visit(this.offset.Expression())
	}
	if this.limit != nil {
		r["limit"] = expression.NewStringer().Visit(this.limit.Expression())
	}
	return json.Marshal(r)
}
Ejemplo n.º 23
0
func (this *Unset) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Unset"}
	s := make([]interface{}, 0, len(this.node.Terms()))
	for _, term := range this.node.Terms() {
		t := make(map[string]interface{})
		t["path"] = expression.NewStringer().Visit(term.Path())
		// FIXME
		//t["expr"] = expression.NewStringer().Visit(term.UpdateFor().Bindings())
		t["expr"] = "FIXME"
		s = append(s, t)
	}
	r["unset_terms"] = s

	return json.Marshal(r)
}
Ejemplo n.º 24
0
// 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
}
Ejemplo n.º 25
0
func collectAggregates(aggs map[string]algebra.Aggregate, exprs ...expression.Expression) {
	stringer := expression.NewStringer()

	for _, expr := range exprs {
		agg, ok := expr.(algebra.Aggregate)
		if ok {
			str := stringer.Visit(agg)
			aggs[str] = agg
		}

		_, ok = expr.(*algebra.Subquery)
		if !ok {
			children := expr.Children()
			if len(children) > 0 {
				collectAggregates(aggs, children...)
			}
		}
	}
}
Ejemplo n.º 26
0
func (this *Merge) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "Merge"}
	r["keyspace"] = this.keyspace.Name()
	r["namespace"] = this.keyspace.NamespaceId()
	r["keyspaceRef"] = this.ref
	r["key"] = expression.NewStringer().Visit(this.key)

	if this.update != nil {
		r["update"] = this.update
	}

	if this.delete != nil {
		r["delete"] = this.delete
	}

	if this.insert != nil {
		r["insert"] = this.insert
	}

	return json.Marshal(r)
}
Ejemplo n.º 27
0
// 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
}
Ejemplo n.º 28
0
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)
	}
}
Ejemplo n.º 29
0
func (this *IndexScan) MarshalJSON() ([]byte, error) {
	r := map[string]interface{}{"#operator": "IndexScan"}
	r["index"] = this.index.Name()
	r["namespace"] = this.term.Namespace()
	r["keyspace"] = this.term.Keyspace()
	r["using"] = this.index.Type()
	r["spans"] = this.spans

	if this.distinct {
		r["distinct"] = this.distinct
	}

	if this.limit != nil {
		r["limit"] = expression.NewStringer().Visit(this.limit)
	}

	if this.covers != nil {
		r["covers"] = this.covers
	}

	return json.Marshal(r)
}
Ejemplo n.º 30
0
// 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
}