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.6 kB

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