Example #1
0
func (this *SendInsert) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_         string `json:"#operator"`
		KeyExpr   string `json:"key"`
		ValueExpr string `json:"value"`
		Keys      string `json:"keyspace"`
		Names     string `json:"namespace"`
		Alias     string `json:"alias"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.KeyExpr != "" {
		this.key, err = parser.Parse(_unmarshalled.KeyExpr)
		if err != nil {
			return err
		}
	}

	if _unmarshalled.ValueExpr != "" {
		this.value, err = parser.Parse(_unmarshalled.ValueExpr)
		if err != nil {
			return err
		}
	}

	this.alias = _unmarshalled.Alias
	this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys)
	return err
}
Example #2
0
func (this *InitialGroup) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_    string   "#operator"
		Keys []string "group_keys"
		Aggs []string "aggregates"
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	this.keys = make(expression.Expressions, len(_unmarshalled.Keys))
	for i, key := range _unmarshalled.Keys {
		key_expr, err := parser.Parse(key)
		if err != nil {
			return err
		}
		this.keys[i] = key_expr
	}

	this.aggregates = make(algebra.Aggregates, len(_unmarshalled.Aggs))
	for i, agg := range _unmarshalled.Aggs {
		agg_expr, err := parser.Parse(agg)
		if err != nil {
			return err
		}
		this.aggregates[i], _ = agg_expr.(algebra.Aggregate)
	}

	return nil
}
func init() {
	qpServer = startQueryport("localhost:9998", serverCallb)

	ns := &namespace{
		name:          "default",
		keyspaceCache: make(map[string]datastore.Keyspace),
	}
	ks := &keyspace{
		namespace: ns,
		name:      "default",
		indexes:   make(map[string]datastore.Index),
	}
	expr, err := parser.Parse(`gender`)
	if err != nil {
		log.Fatal(err)
	}
	equalKey := expression.Expressions{expr}
	expr, err = parser.Parse(`name`)
	if err != nil {
		log.Fatal(err)
	}
	rangeKey := expression.Expressions{expr}
	whereKey, err := parser.Parse("(30 < `age`)")
	if err != nil {
		log.Fatal(err)
	}
	index, _ = new2iIndex(
		"testindex", equalKey, rangeKey, whereKey, "gsi", ks)
	index.setHost([]string{"localhost:9998"})
}
Example #4
0
func (this *Join) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_     string `json:"#operator"`
		Names string `json:"namespace"`
		Keys  string `json:"keyspace"`
		On    string `json:"on_keys"`
		Outer bool   `json:"outer"`
		As    string `json:"as"`
	}
	var keys_expr expression.Expression

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.On != "" {
		keys_expr, err = parser.Parse(_unmarshalled.On)
		if err != nil {
			return err
		}
	}

	this.outer = _unmarshalled.Outer
	this.term = algebra.NewKeyspaceTerm(_unmarshalled.Names, _unmarshalled.Keys,
		nil, _unmarshalled.As, keys_expr)
	this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys)
	return err
}
Example #5
0
func (this *Unnest) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_     string `json:"#operator"`
		Outer bool   `json:"outer"`
		Expr  string `json:"expr"`
		As    string `json:"as"`
	}
	var expr expression.Expression

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.Expr != "" {
		expr, err = parser.Parse(_unmarshalled.Expr)
		if err != nil {
			return err
		}
	}

	this.term = algebra.NewUnnest(nil, _unmarshalled.Outer, expr, _unmarshalled.As)
	this.alias = _unmarshalled.As
	return err
}
Example #6
0
func (this *ValueScan) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_      string `json:"#operator"`
		Values string `json:"values"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.Values == "" {
		return nil
	}

	expr, err := parser.Parse(_unmarshalled.Values)
	if err != nil {
		return err
	}

	array, ok := expr.(*expression.ArrayConstruct)
	if !ok {
		return fmt.Errorf("Invalid VALUES expression %s", _unmarshalled.Values)
	}

	this.values, err = algebra.NewPairs(array)
	return err
}
Example #7
0
func (this *Let) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_        string `json:"#operator"`
		Bindings []struct {
			_    string `json:"type"`
			Var  string `json:"variable"`
			Expr string `json:"variable"`
			Desc bool   `json:"descend"`
		} `json:"bindings"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	this.bindings = make(expression.Bindings, len(_unmarshalled.Bindings))
	for i, binding := range _unmarshalled.Bindings {
		expr, err := parser.Parse(binding.Expr)
		if err != nil {
			return err
		}
		if binding.Desc {
			this.bindings[i] = expression.NewDescendantBinding(binding.Var, expr)
		} else {
			this.bindings[i] = expression.NewBinding(binding.Var, expr)
		}
	}

	return nil
}
Example #8
0
func (this *Merge) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_      string               `json:"#operator"`
		Keys   string               `json:"keyspace"`
		Names  string               `json:"namespace"`
		KRef   *algebra.KeyspaceRef `json:"keyspaceRef"`
		Key    string               `json:"key"`
		Update json.RawMessage      `json:"update"`
		Delete json.RawMessage      `json:"delete"`
		Insert json.RawMessage      `json:"insert"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys)
	if err != nil {
		return err
	}

	if _unmarshalled.Key != "" {
		this.key, err = parser.Parse(_unmarshalled.Key)
		if err != nil {
			return err
		}
	}

	ops := []json.RawMessage{
		_unmarshalled.Update,
		_unmarshalled.Delete,
		_unmarshalled.Insert,
	}
	for i, child := range ops {
		var op_type struct {
			Operator string `json:"#operator"`
		}

		err = json.Unmarshal(child, &op_type)
		if err != nil {
			return err
		}

		switch i {
		case 0:
			this.update, err = MakeOperator(op_type.Operator, child)
		case 1:
			this.delete, err = MakeOperator(op_type.Operator, child)
		case 2:
			this.insert, err = MakeOperator(op_type.Operator, child)
		}
		return err
	}

	return err
}
Example #9
0
func (this *InitialProject) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_ string `json:"#operator"`
		//Terms    []json.RawMessage `json:"result_terms"`
		Terms []struct {
			Expr string `json:"expr"`
			As   string `json:"as"`
			Star bool   `json:"star"`
		} `json:"result_terms"`
		Distinct bool `json:"distinct"`
		Raw      bool `json:"raw"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	terms := make(algebra.ResultTerms, len(_unmarshalled.Terms))
	for i, term_data := range _unmarshalled.Terms {
		/*var term_data struct {
			Expr string `json:"expr"`
			As   string `json:"as"`
			Star bool   `json:"star"`
		}
		err := json.Unmarshal(raw_term, &term_data)
		if err != nil {
			return err
		}
		*/
		expr, err := parser.Parse(term_data.Expr)
		if err != nil {
			return err
		}

		terms[i] = algebra.NewResultTerm(expr, term_data.Star, term_data.As)
	}
	projection := algebra.NewProjection(_unmarshalled.Distinct, terms)
	results := projection.Terms()
	project_terms := make(ProjectTerms, len(results))

	for i, res := range results {
		project_terms[i] = &ProjectTerm{
			result: res,
		}
	}

	this.projection = projection
	this.terms = project_terms

	return nil
}
Example #10
0
func (this *Limit) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_    string `json:"#operator"`
		Expr string `json:"expr"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	this.expr, err = parser.Parse(_unmarshalled.Expr)
	return err
}
Example #11
0
func (this *Unset) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_          string `json:"#operator"`
		UnsetTerms []struct {
			Path string `json:"path"`
			Expr string `json:"expr"`
		} `json:"unset_terms"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	terms := make([]*algebra.UnsetTerm, len(_unmarshalled.UnsetTerms))
	for i, UnsetTerm := range _unmarshalled.UnsetTerms {
		path_expr, err := parser.Parse(UnsetTerm.Path)
		if err != nil {
			return err
		}

		path, is_path := path_expr.(expression.Path)
		if !is_path {
			return fmt.Errorf("Unset.UnmarshalJSON: cannot resolve path expression from %s", UnsetTerm.Path)
		}

		// is expr needed in Unset?
		_, err = parser.Parse(UnsetTerm.Expr)
		if err != nil {
			return err
		}

		terms[i] = algebra.NewUnsetTerm(path, nil)
	}
	this.node = algebra.NewUnset(terms)
	return nil
}
Example #12
0
func (this *Filter) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_         string `json:"#operator"`
		Condition string `json:"condition"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.Condition != "" {
		this.cond, err = parser.Parse(_unmarshalled.Condition)
	}

	return err
}
Example #13
0
func (this *KeyScan) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_    string `json:"#operator"`
		Keys string `json:"keys"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.Keys != "" {
		this.keys, err = parser.Parse(_unmarshalled.Keys)
	}

	return err
}
Example #14
0
func (this *Fetch) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_     string `json:"#operator"`
		Proj  string `json:"projection"`
		Names string `json:"namespace"`
		Keys  string `json:"keyspace"`
		As    string `json:"as"`
	}
	var proj_expr expression.Path

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	if _unmarshalled.Proj != "" {
		expr, err := parser.Parse(_unmarshalled.Proj)

		logging.Infop("Fetch", logging.Pair{"_unmarshalled.Proj", _unmarshalled.Proj},
			logging.Pair{"err", err},
			logging.Pair{"expr", expr},
		)
		if err != nil {
			return err
		}

		_proj_expr, is_path := expr.(expression.Path)
		if !is_path {
			return fmt.Errorf("Fetch.UnmarshalJSON: cannot resolve path expression from %s", _unmarshalled.Proj)
		}
		proj_expr = _proj_expr
	}
	this.term = algebra.NewKeyspaceTerm(_unmarshalled.Names, _unmarshalled.Keys,
		proj_expr, _unmarshalled.As, nil)

	this.keyspace, err = datastore.GetKeyspace(_unmarshalled.Names, _unmarshalled.Keys)

	return err
}
Example #15
0
func (this *Order) UnmarshalJSON(body []byte) error {
	var _unmarshalled struct {
		_     string `json:"#operator"`
		Terms []struct {
			Expr string `json:"expr"`
			Desc bool   `json:"desc"`
		} `json:"sort_terms"`
	}

	err := json.Unmarshal(body, &_unmarshalled)
	if err != nil {
		return err
	}

	this.terms = make(algebra.SortTerms, len(_unmarshalled.Terms))
	for i, term := range _unmarshalled.Terms {
		expr, err := parser.Parse(term.Expr)
		if err != nil {
			return err
		}
		this.terms[i] = algebra.NewSortTerm(expr, term.Desc)
	}
	return nil
}
Example #16
0
func loadViewIndexes(v *viewIndexer) ([]*datastore.Index, error) {

	b := v.keyspace
	rows, err := b.cbbucket.GetDDocs()
	if err != nil {
		return nil, err
	}

	inames := make([]string, 0, len(rows.Rows))
	nonUsableIndexes := make([]string, 0)

	for _, row := range rows.Rows {
		cdoc := row.DDoc
		id := cdoc.Meta["id"].(string)
		if strings.HasPrefix(id, "_design/ddl_") {
			iname := strings.TrimPrefix(id, "_design/ddl_")
			inames = append(inames, iname)
		} else if strings.HasPrefix(id, "_design/dev_") {
			// append this to the list of non-usuable indexes
			iname := strings.TrimPrefix(id, "_design/dev_")
			for _, name := range v.nonUsableIndexes {
				if iname == name {
					continue
				}
			}
			nonUsableIndexes = append(nonUsableIndexes, iname)

		} else if strings.HasPrefix(id, "_design/") {
			iname := strings.TrimPrefix(id, "_design/")
			for _, name := range v.nonUsableIndexes {
				if iname == name {
					continue
				}
			}
			nonUsableIndexes = append(nonUsableIndexes, iname)
		}

	}

	indexes := make([]*datastore.Index, 0, len(inames))
	for _, iname := range inames {
		ddname := "ddl_" + iname
		jdoc, err := getDesignDoc(b, ddname)
		if err != nil {
			return nil, err
		}
		jview, ok := jdoc.Views[iname]
		if !ok {
			nonUsableIndexes = append(nonUsableIndexes, iname)
			logging.Errorf("Missing view for index %v ", iname)
			continue
		}

		exprlist := make([]expression.Expression, 0, len(jdoc.IndexOn))

		for _, ser := range jdoc.IndexOn {
			if iname == PRIMARY_INDEX {
				doc := expression.NewIdentifier(b.Name())
				meta := expression.NewMeta(doc)
				mdid := expression.NewField(meta, expression.NewFieldName("id"))
				exprlist = append(exprlist, mdid)
			} else {
				expr, err := parser.Parse(ser)
				if err != nil {
					nonUsableIndexes = append(nonUsableIndexes, iname)
					logging.Errorf("Cannot unmarshal expression for index  %v", iname)
					continue
				}
				exprlist = append(exprlist, expr)
			}
		}
		if len(exprlist) != len(jdoc.IndexOn) {
			continue
		}

		ddoc := designdoc{
			name:     ddname,
			viewname: iname,
			mapfn:    jview.Map,
			reducefn: jview.Reduce,
		}
		if ddoc.checksum() != jdoc.IndexChecksum {
			nonUsableIndexes = append(nonUsableIndexes, iname)
			logging.Errorf("Warning - checksum failed on index  %v", iname)
			continue
		}

		var index datastore.Index

		logging.Infof("Found index name %v keyspace %v", iname, b.Name())
		if iname == PRIMARY_INDEX {
			index = &viewIndex{
				name:     iname,
				keyspace: b,
				view:     v,
				using:    datastore.VIEW,
				ddoc:     &ddoc,
				on:       exprlist,
			}
			indexes = append(indexes, &index)
		} else {
			index = &viewIndex{
				name:     iname,
				keyspace: b,
				view:     v,
				using:    datastore.VIEW,
				ddoc:     &ddoc,
				on:       exprlist,
			}
			indexes = append(indexes, &index)
		}
	}
	v.nonUsableIndexes = nonUsableIndexes

	if len(indexes) == 0 {
		return nil, nil
	}

	return indexes, nil
}