• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang utils.StringMap类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/cgrates/cgrates/utils.StringMap的典型用法代码示例。如果您正苦于以下问题:Golang StringMap类的具体用法?Golang StringMap怎么用?Golang StringMap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了StringMap类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: getSessionIDsMatchingIndexes

// getSessionIDsMatchingIndexes will check inside indexes if it can find sessionIDs matching all filters
// matchedIndexes returns map[matchedFieldName]possibleMatchedFieldVal so we optimize further to avoid checking them
func (smg *SMGeneric) getSessionIDsMatchingIndexes(fltrs map[string]string) (utils.StringMap, map[string]string) {
	smg.aSIMux.RLock()
	defer smg.aSIMux.RUnlock()
	sessionIDxes := smg.aSessionsIndex // Clone here and unlock sooner if getting slow
	matchedIndexes := make(map[string]string)
	var matchingSessions utils.StringMap
	checkNr := 0
	for fltrName, fltrVal := range fltrs {
		checkNr += 1
		if _, hasFldName := sessionIDxes[fltrName]; !hasFldName {
			continue
		}
		if _, hasFldVal := sessionIDxes[fltrName][fltrVal]; !hasFldVal {
			matchedIndexes[fltrName] = utils.META_NONE
			continue
		}
		matchedIndexes[fltrName] = fltrVal
		if checkNr == 1 { // First run will init the MatchingSessions
			matchingSessions = sessionIDxes[fltrName][fltrVal]
			continue
		}
		// Higher run, takes out non matching indexes
		for sessID := range sessionIDxes[fltrName][fltrVal] {
			if _, hasUUID := matchingSessions[sessID]; !hasUUID {
				delete(matchingSessions, sessID)
			}
		}
	}
	return matchingSessions.Clone(), matchedIndexes
}
开发者ID:rinor,项目名称:cgrates,代码行数:32,代码来源:smgeneric.go


示例2: setBalanceAction

// sets all the fields of the balance
func (acc *Account) setBalanceAction(a *Action) error {
	if a == nil {
		return errors.New("nil action")
	}
	if acc.BalanceMap == nil {
		acc.BalanceMap = make(map[string]Balances)
	}
	var balance *Balance
	var found bool
	var previousSharedGroups utils.StringMap            // kept for comparison
	if a.Balance.Uuid != nil && *a.Balance.Uuid != "" { // balance uuid match
		for balanceType := range acc.BalanceMap {
			for _, b := range acc.BalanceMap[balanceType] {
				if b.Uuid == *a.Balance.Uuid && !b.IsExpired() {
					previousSharedGroups = b.SharedGroups
					balance = b
					found = true
					break // only set one balance
				}
			}
			if found {
				break
			}
		}
		if !found {
			return fmt.Errorf("cannot find balance with uuid: <%s>", *a.Balance.Uuid)
		}
	} else { // balance id match
		for balanceType := range acc.BalanceMap {
			for _, b := range acc.BalanceMap[balanceType] {
				if a.Balance.ID != nil && b.ID == *a.Balance.ID && !b.IsExpired() {
					previousSharedGroups = b.SharedGroups
					balance = b
					found = true
					break // only set one balance
				}
			}
			if found {
				break
			}
		}
		// if it is not found then we create it
		if !found {
			if a.Balance.Type == nil { // cannot create the entry in the balance map without this info
				return errors.New("missing balance type")
			}
			balance = &Balance{}
			balance.Uuid = utils.GenUUID() // alway overwrite the uuid for consistency
			acc.BalanceMap[*a.Balance.Type] = append(acc.BalanceMap[*a.Balance.Type], balance)
		}
	}

	if a.Balance.ID != nil && *a.Balance.ID == utils.META_DEFAULT { // treat it separately since modifyBalance sets expiry and others parameters, not specific for *default
		if a.Balance.Value != nil {
			balance.ID = *a.Balance.ID
			balance.Value = a.Balance.GetValue()
			balance.SetDirty() // Mark the balance as dirty since we have modified and it should be checked by action triggers
		}
	} else {
		a.Balance.ModifyBalance(balance)
	}
	// modify if necessary the shared groups here
	if !found || !previousSharedGroups.Equal(balance.SharedGroups) {
		_, err := Guardian.Guard(func() (interface{}, error) {
			sgs := make([]string, len(balance.SharedGroups))
			i := 0
			for sgID := range balance.SharedGroups {
				// add shared group member
				sg, err := ratingStorage.GetSharedGroup(sgID, false, utils.NonTransactional)
				if err != nil || sg == nil {
					//than is problem
					utils.Logger.Warning(fmt.Sprintf("Could not get shared group: %v", sgID))
				} else {
					if _, found := sg.MemberIds[acc.ID]; !found {
						// add member and save
						if sg.MemberIds == nil {
							sg.MemberIds = make(utils.StringMap)
						}
						sg.MemberIds[acc.ID] = true
						ratingStorage.SetSharedGroup(sg, utils.NonTransactional)
					}
				}
				i++
			}
			ratingStorage.CacheDataFromDB(utils.SHARED_GROUP_PREFIX, sgs, true)
			return 0, nil
		}, 0, balance.SharedGroups.Slice()...)
		if err != nil {
			return err
		}
	}
	acc.InitCounters()
	acc.ExecuteActionTriggers(nil)
	return nil
}
开发者ID:cgrates,项目名称:cgrates,代码行数:96,代码来源:account.go


示例3: setBalanceAction

// sets all the fields of the balance
func (acc *Account) setBalanceAction(a *Action) error {
	if a == nil {
		return errors.New("nil action")
	}
	if a.Balance.Type == nil {
		return errors.New("missing balance type")
	}
	balanceType := *a.Balance.Type
	if acc.BalanceMap == nil {
		acc.BalanceMap = make(map[string]Balances, 1)
	}
	var previousSharedGroups utils.StringMap // kept for comparison
	var balance *Balance
	var found bool
	for _, b := range acc.BalanceMap[balanceType] {
		if b.IsExpired() {
			continue
		}
		if (a.Balance.Uuid != nil && b.Uuid == *a.Balance.Uuid) ||
			(a.Balance.ID != nil && b.ID == *a.Balance.ID) {
			previousSharedGroups = b.SharedGroups
			balance = b
			found = true
			break // only set one balance
		}
	}

	// if it is not found then we add it to the list
	if balance == nil {
		balance = &Balance{}
		balance.Uuid = utils.GenUUID() // alway overwrite the uuid for consistency
		acc.BalanceMap[balanceType] = append(acc.BalanceMap[balanceType], balance)
	}

	if a.Balance.ID != nil && *a.Balance.ID == utils.META_DEFAULT {
		balance.ID = utils.META_DEFAULT
		if a.Balance.Value != nil {
			balance.Value = a.Balance.GetValue()
		}
	} else {
		a.Balance.ModifyBalance(balance)
	}

	if !found || !previousSharedGroups.Equal(balance.SharedGroups) {
		_, err := Guardian.Guard(func() (interface{}, error) {
			for sgID := range balance.SharedGroups {
				// add shared group member
				sg, err := ratingStorage.GetSharedGroup(sgID, false, utils.NonTransactional)
				if err != nil || sg == nil {
					//than is problem
					utils.Logger.Warning(fmt.Sprintf("Could not get shared group: %v", sgID))
				} else {
					if _, found := sg.MemberIds[acc.ID]; !found {
						// add member and save
						if sg.MemberIds == nil {
							sg.MemberIds = make(utils.StringMap)
						}
						sg.MemberIds[acc.ID] = true
						ratingStorage.SetSharedGroup(sg, utils.NonTransactional)
					}
				}
			}
			return 0, nil
		}, 0, balance.SharedGroups.Slice()...)
		if err != nil {
			return err
		}
	}
	acc.InitCounters()
	acc.ExecuteActionTriggers(nil)
	return nil
}
开发者ID:eloycoto,项目名称:cgrates,代码行数:73,代码来源:account.go


示例4: SetBalance

func (self *ApierV1) SetBalance(attr *AttrSetBalance, reply *string) error {
	if missing := utils.MissingStructFields(attr, []string{"Tenant", "Account", "BalanceType"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	if (attr.BalanceID == nil || *attr.BalanceID == "") &&
		(attr.BalanceUUID == nil || *attr.BalanceUUID == "") {
		return utils.NewErrMandatoryIeMissing("BalanceID", "or", "BalanceUUID")
	}
	var err error
	if attr.ExpiryTime != nil {
		attr.expTime, err = utils.ParseTimeDetectLayout(*attr.ExpiryTime, self.Config.DefaultTimezone)
		if err != nil {
			*reply = err.Error()
			return err
		}
	}
	accID := utils.ConcatenatedKey(attr.Tenant, attr.Account)
	_, err = engine.Guardian.Guard(func() (interface{}, error) {
		account, err := self.AccountDb.GetAccount(accID)
		if err != nil {
			return 0, utils.ErrNotFound
		}

		if account.BalanceMap == nil {
			account.BalanceMap = make(map[string]engine.BalanceChain, 1)
		}
		var previousSharedGroups utils.StringMap // kept for comparison
		var balance *engine.Balance
		var found bool
		for _, b := range account.BalanceMap[attr.BalanceType] {
			if b.IsExpired() {
				continue
			}
			if (attr.BalanceUUID != nil && b.Uuid == *attr.BalanceUUID) ||
				(attr.BalanceID != nil && b.Id == *attr.BalanceID) {
				previousSharedGroups = b.SharedGroups
				balance = b
				found = true
				break // only set one balance
			}
		}

		// if it is not found then we add it to the list
		if balance == nil {
			balance = &engine.Balance{}
			balance.Uuid = utils.GenUUID() // alway overwrite the uuid for consistency
			account.BalanceMap[attr.BalanceType] = append(account.BalanceMap[attr.BalanceType], balance)
		}

		if attr.BalanceID != nil && *attr.BalanceID == utils.META_DEFAULT {
			balance.Id = utils.META_DEFAULT
			if attr.Value != nil {
				balance.Value = *attr.Value
			}
		} else {
			attr.SetBalance(balance)
		}

		if !found || !previousSharedGroups.Equal(balance.SharedGroups) {
			_, err = engine.Guardian.Guard(func() (interface{}, error) {
				for sgID := range balance.SharedGroups {
					// add shared group member
					sg, err := self.RatingDb.GetSharedGroup(sgID, false)
					if err != nil || sg == nil {
						//than is problem
						utils.Logger.Warning(fmt.Sprintf("Could not get shared group: %v", sgID))
					} else {
						if _, found := sg.MemberIds[account.Id]; !found {
							// add member and save
							if sg.MemberIds == nil {
								sg.MemberIds = make(utils.StringMap)
							}
							sg.MemberIds[account.Id] = true
							self.RatingDb.SetSharedGroup(sg)
						}
					}
				}
				return 0, nil
			}, 0, balance.SharedGroups.Slice()...)
		}

		account.InitCounters()
		account.ExecuteActionTriggers(nil)
		self.AccountDb.SetAccount(account)
		return 0, nil
	}, 0, accID)
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = utils.OK
	return nil
}
开发者ID:bhepp,项目名称:cgrates,代码行数:93,代码来源:accounts.go



注:本文中的github.com/cgrates/cgrates/utils.StringMap类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang utils.TPAccountActions类代码示例发布时间:2022-05-23
下一篇:
Golang utils.StoredCdr类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap