~wallyworld/juju-core/fast-lxc-everywhere

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"fmt"

	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
)

type sequenceDoc struct {
	Name    string `bson:"_id"`
	Counter int
}

func (s *State) sequence(name string) (int, error) {
	query := s.db.C("sequence").Find(bson.D{{"_id", name}})
	inc := mgo.Change{
		Update: bson.M{"$inc": bson.M{"counter": 1}},
		Upsert: true,
	}
	result := &sequenceDoc{}
	_, err := query.Apply(inc, result)
	if err != nil {
		return -1, fmt.Errorf("cannot increment %q sequence number: %v", name, err)
	}
	return result.Counter, nil
}