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.

127 lines
2.7 KiB

  1. package mongodb
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/repositories"
  7. "github.com/globalsign/mgo"
  8. "github.com/globalsign/mgo/bson"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type changeRepository struct {
  14. restoreIDs bool
  15. changes *mgo.Collection
  16. idCounter *counter
  17. }
  18. func (r *changeRepository) Find(ctx context.Context, id string) (*models.Change, error) {
  19. change := new(models.Change)
  20. err := r.changes.FindId(id).One(change)
  21. if err != nil {
  22. return nil, err
  23. }
  24. return change, nil
  25. }
  26. func (r *changeRepository) List(ctx context.Context, filter models.ChangeFilter) ([]*models.Change, error) {
  27. query := bson.M{}
  28. limit := 0
  29. if filter.EarliestDate != nil && !filter.EarliestDate.IsZero() {
  30. query["date"] = bson.M{"$gte": *filter.EarliestDate}
  31. }
  32. if len(filter.Keys) > 0 {
  33. query["keys"] = bson.M{"$in": filter.Keys}
  34. } else if !r.restoreIDs {
  35. query["listed"] = true
  36. }
  37. if filter.Author != nil && *filter.Author != "" {
  38. query["author"] = *filter.Author
  39. }
  40. if filter.Limit != nil {
  41. limit = *filter.Limit
  42. }
  43. initialSize := 64
  44. if limit > 0 && limit < 256 {
  45. initialSize = limit
  46. }
  47. changes := make([]*models.Change, 0, initialSize)
  48. err := r.changes.Find(query).Sort("-date").Limit(limit).All(&changes)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return changes, nil
  53. }
  54. func (r *changeRepository) Insert(ctx context.Context, change models.Change) (*models.Change, error) {
  55. if !r.restoreIDs || change.ID == "" {
  56. next, err := r.idCounter.Increment(1)
  57. if err != nil {
  58. return nil, err
  59. }
  60. change.ID = "Change_" + strconv.Itoa(next)
  61. } else {
  62. tokens := strings.Split(change.ID, "_")
  63. if len(tokens) != 2 || tokens[0] != "Change" {
  64. return nil, errors.New("Invalid change ID")
  65. }
  66. n, err := strconv.Atoi(tokens[1])
  67. if err != nil {
  68. return nil, err
  69. }
  70. _ = r.idCounter.Bump(n)
  71. }
  72. err := r.changes.Insert(&change)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return &change, nil
  77. }
  78. func (r *changeRepository) Remove(ctx context.Context, change models.Change) error {
  79. return r.changes.RemoveId(change.ID)
  80. }
  81. func newChangeRepository(db *mgo.Database, restoreIDs bool) (repositories.ChangeRepository, error) {
  82. collection := db.C("common.changes")
  83. // Delete the old index if it exists.
  84. _ = collection.DropIndexName("date_1")
  85. err := collection.EnsureIndex(mgo.Index{
  86. Name: "expiry",
  87. Key: []string{"date"},
  88. ExpireAfter: time.Hour * 2400, // 100 days
  89. })
  90. if err != nil {
  91. return nil, err
  92. }
  93. err = collection.EnsureIndexKey("author")
  94. if err != nil {
  95. return nil, err
  96. }
  97. err = collection.EnsureIndexKey("keys")
  98. if err != nil {
  99. return nil, err
  100. }
  101. return &changeRepository{
  102. restoreIDs: restoreIDs,
  103. changes: collection,
  104. idCounter: newCounter(db, "auto_increment", "Change"),
  105. }, nil
  106. }