11package com .redis .enterprise .rest ;
22
3+ import java .util .Collections ;
34import java .util .List ;
45import java .util .stream .Collectors ;
56import java .util .stream .Stream ;
67
78import org .apache .hc .core5 .util .Asserts ;
9+ import org .springframework .util .unit .DataSize ;
810
911import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
1012import com .fasterxml .jackson .annotation .JsonInclude ;
1416@ JsonIgnoreProperties (ignoreUnknown = true )
1517public class Database {
1618
17- public static final long DEFAULT_MEMORY = 520428800 ;
18- public static final int DEFAULT_CLUSTER_SHARD_COUNT = 3 ;
19- public static final String [] DEFAULT_SHARD_KEY_REGEXES = new String [] { ".*\\ {(?<tag>.*)\\ }.*" , "(?<tag>.*)" };
20-
2119 private Long uid ;
2220 private String name ;
23- private Boolean replication ;
24- private Boolean sharding ;
25- private long memory = DEFAULT_MEMORY ;
21+ private boolean replication ;
22+ private boolean sharding ;
23+ private long memory ;
2624 private Integer port ;
2725 private String type ;
28- private Boolean ossCluster ;
26+ private boolean ossCluster ;
2927 private ProxyPolicy proxyPolicy ;
3028 private IPType ossClusterAPIPreferredIPType ;
3129 private List <Regex > shardKeyRegex ;
3230 private Integer shardCount ;
3331 private ShardPlacement shardPlacement ;
3432 private List <ModuleConfig > moduleConfigs ;
3533
34+ private Database () {
35+ }
36+
37+ private Database (Builder builder ) {
38+ this .uid = builder .uid ;
39+ this .name = builder .name ;
40+ this .replication = builder .replication ;
41+ this .sharding = builder .sharding ;
42+ this .memory = builder .memory .toBytes ();
43+ this .port = builder .port ;
44+ this .type = builder .type ;
45+ this .ossCluster = builder .ossCluster ;
46+ this .proxyPolicy = builder .proxyPolicy ;
47+ this .ossClusterAPIPreferredIPType = builder .ossClusterAPIPreferredIPType ;
48+ this .shardKeyRegex = builder .shardKeyRegex ;
49+ this .shardCount = builder .shardCount ;
50+ this .shardPlacement = builder .shardPlacement ;
51+ this .moduleConfigs = builder .moduleConfigs ;
52+ }
53+
3654 public Long getUid () {
3755 return uid ;
3856 }
3957
40- public void setUid (Long uid ) {
58+ public void setUid (long uid ) {
4159 this .uid = uid ;
4260 }
4361
@@ -49,15 +67,15 @@ public void setName(String name) {
4967 this .name = name ;
5068 }
5169
52- public Boolean getReplication () {
70+ public boolean getReplication () {
5371 return replication ;
5472 }
5573
5674 public void setReplication (boolean replication ) {
5775 this .replication = replication ;
5876 }
5977
60- public Boolean getSharding () {
78+ public boolean getSharding () {
6179 return sharding ;
6280 }
6381
@@ -91,19 +109,12 @@ public void setType(String type) {
91109 }
92110
93111 @ JsonProperty ("oss_cluster" )
94- public Boolean getOssCluster () {
112+ public boolean getOssCluster () {
95113 return ossCluster ;
96114 }
97115
98116 public void setOssCluster (boolean ossCluster ) {
99117 this .ossCluster = ossCluster ;
100- if (ossCluster ) {
101- setProxyPolicy (ProxyPolicy .ALL_MASTER_SHARDS );
102- setOssClusterAPIPreferredIPType (IPType .EXTERNAL );
103- if (shardCount == null || shardCount < 2 ) {
104- setShardCount (DEFAULT_CLUSTER_SHARD_COUNT );
105- }
106- }
107118 }
108119
109120 @ JsonProperty ("proxy_policy" )
@@ -139,12 +150,7 @@ public Integer getShardCount() {
139150 }
140151
141152 public void setShardCount (int shardCount ) {
142- Asserts .check (shardCount > 0 , "Shard count must be strictly positive" );
143153 this .shardCount = shardCount ;
144- if (shardCount > 1 ) {
145- this .sharding = true ;
146- this .shardKeyRegex = Stream .of (DEFAULT_SHARD_KEY_REGEXES ).map (Regex ::new ).collect (Collectors .toList ());
147- }
148154 }
149155
150156 @ JsonProperty ("shards_placement" )
@@ -253,4 +259,119 @@ public void setRegex(String regex) {
253259 }
254260
255261 }
262+
263+ public static Builder name (String name ) {
264+ return new Builder ().name (name );
265+ }
266+
267+ public static final class Builder {
268+
269+ public static final long DEFAULT_MEMORY_MB = 100 ;
270+ public static final int DEFAULT_CLUSTER_SHARD_COUNT = 3 ;
271+ public static final String [] DEFAULT_SHARD_KEY_REGEXES = new String [] { ".*\\ {(?<tag>.*)\\ }.*" , "(?<tag>.*)" };
272+
273+ private Long uid ;
274+ private String name ;
275+ private boolean replication ;
276+ private boolean sharding ;
277+ private DataSize memory = DataSize .ofMegabytes (DEFAULT_MEMORY_MB );
278+ private Integer port ;
279+ private String type ;
280+ private boolean ossCluster ;
281+ private ProxyPolicy proxyPolicy ;
282+ private IPType ossClusterAPIPreferredIPType ;
283+ private List <Regex > shardKeyRegex = Collections .emptyList ();
284+ private Integer shardCount ;
285+ private ShardPlacement shardPlacement ;
286+ private List <ModuleConfig > moduleConfigs = Collections .emptyList ();
287+
288+ private Builder () {
289+ }
290+
291+ public Builder uid (Long uid ) {
292+ this .uid = uid ;
293+ return this ;
294+ }
295+
296+ public Builder name (String name ) {
297+ this .name = name ;
298+ return this ;
299+ }
300+
301+ public Builder replication (boolean replication ) {
302+ this .replication = replication ;
303+ return this ;
304+ }
305+
306+ public Builder sharding (boolean sharding ) {
307+ this .sharding = sharding ;
308+ return this ;
309+ }
310+
311+ public Builder memory (DataSize memory ) {
312+ this .memory = memory ;
313+ return this ;
314+ }
315+
316+ public Builder port (int port ) {
317+ this .port = port ;
318+ return this ;
319+ }
320+
321+ public Builder type (String type ) {
322+ this .type = type ;
323+ return this ;
324+ }
325+
326+ public Builder ossCluster (boolean ossCluster ) {
327+ this .ossCluster = ossCluster ;
328+ if (ossCluster ) {
329+ proxyPolicy (ProxyPolicy .ALL_MASTER_SHARDS );
330+ ossClusterAPIPreferredIPType (IPType .EXTERNAL );
331+ if (shardCount == null || shardCount < 2 ) {
332+ shardCount (DEFAULT_CLUSTER_SHARD_COUNT );
333+ }
334+ }
335+ return this ;
336+ }
337+
338+ public Builder proxyPolicy (ProxyPolicy proxyPolicy ) {
339+ this .proxyPolicy = proxyPolicy ;
340+ return this ;
341+ }
342+
343+ public Builder ossClusterAPIPreferredIPType (IPType ossClusterAPIPreferredIPType ) {
344+ this .ossClusterAPIPreferredIPType = ossClusterAPIPreferredIPType ;
345+ return this ;
346+ }
347+
348+ public Builder shardKeyRegex (List <Regex > shardKeyRegex ) {
349+ this .shardKeyRegex = shardKeyRegex ;
350+ return this ;
351+ }
352+
353+ public Builder shardCount (int shardCount ) {
354+ Asserts .check (shardCount > 0 , "Shard count must be strictly positive" );
355+ this .shardCount = shardCount ;
356+ if (shardCount > 1 ) {
357+ this .sharding = true ;
358+ this .shardKeyRegex = Stream .of (DEFAULT_SHARD_KEY_REGEXES ).map (Regex ::new ).collect (Collectors .toList ());
359+ }
360+ return this ;
361+ }
362+
363+ public Builder shardPlacement (ShardPlacement shardPlacement ) {
364+ this .shardPlacement = shardPlacement ;
365+ return this ;
366+ }
367+
368+ public Builder moduleConfigs (List <ModuleConfig > moduleConfigs ) {
369+ this .moduleConfigs = moduleConfigs ;
370+ return this ;
371+ }
372+
373+ public Database build () {
374+ return new Database (this );
375+ }
376+ }
256377}
0 commit comments