GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
738 B

  1. package counter
  2. import (
  3. "git.aiterp.net/rpdata/api/internal/store"
  4. "github.com/globalsign/mgo"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. var collection *mgo.Collection
  8. type counter struct {
  9. ID string `bson:"_id"`
  10. Value int `bson:"value"`
  11. }
  12. // Next gets the next value of a counter, or an error if it hasn't.
  13. func Next(category, name string) (int, error) {
  14. id := category + "." + name
  15. doc := counter{}
  16. _, err := collection.Find(bson.M{"_id": id}).Apply(mgo.Change{
  17. Update: bson.M{"$inc": bson.M{"value": 1}},
  18. Upsert: true,
  19. ReturnNew: true,
  20. }, &doc)
  21. if err != nil {
  22. return -1, err
  23. }
  24. return doc.Value, nil
  25. }
  26. func init() {
  27. store.HandleInit(func(db *mgo.Database) {
  28. collection = db.C("core.counters")
  29. })
  30. }