5737: Merge branch 'master' into 5737-ruby231
[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         ExpiresAt              *time.Time `json:"expires_at,omitempty"`
16         ManifestText           string     `json:"manifest_text,omitempty"`
17         CreatedAt              *time.Time `json:"created_at,omitempty"`
18         ModifiedAt             *time.Time `json:"modified_at,omitempty"`
19         PortableDataHash       string     `json:"portable_data_hash,omitempty"`
20         ReplicationConfirmed   *int       `json:"replication_confirmed,omitempty"`
21         ReplicationConfirmedAt *time.Time `json:"replication_confirmed_at,omitempty"`
22         ReplicationDesired     *int       `json:"replication_desired,omitempty"`
23 }
24
25 // SizedDigests returns the hash+size part of each data block
26 // referenced by the collection.
27 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
28         if c.ManifestText == "" && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
29                 // TODO: Check more subtle forms of corruption, too
30                 return nil, fmt.Errorf("manifest is missing")
31         }
32         var sds []SizedDigest
33         scanner := bufio.NewScanner(strings.NewReader(c.ManifestText))
34         scanner.Buffer(make([]byte, 1048576), len(c.ManifestText))
35         for scanner.Scan() {
36                 line := scanner.Text()
37                 tokens := strings.Split(line, " ")
38                 if len(tokens) < 3 {
39                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
40                 }
41                 for _, token := range tokens[1:] {
42                         if !manifest.LocatorPattern.MatchString(token) {
43                                 // FIXME: ensure it's a file token
44                                 break
45                         }
46                         // FIXME: shouldn't assume 32 char hash
47                         if i := strings.IndexRune(token[33:], '+'); i >= 0 {
48                                 token = token[:33+i]
49                         }
50                         sds = append(sds, SizedDigest(token))
51                 }
52         }
53         return sds, scanner.Err()
54 }
55
56 // CollectionList is an arvados#collectionList resource.
57 type CollectionList struct {
58         Items          []Collection `json:"items"`
59         ItemsAvailable int          `json:"items_available"`
60         Offset         int          `json:"offset"`
61         Limit          int          `json:"limit"`
62 }