1 | /******************************************************************** |
2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
3 | * All rights reserved. |
4 | * |
5 | * Author: Zhanwei Wang |
6 | ********************************************************************/ |
7 | /******************************************************************** |
8 | * 2014 - |
9 | * open source under Apache License Version 2.0 |
10 | ********************************************************************/ |
11 | /** |
12 | * Licensed to the Apache Software Foundation (ASF) under one |
13 | * or more contributor license agreements. See the NOTICE file |
14 | * distributed with this work for additional information |
15 | * regarding copyright ownership. The ASF licenses this file |
16 | * to you under the Apache License, Version 2.0 (the |
17 | * "License"); you may not use this file except in compliance |
18 | * with the License. You may obtain a copy of the License at |
19 | * |
20 | * http://www.apache.org/licenses/LICENSE-2.0 |
21 | * |
22 | * Unless required by applicable law or agreed to in writing, software |
23 | * distributed under the License is distributed on an "AS IS" BASIS, |
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
25 | * See the License for the specific language governing permissions and |
26 | * limitations under the License. |
27 | */ |
28 | #ifndef _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_ |
29 | #define _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_ |
30 | |
31 | #include "BlockReader.h" |
32 | #include "Checksum.h" |
33 | #include "FileWrapper.h" |
34 | #include "Memory.h" |
35 | #include "ReadShortCircuitInfo.h" |
36 | #include "SessionConfig.h" |
37 | |
38 | #include <vector> |
39 | |
40 | namespace Hdfs { |
41 | namespace Internal { |
42 | |
43 | class LocalBlockReader: public BlockReader { |
44 | public: |
45 | LocalBlockReader(const shared_ptr<ReadShortCircuitInfo>& info, |
46 | const ExtendedBlock & block, int64_t offset, bool verify, |
47 | SessionConfig & conf, std::vector<char> & buffer); |
48 | |
49 | ~LocalBlockReader(); |
50 | |
51 | /** |
52 | * Get how many bytes can be read without blocking. |
53 | * @return The number of bytes can be read without blocking. |
54 | */ |
55 | virtual int64_t available() { |
56 | return length - cursor; |
57 | } |
58 | |
59 | /** |
60 | * To read data from block. |
61 | * @param buf the buffer used to filled. |
62 | * @param size the number of bytes to be read. |
63 | * @return return the number of bytes filled in the buffer, |
64 | * it may less than size. Return 0 if reach the end of block. |
65 | */ |
66 | virtual int32_t read(char * buf, int32_t size); |
67 | |
68 | /** |
69 | * Move the cursor forward len bytes. |
70 | * @param len The number of bytes to skip. |
71 | */ |
72 | virtual void skip(int64_t len); |
73 | |
74 | private: |
75 | /** |
76 | * Fill buffer and verify checksum. |
77 | * @param bufferSize The size of buffer. |
78 | */ |
79 | void readAndVerify(int32_t bufferSize); |
80 | int32_t readInternal(char * buf, int32_t len); |
81 | |
82 | private: |
83 | bool verify; //verify checksum or not. |
84 | const char * pbuffer; |
85 | const char * pMetaBuffer; |
86 | const ExtendedBlock & block; |
87 | int checksumSize; |
88 | int chunkSize; |
89 | int localBufferSize; |
90 | int position; //point in buffer. |
91 | int size; //data size in buffer. |
92 | int64_t cursor; //point in block. |
93 | int64_t length; //data size of block. |
94 | shared_ptr<Checksum> checksum; |
95 | shared_ptr<FileWrapper> dataFd; |
96 | shared_ptr<FileWrapper> metaFd; |
97 | shared_ptr<ReadShortCircuitInfo> info; |
98 | std::vector<char> & buffer; |
99 | std::vector<char> metaBuffer; |
100 | }; |
101 | |
102 | } |
103 | } |
104 | |
105 | #endif /* _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_ */ |
106 | |