block_reader.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef BLOCK_READER_H_
  19. #define BLOCK_READER_H_
  20. #include "libhdfs++/options.h"
  21. #include "libhdfs++/status.h"
  22. #include "datatransfer.pb.h"
  23. #include <memory>
  24. namespace hdfs {
  25. template<class Stream>
  26. class RemoteBlockReader : public std::enable_shared_from_this<RemoteBlockReader<Stream> > {
  27. public:
  28. explicit RemoteBlockReader(const BlockReaderOptions &options,
  29. Stream *stream)
  30. : stream_(stream)
  31. , state_(kOpen)
  32. , options_(options)
  33. , chunk_padding_bytes_(0)
  34. {}
  35. template<class MutableBufferSequence, class ReadHandler>
  36. void async_read_some(const MutableBufferSequence& buffers,
  37. const ReadHandler &handler);
  38. template<class MutableBufferSequence>
  39. size_t read_some(const MutableBufferSequence &buffers, Status *status);
  40. Status connect(const std::string &client_name,
  41. const hadoop::common::TokenProto *token,
  42. const hadoop::hdfs::ExtendedBlockProto *block,
  43. uint64_t length, uint64_t offset);
  44. template<class ConnectHandler>
  45. void async_connect(const std::string &client_name,
  46. const hadoop::common::TokenProto *token,
  47. const hadoop::hdfs::ExtendedBlockProto *block,
  48. uint64_t length, uint64_t offset,
  49. const ConnectHandler &handler);
  50. private:
  51. struct ReadPacketHeader;
  52. struct ReadChecksum;
  53. struct ReadPadding;
  54. template<class MutableBufferSequence>
  55. struct ReadData;
  56. struct AckRead;
  57. enum State {
  58. kOpen,
  59. kReadPacketHeader,
  60. kReadChecksum,
  61. kReadPadding,
  62. kReadData,
  63. kFinished,
  64. };
  65. Stream *stream_;
  66. hadoop::hdfs::PacketHeaderProto header_;
  67. State state_;
  68. BlockReaderOptions options_;
  69. size_t packet_len_;
  70. int packet_data_read_bytes_;
  71. int chunk_padding_bytes_;
  72. long long bytes_to_read_;
  73. std::vector<char> checksum_;
  74. };
  75. }
  76. #include "remote_block_reader_impl.h"
  77. #endif