scanner.go 2.2 KB
Newer Older
H
Helin Wang 已提交
1 2 3 4 5 6 7 8
package recordio

import (
	"fmt"
	"os"
	"path/filepath"
)

H
Helin Wang 已提交
9 10
// Scanner is a scanner for multiple recordio files.
type Scanner struct {
H
Helin Wang 已提交
11 12
	paths      []string
	curFile    *os.File
H
Helin Wang 已提交
13
	curScanner *RangeScanner
H
Helin Wang 已提交
14 15 16 17 18
	pathIdx    int
	end        bool
	err        error
}

H
Helin Wang 已提交
19 20
// NewScanner creates a new Scanner.
func NewScanner(paths ...string) (*Scanner, error) {
H
Helin Wang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
	var ps []string
	for _, s := range paths {
		match, err := filepath.Glob(s)
		if err != nil {
			return nil, err
		}

		ps = append(ps, match...)
	}

	if len(ps) == 0 {
		return nil, fmt.Errorf("no valid path provided: %v", paths)
	}

H
Helin Wang 已提交
35
	return &Scanner{paths: ps}, nil
H
Helin Wang 已提交
36 37 38 39
}

// Scan moves the cursor forward for one record and loads the chunk
// containing the record if not yet.
H
Helin Wang 已提交
40
func (s *Scanner) Scan() bool {
H
Helin Wang 已提交
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
	if s.err != nil {
		return false
	}

	if s.end {
		return false
	}

	if s.curScanner == nil {
		more, err := s.nextFile()
		if err != nil {
			s.err = err
			return false
		}

		if !more {
			s.end = true
			return false
		}
	}

	curMore := s.curScanner.Scan()
	s.err = s.curScanner.Err()

	if s.err != nil {
		return curMore
	}

	if !curMore {
		err := s.curFile.Close()
		if err != nil {
			s.err = err
			return false
		}
		s.curFile = nil

		more, err := s.nextFile()
		if err != nil {
			s.err = err
			return false
		}

		if !more {
			s.end = true
			return false
		}

		return s.Scan()
	}
	return true
}

// Err returns the first non-EOF error that was encountered by the
// Scanner.
H
Helin Wang 已提交
95
func (s *Scanner) Err() error {
H
Helin Wang 已提交
96 97 98 99
	return s.err
}

// Record returns the record under the current cursor.
H
Helin Wang 已提交
100
func (s *Scanner) Record() []byte {
H
Helin Wang 已提交
101 102 103 104 105 106 107 108
	if s.curScanner == nil {
		return nil
	}

	return s.curScanner.Record()
}

// Close release the resources.
H
Helin Wang 已提交
109
func (s *Scanner) Close() error {
H
Helin Wang 已提交
110 111 112 113 114 115 116 117 118
	s.curScanner = nil
	if s.curFile != nil {
		err := s.curFile.Close()
		s.curFile = nil
		return err
	}
	return nil
}

H
Helin Wang 已提交
119
func (s *Scanner) nextFile() (bool, error) {
H
Helin Wang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if s.pathIdx >= len(s.paths) {
		return false, nil
	}

	path := s.paths[s.pathIdx]
	s.pathIdx++
	f, err := os.Open(path)
	if err != nil {
		return false, err
	}

	idx, err := LoadIndex(f)
	if err != nil {
		f.Close()
		return false, err
	}

	s.curFile = f
H
Helin Wang 已提交
138
	s.curScanner = NewRangeScanner(f, idx, 0, -1)
H
Helin Wang 已提交
139 140
	return true, nil
}