forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHdfsReader.go
52 lines (44 loc) · 1.27 KB
/
HdfsReader.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package main
import (
"errors"
"github.com/colinmarc/hdfs"
)
// Allows to open an HDFS file as a seekable read-only stream
// Concurrency: not thread safe: at most on request at a time
type HdfsReader struct {
BackendReader *hdfs.FileReader
}
var _ ReadSeekCloser = (*HdfsReader)(nil) // ensure HdfsReader implements ReadSeekCloser
// Creates new instance of HdfsReader
func NewHdfsReader(backendReader *hdfs.FileReader) ReadSeekCloser {
return &HdfsReader{BackendReader: backendReader}
}
// Read a chunk of data
func (this *HdfsReader) Read(buffer []byte) (int, error) {
return this.BackendReader.Read(buffer)
}
// Seeks to a given position
func (this *HdfsReader) Seek(pos int64) error {
actualPos, err := this.BackendReader.Seek(pos, 0)
if err != nil {
return err
}
if pos != actualPos {
return errors.New("Can't seek to requested position")
}
return nil
}
// Returns current position
func (this *HdfsReader) Position() (int64, error) {
actualPos, err := this.BackendReader.Seek(0, 1)
if err != nil {
return 0, err
}
return actualPos, nil
}
// Closes the stream
func (this *HdfsReader) Close() error {
return this.BackendReader.Close()
}