Skip to content

Commit 27557b9

Browse files
author
mpv1989
committed
refactor role awareness
1 parent f6f2e5f commit 27557b9

File tree

7 files changed

+55
-30
lines changed

7 files changed

+55
-30
lines changed

src/main/java/com/arangodb/ArangoDB.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
import javax.net.ssl.SSLContext;
3232

3333
import com.arangodb.entity.ArangoDBVersion;
34-
import com.arangodb.entity.ArangoDBRole;
3534
import com.arangodb.entity.LogEntity;
3635
import com.arangodb.entity.LogLevelEntity;
36+
import com.arangodb.entity.ServerRole;
3737
import com.arangodb.entity.UserEntity;
3838
import com.arangodb.internal.ArangoDBConstants;
3939
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
@@ -457,13 +457,11 @@ public ArangoDBVersion getVersion() throws ArangoDBException {
457457
/**
458458
* Returns the server role.
459459
*
460-
* @see <a href="https://docs.arangodb.com/current/HTTP/MiscellaneousFunctions/index.html#return-server-version">API
461-
* Documentation</a>
462460
* @return the server role
463461
* @throws ArangoDBException
464462
*/
465-
public ArangoDBRole getRole() throws ArangoDBException {
466-
return db().getRole();
463+
public ServerRole getRole() throws ArangoDBException {
464+
return executor.execute(getRoleRequest(), getRoleResponseDeserializer());
467465
}
468466

469467
/**

src/main/java/com/arangodb/ArangoDatabase.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.arangodb.entity.AqlFunctionEntity;
2828
import com.arangodb.entity.AqlParseEntity;
2929
import com.arangodb.entity.ArangoDBVersion;
30-
import com.arangodb.entity.ArangoDBRole;
3130
import com.arangodb.entity.CollectionEntity;
3231
import com.arangodb.entity.CursorEntity;
3332
import com.arangodb.entity.DatabaseEntity;
@@ -88,18 +87,6 @@ public ArangoDBVersion getVersion() throws ArangoDBException {
8887
return executor.execute(getVersionRequest(), ArangoDBVersion.class);
8988
}
9089

91-
/**
92-
* Returns the server name and version number.
93-
*
94-
* @see <a href="https://docs.arangodb.com/current/HTTP/MiscellaneousFunctions/index.html#return-server-version">API
95-
* Documentation</a>
96-
* @return the server version, number
97-
* @throws ArangoDBException
98-
*/
99-
public ArangoDBRole getRole() throws ArangoDBException {
100-
return executor.execute(getRoleRequest(), ArangoDBRole.class);
101-
}
102-
10390
/**
10491
* Retrieves a list of all databases the current user can access
10592
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* 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+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.entity;
22+
23+
/**
24+
* @author Mark - mark at arangodb.com
25+
*
26+
*/
27+
public enum ServerRole {
28+
SINGLE, AGENT, COORDINATOR, PRIMARY
29+
}

src/main/java/com/arangodb/internal/ArangoDBConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ArangoDBConstants {
6161
public static final String PATH_API_ADMIN_LOG_LEVEL = "/_admin/log/level";
6262
public static final String PATH_API_ADMIN_ROUTING_RELOAD = "/_admin/routing/reload";
6363
public static final String PATH_API_IMPORT = "/_api/import";
64-
public static final String PATH_API_ROLE = "_admin/server/role";
64+
public static final String PATH_API_ROLE = "/_admin/server/role";
6565

6666
public static final String ENCRYPTION_PLAIN = "plain";
6767

@@ -113,5 +113,6 @@ public class ArangoDBConstants {
113113
public static final String DETAILS = "details";
114114
public static final String TYPE = "type";
115115
public static final String IS_SYSTEM = "isSystem";
116+
public static final String ROLE = "role";
116117

117118
}

src/main/java/com/arangodb/internal/InternalArangoDB.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.arangodb.ArangoDBException;
3030
import com.arangodb.entity.LogLevelEntity;
31+
import com.arangodb.entity.ServerRole;
3132
import com.arangodb.entity.UserEntity;
3233
import com.arangodb.internal.ArangoExecutor.ResponseDeserializer;
3334
import com.arangodb.internal.velocystream.Connection;
@@ -131,6 +132,19 @@ private static <T> String getProperty(
131132
currentValue != null ? currentValue.toString() : defaultValue != null ? defaultValue.toString() : null);
132133
}
133134

135+
protected Request getRoleRequest() {
136+
return new Request(ArangoDBConstants.SYSTEM, RequestType.GET, ArangoDBConstants.PATH_API_ROLE);
137+
}
138+
139+
protected ResponseDeserializer<ServerRole> getRoleResponseDeserializer() {
140+
return new ResponseDeserializer<ServerRole>() {
141+
@Override
142+
public ServerRole deserialize(final Response response) throws VPackException {
143+
return util().deserialize(response.getBody().get(ArangoDBConstants.ROLE), ServerRole.class);
144+
}
145+
};
146+
}
147+
134148
protected Request createDatabaseRequest(final String name) {
135149
final Request request = new Request(ArangoDBConstants.SYSTEM, RequestType.POST,
136150
ArangoDBConstants.PATH_API_DATABASE);

src/main/java/com/arangodb/internal/InternalArangoDatabase.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ protected Request getVersionRequest() {
9292
return new Request(name, RequestType.GET, ArangoDBConstants.PATH_API_VERSION);
9393
}
9494

95-
protected Request getRoleRequest() {
96-
return new Request(name, RequestType.GET, ArangoDBConstants.PATH_API_ROLE);
97-
}
98-
9995
protected Request createCollectionRequest(final String name, final CollectionCreateOptions options) {
10096
return new Request(name(), RequestType.POST, ArangoDBConstants.PATH_API_COLLECTION).setBody(
10197
executor.serialize(OptionsBuilder.build(options != null ? options : new CollectionCreateOptions(), name)));

src/test/java/com/arangodb/ArangoDatabaseTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import com.arangodb.entity.AqlParseEntity;
5050
import com.arangodb.entity.AqlParseEntity.AstNode;
5151
import com.arangodb.entity.ArangoDBVersion;
52-
import com.arangodb.entity.ArangoDBRole;
5352
import com.arangodb.entity.BaseDocument;
5453
import com.arangodb.entity.BaseEdgeDocument;
5554
import com.arangodb.entity.CollectionEntity;
@@ -62,6 +61,7 @@
6261
import com.arangodb.entity.QueryCachePropertiesEntity.CacheMode;
6362
import com.arangodb.entity.QueryEntity;
6463
import com.arangodb.entity.QueryTrackingPropertiesEntity;
64+
import com.arangodb.entity.ServerRole;
6565
import com.arangodb.entity.TraversalEntity;
6666
import com.arangodb.model.AqlFunctionDeleteOptions;
6767
import com.arangodb.model.AqlQueryOptions;
@@ -125,9 +125,9 @@ public void deleteCollection() {
125125

126126
@Test
127127
public void deleteSystemCollection() {
128-
if (db.getRole().getRole() == "COORDINATOR") {
129-
return;
130-
}
128+
if (arangoDB.getRole() != ServerRole.SINGLE) {
129+
return;
130+
}
131131
final String name = "_system_test";
132132
db.createCollection(name, new CollectionCreateOptions().isSystem(true));
133133
db.collection(name).drop(true);
@@ -140,9 +140,9 @@ public void deleteSystemCollection() {
140140

141141
@Test
142142
public void deleteSystemCollectionFail() {
143-
if (db.getRole().getRole() == "COORDINATOR") {
144-
return;
145-
}
143+
if (arangoDB.getRole() != ServerRole.SINGLE) {
144+
return;
145+
}
146146
final String name = "_system_test";
147147
db.createCollection(name, new CollectionCreateOptions().isSystem(true));
148148
try {

0 commit comments

Comments
 (0)