Exemple #1
0
// CompileN1QLExpression will take expressions defined in N1QL's DDL statement
// and compile them for evaluation.
func CompileN1QLExpression(expressions []string) ([]interface{}, error) {
	cExprs := make([]interface{}, 0, len(expressions))
	for _, expr := range expressions {
		cExpr, err := ast.UnmarshalExpression([]byte(expr))
		if err != nil {
			return nil, err
		}
		cExprs = append(cExprs, cExpr)
	}
	return cExprs, nil
}
Exemple #2
0
func loadViewIndexes(b *bucket) ([]*catalog.Index, error) {

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

	inames := make([]string, 0, len(rows.Rows))
	for _, row := range rows.Rows {
		cdoc := row.DDoc
		id := cdoc.Meta["id"].(string)
		if !strings.HasPrefix(id, "_design/ddl_") {
			continue
		}
		iname := strings.TrimPrefix(id, "_design/ddl_")
		inames = append(inames, iname)
	}

	indexes := make([]*catalog.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 {
			return nil, errors.New("Missing view for index " + iname)
		}

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

		for _, ser := range jdoc.IndexOn {
			// HACK - remove this when Unmarshall supports META()
			if iname == PRIMARY_INDEX {
				meta := ast.NewFunctionCall("meta", ast.FunctionArgExpressionList{})
				mdid := ast.NewDotMemberOperator(meta, ast.NewProperty("id"))
				exprlist = append(exprlist, mdid)
			} else {
				expr, err := ast.UnmarshalExpression([]byte(ser))
				if err != nil {
					return nil, errors.New("Cannot unmarshal expression for index " + iname)
				}
				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 {
			return nil, errors.New("Warning - checksum failed on index " + iname)
		}

		var index catalog.Index

		if iname == PRIMARY_INDEX {
			index = &primaryIndex{
				viewIndex{
					name:   iname,
					bucket: b,
					using:  catalog.VIEW,
					ddoc:   &ddoc,
					on:     exprlist,
				},
			}
			indexes = append(indexes, &index)
		} else {
			index = &viewIndex{
				name:   iname,
				bucket: b,
				using:  catalog.VIEW,
				ddoc:   &ddoc,
				on:     exprlist,
			}
			indexes = append(indexes, &index)
		}
	}

	return indexes, nil
}