當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Builder.AddNode方法代碼示例

本文整理匯總了Golang中github.com/gholt/ring.Builder.AddNode方法的典型用法代碼示例。如果您正苦於以下問題:Golang Builder.AddNode方法的具體用法?Golang Builder.AddNode怎麽用?Golang Builder.AddNode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/gholt/ring.Builder的用法示例。


在下文中一共展示了Builder.AddNode方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: addOrSetCmd

func addOrSetCmd(r ring.Ring, b *ring.Builder, args []string, n ring.BuilderNode) error {
	if r != nil {
		return fmt.Errorf("cannot add a node to ring; use with a builder instead")
	}
	active := true
	capacity := uint32(1)
	var tiers []string
	var addresses []string
	meta := ""
	for _, arg := range args {
		sarg := strings.SplitN(arg, "=", 2)
		if len(sarg) != 2 {
			return fmt.Errorf(`invalid expression %#v; needs "="`, arg)
		}
		if sarg[0] == "" {
			return fmt.Errorf(`invalid expression %#v; nothing was left of "="`, arg)
		}
		if sarg[1] == "" {
			return fmt.Errorf(`invalid expression %#v; nothing was right of "="`, arg)
		}
		switch sarg[0] {
		case "active":
			switch sarg[1] {
			case "true":
				active = true
			case "false":
				active = false
			default:
				return fmt.Errorf(`invalid expression %#v; use "true" or "false" for the value of active`, arg)
			}
			if n != nil {
				n.SetActive(active)
			}
		case "capacity":
			c, err := strconv.Atoi(sarg[1])
			if err != nil {
				return fmt.Errorf("invalid expression %#v; %s", arg, err)
			}
			if c < 0 {
				return fmt.Errorf("invalid expression %#v; min is 0", arg)
			}
			if c > math.MaxUint32 {
				return fmt.Errorf("invalid expression %#v; max is %d", arg, math.MaxUint32)
			}
			capacity = uint32(c)
			if n != nil {
				n.SetCapacity(capacity)
			}
		case "meta":
			meta = sarg[1]
			if n != nil {
				n.SetMeta(meta)
			}
		default:
			if strings.HasPrefix(sarg[0], "tier") {
				level, err := strconv.Atoi(sarg[0][4:])
				if err != nil {
					return fmt.Errorf("invalid expression %#v; %#v doesn't specify a number", arg, sarg[0][4:])
				}
				if level < 0 {
					return fmt.Errorf("invalid expression %#v; minimum level is 0", arg)
				}
				if len(tiers) <= level {
					t := make([]string, level+1)
					copy(t, tiers)
					tiers = t
				}
				tiers[level] = sarg[1]
				if n != nil {
					n.SetTier(level, tiers[level])
				}
			} else if strings.HasPrefix(sarg[0], "address") {
				index, err := strconv.Atoi(sarg[0][7:])
				if err != nil {
					return fmt.Errorf("invalid expression %#v; %#v doesn't specify a number", arg, sarg[0][4:])
				}
				if index < 0 {
					return fmt.Errorf("invalid expression %#v; minimum index is 0", arg)
				}
				if len(addresses) <= index {
					a := make([]string, index+1)
					copy(a, addresses)
					addresses = a
				}
				addresses[index] = sarg[1]
				if n != nil {
					n.SetAddress(index, addresses[index])
				}
			}
		}
	}
	if n == nil {
		n = b.AddNode(active, capacity, tiers, addresses, meta)
		report := [][]string{
			[]string{"ID:", fmt.Sprintf("%016x", n.ID())},
			[]string{"Active:", fmt.Sprintf("%v", n.Active())},
			[]string{"Capacity:", fmt.Sprintf("%d", n.Capacity())},
			[]string{"Tiers:", strings.Join(n.Tiers(), "\n")},
			[]string{"Addresses:", strings.Join(n.Addresses(), "\n")},
			[]string{"Meta:", n.Meta()},
//.........這裏部分代碼省略.........
開發者ID:wreese,項目名稱:valuestore,代碼行數:101,代碼來源:main.go


注:本文中的github.com/gholt/ring.Builder.AddNode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。