15954: Merge branch 'master'
[arvados.git] / sdk / go / arvados / collection.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "bufio"
9         "fmt"
10         "strings"
11         "time"
12
13         "git.arvados.org/arvados.git/sdk/go/blockdigest"
14 )
15
16 // Collection is an arvados#collection resource.
17 type Collection struct {
18         UUID                      string                 `json:"uuid"`
19         Etag                      string                 `json:"etag"`
20         OwnerUUID                 string                 `json:"owner_uuid"`
21         TrashAt                   *time.Time             `json:"trash_at"`
22         ManifestText              string                 `json:"manifest_text"`
23         UnsignedManifestText      string                 `json:"unsigned_manifest_text"`
24         Name                      string                 `json:"name"`
25         CreatedAt                 time.Time              `json:"created_at"`
26         ModifiedAt                time.Time              `json:"modified_at"`
27         ModifiedByClientUUID      string                 `json:"modified_by_client_uuid"`
28         ModifiedByUserUUID        string                 `json:"modified_by_user_uuid"`
29         PortableDataHash          string                 `json:"portable_data_hash"`
30         ReplicationConfirmed      *int                   `json:"replication_confirmed"`
31         ReplicationConfirmedAt    *time.Time             `json:"replication_confirmed_at"`
32         ReplicationDesired        *int                   `json:"replication_desired"`
33         StorageClassesDesired     []string               `json:"storage_classes_desired"`
34         StorageClassesConfirmed   []string               `json:"storage_classes_confirmed"`
35         StorageClassesConfirmedAt *time.Time             `json:"storage_classes_confirmed_at"`
36         DeleteAt                  *time.Time             `json:"delete_at"`
37         IsTrashed                 bool                   `json:"is_trashed"`
38         Properties                map[string]interface{} `json:"properties"`
39         WritableBy                []string               `json:"writable_by,omitempty"`
40         FileCount                 int                    `json:"file_count"`
41         FileSizeTotal             int64                  `json:"file_size_total"`
42         Version                   int                    `json:"version"`
43         PreserveVersion           bool                   `json:"preserve_version"`
44         CurrentVersionUUID        string                 `json:"current_version_uuid"`
45         Description               string                 `json:"description"`
46 }
47
48 func (c Collection) resourceName() string {
49         return "collection"
50 }
51
52 // SizedDigests returns the hash+size part of each data block
53 // referenced by the collection.
54 func (c *Collection) SizedDigests() ([]SizedDigest, error) {
55         manifestText := c.ManifestText
56         if manifestText == "" {
57                 manifestText = c.UnsignedManifestText
58         }
59         if manifestText == "" && c.PortableDataHash != "d41d8cd98f00b204e9800998ecf8427e+0" {
60                 // TODO: Check more subtle forms of corruption, too
61                 return nil, fmt.Errorf("manifest is missing")
62         }
63         var sds []SizedDigest
64         scanner := bufio.NewScanner(strings.NewReader(manifestText))
65         scanner.Buffer(make([]byte, 1048576), len(manifestText))
66         for scanner.Scan() {
67                 line := scanner.Text()
68                 tokens := strings.Split(line, " ")
69                 if len(tokens) < 3 {
70                         return nil, fmt.Errorf("Invalid stream (<3 tokens): %q", line)
71                 }
72                 for _, token := range tokens[1:] {
73                         if !blockdigest.LocatorPattern.MatchString(token) {
74                                 // FIXME: ensure it's a file token
75                                 break
76                         }
77                         // FIXME: shouldn't assume 32 char hash
78                         if i := strings.IndexRune(token[33:], '+'); i >= 0 {
79                                 token = token[:33+i]
80                         }
81                         sds = append(sds, SizedDigest(token))
82                 }
83         }
84         return sds, scanner.Err()
85 }
86
87 type CollectionList struct {
88         Items          []Collection `json:"items"`
89         ItemsAvailable int          `json:"items_available"`
90         Offset         int          `json:"offset"`
91         Limit          int          `json:"limit"`
92 }