SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Read.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #ifndef swcdb_fsbroker_handlers_Read_h
8 #define swcdb_fsbroker_handlers_Read_h
9 
11 
12 
13 namespace SWC { namespace Comm { namespace Protocol {
14 namespace FsBroker { namespace Handler {
15 
16 
17 void read(const ConnHandlerPtr& conn, const Event::Ptr& ev) {
18 
19  int err = Error::OK;
20  size_t offset = 0;
21  StaticBuffer rbuf;
22  try {
23 
24  const uint8_t *ptr = ev->data.base;
25  size_t remain = ev->data.size;
26 
27  Params::ReadReq params;
28  params.decode(&ptr, &remain);
29 
30  auto smartfd = Env::FsBroker::fds().select(params.fd);
31 
32  if(!smartfd) {
33  err = EBADR;
34  } else {
35  offset = smartfd->pos();
36  rbuf.reallocate(params.amount);
37  rbuf.size = Env::FsInterface::fs()->read(
38  err, smartfd, rbuf.base, params.amount);
39  }
40 
41  } catch(...) {
44  err = e.code();
45  }
46 
47  auto cbp = Buffers::make(ev, Params::ReadRsp(offset), rbuf, 4);
48  cbp->append_i32(err);
49  conn->send_response(cbp);
50 
51 }
52 
53 
54 }}}}}
55 
56 #endif // swcdb_fsbroker_handlers_Read_h
SWC::Error::Exception::code
constexpr SWC_CAN_INLINE int code() const noexcept
Definition: Exception.h:51
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::Core::Buffer::reallocate
SWC_CAN_INLINE void reallocate(size_t len)
Definition: Buffer.h:92
SWC_LOG_OUT
#define SWC_LOG_OUT(pr, _code_)
Definition: Logger.h:178
SWC::Comm::Protocol::FsBroker::Handler::read
void read(const ConnHandlerPtr &conn, const Event::Ptr &ev)
Definition: Read.h:17
SWC::Comm::Protocol::FsBroker::Params::ReadReq
Definition: Read.h:17
SWC::FsBroker::Fds::select
SWC_CAN_INLINE FS::SmartFd::Ptr select(int32_t fd) noexcept
Definition: FsBrokerEnv.h:33
Read.h
SWC::Error::OK
@ OK
Definition: Error.h:45
SWC::Comm::Protocol::FsBroker::Params::ReadReq::amount
uint32_t amount
Definition: Read.h:29
SWC_CURRENT_EXCEPTION
#define SWC_CURRENT_EXCEPTION(_msg_)
Definition: Exception.h:119
SWC::Comm::Protocol::FsBroker::Params::ReadRsp
Definition: Read.h:43
SWC::Core::Buffer::base
value_type * base
Definition: Buffer.h:131
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::Buffer
Definition: Buffer.h:18
SWC::Core::Buffer::size
size_t size
Definition: Buffer.h:130
SWC::Env::FsInterface::fs
static SWC_CAN_INLINE FS::FileSystem::Ptr & fs() noexcept
Definition: Interface.h:155
SWC::LOG_ERROR
@ LOG_ERROR
Definition: Logger.h:32
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
SWC::Comm::Buffers::make
static SWC_CAN_INLINE Ptr make(uint32_t reserve=0)
Definition: Buffers.h:27
SWC::Comm::Serializable::decode
void decode(const uint8_t **bufp, size_t *remainp)
Definition: Serializable.h:59
SWC::Comm::Event::Ptr
std::shared_ptr< Event > Ptr
Definition: Event.h:33
SWC::Env::FsBroker::fds
static SWC_CAN_INLINE SWC::FsBroker::Fds & fds() noexcept
Definition: FsBrokerEnv.h:70
SWC::Error::Exception
Definition: Exception.h:21
SWC::Comm::Protocol::FsBroker::Params::ReadReq::fd
int32_t fd
Definition: Read.h:28