8784: Fix test for latest firefox.
[arvados.git] / services / keep-balance / change_set.go
1 package main
2
3 import (
4         "encoding/json"
5         "fmt"
6         "sync"
7
8         "git.curoverse.com/arvados.git/sdk/go/arvados"
9 )
10
11 // Pull is a request to retrieve a block from a remote server, and
12 // store it locally.
13 type Pull struct {
14         arvados.SizedDigest
15         Source *KeepService
16 }
17
18 // MarshalJSON formats a pull request the way keepstore wants to see
19 // it.
20 func (p Pull) MarshalJSON() ([]byte, error) {
21         type KeepstorePullRequest struct {
22                 Locator string   `json:"locator"`
23                 Servers []string `json:"servers"`
24         }
25         return json.Marshal(KeepstorePullRequest{
26                 Locator: string(p.SizedDigest[:32]),
27                 Servers: []string{p.Source.URLBase()}})
28 }
29
30 // Trash is a request to delete a block.
31 type Trash struct {
32         arvados.SizedDigest
33         Mtime int64
34 }
35
36 // MarshalJSON formats a trash request the way keepstore wants to see
37 // it, i.e., as a bare locator with no +size hint.
38 func (t Trash) MarshalJSON() ([]byte, error) {
39         type KeepstoreTrashRequest struct {
40                 Locator    string `json:"locator"`
41                 BlockMtime int64  `json:"block_mtime"`
42         }
43         return json.Marshal(KeepstoreTrashRequest{
44                 Locator:    string(t.SizedDigest[:32]),
45                 BlockMtime: t.Mtime})
46 }
47
48 // ChangeSet is a set of change requests that will be sent to a
49 // keepstore server.
50 type ChangeSet struct {
51         Pulls   []Pull
52         Trashes []Trash
53         mutex   sync.Mutex
54 }
55
56 // AddPull adds a Pull operation.
57 func (cs *ChangeSet) AddPull(p Pull) {
58         cs.mutex.Lock()
59         cs.Pulls = append(cs.Pulls, p)
60         cs.mutex.Unlock()
61 }
62
63 // AddTrash adds a Trash operation
64 func (cs *ChangeSet) AddTrash(t Trash) {
65         cs.mutex.Lock()
66         cs.Trashes = append(cs.Trashes, t)
67         cs.mutex.Unlock()
68 }
69
70 // String implements fmt.Stringer.
71 func (cs *ChangeSet) String() string {
72         cs.mutex.Lock()
73         defer cs.mutex.Unlock()
74         return fmt.Sprintf("ChangeSet{Pulls:%d, Trashes:%d}", len(cs.Pulls), len(cs.Trashes))
75 }