You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset.go 4.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package models
  2. import (
  3. "fmt"
  4. "sort"
  5. "code.gitea.io/gitea/modules/timeutil"
  6. "xorm.io/builder"
  7. )
  8. const (
  9. DatasetStatusPrivate int = iota
  10. DatasetStatusPublic
  11. DatasetStatusDeleted
  12. )
  13. type DatasetList []*Dataset
  14. type SearchDatasetOptions struct {
  15. ListOptions
  16. Keyword string
  17. OwnerID int64
  18. IsPublic bool
  19. }
  20. type Dataset struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. Title string `xorm:"INDEX NOT NULL"`
  23. Status int32 `xorm:"INDEX"` // normal_private: 0, pulbic: 1, is_delete: 2
  24. Category string
  25. Description string `xorm:"TEXT"`
  26. DownloadTimes int64
  27. License string
  28. Task string
  29. ReleaseID int64 `xorm:"INDEX"`
  30. UserID int64 `xorm:"INDEX"`
  31. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  32. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  33. Attachments []*Attachment `xorm:"-"`
  34. }
  35. func CreateDataset(dataset *Dataset) (err error) {
  36. if _, err = x.Insert(dataset); err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. // AddDatasetAttachments adds a Dataset attachments
  42. func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) {
  43. // Check attachments
  44. attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
  45. if err != nil {
  46. return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
  47. }
  48. for i := range attachments {
  49. attachments[i].DatasetID = DatasetID
  50. // No assign value could be 0, so ignore AllCols().
  51. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  52. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  53. }
  54. }
  55. return
  56. }
  57. // GetDatasetByID returns Dataset with given ID.
  58. func GetDatasetByID(id int64) (*Dataset, error) {
  59. rel := new(Dataset)
  60. has, err := x.
  61. ID(id).
  62. Get(rel)
  63. if err != nil {
  64. return nil, err
  65. } else if !has {
  66. return nil, ErrDatasetNotExist{id}
  67. }
  68. return rel, nil
  69. }
  70. func UpdateDataset(ctx DBContext, rel *Dataset) error {
  71. _, err := ctx.e.ID(rel.ID).AllCols().Update(rel)
  72. return err
  73. }
  74. func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) {
  75. cond := SearchDatasetCondition(opts)
  76. return SearchDatasetByCondition(opts, cond)
  77. }
  78. func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond {
  79. var cond = builder.NewCond()
  80. cond = cond.And(builder.Eq{"user_id": opts.OwnerID})
  81. if opts.IsPublic {
  82. cond = cond.And(builder.Eq{"status": DatasetStatusPublic})
  83. } else {
  84. cond = cond.And(builder.Neq{"status": DatasetStatusDeleted})
  85. }
  86. return cond
  87. }
  88. func SearchDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (DatasetList, int64, error) {
  89. if opts.Page <= 0 {
  90. opts.Page = 1
  91. }
  92. var err error
  93. sess := x.NewSession()
  94. defer sess.Close()
  95. // count, err := sess.Where(cond).Count(new(DatasetList))
  96. // if err != nil {
  97. // return nil, 0, fmt.Errorf("Count: %v", err)
  98. // }
  99. repos := make(DatasetList, 0, opts.PageSize)
  100. sess.Where(cond)
  101. if opts.PageSize > 0 {
  102. sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  103. }
  104. if err = sess.Find(&repos); err != nil {
  105. return nil, 0, fmt.Errorf("Dataset: %v", err)
  106. }
  107. return repos, 0, nil
  108. }
  109. type datasetMetaSearch struct {
  110. ID []int64
  111. Rel []*Dataset
  112. }
  113. func (s datasetMetaSearch) Len() int {
  114. return len(s.ID)
  115. }
  116. func (s datasetMetaSearch) Swap(i, j int) {
  117. s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
  118. s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
  119. }
  120. func (s datasetMetaSearch) Less(i, j int) bool {
  121. return s.ID[i] < s.ID[j]
  122. }
  123. func GeDatasetAttachments(rels ...*Dataset) (err error) {
  124. return geDatasetAttachments(x, rels...)
  125. }
  126. func geDatasetAttachments(e Engine, rels ...*Dataset) (err error) {
  127. if len(rels) == 0 {
  128. return
  129. }
  130. // To keep this efficient as possible sort all datasets by id,
  131. // select attachments by dataset id,
  132. // then merge join them
  133. // Sort
  134. var sortedRels = datasetMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Dataset, len(rels))}
  135. var attachments []*Attachment
  136. for index, element := range rels {
  137. element.Attachments = []*Attachment{}
  138. sortedRels.ID[index] = element.ID
  139. sortedRels.Rel[index] = element
  140. }
  141. sort.Sort(sortedRels)
  142. // Select attachments
  143. err = e.
  144. Asc("dataset_id").
  145. In("dataset_id", sortedRels.ID).
  146. Find(&attachments, Attachment{})
  147. if err != nil {
  148. return err
  149. }
  150. // merge join
  151. var currentIndex = 0
  152. for _, attachment := range attachments {
  153. for sortedRels.ID[currentIndex] < attachment.DatasetID {
  154. currentIndex++
  155. }
  156. sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
  157. }
  158. return
  159. }