1 /* Deals with parsing Manifest Text. */
3 // Inspired by the Manifest class in arvados/sdk/ruby/lib/arvados/keep.rb
16 var LocatorPattern = regexp.MustCompile(
17 "^[0-9a-fA-F]{32}\\+[0-9]+(\\+[A-Z][A-Za-z0-9@_-]+)*$")
19 type Manifest struct {
23 type BlockLocator struct {
29 type ManifestLine struct {
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\".",
39 LocatorPattern.String())
41 tokens := strings.Split(s, "+")
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)
48 b.Size = int(blockSize)
55 func parseManifestLine(s string) (m ManifestLine) {
56 tokens := strings.Split(s, " ")
57 m.StreamName = tokens[0]
60 for i = range tokens {
61 if !LocatorPattern.MatchString(tokens[i]) {
70 func (m *Manifest) LineIter() <-chan ManifestLine {
71 ch := make(chan ManifestLine)
72 go func(input string) {
73 scanner := bufio.NewScanner(strings.NewReader(input))
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())
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 {
96 log.Printf("ERROR: Failed to parse block: %v", err)