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


Golang Op.Update方法代碼示例

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


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

示例1: SetAPIHostPorts

// SetAPIHostPorts sets the addresses of the API server instances.
// Each server is represented by one element in the top level slice.
// If prefer-ipv6 environment setting is true, the addresses will be
// sorted before setting them to bring IPv6 addresses on top (if
// available).
func (st *State) SetAPIHostPorts(hps [][]network.HostPort) error {
	envConfig, err := st.EnvironConfig()
	if err != nil {
		return err
	}
	for i, _ := range hps {
		network.SortHostPorts(hps[i], envConfig.PreferIPv6())
	}
	doc := apiHostPortsDoc{
		APIHostPorts: instanceHostPortsToHostPorts(hps),
	}
	buildTxn := func(attempt int) ([]txn.Op, error) {
		existing, err := st.APIHostPorts()
		if err != nil {
			return nil, err
		}
		op := txn.Op{
			C:  st.stateServers.Name,
			Id: apiHostPortsKey,
			Assert: bson.D{{
				"apihostports", instanceHostPortsToHostPorts(existing),
			}},
		}
		if !hostPortsEqual(hps, existing) {
			op.Update = bson.D{{
				"$set", bson.D{{"apihostports", doc.APIHostPorts}},
			}}
		}
		return []txn.Op{op}, nil
	}
	if err := st.run(buildTxn); err != nil {
		return errors.Annotate(err, "cannot set API addresses")
	}
	return nil
}
開發者ID:klyachin,項目名稱:juju,代碼行數:40,代碼來源:address.go

示例2: setAddresses

// setAddresses updates the machine's addresses (either Addresses or
// MachineAddresses, depending on the field argument).
func (m *Machine) setAddresses(addresses []network.Address, field *[]address, fieldName string) error {
	var changed bool
	envConfig, err := m.st.EnvironConfig()
	if err != nil {
		return err
	}
	network.SortAddresses(addresses, envConfig.PreferIPv6())
	stateAddresses := instanceAddressesToAddresses(addresses)
	buildTxn := func(attempt int) ([]txn.Op, error) {
		changed = false
		if attempt > 0 {
			if err := m.Refresh(); err != nil {
				return nil, err
			}
		}
		if m.doc.Life == Dead {
			return nil, errDead
		}
		op := txn.Op{
			C:      m.st.machines.Name,
			Id:     m.doc.Id,
			Assert: append(bson.D{{fieldName, *field}}, notDeadDoc...),
		}
		if !addressesEqual(addresses, addressesToInstanceAddresses(*field)) {
			op.Update = bson.D{{"$set", bson.D{{fieldName, stateAddresses}}}}
			changed = true
		}
		return []txn.Op{op}, nil
	}
	switch err := m.st.run(buildTxn); err {
	case nil:
	case jujutxn.ErrExcessiveContention:
		return errors.Annotatef(err, "cannot set %s for machine %s", fieldName, m)
	default:
		return err
	}
	if !changed {
		return nil
	}
	*field = stateAddresses
	return nil
}
開發者ID:klyachin,項目名稱:juju,代碼行數:44,代碼來源:machine.go


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