OSDN Git Service

47860ecf2b7e43d1d4a2940ba2bb34ae2999fa6d
[bytom/vapor.git] / toolbar / osssync / sync / infofile.go
1 package sync
2
3 import "github.com/bytom/vapor/toolbar/osssync/util"
4
5 // Interval determines the number of blocks in a Gzip file in the Interval of blockHeight
6 // StartBlockHeight is the start of the Interval
7 // EndBlockHeight: the end of the Interval
8 // GzSize is the number of blocks store in a Gzip file
9 type Interval struct {
10         StartBlockHeight uint64
11         EndBlockHeight   uint64
12         GzSize           uint64
13 }
14
15 // NewInterval creates a new Interval from info.json
16 func NewInterval(start, end, gzSize uint64) *Interval {
17         return &Interval{
18                 StartBlockHeight: start,
19                 EndBlockHeight:   end,
20                 GzSize:           gzSize,
21         }
22 }
23
24 // Info is a struct for info.json
25 type Info struct {
26         LatestBlockHeight uint64
27         Interval          []*Interval
28 }
29
30 // NewInfo creates a new Info for info.json
31 func NewInfo(end, gzSize uint64) *Info {
32         newInvl := NewInterval(0, end, gzSize)
33         var arr []*Interval
34         arr = append(arr, newInvl)
35         return &Info{0, arr}
36 }
37
38 // GetInfoJson Download info.json
39 func (s *Sync) GetInfoJson() (*Info, error) {
40         data, err := s.GetObjToData("info.json")
41         if err != nil {
42                 return nil, err
43         }
44
45         info := new(Info)
46         err = util.Json2Struct(data, &info)
47         return info, err
48 }
49
50 // Upload info.json
51 func (s *Sync) PutInfoJson(infoData *Info) error {
52         jsonData, err := util.Struct2Json(infoData)
53         if err != nil {
54                 return err
55         }
56
57         // Upload
58         return s.PutObjByteArr("info.json", jsonData)
59 }
60
61 // SetLatestBlockHeight set new latest blockHeight on OSS
62 func (s *Sync) SetLatestBlockHeight(newLatestBlockHeight uint64) error {
63         info, err := s.GetInfoJson()
64         if err != nil {
65                 return err
66         }
67
68         info.LatestBlockHeight = newLatestBlockHeight
69         return s.PutInfoJson(info)
70 }
71
72 // AddInterval adds an interval to the end of info.json
73 func (s *Sync) AddInterval(end, gzSize uint64) error {
74         isJsonExist, err := s.OssBucket.IsObjectExist("info.json")
75         if err != nil {
76                 return err
77         }
78
79         var info *Info
80         if isJsonExist {
81                 // Download info.json
82                 info, err = s.GetInfoJson()
83                 if err != nil {
84                         return err
85                 }
86
87                 // Add Interval
88                 prevInvl := info.Interval[len(info.Interval)-1]
89                 newInvl := NewInterval(prevInvl.EndBlockHeight+1, end, gzSize)
90                 info.Interval = append(info.Interval, newInvl)
91         } else {
92                 info = NewInfo(end, gzSize)
93         }
94         return s.PutInfoJson(info)
95 }