/
MapConfig.java
641 lines (558 loc) · 21.6 KB
/
MapConfig.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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.hazelcast.config;
import com.hazelcast.merge.AddNewEntryMergePolicy;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializable;
import com.hazelcast.util.ByteUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MapConfig implements DataSerializable {
public final static int MIN_BACKUP_COUNT = 0;
public final static int DEFAULT_BACKUP_COUNT = 1;
public final static int MAX_BACKUP_COUNT = 6;
public final static int MIN_EVICTION_PERCENTAGE = 0;
public final static int DEFAULT_EVICTION_PERCENTAGE = 25;
public final static int MAX_EVICTION_PERCENTAGE = 100;
public final static int DEFAULT_EVICTION_DELAY_SECONDS = 3;
public final static int DEFAULT_TTL_SECONDS = 0;
public final static int DEFAULT_MAX_IDLE_SECONDS = 0;
public final static int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
public final static EvictionPolicy DEFAULT_EVICTION_POLICY = EvictionPolicy.NONE;
public final static RecordType DEFAULT_RECORD_TYPE = RecordType.DATA;
public final static String DEFAULT_MERGE_POLICY = AddNewEntryMergePolicy.NAME;
private String name = null;
private int backupCount = DEFAULT_BACKUP_COUNT;
private int asyncBackupCount = MIN_BACKUP_COUNT;
private int evictionPercentage = DEFAULT_EVICTION_PERCENTAGE;
private int timeToLiveSeconds = DEFAULT_TTL_SECONDS;
private int maxIdleSeconds = DEFAULT_TTL_SECONDS;
private int evictionDelaySeconds = DEFAULT_EVICTION_DELAY_SECONDS;
private MaxSizeConfig maxSizeConfig = new MaxSizeConfig();
private EvictionPolicy evictionPolicy = DEFAULT_EVICTION_POLICY;
private boolean valueIndexed = false;
private MapStoreConfig mapStoreConfig = null;
private NearCacheConfig nearCacheConfig = null;
private boolean readBackupData = false;
private String mergePolicy = DEFAULT_MERGE_POLICY;
private RecordType recordType = DEFAULT_RECORD_TYPE;
private WanReplicationRef wanReplicationRef;
private List<EntryListenerConfig> listenerConfigs;
private List<MapIndexConfig> mapIndexConfigs;
private StorageType storageType = null;
public enum RecordType {
DATA, OBJECT, CACHED
}
public enum StorageType {
HEAP, OFFHEAP
}
public enum EvictionPolicy {
LRU, LFU, NONE
}
public MapConfig(String name) {
this.name = name;
}
public MapConfig() {
}
public MapConfig(MapConfig config) {
this.name = config.name;
this.backupCount = config.backupCount;
this.evictionPercentage = config.evictionPercentage;
this.timeToLiveSeconds = config.timeToLiveSeconds;
this.maxIdleSeconds = config.maxIdleSeconds;
this.evictionDelaySeconds = config.evictionDelaySeconds;
this.maxSizeConfig = config.maxSizeConfig;
this.evictionPolicy = config.evictionPolicy;
this.recordType = config.recordType;
this.valueIndexed = config.valueIndexed;
this.mapStoreConfig = config.mapStoreConfig;
this.nearCacheConfig = config.nearCacheConfig;
this.readBackupData = config.readBackupData;
this.mergePolicy = config.mergePolicy;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public MapConfig setName(String name) {
this.name = name;
return this;
}
/**
* @return data type that will be used for storing records.
*/
public RecordType getRecordType() {
return recordType;
}
/**
* Data type that will be used for storing records.
* Possible values:
* DATA (default): keys and values will be stored as binary data
* OBJECT : values will be stored in their object forms
* CACHED: object form of values will be cached
*
* @param recordType the record type to set
*/
public MapConfig setRecordType(RecordType recordType) {
this.recordType = recordType;
return this;
}
/**
* Returns if the value of the mapEntry should be indexed for
* faster containsValue(obj) operations.
* <p/>
* Default is false.
*
* @return true if value is indexed, false otherwise
*/
public boolean isValueIndexed() {
return valueIndexed;
}
/**
* Sets if the value of the map entries should be indexed for
* faster containsValue(obj) operations.
* <p/>
* Default is false.
*
* @param valueIndexed
*/
public MapConfig setValueIndexed(boolean valueIndexed) {
this.valueIndexed = valueIndexed;
return this;
}
/**
* @return the backupCount
* @see #getAsyncBackupCount()
*/
public int getBackupCount() {
return backupCount;
}
/**
* Number of synchronous backups. If 1 is set as the backup-count for example,
* then all entries of the map will be copied to another JVM for
* fail-safety. 0 means no sync backup.
*
* @param backupCount the backupCount to set
* @see #setAsyncBackupCount(int)
* @see #setBackupCounts(int, int)
*/
public MapConfig setBackupCount(final int backupCount) {
if (backupCount < MIN_BACKUP_COUNT) {
throw new IllegalArgumentException("map backup count must be equal to or bigger than "
+ MIN_BACKUP_COUNT);
}
if ((backupCount + this.asyncBackupCount) > MAX_BACKUP_COUNT) {
throw new IllegalArgumentException("total (sync + async) map backup count must be less than "
+ MAX_BACKUP_COUNT);
}
this.backupCount = backupCount;
return this;
}
/**
* @return the asyncBackupCount
* @see #setBackupCount(int)
*/
public int getAsyncBackupCount() {
return asyncBackupCount;
}
/**
* Number of asynchronous backups.
* 0 means no backup.
*
* @param asyncBackupCount the asyncBackupCount to set
* @see #setBackupCount(int)
* @see #setBackupCounts(int, int)
*/
public MapConfig setAsyncBackupCount(final int asyncBackupCount) {
if (asyncBackupCount < MIN_BACKUP_COUNT) {
throw new IllegalArgumentException("map async backup count must be equal to or bigger than "
+ MIN_BACKUP_COUNT);
}
if ((this.backupCount + asyncBackupCount) > MAX_BACKUP_COUNT) {
throw new IllegalArgumentException("total (sync + async) map backup count must be less than "
+ MAX_BACKUP_COUNT);
}
this.asyncBackupCount = asyncBackupCount;
return this;
}
/**
* Number of sync and async backups.
* 0 means no backup.
*
* @param backupCount the sync backup count to set
* @param asyncBackupCount the async backup count to set
* @see #setBackupCount(int)
* @see #setAsyncBackupCount(int)
*/
public MapConfig setBackupCounts(final int backupCount, final int asyncBackupCount) {
if (backupCount < MIN_BACKUP_COUNT || asyncBackupCount < MIN_BACKUP_COUNT) {
throw new IllegalArgumentException("map backup count must be equal to or bigger than "
+ MIN_BACKUP_COUNT);
}
if ((backupCount + asyncBackupCount) > MAX_BACKUP_COUNT) {
throw new IllegalArgumentException("total (sync + async) map backup count must be less than "
+ MAX_BACKUP_COUNT);
}
this.backupCount = backupCount;
this.asyncBackupCount = asyncBackupCount;
return this;
}
public int getTotalBackupCount() {
return backupCount + asyncBackupCount;
}
/**
* @return the evictionPercentage
*/
public int getEvictionPercentage() {
return evictionPercentage;
}
/**
* When max. size is reached, specified percentage of the map will be evicted.
* Any integer between 0 and 100 is allowed.
* If 25 is set for example, 25% of the entries will get evicted.
*
* @param evictionPercentage the evictionPercentage to set
* @throws IllegalArgumentException if evictionPercentage is not in the 0-100 range.
*/
public MapConfig setEvictionPercentage(final int evictionPercentage) {
if (evictionPercentage < MIN_EVICTION_PERCENTAGE) {
throw new IllegalArgumentException("eviction percentage must be greater or equal than 0");
}
if (evictionPercentage > MAX_EVICTION_PERCENTAGE) {
throw new IllegalArgumentException("eviction percentage must be smaller or equal than 100");
}
this.evictionPercentage = evictionPercentage;
return this;
}
/**
* @return the evictionDelaySeconds
* @deprecated
*/
public int getEvictionDelaySeconds() {
return evictionDelaySeconds;
}
/**
* @param evictionDelaySeconds the evictionPercentage to set
* @deprecated
*/
public MapConfig setEvictionDelaySeconds(int evictionDelaySeconds) {
this.evictionDelaySeconds = evictionDelaySeconds;
return this;
}
/**
* @return the timeToLiveSeconds
*/
public int getTimeToLiveSeconds() {
return timeToLiveSeconds;
}
/**
* Maximum number of seconds for each entry to stay in the map. Entries that are
* older than timeToLiveSeconds will get automatically evicted from the map.
* Updates on the entry don't change the eviction time.
* Any integer between 0 and Integer.MAX_VALUE.
* 0 means infinite. Default is 0.
*
* @param timeToLiveSeconds the timeToLiveSeconds to set
*/
public MapConfig setTimeToLiveSeconds(int timeToLiveSeconds) {
this.timeToLiveSeconds = timeToLiveSeconds;
return this;
}
/**
* @return the maxIdleSeconds
*/
public int getMaxIdleSeconds() {
return maxIdleSeconds;
}
/**
* Maximum number of seconds for each entry to stay idle in the map. Entries that are
* idle(not touched) for more than maxIdleSeconds will get
* automatically evicted from the map. Entry is touched if get, put or
* containsKey is called.
* Any integer between 0 and Integer.MAX_VALUE.
* 0 means infinite. Default is 0.
*
* @param maxIdleSeconds the maxIdleSeconds to set
*/
public MapConfig setMaxIdleSeconds(int maxIdleSeconds) {
this.maxIdleSeconds = maxIdleSeconds;
return this;
}
/**
* @return the maxSize
* @deprecated use MaxSizeConfig.getSize
*/
public int getMaxSize() {
return maxSizeConfig.getSize();
}
/**
* @param maxSize the maxSize to set
* @deprecated use MaxSizeConfig.setSize
*/
public MapConfig setMaxSize(final int maxSize) {
if (maxSize < 0) {
throw new IllegalArgumentException("map max size must be greater than 0");
}
this.maxSizeConfig.setSize(maxSize);
return this;
}
public MaxSizeConfig getMaxSizeConfig() {
return maxSizeConfig;
}
public MapConfig setMaxSizeConfig(MaxSizeConfig maxSizeConfig) {
this.maxSizeConfig = maxSizeConfig;
return this;
}
/**
* @return the evictionPolicy
*/
public EvictionPolicy getEvictionPolicy() {
return evictionPolicy;
}
/**
* @param evictionPolicy the evictionPolicy to set
*/
public MapConfig setEvictionPolicy(EvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
return this;
}
/**
* Returns the map store configuration
*
* @return the mapStoreConfig
*/
public MapStoreConfig getMapStoreConfig() {
return mapStoreConfig;
}
/**
* Sets the mapStore configuration
*
* @param mapStoreConfig the mapStoreConfig to set
*/
public MapConfig setMapStoreConfig(MapStoreConfig mapStoreConfig) {
this.mapStoreConfig = mapStoreConfig;
return this;
}
public NearCacheConfig getNearCacheConfig() {
return nearCacheConfig;
}
public MapConfig setNearCacheConfig(NearCacheConfig nearCacheConfig) {
this.nearCacheConfig = nearCacheConfig;
return this;
}
public String getMergePolicy() {
return mergePolicy;
}
public MapConfig setMergePolicy(String mergePolicyName) {
this.mergePolicy = mergePolicyName;
return this;
}
public boolean isReadBackupData() {
return readBackupData;
}
public MapConfig setReadBackupData(boolean readBackupData) {
this.readBackupData = readBackupData;
return this;
}
public WanReplicationRef getWanReplicationRef() {
return wanReplicationRef;
}
public MapConfig setWanReplicationRef(WanReplicationRef wanReplicationRef) {
this.wanReplicationRef = wanReplicationRef;
return this;
}
public StorageType getStorageType() {
return storageType;
}
public MapConfig setStorageType(StorageType storageType) {
this.storageType = storageType;
return this;
}
public MapConfig addEntryListenerConfig(EntryListenerConfig listenerConfig) {
getEntryListenerConfigs().add(listenerConfig);
return this;
}
public List<EntryListenerConfig> getEntryListenerConfigs() {
if (listenerConfigs == null) {
listenerConfigs = new ArrayList<EntryListenerConfig>();
}
return listenerConfigs;
}
public MapConfig setEntryListenerConfigs(List<EntryListenerConfig> listenerConfigs) {
this.listenerConfigs = listenerConfigs;
return this;
}
public MapConfig addMapIndexConfig(MapIndexConfig mapIndexConfig) {
getMapIndexConfigs().add(mapIndexConfig);
return this;
}
public List<MapIndexConfig> getMapIndexConfigs() {
if (mapIndexConfigs == null) {
mapIndexConfigs = new ArrayList<MapIndexConfig>();
}
return mapIndexConfigs;
}
public MapConfig setMapIndexConfigs(List<MapIndexConfig> mapIndexConfigs) {
this.mapIndexConfigs = mapIndexConfigs;
return this;
}
public boolean isCompatible(MapConfig other) {
if (this == other) {
return true;
}
return other != null &&
(this.name != null ? this.name.equals(other.name) : other.name == null) &&
this.backupCount == other.backupCount &&
this.asyncBackupCount == other.asyncBackupCount &&
this.evictionDelaySeconds == other.evictionDelaySeconds &&
this.evictionPercentage == other.evictionPercentage &&
this.maxIdleSeconds == other.maxIdleSeconds &&
(this.maxSizeConfig.getSize() == other.maxSizeConfig.getSize() ||
(Math.min(maxSizeConfig.getSize(), other.maxSizeConfig.getSize()) == 0
&& Math.max(maxSizeConfig.getSize(), other.maxSizeConfig.getSize()) == Integer.MAX_VALUE)) &&
this.timeToLiveSeconds == other.timeToLiveSeconds &&
this.readBackupData == other.readBackupData &&
this.valueIndexed == other.valueIndexed;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.backupCount;
result = prime * result + this.asyncBackupCount;
result = prime * result + this.evictionDelaySeconds;
result = prime * result + this.evictionPercentage;
result = prime
* result
+ ((this.evictionPolicy == null) ? 0 : this.evictionPolicy
.hashCode());
result = prime
* result
+ ((this.mapStoreConfig == null) ? 0 : this.mapStoreConfig
.hashCode());
result = prime * result + this.maxIdleSeconds;
result = prime * result + this.maxSizeConfig.getSize();
result = prime
* result
+ ((this.mergePolicy == null) ? 0 : this.mergePolicy.hashCode());
result = prime * result
+ ((this.name == null) ? 0 : this.name.hashCode());
result = prime
* result
+ ((this.nearCacheConfig == null) ? 0 : this.nearCacheConfig
.hashCode());
result = prime * result + this.timeToLiveSeconds;
result = prime * result + (this.readBackupData ? 1231 : 1237);
result = prime * result + (this.valueIndexed ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MapConfig)) {
return false;
}
MapConfig other = (MapConfig) obj;
return
(this.name != null ? this.name.equals(other.name) : other.name == null) &&
this.backupCount == other.backupCount &&
this.asyncBackupCount == other.asyncBackupCount &&
this.evictionDelaySeconds == other.evictionDelaySeconds &&
this.evictionPercentage == other.evictionPercentage &&
this.maxIdleSeconds == other.maxIdleSeconds &&
this.maxSizeConfig.getSize() == other.maxSizeConfig.getSize() &&
this.timeToLiveSeconds == other.timeToLiveSeconds &&
this.readBackupData == other.readBackupData &&
this.valueIndexed == other.valueIndexed &&
(this.mergePolicy != null ? this.mergePolicy.equals(other.mergePolicy) : other.mergePolicy == null) &&
(this.recordType != null ? this.recordType.equals(other.recordType) : other.recordType == null) &&
(this.evictionPolicy != null ? this.evictionPolicy.equals(other.evictionPolicy)
: other.evictionPolicy == null) &&
(this.mapStoreConfig != null ? this.mapStoreConfig.equals(other.mapStoreConfig)
: other.mapStoreConfig == null) &&
(this.nearCacheConfig != null ? this.nearCacheConfig.equals(other.nearCacheConfig)
: other.nearCacheConfig == null);
}
public void readData(ObjectDataInput in) throws IOException {
name = in.readUTF();
recordType = RecordType.valueOf(in.readUTF());
backupCount = in.readInt();
asyncBackupCount = in.readInt();
evictionPercentage = in.readInt();
timeToLiveSeconds = in.readInt();
maxIdleSeconds = in.readInt();
evictionDelaySeconds = in.readInt();
maxSizeConfig.readData(in);
boolean[] b = ByteUtil.fromByte(in.readByte());
valueIndexed = b[0];
readBackupData = b[1];
evictionPolicy = MapConfig.EvictionPolicy.valueOf(in.readUTF());
mergePolicy = in.readUTF();
// TODO: MapStoreConfig mapStoreConfig
// TODO: NearCacheConfig nearCacheConfig
}
public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(name);
out.writeUTF(recordType.toString());
out.writeInt(backupCount);
out.writeInt(asyncBackupCount);
out.writeInt(evictionPercentage);
out.writeInt(timeToLiveSeconds);
out.writeInt(maxIdleSeconds);
out.writeInt(evictionDelaySeconds);
maxSizeConfig.writeData(out);
out.writeByte(ByteUtil.toByte(valueIndexed, readBackupData));
out.writeUTF(evictionPolicy.name());
out.writeUTF(mergePolicy);
// TODO: MapStoreConfig mapStoreConfig
// TODO: NearCacheConfig nearCacheConfig
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("MapConfig");
sb.append("{name='").append(name).append('\'');
sb.append(", recordType=").append(recordType).append('\'');
sb.append(", backupCount=").append(backupCount);
sb.append(", asyncBackupCount=").append(asyncBackupCount);
sb.append(", timeToLiveSeconds=").append(timeToLiveSeconds);
sb.append(", maxIdleSeconds=").append(maxIdleSeconds);
sb.append(", evictionPolicy='").append(evictionPolicy).append('\'');
sb.append(", evictionPercentage=").append(evictionPercentage);
sb.append(", evictionDelaySeconds=").append(evictionDelaySeconds);
sb.append(", maxSizeConfig=").append(maxSizeConfig);
sb.append(", readBackupData=").append(readBackupData);
sb.append(", nearCacheConfig=").append(nearCacheConfig);
sb.append(", mapStoreConfig=").append(mapStoreConfig);
sb.append(", mergePolicy='").append(mergePolicy).append('\'');
sb.append(", wanReplicationRef=").append(wanReplicationRef);
sb.append(", listenerConfigs=").append(listenerConfigs);
sb.append(", valueIndexed=").append(valueIndexed);
sb.append(", mapIndexConfigs=").append(mapIndexConfigs);
sb.append(", storageType=").append(storageType);
sb.append('}');
return sb.toString();
}
}