Moved manifest package from services/datamanager to sdk/go
[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         "bufio"
9         "fmt"
10         "log"
11         "regexp"
12         "strconv"
13         "strings"
14 )
15
16 var LocatorPattern = regexp.MustCompile(
17         "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]+)*$")
18
19 type Manifest struct {
20         Text string
21 }
22
23 type BlockLocator struct {
24         Digest  string
25         Size    int
26         Hints   []string
27 }
28
29 type ManifestLine struct {
30         StreamName  string
31         Blocks       []string
32         Files        []string
33 }
34
35 func parseBlockLocator(s string) (b BlockLocator, err error) {
36         if !LocatorPattern.MatchString(s) {
37                 fmt.Errorf("String \"%s\" does not match BlockLocator pattern \"%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                 scanner := bufio.NewScanner(strings.NewReader(input))
74                 for scanner.Scan() {
75                         // We parse one line at a time, to save effort if we only need
76                         // the first few lines.
77                         ch <- parseManifestLine(scanner.Text())
78                 }
79                 close(ch)
80         }(m.Text)
81         return ch
82 }
83
84
85 // Blocks may appear mulitple times within the same manifest if they
86 // are used by multiple files. In that case this Iterator will output
87 // the same block multiple times.
88 func (m *Manifest) BlockIterWithDuplicates() <-chan BlockLocator {
89         blockChannel := make(chan BlockLocator)
90         go func(lineChannel <-chan ManifestLine) {
91                 for m := range lineChannel {
92                         for _, block := range m.Blocks {
93                                 if b, err := parseBlockLocator(block); err == nil {
94                                         blockChannel <- b
95                                 } else {
96                                         log.Printf("ERROR: Failed to parse block: %v", err)
97                                 }
98                         }
99                 }
100                 close(blockChannel)
101         }(m.LineIter())
102         return blockChannel
103 }