Esempio n. 1
0
func newPrimaryIndex(b *bucket) (*primaryIndex, error) {
	ddoc := newPrimaryDDoc()
	meta := ast.NewFunctionCall("meta", ast.FunctionArgExpressionList{})
	mdid := ast.NewDotMemberOperator(meta, ast.NewProperty("id"))
	inst := primaryIndex{
		viewIndex{
			name:   PRIMARY_INDEX,
			using:  catalog.VIEW,
			on:     catalog.IndexKey{mdid},
			ddoc:   ddoc,
			bucket: b,
		},
	}

	err := inst.putDesignDoc()
	if err != nil {
		return nil, err
	}

	err = inst.WaitForIndex()
	if err != nil {
		return nil, err
	}

	return &inst, nil
}
Esempio n. 2
0
func newAllDocsIndex(b *bucket) *primaryIndex {
	meta := ast.NewFunctionCall("meta", ast.FunctionArgExpressionList{})
	mdid := ast.NewDotMemberOperator(meta, ast.NewProperty("id"))
	ddoc := designdoc{name: "", viewname: "_all_docs"}
	idx := primaryIndex{
		viewIndex{
			name:   ALLDOCS_INDEX,
			using:  catalog.VIEW,
			on:     catalog.IndexKey{mdid},
			ddoc:   &ddoc,
			bucket: b,
		},
	}
	return &idx
}
Esempio n. 3
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
}