Example #1
0
func newIndexMetaData(info *indexInfo, queryport string) *mclient.IndexMetadata {
	defn := &common.IndexDefn{
		DefnId:       common.IndexDefnId(info.DefnID),
		Name:         info.Name,
		Using:        common.IndexType(info.Using),
		Bucket:       info.Bucket,
		IsPrimary:    info.IsPrimary,
		ExprType:     common.ExprType(info.ExprType),
		SecExprs:     info.SecExprs,
		PartitionKey: info.PartnExpr,
	}
	instances := []*mclient.InstanceDefn{
		&mclient.InstanceDefn{
			InstId: common.IndexInstId(info.DefnID), // TODO: defnID as InstID
			State:  common.INDEX_STATE_READY,
			Endpts: []common.Endpoint{common.Endpoint(queryport)},
		},
	}
	imeta := &mclient.IndexMetadata{
		Definition: defn,
		Instances:  instances,
	}
	return imeta
}
Example #2
0
func (o *MetadataProvider) CreateIndexWithPlan(
	name, bucket, using, exprType, partnExpr, whereExpr string,
	secExprs []string, isPrimary bool, plan map[string]interface{}) (c.IndexDefnId, error, bool) {

	// FindIndexByName will only return valid index
	if o.FindIndexByName(name, bucket) != nil {
		return c.IndexDefnId(0), errors.New(fmt.Sprintf("Index %s already exist.", name)), false
	}

	var deferred bool = false
	var wait bool = true
	var nodes []string = nil

	if plan != nil {
		logging.Debugf("MetadataProvider:CreateIndexWithPlan(): plan %v", plan)

		ns, ok := plan["nodes"].([]interface{})
		if ok {
			if len(ns) != 1 {
				return c.IndexDefnId(0), errors.New("Create Index is allowed for one and only one node"), false
			}
			n, ok := ns[0].(string)
			if ok {
				nodes = []string{n}
			} else {
				return c.IndexDefnId(0),
					errors.New(fmt.Sprintf("Fails to create index.  Node '%v' is not valid", plan["nodes"])),
					false
			}
		} else {
			n, ok := plan["nodes"].(string)
			if ok {
				nodes = []string{n}
			} else if _, ok := plan["nodes"]; ok {
				return c.IndexDefnId(0),
					errors.New(fmt.Sprintf("Fails to create index.  Node '%v' is not valid", plan["nodes"])),
					false
			}
		}

		deferred2, ok := plan["defer_build"].(bool)
		if !ok {
			deferred_str, ok := plan["defer_build"].(string)
			if ok {
				var err error
				deferred2, err = strconv.ParseBool(deferred_str)
				if err != nil {
					return c.IndexDefnId(0),
						errors.New("Fails to create index.  Parameter defer_build must be a boolean value of (true or false)."),
						false
				}
				deferred = deferred2
				wait = !deferred

			} else if _, ok := plan["defer_build"]; ok {
				return c.IndexDefnId(0),
					errors.New("Fails to create index.  Parameter defer_build must be a boolean value of (true or false)."),
					false
			}
		} else {
			deferred = deferred2
			wait = !deferred
		}
	}

	logging.Debugf("MetadataProvider:CreateIndex(): deferred_build %v sync %v nodes %v", deferred, wait, nodes)

	watcher, err, retry := o.findWatcherWithRetry(nodes)
	if err != nil {
		return c.IndexDefnId(0), err, retry
	}

	// set the node list using indexerId
	nodes = []string{string(watcher.getIndexerId())}

	defnID, err := c.NewIndexDefnId()
	if err != nil {
		return c.IndexDefnId(0),
			errors.New(fmt.Sprintf("Fails to create index. Fail to create uuid for index definition.")),
			false
	}

	idxDefn := &c.IndexDefn{
		DefnId:          defnID,
		Name:            name,
		Using:           c.IndexType(using),
		Bucket:          bucket,
		IsPrimary:       isPrimary,
		SecExprs:        secExprs,
		ExprType:        c.ExprType(exprType),
		PartitionScheme: c.SINGLE,
		PartitionKey:    partnExpr,
		WhereExpr:       whereExpr,
		Deferred:        deferred,
		Nodes:           nodes}

	content, err := c.MarshallIndexDefn(idxDefn)
	if err != nil {
		return 0, err, false
	}

	key := fmt.Sprintf("%d", defnID)
	if _, err = watcher.makeRequest(OPCODE_CREATE_INDEX, key, content); err != nil {
		return defnID, err, false
	}

	if wait {
		err := watcher.waitForEvent(defnID, []c.IndexState{c.INDEX_STATE_ACTIVE, c.INDEX_STATE_DELETED})
		return defnID, err, false
	}

	return defnID, nil, false
}