indexObjects.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package gindex
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "time"
  6. "github.com/G-Node/gig"
  7. log "github.com/Sirupsen/logrus"
  8. "github.com/G-Node/go-annex"
  9. "fmt"
  10. )
  11. type IndexBlob struct {
  12. *gig.Blob
  13. GinRepoName string
  14. GinRepoId string
  15. FirstCommit string
  16. Id int64
  17. Oid gig.SHA1
  18. IndexingTime time.Time
  19. Content string
  20. Path string
  21. }
  22. func NewCommitFromGig(gCommit *gig.Commit, repoid string, reponame string, oid gig.SHA1) *IndexCommit {
  23. commit := &IndexCommit{gCommit, repoid, oid,
  24. reponame, time.Now()}
  25. return commit
  26. }
  27. func NewBlobFromGig(gBlob *gig.Blob, repoid string, oid gig.SHA1, commit string, path string, reponame string) *IndexBlob {
  28. // Remember keeping the id
  29. blob := IndexBlob{Blob: gBlob, GinRepoId: repoid, Oid: oid, FirstCommit: commit, Path: path, GinRepoName: reponame}
  30. return &blob
  31. }
  32. type IndexCommit struct {
  33. *gig.Commit
  34. GinRepoId string
  35. Oid gig.SHA1
  36. GinRepoName string
  37. IndexingTime time.Time
  38. }
  39. func BlobFromJson(data []byte) (*IndexBlob, error) {
  40. bl := &IndexBlob{}
  41. err := json.Unmarshal(data, bl)
  42. return bl, err
  43. }
  44. func (c *IndexCommit) ToJson() ([]byte, error) {
  45. return json.Marshal(c)
  46. }
  47. func (c *IndexCommit) AddToIndex(server *ElServer, index string, id gig.SHA1) error {
  48. data, err := c.ToJson()
  49. if err != nil {
  50. return err
  51. }
  52. indexid := GetIndexCommitId(id.String(), c.GinRepoId)
  53. err = AddToIndex(data, server, index, "commit", indexid)
  54. return err
  55. }
  56. func (bl *IndexBlob) ToJson() ([]byte, error) {
  57. return json.Marshal(bl)
  58. }
  59. func (bl *IndexBlob) AddToIndex(server *ElServer, index, repopath string, id gig.SHA1) error {
  60. indexid := GetIndexCommitId(id.String(), bl.GinRepoId)
  61. if bl.Size() > gannex.MEGABYTE*10 {
  62. return fmt.Errorf("File to big")
  63. }
  64. f_type, blobBuffer, err := BlobFileType(bl)
  65. if err != nil {
  66. log.Errorf("Could not determine file type: %+v", err)
  67. return nil
  68. }
  69. switch f_type {
  70. case ANNEX:
  71. fallthrough // deactivated fort the time being
  72. /* APFileC, err := ioutil.ReadAll(blobBuffer)
  73. log.Debugf("Annex file:%s", APFileC)
  74. if err != nil {
  75. log.Errorf("Could not open annex pointer file: %+v", err)
  76. return err
  77. }
  78. Afile, err := gannex.NewAFile(repopath, "", "", APFileC)
  79. if err != nil {
  80. log.Errorf("Could not get annex file%+v", err)
  81. return err
  82. }
  83. fp, err := Afile.Open()
  84. if err != nil {
  85. log.Errorf("Could not open annex file: %+v", err)
  86. return err
  87. }
  88. defer fp.Close()
  89. bl.Blob = gig.MakeAnnexBlob(fp, Afile.Info.Size())
  90. return bl.AddToIndex(server, index, repopath, id)*/
  91. case TEXT:
  92. ct, err := ioutil.ReadAll(blobBuffer)
  93. if err != nil {
  94. log.Errorf("Could not read text file content:%+v", err)
  95. return err
  96. }
  97. bl.Content = string(ct)
  98. case ODML_XML:
  99. ct, err := ioutil.ReadAll(blobBuffer)
  100. if err != nil {
  101. return err
  102. }
  103. bl.Content = string(ct)
  104. }
  105. data, err := bl.ToJson()
  106. if err != nil {
  107. return err
  108. }
  109. err = AddToIndex(data, server, index, "blob", indexid)
  110. return err
  111. }
  112. func (bl *IndexBlob) IsInIndex() (bool, error) {
  113. return false, nil
  114. }
  115. func AddToIndex(data []byte, server *ElServer, index, doctype string, id gig.SHA1) error {
  116. resp, err := server.Index(index, doctype, data, id)
  117. if err != nil {
  118. return err
  119. }
  120. resp.Body.Close()
  121. return err
  122. }