1 /* Deals with parsing Manifest Text. */
3 // Inspired by the Manifest class in arvados/sdk/ruby/lib/arvados/keep.rb
15 var LocatorPattern = regexp.MustCompile(
16 "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]+)*$")
18 type Manifest struct {
22 type BlockLocator struct {
28 type ManifestLine struct {
34 func parseBlockLocator(s string) (b BlockLocator, err error) {
35 if !LocatorPattern.MatchString(s) {
36 fmt.Errorf("String \"%s\" does not match BlockLocator pattern \"%s\".",
38 LocatorPattern.String())
40 tokens := strings.Split(s, "+")
42 // We expect ParseInt to succeed since LocatorPattern restricts
43 // tokens[1] to contain exclusively digits.
44 blockSize, err = strconv.ParseInt(tokens[1], 10, 0)
47 b.Size = int(blockSize)
54 func parseManifestLine(s string) (m ManifestLine) {
55 tokens := strings.Split(s, " ")
56 m.StreamName = tokens[0]
60 for i, token = range tokens {
61 if !LocatorPattern.MatchString(token) {
70 func (m *Manifest) LineIter() <-chan ManifestLine {
71 ch := make(chan ManifestLine)
72 go func(remaining string) {
74 // We parse one line at a time, to save effort if we only need
75 // the first few lines.
76 splitsies := strings.SplitN(remaining, "\n", 2)
77 ch <- parseManifestLine(splitsies[0])
78 if len(splitsies) == 1 {
81 remaining = splitsies[1]
89 // Blocks may appear mulitple times within the same manifest if they
90 // are used by multiple files. In that case this Iterator will output
91 // the same block multiple times.
92 func (m *Manifest) DuplicateBlockIter() <-chan BlockLocator {
93 blockChannel := make(chan BlockLocator)
94 go func(lineChannel <-chan ManifestLine) {
95 for m := range lineChannel {
96 for _, block := range m.Blocks {
97 if b, err := parseBlockLocator(block); err == nil {
100 log.Printf("ERROR: Failed to parse block: %v", err)