Skip to content

Commit acbcf89

Browse files
julienbourdeauaseure
authored andcommitted
Added: Support for Multi Cluster Management (MCM)
1 parent d7d78b5 commit acbcf89

File tree

16 files changed

+721
-0
lines changed

16 files changed

+721
-0
lines changed

algoliasearch-common/src/main/java/com/algolia/search/APIClient.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.search;
22

3+
import com.algolia.search.exceptions.AlgoliaEncodingException;
34
import com.algolia.search.exceptions.AlgoliaException;
45
import com.algolia.search.exceptions.AlgoliaIndexNotFoundException;
56
import com.algolia.search.http.AlgoliaHttpClient;
@@ -18,6 +19,8 @@
1819
import com.algolia.search.responses.*;
1920
import com.google.common.base.Preconditions;
2021
import com.google.common.collect.ImmutableMap;
22+
import java.io.UnsupportedEncodingException;
23+
import java.net.URLEncoder;
2124
import java.util.*;
2225
import java.util.stream.Collectors;
2326
import javax.annotation.Nonnull;
@@ -1435,6 +1438,124 @@ public ABTests getABTests(int offset, int limit) throws AlgoliaException {
14351438
"limit", Integer.toString(limit))));
14361439
}
14371440

1441+
public List<Cluster> listClusters() throws AlgoliaException {
1442+
return this.listClusters(RequestOptions.empty);
1443+
}
1444+
1445+
public List<Cluster> listClusters(@Nonnull RequestOptions requestOptions)
1446+
throws AlgoliaException {
1447+
Clusters result =
1448+
httpClient.requestWithRetry(
1449+
new AlgoliaRequest<>(
1450+
HttpMethod.GET,
1451+
AlgoliaRequestKind.SEARCH_API_READ,
1452+
Arrays.asList("1", "clusters"),
1453+
requestOptions,
1454+
Clusters.class));
1455+
1456+
return result.getClusters();
1457+
}
1458+
1459+
public UserIDs listUserIDs() throws AlgoliaException {
1460+
return this.listUserIDs(0, 20);
1461+
}
1462+
1463+
public UserIDs listUserIDs(Integer page, Integer hitsPerPage) throws AlgoliaException {
1464+
return this.listUserIDs(page, hitsPerPage, RequestOptions.empty);
1465+
}
1466+
1467+
public UserIDs listUserIDs(
1468+
@Nonnull Integer page, @Nonnull Integer hitsPerPage, @Nonnull RequestOptions requestOptions)
1469+
throws AlgoliaException {
1470+
return httpClient.requestWithRetry(
1471+
new AlgoliaRequest<>(
1472+
HttpMethod.GET,
1473+
AlgoliaRequestKind.SEARCH_API_READ,
1474+
Arrays.asList("1", "clusters", "mapping"),
1475+
requestOptions,
1476+
UserIDs.class)
1477+
.setParameters(
1478+
ImmutableMap.of("page", page.toString(), "hitsPerPage", hitsPerPage.toString())));
1479+
}
1480+
1481+
public Map<String, List<UserID>> getTopUserID() throws AlgoliaException {
1482+
return this.getTopUserID(RequestOptions.empty);
1483+
}
1484+
1485+
public Map<String, List<UserID>> getTopUserID(RequestOptions requestOptions)
1486+
throws AlgoliaException {
1487+
TopUserResult result =
1488+
httpClient.requestWithRetry(
1489+
new AlgoliaRequest<>(
1490+
HttpMethod.GET,
1491+
AlgoliaRequestKind.SEARCH_API_READ,
1492+
Arrays.asList("1", "clusters", "mapping", "top"),
1493+
requestOptions,
1494+
TopUserResult.class));
1495+
1496+
return result.getTopUsers();
1497+
}
1498+
1499+
public AssignUserID assignUserID(@Nonnull String userID, @Nonnull String clusterName)
1500+
throws AlgoliaException {
1501+
return this.assignUserID(userID, clusterName, RequestOptions.empty);
1502+
}
1503+
1504+
public AssignUserID assignUserID(
1505+
@Nonnull String userID, @Nonnull String clusterName, RequestOptions requestOptions)
1506+
throws AlgoliaException {
1507+
requestOptions.addExtraHeader("X-Algolia-User-ID", userID);
1508+
1509+
return httpClient.requestWithRetry(
1510+
new AlgoliaRequest<>(
1511+
HttpMethod.POST,
1512+
AlgoliaRequestKind.SEARCH_API_WRITE,
1513+
Arrays.asList("1", "clusters", "mapping"),
1514+
requestOptions,
1515+
AssignUserID.class)
1516+
.setData(ImmutableMap.of("cluster", clusterName)));
1517+
}
1518+
1519+
public UserID getUserID(@Nonnull String userID) throws AlgoliaException {
1520+
return this.getUserID(userID, RequestOptions.empty);
1521+
}
1522+
1523+
public UserID getUserID(@Nonnull String userID, RequestOptions requestOptions)
1524+
throws AlgoliaException {
1525+
String encodedUserID = "";
1526+
1527+
try {
1528+
encodedUserID = URLEncoder.encode(userID, "UTF-8");
1529+
} catch (UnsupportedEncodingException e) {
1530+
throw new AlgoliaEncodingException("cannot encode given userID", e);
1531+
}
1532+
1533+
return httpClient.requestWithRetry(
1534+
new AlgoliaRequest<>(
1535+
HttpMethod.GET,
1536+
AlgoliaRequestKind.SEARCH_API_READ,
1537+
Arrays.asList("1", "clusters", "mapping", encodedUserID),
1538+
requestOptions,
1539+
UserID.class));
1540+
}
1541+
1542+
public DeleteUserID removeUserID(@Nonnull String userID) throws AlgoliaException {
1543+
return this.removeUserID(userID, RequestOptions.empty);
1544+
}
1545+
1546+
public DeleteUserID removeUserID(@Nonnull String userID, RequestOptions requestOptions)
1547+
throws AlgoliaException {
1548+
requestOptions.addExtraHeader("X-Algolia-User-ID", userID);
1549+
1550+
return httpClient.requestWithRetry(
1551+
new AlgoliaRequest<>(
1552+
HttpMethod.DELETE,
1553+
AlgoliaRequestKind.SEARCH_API_WRITE,
1554+
Arrays.asList("1", "clusters", "mapping"),
1555+
requestOptions,
1556+
DeleteUserID.class));
1557+
}
1558+
14381559
/** Used internally for deleteByQuery */
14391560
private static class ObjectID {
14401561

algoliasearch-common/src/main/java/com/algolia/search/AsyncAPIClient.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.algolia.search;
22

3+
import com.algolia.search.exceptions.AlgoliaEncodingException;
34
import com.algolia.search.exceptions.AlgoliaException;
45
import com.algolia.search.exceptions.AlgoliaIndexNotFoundException;
56
import com.algolia.search.http.AlgoliaRequest;
@@ -17,6 +18,8 @@
1718
import com.algolia.search.responses.*;
1819
import com.google.common.base.Preconditions;
1920
import com.google.common.collect.ImmutableMap;
21+
import java.io.UnsupportedEncodingException;
22+
import java.net.URLEncoder;
2023
import java.util.*;
2124
import java.util.concurrent.CancellationException;
2225
import java.util.concurrent.CompletableFuture;
@@ -1357,4 +1360,118 @@ CompletableFuture<ABTests> getABTests(int offset, int limit) {
13571360
"offset", Integer.toString(offset),
13581361
"limit", Integer.toString(limit))));
13591362
}
1363+
1364+
public CompletableFuture<List<Cluster>> listClusters() {
1365+
return this.listClusters(RequestOptions.empty);
1366+
}
1367+
1368+
public CompletableFuture<List<Cluster>> listClusters(@Nonnull RequestOptions requestOptions) {
1369+
CompletableFuture<Clusters> result =
1370+
httpClient.requestWithRetry(
1371+
new AlgoliaRequest<>(
1372+
HttpMethod.GET,
1373+
AlgoliaRequestKind.SEARCH_API_READ,
1374+
Arrays.asList("1", "clusters"),
1375+
requestOptions,
1376+
Clusters.class));
1377+
1378+
return result.thenApply(Clusters::getClusters);
1379+
}
1380+
1381+
public CompletableFuture<UserIDs> listUserIDs() {
1382+
return this.listUserIDs(0, 20, new RequestOptions());
1383+
}
1384+
1385+
public CompletableFuture<UserIDs> listUserIDs(Integer page, Integer hitsPerPage) {
1386+
return this.listUserIDs(page, hitsPerPage, RequestOptions.empty);
1387+
}
1388+
1389+
public CompletableFuture<UserIDs> listUserIDs(
1390+
@Nonnull Integer page, @Nonnull Integer hitsPerPage, @Nonnull RequestOptions requestOptions) {
1391+
return httpClient.requestWithRetry(
1392+
new AlgoliaRequest<>(
1393+
HttpMethod.GET,
1394+
AlgoliaRequestKind.SEARCH_API_READ,
1395+
Arrays.asList("1", "clusters", "mapping"),
1396+
requestOptions,
1397+
UserIDs.class)
1398+
.setParameters(
1399+
ImmutableMap.of("page", page.toString(), "hitsPerPage", hitsPerPage.toString())));
1400+
}
1401+
1402+
public CompletableFuture<Map<String, List<UserID>>> getTopUserID() {
1403+
return this.getTopUserID(RequestOptions.empty);
1404+
}
1405+
1406+
public CompletableFuture<Map<String, List<UserID>>> getTopUserID(RequestOptions requestOptions) {
1407+
CompletableFuture<TopUserResult> result =
1408+
httpClient.requestWithRetry(
1409+
new AlgoliaRequest<>(
1410+
HttpMethod.GET,
1411+
AlgoliaRequestKind.SEARCH_API_READ,
1412+
Arrays.asList("1", "clusters", "mapping", "top"),
1413+
requestOptions,
1414+
TopUserResult.class));
1415+
1416+
return result.thenApply(TopUserResult::getTopUsers);
1417+
}
1418+
1419+
public CompletableFuture<AssignUserID> assignUserID(@Nonnull String userID, @Nonnull String clusterName) {
1420+
return this.assignUserID(userID, clusterName, RequestOptions.empty);
1421+
}
1422+
1423+
public CompletableFuture<AssignUserID> assignUserID(
1424+
@Nonnull String userID, @Nonnull String clusterName, RequestOptions requestOptions) {
1425+
requestOptions.addExtraHeader("X-Algolia-User-ID", userID);
1426+
1427+
return httpClient.requestWithRetry(
1428+
new AlgoliaRequest<>(
1429+
HttpMethod.POST,
1430+
AlgoliaRequestKind.SEARCH_API_WRITE,
1431+
Arrays.asList("1", "clusters", "mapping"),
1432+
requestOptions,
1433+
AssignUserID.class)
1434+
.setData(ImmutableMap.of("cluster", clusterName)));
1435+
}
1436+
1437+
public CompletableFuture<UserID> getUserID(@Nonnull String userID) {
1438+
return this.getUserID(userID, RequestOptions.empty);
1439+
}
1440+
1441+
public CompletableFuture<UserID> getUserID(
1442+
@Nonnull String userID, RequestOptions requestOptions) {
1443+
String encodedUserID = "";
1444+
1445+
try {
1446+
encodedUserID = URLEncoder.encode(userID, "UTF-8");
1447+
} catch (UnsupportedEncodingException e) {
1448+
CompletableFuture.supplyAsync(
1449+
() -> new AlgoliaEncodingException("cannot encode given userID", e));
1450+
}
1451+
1452+
return httpClient.requestWithRetry(
1453+
new AlgoliaRequest<>(
1454+
HttpMethod.GET,
1455+
AlgoliaRequestKind.SEARCH_API_READ,
1456+
Arrays.asList("1", "clusters", "mapping", encodedUserID),
1457+
requestOptions,
1458+
UserID.class));
1459+
}
1460+
1461+
public CompletableFuture<DeleteUserID> removeUserID(@Nonnull String userID) {
1462+
return this.removeUserID(userID, RequestOptions.empty);
1463+
}
1464+
1465+
public CompletableFuture<DeleteUserID> removeUserID(
1466+
@Nonnull String userID, RequestOptions requestOptions) {
1467+
requestOptions.addExtraHeader("X-Algolia-User-ID", userID);
1468+
1469+
return httpClient.requestWithRetry(
1470+
new AlgoliaRequest<>(
1471+
HttpMethod.DELETE,
1472+
AlgoliaRequestKind.SEARCH_API_WRITE,
1473+
Arrays.asList("1", "clusters", "mapping"),
1474+
requestOptions,
1475+
DeleteUserID.class));
1476+
}
13601477
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.algolia.search.exceptions;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public class AlgoliaEncodingException extends AlgoliaException {
7+
public AlgoliaEncodingException(String message) {
8+
super(message);
9+
}
10+
11+
public AlgoliaEncodingException(String message, Exception e) {
12+
super(message, e);
13+
}
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.algolia.search.objects;
2+
3+
import java.io.Serializable;
4+
5+
public class Cluster implements Serializable {
6+
7+
private String clusterName;
8+
9+
private Long nbRecords;
10+
11+
private Integer nbUserIDs;
12+
13+
private Long dataSize;
14+
15+
public String getClusterName() {
16+
return clusterName;
17+
}
18+
19+
public Long getNbRecords() {
20+
return nbRecords;
21+
}
22+
23+
public Integer getNbUserIDs() {
24+
return nbUserIDs;
25+
}
26+
27+
public Long getDataSize() {
28+
return dataSize;
29+
}
30+
31+
public String toString() {
32+
return "Cluster{"
33+
+ "clusterName='"
34+
+ clusterName
35+
+ '\''
36+
+ ", nbRecords="
37+
+ nbRecords
38+
+ '\''
39+
+ ", nbUserIDs="
40+
+ nbUserIDs
41+
+ '\''
42+
+ ", dataSize="
43+
+ dataSize
44+
+ '}';
45+
}
46+
}

0 commit comments

Comments
 (0)