NebulaGraph CPP Client  release-3.8
MetaClient.h
1 /* Copyright (c) 2020 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 <functional>
9 #include <memory>
10 #include <string>
11 #include <thread>
12 #include <unordered_map>
13 #include <vector>
14 
15 #include "common/datatypes/HostAddr.h"
16 #include "common/thrift/ThriftTypes.h"
17 #include "nebula/mclient/MConfig.h"
18 
19 struct pair_hash {
20  template <class T1, class T2>
21  std::size_t operator()(const std::pair<T1, T2> &pair) const {
22  return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
23  }
24 };
25 
26 namespace folly {
27 
28 class IOThreadPoolExecutor;
29 template <class T>
30 class Promise;
31 
32 } // namespace folly
33 
34 namespace nebula {
35 
36 namespace thrift {
37 
38 template <class ClientType>
40 
41 } // namespace thrift
42 
43 namespace meta {
44 namespace cpp2 {
45 
46 enum class ListHostType;
47 class HostItem;
48 class MetaServiceAsyncClient;
49 class ListSpacesReq;
50 class ListSpacesResp;
51 class IdName;
52 class EdgeItem;
53 class TagItem;
54 class ListEdgesReq;
55 class ListEdgesResp;
56 
57 } // namespace cpp2
58 } // namespace meta
59 
60 using SpaceIdName = std::pair<GraphSpaceID, std::string>;
61 using SpaceNameIdMap = std::unordered_map<std::string, GraphSpaceID>;
62 using SpaceEdgeNameTypeMap =
63  std::unordered_map<std::pair<GraphSpaceID, std::string>, EdgeType, pair_hash>;
64 
65 using SpaceTagNameTypeMap =
66  std::unordered_map<std::pair<GraphSpaceID, std::string>, TagID, pair_hash>;
67 
68 class MetaClient {
69  public:
70  explicit MetaClient(const std::vector<std::string> &metaAddrs,
71  const MConfig &mConfig = MConfig{});
72 
73  ~MetaClient();
74 
75  std::pair<bool, GraphSpaceID> getSpaceIdByNameFromCache(const std::string &name);
76 
77  std::pair<bool, EdgeType> getEdgeTypeByNameFromCache(GraphSpaceID spaceId,
78  const std::string &name);
79 
80  std::pair<bool, TagID> getTagIdByNameFromCache(GraphSpaceID spaceId, const std::string &name);
81 
82  std::pair<bool, std::vector<PartitionID>> getPartsFromCache(GraphSpaceID spaceId);
83 
84  std::pair<bool, HostAddr> getPartLeaderFromCache(GraphSpaceID spaceId, PartitionID partId);
85 
86  private:
87  bool loadData();
88 
89  std::pair<bool, std::vector<SpaceIdName>> listSpaces();
90 
91  std::pair<bool, std::vector<meta::cpp2::HostItem>> listHosts(meta::cpp2::ListHostType tp);
92 
93  std::pair<bool, std::vector<meta::cpp2::EdgeItem>> listEdgeSchemas(GraphSpaceID spaceId);
94 
95  std::pair<bool, std::vector<meta::cpp2::TagItem>> listTagSchemas(GraphSpaceID spaceId);
96 
97  void loadLeader(const std::vector<nebula::meta::cpp2::HostItem> &hostItems,
98  const SpaceNameIdMap &spaceIndexByName);
99 
100  std::vector<SpaceIdName> toSpaceIdName(const std::vector<meta::cpp2::IdName> &tIdNames);
101 
102  template <class Request,
103  class RemoteFunc,
104  class RespGenerator,
105  class RpcResponse = typename std::result_of<RemoteFunc(
106  std::shared_ptr<meta::cpp2::MetaServiceAsyncClient>, Request)>::type::value_type,
107  class Response = typename std::result_of<RespGenerator(RpcResponse)>::type>
108  void getResponse(Request req,
109  RemoteFunc remoteFunc,
110  RespGenerator respGen,
111  folly::Promise<std::pair<bool, Response>> pro);
112 
113  private:
114  std::vector<HostAddr> metaAddrs_;
115  MConfig mConfig_;
116  SpaceNameIdMap spaceIndexByName_;
117  SpaceEdgeNameTypeMap spaceEdgeIndexByName_;
118  SpaceTagNameTypeMap spaceTagIndexByName_;
119  std::unordered_map<std::pair<GraphSpaceID, PartitionID>, HostAddr, pair_hash> spacePartLeaderMap_;
120  std::unordered_map<GraphSpaceID, std::vector<PartitionID>> spacePartsMap_;
121  std::shared_ptr<folly::IOThreadPoolExecutor> ioExecutor_;
122  std::shared_ptr<thrift::ThriftClientManager<meta::cpp2::MetaServiceAsyncClient>> clientsMan_;
123 };
124 
125 } // namespace nebula