Example #1
0
func (this *Dispatcher) CreateIndex(method string, parms map[string]string, body []byte) error {

	var idxstruct utils.IndexStrct

	if err := json.Unmarshal(body, &idxstruct); err != nil {
		this.Logger.Error("[ERROR] json error  %v", err)
		return err
	}

	if _, ok := this.IndexInfo[idxstruct.IndexName]; ok {
		this.Logger.Error("[ERROR] index [%v] already has ", idxstruct.IndexName)
		return fmt.Errorf("[ERROR] index [%v] already has ", idxstruct.IndexName)
	}

	sis := make([]ShardInfo, 0)
	seeds := make([]bool, len(this.NodeInfos))
	if len(this.NodeInfos) < int(idxstruct.ShardNum) {
		this.Logger.Error("[ERROR] ShardNum[%v] is to large", idxstruct.ShardNum)
		return fmt.Errorf("[ERROR] ShardNum[%v] is to large", idxstruct.ShardNum)
	}
	var nodeinfo utils.NodeIndex
	nodeinfo.IndexName = idxstruct.IndexName
	nodeinfo.ShardNum = idxstruct.ShardNum
	nodeinfo.IndexMapping = idxstruct.IndexMapping
	nodeinfo.ShardNodes = make(map[uint64][]string)
	//nodeinfo.Shard = make([]uint64, idxstruct.ShardNum)
	for i := uint64(0); i < idxstruct.ShardNum; i++ {
		si := ShardInfo{ShardPathName: fmt.Sprintf("%v/%v_%v", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i),
			MaxOffset: 0, NodeInfos: make([]string, 0)}
		si.OffsetMmap, _ = utils.NewMmap(fmt.Sprintf("%v/%v_%v.offset", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i), utils.MODE_APPEND)
		si.DetailMmap, _ = utils.NewMmap(fmt.Sprintf("%v/%v_%v.dtl", utils.IDX_DETAIL_PATH, idxstruct.IndexName, i), utils.MODE_APPEND)

		for {
			seed := rand.Intn(len(seeds))
			if seeds[seed] == false {
				seeds[seed] = true
				net := make([]string, 0)
				si.NodeInfos = append(si.NodeInfos, fmt.Sprintf("%v:%v", this.NodeInfos[seed].Addr, this.NodeInfos[seed].MPort))
				net = append(net, fmt.Sprintf("%v:%v", this.NodeInfos[seed].Addr, this.NodeInfos[seed].MPort))

				nodeinfo.ShardNodes[uint64(seed)] = net
				this.Logger.Info("[INFO] NodeInfos::: %v", si.NodeInfos)
				break
			}
		}

		sis = append(sis, si)

	}
	info := idxInfo{Name: idxstruct.IndexName, ShardNum: idxstruct.ShardNum, MaxCnt: 0,
		IndexMapping: idxstruct.IndexMapping, ShardInfos: sis, ShardField: idxstruct.ShardField}

	this.IndexInfo[idxstruct.IndexName] = info

	for _, iinfo := range this.NodeInfos {
		iinfo.IdxChan <- nodeinfo
	}

	return this.storeStruct()

}