本文整理汇总了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
}
示例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
}