Merge branch 'master' into 10797-ruby-2.3
[arvados.git] / sdk / go / arvados / collection.go
1 package arvados
2
3 import (
4         "bufio"
5         "fmt"
6         "strings"
7         "time"
8
9         "git.curoverse.com/arvados.git/sdk/go/manifest"
10 )
11
12 // Collection is an arvados#collection resource.
13 type Collection struct {
14         UUID                   string     `json:"uuid,omitempty"`
15         TrashAt                *time.Time `json:"trash_at,omitempty"`
16         ManifestText           string     `json:"manifest_text,omitempty"`
17         UnsignedManifestText   string     `json:"unsigned_manifest_text,omitempty"`
18         CreatedAt              *time.Time `json:"created_at,omitempty"`
19         ModifiedAt             *time.Time `json:"modified_at,omitempty"`
20         PortableDataHash       string     `json:"portable_data_hash,omitempty"`
21         ReplicationConfirmed   *int       `json:"replication_confirmed,omitempty"`
22         ReplicationConfirmedAt *time.Time `json:"replication_confirmed_at,omitempty"`
23         ReplicationDesired     *int       `json:"replication_desired,omitempty"`
24         DeleteAt               *time.Time `json:"delete_at,omitempty"`
25         IsTrashed              bool       `json:"is_trashed,omitempty"`
26 }
27
28 // SizedDigests returns the hash+size part of each data block
29 // referenced by the collection.
30 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
31         manifestText := c.ManifestText
32         if manifestText == "" {
33                 manifestText = c.UnsignedManifestText
34         }
35         if manifestText == "" && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
36                 // TODO: Check more subtle forms of corruption, too
37                 return nil, fmt.Errorf("manifest is missing")
38         }
39         var sds []SizedDigest
40         scanner := bufio.NewScanner(strings.NewReader(manifestText))
41         scanner.Buffer(make([]byte, 1048576), len(manifestText))
42         for scanner.Scan() {
43                 line := scanner.Text()
44                 tokens := strings.Split(line, " ")
45                 if len(tokens) < 3 {
46                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
47                 }
48                 for _, token := range tokens[1:] {
49                         if !manifest.LocatorPattern.MatchString(token) {
50                                 // FIXME: ensure it's a file token
51                                 break
52                         }
53                         // FIXME: shouldn't assume 32 char hash
54                         if i := strings.IndexRune(token[33:], '+'); i >= 0 {
55                                 token = token[:33+i]
56                         }
57                         sds = append(sds, SizedDigest(token))
58                 }
59         }
60         return sds, scanner.Err()
61 }
62
63 // CollectionList is an arvados#collectionList resource.
64 type CollectionList struct {
65         Items          []Collection `json:"items"`
66         ItemsAvailable int          `json:"items_available"`
67         Offset         int          `json:"offset"`
68         Limit          int          `json:"limit"`
69 }