Long overdue checkin of data manager. Current code runs, but uses way too much memory...
[arvados.git] / sdk / go / manifest / manifest.go
1 /* Deals with parsing Manifest Text. */
2
3 // Inspired by the Manifest class in arvados/sdk/ruby/lib/arvados/keep.rb
4
5 package manifest
6
7 import (
8         "fmt"
9         "log"
10         "regexp"
11         "strconv"
12         "strings"
13 )
14
15 var LocatorPattern = regexp.MustCompile(
16         "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]+)*$")
17
18 type Manifest struct {
19         Text string
20 }
21
22 type BlockLocator struct {
23         Digest  string
24         Size    int
25         Hints   []string
26 }
27
28 type ManifestLine struct {
29         StreamName  string
30         Blocks       []string
31         Files        []string
32 }
33
34 func ParseBlockLocator(s string) (b BlockLocator, err error) {
35         if !LocatorPattern.MatchString(s) {
36                 err = fmt.Errorf("String \"%s\" does not match BlockLocator pattern " +
37                         "\"%s\".",
38                         s,
39                         LocatorPattern.String())
40         } else {
41                 tokens := strings.Split(s, "+")
42                 var blockSize int64
43                 // We expect ParseInt to succeed since LocatorPattern restricts
44                 // tokens[1] to contain exclusively digits.
45                 blockSize, err = strconv.ParseInt(tokens[1], 10, 0)
46                 if err == nil {
47                         b.Digest = tokens[0]
48                         b.Size = int(blockSize)
49                         b.Hints = tokens[2:]
50                 }
51         }
52         return
53 }
54
55 func parseManifestLine(s string) (m ManifestLine) {
56         tokens := strings.Split(s, " ")
57         m.StreamName = tokens[0]
58         tokens = tokens[1:]
59         var i int
60         for i = range tokens {
61                 if !LocatorPattern.MatchString(tokens[i]) {
62                         break
63                 }
64         }
65         m.Blocks = tokens[:i]
66         m.Files = tokens[i:]
67         return
68 }
69
70 func (m *Manifest) LineIter() <-chan ManifestLine {
71         ch := make(chan ManifestLine)
72         go func(input string) {
73                 // This slice holds the current line and the remainder of the
74                 // manifest.  We parse one line at a time, to save effort if we
75                 // only need the first few lines.
76                 lines := []string{"", input}
77                 for {
78                         lines = strings.SplitN(lines[1], "\n", 2)
79                         if len(lines[0]) > 0 {
80                                 // Only parse non-blank lines
81                                 ch <- parseManifestLine(lines[0])
82                         }
83                         if len(lines) == 1 {
84                                 break
85                         }
86                 }
87                 close(ch)
88         }(m.Text)
89         return ch
90 }
91
92
93 // Blocks may appear mulitple times within the same manifest if they
94 // are used by multiple files. In that case this Iterator will output
95 // the same block multiple times.
96 func (m *Manifest) BlockIterWithDuplicates() <-chan BlockLocator {
97         blockChannel := make(chan BlockLocator)
98         go func(lineChannel <-chan ManifestLine) {
99                 for m := range lineChannel {
100                         for _, block := range m.Blocks {
101                                 if b, err := ParseBlockLocator(block); err == nil {
102                                         blockChannel <- b
103                                 } else {
104                                         log.Printf("ERROR: Failed to parse block: %v", err)
105                                 }
106                         }
107                 }
108                 close(blockChannel)
109         }(m.LineIter())
110         return blockChannel
111 }