/
HugeClient.java
144 lines (122 loc) · 4.48 KB
/
HugeClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.baidu.hugegraph.driver;
import java.io.Closeable;
import javax.ws.rs.ProcessingException;
import com.baidu.hugegraph.client.RestClient;
import com.baidu.hugegraph.rest.ClientException;
import com.baidu.hugegraph.util.VersionUtil;
import com.baidu.hugegraph.version.ClientVersion;
public class HugeClient implements Closeable {
static {
ClientVersion.check();
}
private final RestClient client;
private VersionManager version;
private GraphsManager graphs;
private SchemaManager schema;
private GraphManager graph;
private GremlinManager gremlin;
private TraverserManager traverser;
private VariablesManager variable;
private JobManager job;
private TaskManager task;
private AuthManager auth;
private MetricsManager metrics;
public HugeClient(HugeClientBuilder builder) {
try {
this.client = new RestClient(builder.url(),
builder.username(),
builder.password(),
builder.timeout(),
builder.maxConns(),
builder.maxConnsPerRoute(),
builder.trustStoreFile(),
builder.trustStorePassword());
} catch (ProcessingException e) {
throw new ClientException("Failed to connect url '%s'", builder.url());
}
try {
this.initManagers(this.client, builder.graph());
} catch (Throwable e) {
this.client.close();
throw e;
}
}
public static HugeClientBuilder builder(String url, String graph) {
return new HugeClientBuilder(url, graph);
}
@Override
public void close() {
this.client.close();
}
private void initManagers(RestClient client, String graph) {
assert client != null;
// Check hugegraph-server api version
this.version = new VersionManager(client);
this.checkServerApiVersion();
this.graphs = new GraphsManager(client);
this.schema = new SchemaManager(client, graph);
this.graph = new GraphManager(client, graph);
this.gremlin = new GremlinManager(client, graph, this.graph);
this.traverser = new TraverserManager(client, this.graph);
this.variable = new VariablesManager(client, graph);
this.job = new JobManager(client, graph);
this.task = new TaskManager(client, graph);
this.auth = new AuthManager(client, graph);
this.metrics = new MetricsManager(client);
}
private void checkServerApiVersion() {
VersionUtil.Version apiVersion = VersionUtil.Version.of(
this.version.getApiVersion());
VersionUtil.check(apiVersion, "0.38", "0.60",
"hugegraph-api in server");
this.client.apiVersion(apiVersion);
}
public GraphsManager graphs() {
return this.graphs;
}
public SchemaManager schema() {
return this.schema;
}
public GraphManager graph() {
return this.graph;
}
public GremlinManager gremlin() {
return this.gremlin;
}
public TraverserManager traverser() {
return this.traverser;
}
public VariablesManager variables() {
return this.variable;
}
public JobManager job() {
return this.job;
}
public TaskManager task() {
return this.task;
}
public AuthManager auth() {
return this.auth;
}
public MetricsManager metrics() {
return this.metrics;
}
}