forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_reader.go
348 lines (291 loc) · 7.74 KB
/
file_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package hdfs
import (
"crypto/md5"
"errors"
"fmt"
"io"
"os"
hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs"
"github.com/colinmarc/hdfs/rpc"
"github.com/golang/protobuf/proto"
)
const maxReadDir = 1024
// A FileReader represents an existing file or directory in HDFS. It implements
// io.Reader, io.ReaderAt, io.Seeker, and io.Closer, and can only be used for
// reads. For writes, see FileWriter and Client.Create.
type FileReader struct {
client *SimpleClient
name string
info os.FileInfo
blocks []*hdfs.LocatedBlockProto
blockReader *rpc.BlockReader
offset int64
readdirLast string
closed bool
}
// Open returns an FileReader which can be used for reading.
func (c *SimpleClient) Open(name string) (*FileReader, error) {
info, err := c.getFileInfo(name)
if err != nil {
return nil, &os.PathError{"open", name, err}
}
return &FileReader{
client: c,
name: name,
info: info,
closed: false,
}, nil
}
// Name returns the name of the file.
func (f *FileReader) Name() string {
return f.info.Name()
}
// Stat returns the FileInfo structure describing file.
func (f *FileReader) Stat() os.FileInfo {
return f.info
}
// Checksum returns HDFS's internal "MD5MD5CRC32C" checksum for a given file.
//
// Internally to HDFS, it works by calculating the MD5 of all the CRCs (which
// are stored alongside the data) for each block, and then calculating the MD5
// of all of those.
func (f *FileReader) Checksum() ([]byte, error) {
if f.info.IsDir() {
return nil, &os.PathError{
"checksum",
f.name,
errors.New("is a directory"),
}
}
if f.blocks == nil {
err := f.getBlocks()
if err != nil {
return nil, err
}
}
// The way the hadoop code calculates this, it writes all the checksums out to
// a byte array, which is automatically padded with zeroes out to the next
// power of 2 (with a minimum of 32)... and then takes the MD5 of that array,
// including the zeroes. This is pretty shady business, but we want to track
// the 'hadoop fs -checksum' behavior if possible.
paddedLength := 32
totalLength := 0
checksum := md5.New()
for _, block := range f.blocks {
cr := rpc.NewChecksumReader(block)
blockChecksum, err := cr.ReadChecksum()
if err != nil {
return nil, err
}
checksum.Write(blockChecksum)
totalLength += len(blockChecksum)
if paddedLength < totalLength {
paddedLength *= 2
}
}
checksum.Write(make([]byte, paddedLength-totalLength))
return checksum.Sum(nil), nil
}
// Seek implements io.Seeker.
//
// The seek is virtual - it starts a new block read at the new position.
func (f *FileReader) Seek(offset int64, whence int) (int64, error) {
if f.closed {
return 0, io.ErrClosedPipe
}
var off int64
if whence == 0 {
off = offset
} else if whence == 1 {
off = f.offset + offset
} else if whence == 2 {
off = f.info.Size() + offset
} else {
return f.offset, fmt.Errorf("Invalid whence: %d", whence)
}
if off < 0 || off > f.info.Size() {
return f.offset, fmt.Errorf("Invalid resulting offset: %d", off)
}
if f.offset != off {
f.offset = off
if f.blockReader != nil {
f.blockReader.Close()
f.blockReader = nil
}
}
return f.offset, nil
}
// Read implements io.Reader.
func (f *FileReader) Read(b []byte) (int, error) {
if f.closed {
return 0, io.ErrClosedPipe
}
if f.info.IsDir() {
return 0, &os.PathError{
"read",
f.name,
errors.New("is a directory"),
}
}
if f.offset >= f.info.Size() {
return 0, io.EOF
}
if f.blocks == nil {
err := f.getBlocks()
if err != nil {
return 0, err
}
}
if f.blockReader == nil {
err := f.getNewBlockReader()
if err != nil {
return 0, err
}
}
for {
n, err := f.blockReader.Read(b)
f.offset += int64(n)
if err != nil && err != io.EOF {
f.blockReader.Close()
f.blockReader = nil
return n, err
} else if n > 0 {
return n, nil
} else {
f.blockReader.Close()
f.getNewBlockReader()
}
}
}
// ReadAt implements io.ReaderAt.
func (f *FileReader) ReadAt(b []byte, off int64) (int, error) {
if f.closed {
return 0, io.ErrClosedPipe
}
if off < 0 {
return 0, &os.PathError{"readat", f.name, errors.New("negative offset")}
}
_, err := f.Seek(off, 0)
if err != nil {
return 0, err
}
n, err := io.ReadFull(f, b)
// For some reason, os.File.ReadAt returns io.EOF in this case instead of
// io.ErrUnexpectedEOF.
if err == io.ErrUnexpectedEOF {
err = io.EOF
}
return n, err
}
// Readdir reads the contents of the directory associated with file and returns
// a slice of up to n os.FileInfo values, as would be returned by Stat, in
// directory order. Subsequent calls on the same file will yield further
// os.FileInfos.
//
// If n > 0, Readdir returns at most n os.FileInfo values. In this case, if
// Readdir returns an empty slice, it will return a non-nil error explaining
// why. At the end of a directory, the error is io.EOF.
//
// If n <= 0, Readdir returns all the os.FileInfo from the directory in a single
// slice. In this case, if Readdir succeeds (reads all the way to the end of
// the directory), it returns the slice and a nil error. If it encounters an
// error before the end of the directory, Readdir returns the os.FileInfo read
// until that point and a non-nil error.
func (f *FileReader) Readdir(n int) ([]os.FileInfo, error) {
if f.closed {
return nil, io.ErrClosedPipe
}
if !f.info.IsDir() {
return nil, &os.PathError{
"readdir",
f.name,
errors.New("the file is not a directory"),
}
}
if n <= 0 {
f.readdirLast = ""
}
res := make([]os.FileInfo, 0)
for {
k := n - len(res)
if n <= 0 || k > maxReadDir {
k = maxReadDir
}
batch, err := f.client.getDirList(f.name, f.readdirLast, k)
if err != nil {
return nil, err
}
if len(batch) > 0 {
f.readdirLast = batch[len(batch)-1].Name()
}
res = append(res, batch...)
if len(batch) < k || (n > 0 && len(res) == n) {
break
}
}
var err error
if len(res) == 0 {
err = io.EOF
}
return res, err
}
// Readdirnames reads and returns a slice of names from the directory f.
//
// If n > 0, Readdirnames returns at most n names. In this case, if Readdirnames
// returns an empty slice, it will return a non-nil error explaining why. At the
// end of a directory, the error is io.EOF.
//
// If n <= 0, Readdirnames returns all the names from the directory in a single
// slice. In this case, if Readdirnames succeeds (reads all the way to the end
// of the directory), it returns the slice and a nil error. If it encounters an
// error before the end of the directory, Readdirnames returns the names read
// until that point and a non-nil error.
func (f *FileReader) Readdirnames(n int) ([]string, error) {
if f.closed {
return nil, io.ErrClosedPipe
}
fis, err := f.Readdir(n)
if err != nil {
return nil, err
}
names := make([]string, 0, len(fis))
for _, fi := range fis {
names = append(names, fi.Name())
}
return names, nil
}
// Close implements io.Closer.
func (f *FileReader) Close() error {
f.closed = true
if f.blockReader != nil {
f.blockReader.Close()
}
return nil
}
func (f *FileReader) getBlocks() error {
req := &hdfs.GetBlockLocationsRequestProto{
Src: proto.String(f.name),
Offset: proto.Uint64(0),
Length: proto.Uint64(uint64(f.info.Size())),
}
resp := &hdfs.GetBlockLocationsResponseProto{}
err := f.client.namenode.Execute("getBlockLocations", req, resp)
if err != nil {
return err
}
f.blocks = resp.GetLocations().GetBlocks()
return nil
}
func (f *FileReader) getNewBlockReader() error {
off := uint64(f.offset)
for _, block := range f.blocks {
start := block.GetOffset()
end := start + block.GetB().GetNumBytes()
if start <= off && off < end {
br := rpc.NewBlockReader(block, int64(off-start), f.client.namenode.ClientName())
f.blockReader = br
return nil
}
}
return fmt.Errorf("Couldn't find block for offset: %d", off)
}