-
Notifications
You must be signed in to change notification settings - Fork 21
/
io.go
166 lines (150 loc) · 5.42 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package core
import (
"errors"
"fmt"
"github.com/MG-RAST/AWE/lib/logger"
"github.com/MG-RAST/AWE/lib/shock"
"github.com/MG-RAST/golib/go-uuid/uuid"
"net/url"
"strings"
)
type IO struct {
FileName string `bson:"filename" json:"filename"`
Name string `bson:"name" json:"name"` // specifies abstract name of output as defined by the app
AppPosition int `bson:"appposition" json:"-"` // specifies position in app output array
Directory string `bson:"directory" json:"directory"`
Host string `bson:"host" json:"host"`
Node string `bson:"node" json:"node"`
Url string `bson:"url" json:"url"` // can be shock or any other url
Size int64 `bson:"size" json:"size"`
MD5 string `bson:"md5" json:"-"`
Cache bool `bson:"cache" json:"cache"` // indicates that this files is "predata"" that needs to be cached
Origin string `bson:"origin" json:"origin"`
Path string `bson:"path" json:"-"`
Optional bool `bson:"optional" json:"-"`
Nonzero bool `bson:"nonzero" json:"nonzero"`
DataToken string `bson:"datatoken" json:"-"`
Intermediate bool `bson:"Intermediate" json:"-"`
Temporary bool `bson:"temporary" json:"temporary"`
ShockFilename string `bson:"shockfilename" json:"shockfilename"`
ShockIndex string `bson:"shockindex" json:"shockindex"` // on input it indicates that Shock node has to be indexed by AWE server
AttrFile string `bson:"attrfile" json:"attrfile"`
NoFile bool `bson:"nofile" json:"nofile"`
Delete bool `bson:"delete" json:"delete"`
Type string `bson:"type" json:"type"`
NodeAttr map[string]interface{} `bson:"nodeattr" json:"nodeattr"` // specifies attribute data to be stored in shock node (output only)
FormOptions map[string]string `bson:"formoptions" json:"formoptions"`
Uncompress string `bson:"uncompress" json:"uncompress"` // tells AWE client to uncompress this file, e.g. "gzip"
}
type PartInfo struct {
Input string `bson:"input" json:"input"`
Index string `bson:"index" json:"index"`
TotalIndex int `bson:"totalindex" json:"totalindex"`
MaxPartSizeMB int `bson:"maxpartsize_mb" json:"maxpartsize_mb"`
Options string `bson:"options" json:"-"`
}
type IOmap map[string]*IO // [filename]attributes
func NewIOmap() IOmap {
return IOmap{}
}
func (i IOmap) Add(name string, host string, node string, md5 string, cache bool) {
i[name] = &IO{FileName: name, Host: host, Node: node, MD5: md5, Cache: cache}
return
}
func (i IOmap) Has(name string) bool {
if _, has := i[name]; has {
return true
}
return false
}
func (i IOmap) Find(name string) *IO {
if val, has := i[name]; has {
return val
}
return nil
}
func NewIO() *IO {
return &IO{}
}
func (io *IO) DataUrl() (dataurl string, err error) {
if io.Url != "" {
// parse and test url
u, _ := url.Parse(io.Url)
if (u.Scheme == "") || (u.Host == "") || (u.Path == "") {
return "", errors.New("Not a valid url: " + io.Url)
}
// get shock info from url
if (io.Host == "") || (io.Node == "") || (io.Node == "-") {
trimPath := strings.Trim(u.Path, "/")
cleanUuid := strings.Trim(strings.TrimPrefix(trimPath, "node"), "/")
// appears to be a shock url
if (cleanUuid != trimPath) && (uuid.Parse(cleanUuid) != nil) {
io.Host = u.Scheme + "://" + u.Host
io.Node = cleanUuid
}
}
return io.Url, nil
} else if (io.Host != "") && (io.Node != "") && (io.Node != "-") {
io.Url = fmt.Sprintf("%s/node/%s?download", io.Host, io.Node)
return io.Url, nil
} else {
// empty IO is valid
return "", nil
}
}
func (io *IO) TotalUnits(indextype string) (count int, err error) {
count, err = io.GetIndexUnits(indextype)
return
}
func (io *IO) GetFileSize() int64 {
if io.Size > 0 {
return io.Size
}
shocknode, err := io.GetShockNode()
if err != nil {
logger.Error(fmt.Sprintf("GetFileSize error: %s, node: %s", err.Error(), io.Node))
return -1
}
io.Size = shocknode.File.Size
return io.Size
}
func (io *IO) GetIndexInfo() (idxinfo map[string]shock.IdxInfo, err error) {
var shocknode *shock.ShockNode
shocknode, err = io.GetShockNode()
if err != nil {
return
}
idxinfo = shocknode.Indexes
return
}
func (io *IO) GetShockNode() (node *shock.ShockNode, err error) {
if io.Host == "" {
return nil, errors.New("empty shock host")
}
if io.Node == "-" {
return nil, errors.New("empty node id")
}
return shock.ShockGet(io.Host, io.Node, io.DataToken)
}
func (io *IO) GetIndexUnits(indextype string) (totalunits int, err error) {
var shocknode *shock.ShockNode
shocknode, err = io.GetShockNode()
if err != nil {
return
}
if _, ok := shocknode.Indexes[indextype]; ok {
if shocknode.Indexes[indextype].TotalUnits > 0 {
return int(shocknode.Indexes[indextype].TotalUnits), nil
}
}
return 0, errors.New("invalid totalunits for shock node:" + io.Node)
}
func (io *IO) DeleteNode() (nodeid string, err error) {
if io.Delete {
if err := shock.ShockDelete(io.Host, io.Node, io.DataToken); err != nil {
return io.Node, err
}
return io.Node, nil
}
return "", nil
}