Skip to content

Commit a05ca1a

Browse files
committed
Added module to database builder
1 parent 3bad66d commit a05ca1a

File tree

3 files changed

+100
-31
lines changed

3 files changed

+100
-31
lines changed

subprojects/admin/src/main/java/com/redis/enterprise/Admin.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.net.URISyntaxException;
88
import java.security.GeneralSecurityException;
99
import java.time.Duration;
10+
import java.util.HashMap;
1011
import java.util.List;
12+
import java.util.Map;
1113

1214
import javax.net.ssl.SSLContext;
1315

@@ -55,6 +57,7 @@
5557
import com.redis.enterprise.rest.Command;
5658
import com.redis.enterprise.rest.CommandResponse;
5759
import com.redis.enterprise.rest.Database;
60+
import com.redis.enterprise.rest.Database.ModuleConfig;
5861
import com.redis.enterprise.rest.Module;
5962
import com.redis.enterprise.rest.ModuleInstallResponse;
6063

@@ -186,6 +189,16 @@ public List<Module> getModules() throws ParseException, IOException {
186189
}
187190

188191
public Database createDatabase(Database database) throws ParseException, IOException {
192+
Map<String, Module> installedModules = new HashMap<>();
193+
for (Module module : getModules()) {
194+
installedModules.put(module.getName(), module);
195+
}
196+
for (ModuleConfig moduleConfig : database.getModules()) {
197+
if (!installedModules.containsKey(moduleConfig.getName())) {
198+
throw new IllegalArgumentException(String.format("Module %s not installed", moduleConfig.getName()));
199+
}
200+
moduleConfig.setId(installedModules.get(moduleConfig.getName()).getId());
201+
}
189202
Database response = post(v1(BDBS), database, Database.class);
190203
long uid = response.getUid();
191204
Awaitility.await().until(() -> {
@@ -245,7 +258,13 @@ public ModuleInstallResponse installModule(String filename, InputStream inputStr
245258
return response;
246259
}
247260

248-
public Bootstrap getBootstrap() throws ParseException, IOException {
261+
public void waitForBoostrap() {
262+
Awaitility.await().pollInterval(Duration.ofSeconds(3)).timeout(Duration.ofMinutes(1))
263+
.until(() -> "idle".equals(getBootstrap().getStatus().getState()));
264+
265+
}
266+
267+
private Bootstrap getBootstrap() throws ParseException, IOException {
249268
return get(v1(BOOTSTRAP), Bootstrap.class);
250269
}
251270

subprojects/admin/src/main/java/com/redis/enterprise/rest/Database.java

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.redis.enterprise.rest;
22

3-
import java.util.Collections;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56
import java.util.stream.Collectors;
6-
import java.util.stream.Stream;
77

88
import org.apache.hc.core5.util.Asserts;
99
import org.springframework.util.unit.DataSize;
@@ -26,10 +26,10 @@ public class Database {
2626
private boolean ossCluster;
2727
private ProxyPolicy proxyPolicy;
2828
private IPType ossClusterAPIPreferredIPType;
29-
private List<Regex> shardKeyRegex;
29+
private List<ShardKeyRegex> shardKeyRegex;
3030
private Integer shardCount;
3131
private ShardPlacement shardPlacement;
32-
private List<ModuleConfig> moduleConfigs;
32+
private List<ModuleConfig> modules;
3333

3434
private Database() {
3535
}
@@ -45,10 +45,10 @@ private Database(Builder builder) {
4545
this.ossCluster = builder.ossCluster;
4646
this.proxyPolicy = builder.proxyPolicy;
4747
this.ossClusterAPIPreferredIPType = builder.ossClusterAPIPreferredIPType;
48-
this.shardKeyRegex = builder.shardKeyRegex;
48+
this.shardKeyRegex = builder.shardKeyRegexes.stream().map(ShardKeyRegex::new).collect(Collectors.toList());
4949
this.shardCount = builder.shardCount;
5050
this.shardPlacement = builder.shardPlacement;
51-
this.moduleConfigs = builder.moduleConfigs;
51+
this.modules = builder.moduleConfigs;
5252
}
5353

5454
public Long getUid() {
@@ -136,11 +136,11 @@ public void setOssClusterAPIPreferredIPType(IPType ossClusterAPIPreferredIPType)
136136
}
137137

138138
@JsonProperty("shard_key_regex")
139-
public List<Regex> getShardKeyRegex() {
139+
public List<ShardKeyRegex> getShardKeyRegex() {
140140
return shardKeyRegex;
141141
}
142142

143-
public void setShardKeyRegex(List<Regex> shardKeyRegex) {
143+
public void setShardKeyRegex(List<ShardKeyRegex> shardKeyRegex) {
144144
this.shardKeyRegex = shardKeyRegex;
145145
}
146146

@@ -163,16 +163,12 @@ public void setShardPlacement(ShardPlacement shardPlacement) {
163163
}
164164

165165
@JsonProperty("module_list")
166-
public List<ModuleConfig> getModuleConfigs() {
167-
return moduleConfigs;
166+
public List<ModuleConfig> getModules() {
167+
return modules;
168168
}
169169

170-
public void setModuleConfigs(List<ModuleConfig> moduleConfigs) {
171-
this.moduleConfigs = moduleConfigs;
172-
}
173-
174-
public void setModules(List<String> names) {
175-
this.setModuleConfigs(names.stream().map(ModuleConfig::new).collect(Collectors.toList()));
170+
public void setModules(List<ModuleConfig> modules) {
171+
this.modules = modules;
176172
}
177173

178174
public enum IPType {
@@ -239,14 +235,14 @@ public void setArgs(String args) {
239235
}
240236

241237
@JsonInclude(JsonInclude.Include.NON_NULL)
242-
public static class Regex {
238+
public static class ShardKeyRegex {
243239

244240
private String regex;
245241

246-
public Regex() {
242+
public ShardKeyRegex() {
247243
}
248244

249-
public Regex(String regex) {
245+
public ShardKeyRegex(String regex) {
250246
this.regex = regex;
251247
}
252248

@@ -280,10 +276,10 @@ public static final class Builder {
280276
private boolean ossCluster;
281277
private ProxyPolicy proxyPolicy;
282278
private IPType ossClusterAPIPreferredIPType;
283-
private List<Regex> shardKeyRegex = Collections.emptyList();
279+
private List<String> shardKeyRegexes = new ArrayList<>();
284280
private Integer shardCount;
285281
private ShardPlacement shardPlacement;
286-
private List<ModuleConfig> moduleConfigs = Collections.emptyList();
282+
private List<ModuleConfig> moduleConfigs = new ArrayList<>();
287283

288284
private Builder() {
289285
}
@@ -345,17 +341,24 @@ public Builder ossClusterAPIPreferredIPType(IPType ossClusterAPIPreferredIPType)
345341
return this;
346342
}
347343

348-
public Builder shardKeyRegex(List<Regex> shardKeyRegex) {
349-
this.shardKeyRegex = shardKeyRegex;
344+
public Builder shardKeyRegex(String regex) {
345+
this.shardKeyRegexes.add(regex);
346+
return this;
347+
}
348+
349+
public Builder shardKeyRegexes(String... regexes) {
350+
for (String regex : regexes) {
351+
shardKeyRegex(regex);
352+
}
350353
return this;
351354
}
352355

353356
public Builder shardCount(int shardCount) {
354357
Asserts.check(shardCount > 0, "Shard count must be strictly positive");
355358
this.shardCount = shardCount;
356359
if (shardCount > 1) {
357-
this.sharding = true;
358-
this.shardKeyRegex = Stream.of(DEFAULT_SHARD_KEY_REGEXES).map(Regex::new).collect(Collectors.toList());
360+
sharding(true);
361+
shardKeyRegexes(DEFAULT_SHARD_KEY_REGEXES);
359362
}
360363
return this;
361364
}
@@ -365,13 +368,46 @@ public Builder shardPlacement(ShardPlacement shardPlacement) {
365368
return this;
366369
}
367370

368-
public Builder moduleConfigs(List<ModuleConfig> moduleConfigs) {
369-
this.moduleConfigs = moduleConfigs;
371+
public Builder module(Module module) {
372+
this.moduleConfigs.add(new ModuleConfig(module.getName()));
373+
return this;
374+
}
375+
376+
public Builder modules(Module... modules) {
377+
for (Module module : modules) {
378+
module(module);
379+
}
380+
return this;
381+
}
382+
383+
public Builder moduleConfig(ModuleConfig moduleConfig) {
384+
this.moduleConfigs.add(moduleConfig);
385+
return this;
386+
}
387+
388+
public Builder moduleConfigs(ModuleConfig... moduleConfigs) {
389+
this.moduleConfigs = Arrays.asList(moduleConfigs);
370390
return this;
371391
}
372392

373393
public Database build() {
374394
return new Database(this);
375395
}
396+
397+
public enum Module {
398+
399+
BLOOM("bf"), GEARS("rg"), GRAPH("graph"), JSON("ReJSON"), SEARCH("search"), TIMESERIES("timeseries");
400+
401+
private final String name;
402+
403+
Module(String name) {
404+
this.name = name;
405+
}
406+
407+
public String getName() {
408+
return name;
409+
}
410+
411+
}
376412
}
377413
}

subprojects/admin/src/test/java/com/redis/enterprise/AdminTests.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.testcontainers.junit.jupiter.Testcontainers;
2525

2626
import com.redis.enterprise.rest.Database;
27+
import com.redis.enterprise.rest.Database.Builder.Module;
2728
import com.redis.enterprise.rest.ModuleInstallResponse;
2829
import com.redis.testcontainers.RedisEnterpriseContainer;
2930
import com.redis.testcontainers.RedisEnterpriseContainer.RedisModule;
@@ -63,15 +64,24 @@ void deleteAllDatabases() throws GeneralSecurityException, IOException, ParseExc
6364

6465
@Test
6566
void createDatabase() throws ParseException, GeneralSecurityException, IOException {
66-
String databaseName = "CreateTestDB";
67+
String databaseName = "CreateDBTest";
6768
admin.createDatabase(Database.name(databaseName).ossCluster(true).build());
6869
Stream<Database> stream = admin.getDatabases().stream().filter(d -> d.getName().equals(databaseName));
6970
Assertions.assertEquals(1, stream.count());
7071
}
7172

73+
@Test
74+
void createSearchDatabase() throws ParseException, IOException {
75+
String databaseName = "CreateSearchDBTest";
76+
admin.createDatabase(Database.name(databaseName).module(Module.SEARCH).build());
77+
List<Database> databases = admin.getDatabases();
78+
Assertions.assertEquals(1, databases.size());
79+
Assertions.assertEquals(Module.SEARCH.getName(), databases.get(0).getModules().get(0).getName());
80+
}
81+
7282
@Test
7383
void deleteDatabase() throws ParseException, GeneralSecurityException, IOException {
74-
String databaseName = "DeleteTestDB";
84+
String databaseName = "DeleteDBTest";
7585
Database database = admin.createDatabase(Database.name(databaseName).build());
7686
admin.deleteDatabase(database.getUid());
7787
Awaitility.await().until(() -> admin.getDatabases().stream().noneMatch(d -> d.getUid() == database.getUid()));
@@ -87,10 +97,14 @@ void installModule() throws IOException, ParseException {
8797
Assertions.assertTrue(
8898
admin.getModules().stream().anyMatch(m -> m.getName().equals(RedisModule.GEARS.getName())));
8999
}
100+
admin.createDatabase(Database.name("ModuleInstallDBTest").module(Module.SEARCH).build());
101+
List<Database> databases = admin.getDatabases();
102+
Assertions.assertEquals(1, databases.size());
103+
Assertions.assertEquals(Module.SEARCH.getName(), databases.get(0).getModules().get(0).getName());
90104
}
91105

92106
@Test
93-
void databaseCreateException() throws ParseException, IOException {
107+
void createDatabaseException() throws ParseException, IOException {
94108
Assertions.assertThrows(HttpResponseException.class, () -> admin.createDatabase(
95109
Database.name("DatabaseCreateExceptionTestDB").memory(DataSize.ofGigabytes(10)).build()));
96110
}

0 commit comments

Comments
 (0)