hdfspp.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 LIBHDFSPP_HDFSPP_H_
  19. #define LIBHDFSPP_HDFSPP_H_
  20. #include "hdfspp/options.h"
  21. #include "hdfspp/status.h"
  22. #include <functional>
  23. #include <memory>
  24. #include <set>
  25. #include <iostream>
  26. namespace hdfs {
  27. /**
  28. * An IoService manages a queue of asynchronous tasks. All libhdfs++
  29. * operations are filed against a particular IoService.
  30. *
  31. * When an operation is queued into an IoService, the IoService will
  32. * run the callback handler associated with the operation. Note that
  33. * the IoService must be stopped before destructing the objects that
  34. * file the operations.
  35. *
  36. * From an implementation point of view the IoService object wraps the
  37. * ::asio::io_service objects. Please see the related documentation
  38. * for more details.
  39. **/
  40. class IoService {
  41. public:
  42. static IoService *New();
  43. /**
  44. * Run the asynchronous tasks associated with this IoService.
  45. **/
  46. virtual void Run() = 0;
  47. /**
  48. * Stop running asynchronous tasks associated with this IoService.
  49. **/
  50. virtual void Stop() = 0;
  51. virtual ~IoService();
  52. };
  53. /**
  54. * A node exclusion rule provides a simple way of testing if the
  55. * client should attempt to connect to a node based on the node's
  56. * UUID. The FileSystem and FileHandle use the BadDataNodeTracker
  57. * by default. AsyncPreadSome takes an optional NodeExclusionRule
  58. * that will override the BadDataNodeTracker.
  59. **/
  60. class NodeExclusionRule {
  61. public:
  62. virtual ~NodeExclusionRule(){};
  63. virtual bool IsBadNode(const std::string &node_uuid) = 0;
  64. };
  65. /**
  66. * Applications opens a FileHandle to read files in HDFS.
  67. **/
  68. class FileHandle {
  69. public:
  70. /**
  71. * Read data from a specific position. The current implementation
  72. * stops at the block boundary.
  73. *
  74. * @param buf the pointer to the buffer
  75. * @param nbyte the size of the buffer
  76. * @param offset the offset the file
  77. *
  78. * The handler returns the datanode that serves the block and the number of
  79. * bytes has read.
  80. **/
  81. virtual void
  82. PositionRead(void *buf, size_t nbyte, uint64_t offset,
  83. const std::function<void(const Status &, size_t)> &handler) = 0;
  84. virtual Status PositionRead(void *buf, size_t *nbyte, off_t offset) = 0;
  85. virtual Status Read(void *buf, size_t *nbyte) = 0;
  86. virtual Status Seek(off_t *offset, std::ios_base::seekdir whence) = 0;
  87. /**
  88. * Determine if a datanode should be excluded from future operations
  89. * based on the return Status.
  90. *
  91. * @param status the Status object returned by InputStream::PositionRead
  92. * @return true if the status indicates a failure that is not recoverable
  93. * by the client and false otherwise.
  94. **/
  95. static bool ShouldExclude(const Status &status);
  96. virtual ~FileHandle();
  97. };
  98. /**
  99. * FileSystem implements APIs to interact with HDFS.
  100. **/
  101. class FileSystem {
  102. public:
  103. /**
  104. * Create a new instance of the FileSystem object. The call
  105. * initializes the RPC connections to the NameNode and returns an
  106. * FileSystem object.
  107. **/
  108. static FileSystem * New(
  109. IoService *&io_service, const Options &options);
  110. virtual void Connect(const std::string &server,
  111. const std::string &service,
  112. const std::function<void(const Status &, FileSystem *)> &&handler) = 0;
  113. /* Synchronous call of Connect */
  114. virtual Status Connect(const std::string &server,
  115. const std::string &service) = 0;
  116. /**
  117. * Open a file on HDFS. The call issues an RPC to the NameNode to
  118. * gather the locations of all blocks in the file and to return a
  119. * new instance of the @ref InputStream object.
  120. **/
  121. virtual void
  122. Open(const std::string &path,
  123. const std::function<void(const Status &, FileHandle *)> &handler) = 0;
  124. virtual Status Open(const std::string &path, FileHandle **handle) = 0;
  125. /**
  126. * Note that it is an error to destroy the filesystem from within a filesystem
  127. * callback. It will lead to a deadlock and the termination of the process.
  128. */
  129. virtual ~FileSystem() {};
  130. };
  131. }
  132. #endif