~themue/juju-core/006-state-retry-delay

« back to all changes in this revision

Viewing changes to state/settings.go

  • Committer: Frank Mueller
  • Date: 2013-01-17 08:51:34 UTC
  • mfrom: (774.1.63 juju-core)
  • Revision ID: themue@gmail.com-20130117085134-f8ymqidn5bc6js2d
trivial: added attempt interval behaviours

Show diffs side-by-side

added added

removed removed

Lines of Context:
217
217
 
218
218
var errSettingsExist = fmt.Errorf("cannot overwrite existing settings")
219
219
 
 
220
func createSettingsOp(st *State, key string, values map[string]interface{}) txn.Op {
 
221
        return txn.Op{
 
222
                C:      st.settings.Name,
 
223
                Id:     key,
 
224
                Assert: txn.DocMissing,
 
225
                Insert: values,
 
226
        }
 
227
}
 
228
 
220
229
// createSettings writes an initial config node.
221
230
func createSettings(st *State, key string, values map[string]interface{}) (*Settings, error) {
222
231
        s := newSettings(st, key)
223
232
        s.core = copyMap(values)
224
 
        ops := []txn.Op{{
225
 
                C:      s.st.settings.Name,
226
 
                Id:     s.key,
227
 
                Assert: txn.DocMissing,
228
 
                Insert: s.core,
229
 
        }}
 
233
        ops := []txn.Op{createSettingsOp(st, key, values)}
230
234
        err := s.st.runner.Run(ops, "", nil)
231
235
        if err == txn.ErrAborted {
232
236
                return nil, errSettingsExist
245
249
        }
246
250
        return nil
247
251
}
 
252
 
 
253
// replaceSettingsOp returns a txn.Op that deletes the document's contents and
 
254
// replaces it with the supplied values, and a function that should be called on
 
255
// txn failure to determine whether this operation failed (due to a concurrent
 
256
// settings change).
 
257
func replaceSettingsOp(st *State, key string, values map[string]interface{}) (txn.Op, func() (bool, error), error) {
 
258
        s, err := readSettings(st, key)
 
259
        if err != nil {
 
260
                return txn.Op{}, nil, err
 
261
        }
 
262
        deletes := map[string]int{}
 
263
        for k := range s.disk {
 
264
                if _, found := values[k]; !found {
 
265
                        deletes[k] = 1
 
266
                }
 
267
        }
 
268
        op := txn.Op{
 
269
                C:      st.settings.Name,
 
270
                Id:     key,
 
271
                Assert: D{{"txn-revno", s.txnRevno}},
 
272
                Update: D{
 
273
                        {"$set", values},
 
274
                        {"$unset", deletes},
 
275
                },
 
276
        }
 
277
        assertFailed := func() (bool, error) {
 
278
                latest, err := readSettings(st, key)
 
279
                if err != nil {
 
280
                        return false, err
 
281
                }
 
282
                return latest.txnRevno != s.txnRevno, nil
 
283
        }
 
284
        return op, assertFailed, nil
 
285
}