forked from voldemort/voldemort
/
StoreDefinitionsMapper.java
568 lines (514 loc) · 30.3 KB
/
StoreDefinitionsMapper.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
/*
* Copyright 2008-2009 LinkedIn, Inc
*
* 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 voldemort.xml;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMSource;
import org.xml.sax.SAXException;
import voldemort.client.RoutingTier;
import voldemort.routing.RoutingStrategyType;
import voldemort.serialization.Compression;
import voldemort.serialization.SerializerDefinition;
import voldemort.store.StoreDefinition;
import voldemort.store.StoreDefinitionBuilder;
import voldemort.store.StoreUtils;
import voldemort.store.slop.strategy.HintedHandoffStrategyType;
import voldemort.store.system.SystemStoreConstants;
import voldemort.store.views.ViewStorageConfiguration;
import voldemort.utils.Utils;
import com.google.common.collect.Lists;
/**
* Parses a stores.xml file
*
*
*/
public class StoreDefinitionsMapper {
public final static String STORES_ELMT = "stores";
public final static String STORE_ELMT = "store";
public final static String STORE_DESCRIPTION_ELMT = "description";
public final static String STORE_OWNERS_ELMT = "owners";
public final static String STORE_NAME_ELMT = "name";
public final static String STORE_PERSISTENCE_ELMT = "persistence";
public final static String STORE_KEY_SERIALIZER_ELMT = "key-serializer";
public final static String STORE_VALUE_SERIALIZER_ELMT = "value-serializer";
public final static String STORE_TRANSFORM_SERIALIZER_ELMT = "transforms-serializer";
public final static String STORE_SERIALIZATION_TYPE_ELMT = "type";
public final static String STORE_SERIALIZATION_META_ELMT = "schema-info";
public final static String STORE_COMPRESSION_ELMT = "compression";
public final static String STORE_COMPRESSION_TYPE_ELMT = "type";
public final static String STORE_COMPRESSION_OPTIONS_ELMT = "options";
public final static String STORE_ROUTING_TIER_ELMT = "routing";
public final static String STORE_REPLICATION_FACTOR_ELMT = "replication-factor";
public final static String STORE_REQUIRED_WRITES_ELMT = "required-writes";
public final static String STORE_PREFERRED_WRITES_ELMT = "preferred-writes";
public final static String STORE_REQUIRED_READS_ELMT = "required-reads";
public final static String STORE_PREFERRED_READS_ELMT = "preferred-reads";
public final static String STORE_RETENTION_POLICY_ELMT = "retention-days";
public final static String STORE_RETENTION_FREQ_ELMT = "retention-frequency";
public final static String STORE_RETENTION_SCAN_THROTTLE_RATE_ELMT = "retention-scan-throttle-rate";
public final static String STORE_ROUTING_STRATEGY = "routing-strategy";
public final static String STORE_ZONE_ID_ELMT = "zone-id";
public final static String STORE_ZONE_REPLICATION_FACTOR_ELMT = "zone-replication-factor";
public final static String STORE_ZONE_COUNT_READS = "zone-count-reads";
public final static String STORE_ZONE_COUNT_WRITES = "zone-count-writes";
public final static String HINTED_HANDOFF_STRATEGY = "hinted-handoff-strategy";
public final static String HINT_PREFLIST_SIZE = "hint-preflist-size";
public final static String VIEW_ELMT = "view";
public final static String VIEW_TARGET_ELMT = "view-of";
public final static String VIEW_TRANS_ELMT = "view-class";
public final static String VIEW_SERIALIZER_FACTORY_ELMT = "view-serializer-factory";
private final static String STORE_VERSION_ATTR = "version";
private final static String STORE_MEMORY_FOOTPRINT = "memory-footprint";
private final Schema schema;
public StoreDefinitionsMapper() {
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source source = new StreamSource(StoreDefinitionsMapper.class.getResourceAsStream("stores.xsd"));
this.schema = factory.newSchema(source);
} catch(SAXException e) {
throw new MappingException(e);
}
}
public List<StoreDefinition> readStoreList(File f) throws IOException {
FileReader reader = null;
try {
reader = new FileReader(f);
return readStoreList(reader);
} finally {
if(reader != null)
reader.close();
}
}
public List<StoreDefinition> readStoreList(Reader input) {
return readStoreList(input, true);
}
public List<StoreDefinition> readStoreList(Reader input, boolean verifySchema) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(input);
if(verifySchema) {
Validator validator = schema.newValidator();
validator.validate(new JDOMSource(doc));
}
Element root = doc.getRootElement();
if(!root.getName().equals(STORES_ELMT))
throw new MappingException("Invalid root element: "
+ doc.getRootElement().getName());
List<StoreDefinition> stores = new ArrayList<StoreDefinition>();
for(Object store: root.getChildren(STORE_ELMT))
stores.add(readStore((Element) store));
for(Object view: root.getChildren(VIEW_ELMT))
stores.add(readView((Element) view, stores));
return stores;
} catch(JDOMException e) {
throw new MappingException(e);
} catch(SAXException e) {
throw new MappingException(e);
} catch(IOException e) {
throw new MappingException(e);
}
}
public StoreDefinition readStore(Reader input) {
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(input);
Element root = doc.getRootElement();
return readStore(root);
} catch(JDOMException e) {
throw new MappingException(e);
} catch(IOException e) {
throw new MappingException(e);
}
}
@SuppressWarnings("unchecked")
private StoreDefinition readStore(Element store) {
String name = store.getChildText(STORE_NAME_ELMT);
String storeType = store.getChildText(STORE_PERSISTENCE_ELMT);
String description = store.getChildText(STORE_DESCRIPTION_ELMT);
String ownerText = store.getChildText(STORE_OWNERS_ELMT);
List<String> owners = Lists.newArrayList();
if(ownerText != null) {
for(String owner: Utils.COMMA_SEP.split(ownerText.trim()))
if(owner.trim().length() > 0)
owners.add(owner);
}
int replicationFactor = Integer.parseInt(store.getChildText(STORE_REPLICATION_FACTOR_ELMT));
HashMap<Integer, Integer> zoneReplicationFactor = null;
Element zoneReplicationFactorNode = store.getChild(STORE_ZONE_REPLICATION_FACTOR_ELMT);
if(zoneReplicationFactorNode != null) {
zoneReplicationFactor = new HashMap<Integer, Integer>();
for(Element node: (List<Element>) zoneReplicationFactorNode.getChildren(STORE_REPLICATION_FACTOR_ELMT)) {
int zone = Integer.parseInt(node.getAttribute(STORE_ZONE_ID_ELMT).getValue());
int repFactor = Integer.parseInt(node.getText());
zoneReplicationFactor.put(zone, repFactor);
}
}
String zoneCountReadsStr = store.getChildText(STORE_ZONE_COUNT_READS);
Integer zoneCountReads = null;
if(zoneCountReadsStr != null)
zoneCountReads = Integer.parseInt(zoneCountReadsStr);
String zoneCountWritesStr = store.getChildText(STORE_ZONE_COUNT_WRITES);
Integer zoneCountWrites = null;
if(zoneCountWritesStr != null)
zoneCountWrites = Integer.parseInt(zoneCountWritesStr);
int requiredReads = Integer.parseInt(store.getChildText(STORE_REQUIRED_READS_ELMT));
int requiredWrites = Integer.parseInt(store.getChildText(STORE_REQUIRED_WRITES_ELMT));
String preferredReadsStr = store.getChildText(STORE_PREFERRED_READS_ELMT);
Integer preferredReads = null;
if(preferredReadsStr != null)
preferredReads = Integer.parseInt(preferredReadsStr);
String preferredWritesStr = store.getChildText(STORE_PREFERRED_WRITES_ELMT);
Integer preferredWrites = null;
if(preferredWritesStr != null)
preferredWrites = Integer.parseInt(preferredWritesStr);
SerializerDefinition keySerializer = readSerializer(store.getChild(STORE_KEY_SERIALIZER_ELMT));
if(keySerializer.getAllSchemaInfoVersions().size() > 1)
throw new MappingException("Only a single schema is allowed for the store key.");
SerializerDefinition valueSerializer = readSerializer(store.getChild(STORE_VALUE_SERIALIZER_ELMT));
RoutingTier routingTier = RoutingTier.fromDisplay(store.getChildText(STORE_ROUTING_TIER_ELMT));
String routingStrategyType = (null != store.getChildText(STORE_ROUTING_STRATEGY)) ? store.getChildText(STORE_ROUTING_STRATEGY)
: RoutingStrategyType.CONSISTENT_STRATEGY;
Element retention = store.getChild(STORE_RETENTION_POLICY_ELMT);
Integer retentionPolicyDays = null;
Integer retentionThrottleRate = null;
Integer retentionFreqDays = null;
if(retention != null) {
retentionPolicyDays = Integer.parseInt(retention.getText());
Element throttleRate = store.getChild(STORE_RETENTION_SCAN_THROTTLE_RATE_ELMT);
if(throttleRate != null)
retentionThrottleRate = Integer.parseInt(throttleRate.getText());
Element retentionFreqDaysElement = store.getChild(STORE_RETENTION_FREQ_ELMT);
if(retentionFreqDaysElement != null)
retentionFreqDays = Integer.parseInt(retentionFreqDaysElement.getText());
}
if(routingStrategyType.compareTo(RoutingStrategyType.ZONE_STRATEGY) == 0
&& !SystemStoreConstants.isSystemStore(name)) {
if(zoneCountReads == null || zoneCountWrites == null || zoneReplicationFactor == null) {
throw new MappingException("Have not set one of the following correctly for store '"
+ name
+ "' - "
+ STORE_ZONE_COUNT_READS
+ ", "
+ STORE_ZONE_COUNT_WRITES
+ ", "
+ STORE_ZONE_REPLICATION_FACTOR_ELMT);
}
}
HintedHandoffStrategyType hintedHandoffStrategy = null;
if(store.getChildText(HINTED_HANDOFF_STRATEGY) != null)
hintedHandoffStrategy = HintedHandoffStrategyType.fromDisplay(store.getChildText(HINTED_HANDOFF_STRATEGY));
String hintPrefListSizeStr = store.getChildText(HINT_PREFLIST_SIZE);
Integer hintPrefListSize = (null != hintPrefListSizeStr) ? Integer.parseInt(hintPrefListSizeStr)
: null;
String memoryFootprintStr = store.getChildText(STORE_MEMORY_FOOTPRINT);
long memoryFootprintMB = 0;
if(memoryFootprintStr != null)
memoryFootprintMB = Long.parseLong(memoryFootprintStr);
return new StoreDefinitionBuilder().setName(name)
.setType(storeType)
.setDescription(description)
.setOwners(owners)
.setKeySerializer(keySerializer)
.setValueSerializer(valueSerializer)
.setRoutingPolicy(routingTier)
.setRoutingStrategyType(routingStrategyType)
.setReplicationFactor(replicationFactor)
.setPreferredReads(preferredReads)
.setRequiredReads(requiredReads)
.setPreferredWrites(preferredWrites)
.setRequiredWrites(requiredWrites)
.setRetentionPeriodDays(retentionPolicyDays)
.setRetentionScanThrottleRate(retentionThrottleRate)
.setRetentionFrequencyDays(retentionFreqDays)
.setZoneReplicationFactor(zoneReplicationFactor)
.setZoneCountReads(zoneCountReads)
.setZoneCountWrites(zoneCountWrites)
.setHintedHandoffStrategy(hintedHandoffStrategy)
.setHintPrefListSize(hintPrefListSize)
.setMemoryFootprintMB(memoryFootprintMB)
.build();
}
private StoreDefinition readView(Element store, List<StoreDefinition> stores) {
String name = store.getChildText(STORE_NAME_ELMT);
String targetName = store.getChildText(VIEW_TARGET_ELMT);
String description = store.getChildText(STORE_DESCRIPTION_ELMT);
String ownerText = store.getChildText(STORE_OWNERS_ELMT);
List<String> owners = Lists.newArrayList();
if(ownerText != null) {
for(String owner: Utils.COMMA_SEP.split(ownerText.trim()))
if(owner.trim().length() > 0)
owners.add(owner);
}
StoreDefinition target = StoreUtils.getStoreDef(stores, targetName);
if(target == null)
throw new MappingException("View \"" + name + "\" has target store \"" + targetName
+ "\" but no such store exists");
int requiredReads = getChildWithDefault(store,
STORE_REQUIRED_READS_ELMT,
target.getRequiredReads());
int preferredReads = getChildWithDefault(store,
STORE_PREFERRED_READS_ELMT,
target.getRequiredReads());
int requiredWrites = getChildWithDefault(store,
STORE_REQUIRED_WRITES_ELMT,
target.getRequiredReads());
int preferredWrites = getChildWithDefault(store,
STORE_PREFERRED_WRITES_ELMT,
target.getRequiredReads());
Integer zoneCountReads = getChildWithDefault(store,
STORE_ZONE_COUNT_READS,
target.getZoneCountReads());
Integer zoneCountWrites = getChildWithDefault(store,
STORE_ZONE_COUNT_WRITES,
target.getZoneCountWrites());
String viewSerializerFactoryName = null;
if(store.getChildText(VIEW_SERIALIZER_FACTORY_ELMT) != null) {
viewSerializerFactoryName = store.getChild(VIEW_SERIALIZER_FACTORY_ELMT).getText();
}
SerializerDefinition keySerializer = target.getKeySerializer();
SerializerDefinition valueSerializer = target.getValueSerializer();
if(store.getChild(STORE_VALUE_SERIALIZER_ELMT) != null)
valueSerializer = readSerializer(store.getChild(STORE_VALUE_SERIALIZER_ELMT));
SerializerDefinition transformSerializer = target.getTransformsSerializer();
if(store.getChild(STORE_TRANSFORM_SERIALIZER_ELMT) != null)
transformSerializer = readSerializer(store.getChild(STORE_TRANSFORM_SERIALIZER_ELMT));
RoutingTier routingTier = null;
if(store.getChildText(STORE_ROUTING_TIER_ELMT) != null) {
routingTier = RoutingTier.fromDisplay(store.getChildText(STORE_ROUTING_TIER_ELMT));
} else {
routingTier = target.getRoutingPolicy();
}
String viewClass = store.getChildText(VIEW_TRANS_ELMT);
return new StoreDefinitionBuilder().setName(name)
.setViewOf(targetName)
.setType(ViewStorageConfiguration.TYPE_NAME)
.setDescription(description)
.setOwners(owners)
.setRoutingPolicy(routingTier)
.setRoutingStrategyType(target.getRoutingStrategyType())
.setKeySerializer(keySerializer)
.setValueSerializer(valueSerializer)
.setTransformsSerializer(transformSerializer)
.setReplicationFactor(target.getReplicationFactor())
.setZoneReplicationFactor(target.getZoneReplicationFactor())
.setPreferredReads(preferredReads)
.setRequiredReads(requiredReads)
.setPreferredWrites(preferredWrites)
.setRequiredWrites(requiredWrites)
.setZoneCountReads(zoneCountReads)
.setZoneCountWrites(zoneCountWrites)
.setView(viewClass)
.setSerializerFactory(viewSerializerFactoryName)
.build();
}
public static SerializerDefinition readSerializer(Element elmt) {
String name = elmt.getChild(STORE_SERIALIZATION_TYPE_ELMT).getText();
boolean hasVersion = true;
Map<Integer, String> schemaInfosByVersion = new HashMap<Integer, String>();
for(Object schemaInfo: elmt.getChildren(STORE_SERIALIZATION_META_ELMT)) {
Element schemaInfoElmt = (Element) schemaInfo;
String versionStr = schemaInfoElmt.getAttributeValue(STORE_VERSION_ATTR);
int version;
if(versionStr == null) {
version = 0;
} else if(versionStr.equals("none")) {
version = 0;
hasVersion = false;
} else {
version = Integer.parseInt(versionStr);
}
String info = schemaInfoElmt.getText();
String previous = schemaInfosByVersion.put(version, info);
if(previous != null)
throw new MappingException("Duplicate version " + version
+ " found in schema info.");
}
if(!hasVersion && schemaInfosByVersion.size() > 1)
throw new IllegalArgumentException("Specified multiple schemas AND version=none, which is not permitted.");
Element compressionElmt = elmt.getChild(STORE_COMPRESSION_ELMT);
Compression compression = null;
if(compressionElmt != null)
compression = new Compression(compressionElmt.getChildText("type"),
compressionElmt.getChildText("options"));
return new SerializerDefinition(name, schemaInfosByVersion, hasVersion, compression);
}
public String writeStoreList(List<StoreDefinition> stores) {
Element root = new Element(STORES_ELMT);
for(StoreDefinition def: stores) {
if(def.isView())
root.addContent(viewToElement(def));
else
root.addContent(storeToElement(def));
}
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
return serializer.outputString(root);
}
public String writeStore(StoreDefinition store) {
XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
if(store.isView())
return serializer.outputString(viewToElement(store));
else
return serializer.outputString(storeToElement(store));
}
private Element storeToElement(StoreDefinition storeDefinition) {
Element store = new Element(STORE_ELMT);
store.addContent(new Element(STORE_NAME_ELMT).setText(storeDefinition.getName()));
store.addContent(new Element(STORE_PERSISTENCE_ELMT).setText(storeDefinition.getType()));
if(storeDefinition.getDescription() != null)
store.addContent(new Element(STORE_DESCRIPTION_ELMT).setText(storeDefinition.getDescription()));
if(storeDefinition.getOwners() != null && storeDefinition.getOwners().size() > 0) {
String ownersText = StringUtils.join(storeDefinition.getOwners().toArray(), ", ");
store.addContent(new Element(STORE_OWNERS_ELMT).setText(ownersText));
}
store.addContent(new Element(STORE_ROUTING_STRATEGY).setText(storeDefinition.getRoutingStrategyType()));
store.addContent(new Element(STORE_ROUTING_TIER_ELMT).setText(storeDefinition.getRoutingPolicy()
.toDisplay()));
store.addContent(new Element(STORE_REPLICATION_FACTOR_ELMT).setText(Integer.toString(storeDefinition.getReplicationFactor())));
HashMap<Integer, Integer> zoneReplicationFactor = storeDefinition.getZoneReplicationFactor();
if(zoneReplicationFactor != null) {
Element zoneReplicationFactorNode = new Element(STORE_ZONE_REPLICATION_FACTOR_ELMT);
for(Integer zone: zoneReplicationFactor.keySet()) {
zoneReplicationFactorNode.addContent(new Element(STORE_REPLICATION_FACTOR_ELMT).setText(Integer.toString(zoneReplicationFactor.get(zone)))
.setAttribute(STORE_ZONE_ID_ELMT,
Integer.toString(zone)));
}
store.addContent(zoneReplicationFactorNode);
}
if(storeDefinition.hasPreferredReads())
store.addContent(new Element(STORE_PREFERRED_READS_ELMT).setText(Integer.toString(storeDefinition.getPreferredReads())));
store.addContent(new Element(STORE_REQUIRED_READS_ELMT).setText(Integer.toString(storeDefinition.getRequiredReads())));
if(storeDefinition.hasPreferredWrites())
store.addContent(new Element(STORE_PREFERRED_WRITES_ELMT).setText(Integer.toString(storeDefinition.getPreferredWrites())));
store.addContent(new Element(STORE_REQUIRED_WRITES_ELMT).setText(Integer.toString(storeDefinition.getRequiredWrites())));
if(storeDefinition.hasZoneCountReads())
store.addContent(new Element(STORE_ZONE_COUNT_READS).setText(Integer.toString(storeDefinition.getZoneCountReads())));
if(storeDefinition.hasZoneCountWrites())
store.addContent(new Element(STORE_ZONE_COUNT_WRITES).setText(Integer.toString(storeDefinition.getZoneCountWrites())));
if(storeDefinition.hasHintedHandoffStrategyType())
store.addContent(new Element(HINTED_HANDOFF_STRATEGY).setText(storeDefinition.getHintedHandoffStrategyType()
.toDisplay()));
if(storeDefinition.hasHintPreflistSize())
store.addContent(new Element(HINT_PREFLIST_SIZE).setText(Integer.toString(storeDefinition.getHintPrefListSize())));
Element keySerializer = new Element(STORE_KEY_SERIALIZER_ELMT);
addSerializer(keySerializer, storeDefinition.getKeySerializer());
store.addContent(keySerializer);
Element valueSerializer = new Element(STORE_VALUE_SERIALIZER_ELMT);
addSerializer(valueSerializer, storeDefinition.getValueSerializer());
store.addContent(valueSerializer);
if(storeDefinition.hasRetentionPeriod())
store.addContent(new Element(STORE_RETENTION_POLICY_ELMT).setText(Integer.toString(storeDefinition.getRetentionDays())));
if(storeDefinition.hasRetentionScanThrottleRate())
store.addContent(new Element(STORE_RETENTION_SCAN_THROTTLE_RATE_ELMT).setText(Integer.toString(storeDefinition.getRetentionScanThrottleRate())));
if(storeDefinition.hasMemoryFootprint()) {
store.addContent(new Element(STORE_MEMORY_FOOTPRINT).setText(Long.toString(storeDefinition.getMemoryFootprintMB())));
}
return store;
}
private Element viewToElement(StoreDefinition storeDefinition) {
Element store = new Element(VIEW_ELMT);
store.addContent(new Element(STORE_NAME_ELMT).setText(storeDefinition.getName()));
store.addContent(new Element(VIEW_TARGET_ELMT).setText(storeDefinition.getViewTargetStoreName()));
if(storeDefinition.getDescription() != null)
store.addContent(new Element(STORE_DESCRIPTION_ELMT).setText(storeDefinition.getDescription()));
if(storeDefinition.getOwners() != null && storeDefinition.getOwners().size() > 0) {
String ownersText = StringUtils.join(storeDefinition.getOwners().toArray(), ", ");
store.addContent(new Element(STORE_OWNERS_ELMT).setText(ownersText));
}
if(storeDefinition.getValueTransformation() == null)
throw new MappingException("View " + storeDefinition.getName()
+ " has no defined transformation class.");
store.addContent(new Element(VIEW_TRANS_ELMT).setText(storeDefinition.getValueTransformation()));
store.addContent(new Element(STORE_ROUTING_TIER_ELMT).setText(storeDefinition.getRoutingPolicy()
.toDisplay()));
if(storeDefinition.hasPreferredReads())
store.addContent(new Element(STORE_PREFERRED_READS_ELMT).setText(Integer.toString(storeDefinition.getPreferredReads())));
store.addContent(new Element(STORE_REQUIRED_READS_ELMT).setText(Integer.toString(storeDefinition.getRequiredReads())));
if(storeDefinition.hasPreferredWrites())
store.addContent(new Element(STORE_PREFERRED_WRITES_ELMT).setText(Integer.toString(storeDefinition.getPreferredWrites())));
store.addContent(new Element(STORE_REQUIRED_WRITES_ELMT).setText(Integer.toString(storeDefinition.getRequiredWrites())));
if(storeDefinition.hasZoneCountReads())
store.addContent(new Element(STORE_ZONE_COUNT_READS).setText(Integer.toString(storeDefinition.getZoneCountReads())));
if(storeDefinition.hasZoneCountWrites())
store.addContent(new Element(STORE_ZONE_COUNT_WRITES).setText(Integer.toString(storeDefinition.getZoneCountWrites())));
Element valueSerializer = new Element(STORE_VALUE_SERIALIZER_ELMT);
addSerializer(valueSerializer, storeDefinition.getValueSerializer());
store.addContent(valueSerializer);
Element transformsSerializer = new Element(STORE_TRANSFORM_SERIALIZER_ELMT);
if(storeDefinition.getTransformsSerializer() != null) {
addSerializer(transformsSerializer, storeDefinition.getTransformsSerializer());
store.addContent(transformsSerializer);
}
Element serializerFactory = new Element(VIEW_SERIALIZER_FACTORY_ELMT);
if(storeDefinition.getSerializerFactory() != null) {
serializerFactory.setText(storeDefinition.getSerializerFactory());
store.addContent(serializerFactory);
}
return store;
}
public static void addSerializer(Element parent, SerializerDefinition def) {
parent.addContent(new Element(STORE_SERIALIZATION_TYPE_ELMT).setText(def.getName()));
if(def.hasSchemaInfo()) {
for(Map.Entry<Integer, String> entry: def.getAllSchemaInfoVersions().entrySet()) {
Element schemaElmt = new Element(STORE_SERIALIZATION_META_ELMT);
if(def.hasVersion())
schemaElmt.setAttribute(STORE_VERSION_ATTR, Integer.toString(entry.getKey()));
else
schemaElmt.setAttribute(STORE_VERSION_ATTR, "none");
schemaElmt.setText(entry.getValue());
parent.addContent(schemaElmt);
}
}
if(def.hasCompression()) {
Compression compression = def.getCompression();
Element compressionElmt = new Element(STORE_COMPRESSION_ELMT);
Element type = new Element(STORE_COMPRESSION_TYPE_ELMT);
type.setText(compression.getType());
compressionElmt.addContent(type);
String optionsText = compression.getOptions();
if(optionsText != null) {
Element options = new Element(STORE_COMPRESSION_OPTIONS_ELMT);
options.setText(optionsText);
compressionElmt.addContent(options);
}
parent.addContent(compressionElmt);
}
}
public Integer getChildWithDefault(Element elmt, String property, Integer defaultVal) {
if(elmt.getChildText(property) == null)
return defaultVal;
else
return Integer.parseInt(elmt.getChildText(property));
}
}