NebulaGraph CPP Client  release-3.8
StorageClient.h
1 /* Copyright (c) 2018 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <memory>
9 #include <string>
10 #include <thread>
11 #include <unordered_map>
12 #include <vector>
13 
14 #include "ScanEdgeIter.h"
15 #include "common/datatypes/HostAddr.h"
16 #include "common/graph/Response.h"
17 #include "common/thrift/ThriftTypes.h"
18 #include "nebula/mclient/MetaClient.h"
19 #include "nebula/sclient/SConfig.h"
20 #include "nebula/sclient/ScanEdgeIter.h"
21 #include "nebula/sclient/ScanVertexIter.h"
22 
23 namespace folly {
24 class IOThreadPoolExecutor;
25 template <class T>
26 class Promise;
27 } // namespace folly
28 
29 namespace nebula {
30 
31 namespace thrift {
32 
33 template <class ClientType>
34 class ThriftClientManager;
35 
36 } // namespace thrift
37 
38 namespace meta {
39 namespace cpp2 {
40 
41 class MetaServiceAsyncClient;
42 
43 } // namespace cpp2
44 } // namespace meta
45 
46 namespace storage {
47 namespace cpp2 {
48 
49 class GraphStorageServiceAsyncClient;
50 class ScanCursor;
51 class ScanEdgeRequest;
52 class ScanResponse;
53 
54 } // namespace cpp2
55 } // namespace storage
56 
57 #define DEFAULT_LIMIT std::numeric_limits<int64_t>::max()
58 #define DEFAULT_START_TIME 0
59 #define DEFAULT_END_TIME std::numeric_limits<int64_t>::max()
60 
62  friend struct ScanEdgeIter;
63  friend struct ScanVertexIter;
64 
65  public:
66  explicit StorageClient(const std::vector<std::string>& metaAddrs,
67  const MConfig& mConfig = MConfig{},
68  const SConfig& sConfig = SConfig{});
69 
70  ~StorageClient();
71 
72  std::vector<PartitionID> getParts(const std::string& spaceName);
73 
74  ScanEdgeIter scanEdgeWithPart(std::string spaceName,
75  int32_t partID,
76  std::string edgeName,
77  std::vector<std::string> propNames,
78  int64_t limit = DEFAULT_LIMIT,
79  int64_t startTime = DEFAULT_START_TIME,
80  int64_t endTime = DEFAULT_END_TIME,
81  std::string filter = "",
82  bool onlyLatestVersion = false,
83  bool enableReadFromFollower = true,
84  bool needAuth = false,
85  const std::string& username = "",
86  const std::string& password = "");
87 
88  ScanVertexIter scanVertexWithPart(
89  std::string spaceName,
90  int32_t partID,
91  // tag name -> prop names
92  std::unordered_map<std::string, std::vector<std::string>> tagProps,
93  int64_t limit = DEFAULT_LIMIT,
94  int64_t startTime = DEFAULT_START_TIME,
95  int64_t endTime = DEFAULT_END_TIME,
96  std::string filter = "",
97  bool onlyLatestVersion = false,
98  bool enableReadFromFollower = true,
99  bool needAuth = false,
100  const std::string& username = "",
101  const std::string& password = "");
102 
103  MetaClient* getMetaClient() {
104  return mClient_.get();
105  }
106 
107  private:
108  std::pair<::nebula::ErrorCode, storage::cpp2::ScanResponse> doScanEdge(
109  const storage::cpp2::ScanEdgeRequest& req);
110 
111  std::pair<::nebula::ErrorCode, storage::cpp2::ScanResponse> doScanVertex(
112  const storage::cpp2::ScanVertexRequest& req);
113 
114  template <typename Request, typename RemoteFunc, typename Response>
115  void getResponse(std::pair<HostAddr, Request>&& request,
116  RemoteFunc&& remoteFunc,
117  folly::Promise<std::pair<::nebula::ErrorCode, Response>> pro);
118 
119  std::unique_ptr<MetaClient> mClient_;
120  SConfig sConfig_;
121  std::shared_ptr<folly::IOThreadPoolExecutor> ioExecutor_;
122  std::shared_ptr<thrift::ThriftClientManager<storage::cpp2::GraphStorageServiceAsyncClient>>
123  clientsMan_;
124 };
125 
126 } // namespace nebula