use of org.apache.helix.PropertyType in project helix by apache.
the class ZKHelixDataAccessor method createChildren.
@Override
public <T extends HelixProperty> boolean[] createChildren(List<PropertyKey> keys, List<T> children) {
// TODO: add validation
int options = -1;
List<String> paths = new ArrayList<String>();
List<ZNRecord> records = new ArrayList<ZNRecord>();
for (int i = 0; i < keys.size(); i++) {
PropertyKey key = keys.get(i);
PropertyType type = key.getType();
String path = key.getPath();
paths.add(path);
HelixProperty value = children.get(i);
records.add(value.getRecord());
options = constructOptions(type);
}
return _baseDataAccessor.createChildren(paths, records, options);
}
use of org.apache.helix.PropertyType in project helix by apache.
the class ZKHelixDataAccessor method getChildValues.
@Override
public <T extends HelixProperty> List<T> getChildValues(PropertyKey key, boolean throwException) {
PropertyType type = key.getType();
String parentPath = key.getPath();
int options = constructOptions(type);
List<T> childValues = new ArrayList<T>();
List<ZNRecord> children;
if (throwException) {
children = _baseDataAccessor.getChildren(parentPath, null, options, 1, 0);
} else {
children = _baseDataAccessor.getChildren(parentPath, null, options);
}
if (children != null) {
for (ZNRecord record : children) {
switch(type) {
case CURRENTSTATES:
case IDEALSTATES:
case EXTERNALVIEW:
if (record != null) {
HelixProperty property = new HelixProperty(record);
int bucketSize = property.getBucketSize();
if (bucketSize > 0) {
// TODO: fix this if record.id != pathName
String childPath = parentPath + "/" + record.getId();
List<ZNRecord> childRecords;
if (throwException) {
childRecords = _baseDataAccessor.getChildren(childPath, null, options, 1, 0);
} else {
childRecords = _baseDataAccessor.getChildren(childPath, null, options);
}
ZNRecord assembledRecord = new ZNRecordAssembler().assemble(childRecords);
// merge with parent node value
if (assembledRecord != null) {
record.getSimpleFields().putAll(assembledRecord.getSimpleFields());
record.getListFields().putAll(assembledRecord.getListFields());
record.getMapFields().putAll(assembledRecord.getMapFields());
}
}
}
break;
default:
break;
}
if (record != null) {
@SuppressWarnings("unchecked") T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
childValues.add(t);
}
}
}
return childValues;
}
use of org.apache.helix.PropertyType in project helix by apache.
the class ZKHelixDataAccessor method setProperty.
@Override
public <T extends HelixProperty> boolean setProperty(PropertyKey key, T value) {
PropertyType type = key.getType();
if (!value.isValid()) {
throw new HelixMetaDataAccessException("The ZNRecord for " + type + " is not valid.");
}
String path = key.getPath();
int options = constructOptions(type);
boolean success = false;
switch(type) {
case IDEALSTATES:
case EXTERNALVIEW:
// check if bucketized
if (value.getBucketSize() > 0) {
// set parent node
ZNRecord metaRecord = new ZNRecord(value.getId());
metaRecord.setSimpleFields(value.getRecord().getSimpleFields());
success = _baseDataAccessor.set(path, metaRecord, options);
if (success) {
ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(value.getBucketSize());
Map<String, ZNRecord> map = bucketizer.bucketize(value.getRecord());
List<String> paths = new ArrayList<String>();
List<ZNRecord> bucketizedRecords = new ArrayList<ZNRecord>();
for (String bucketName : map.keySet()) {
paths.add(path + "/" + bucketName);
bucketizedRecords.add(map.get(bucketName));
}
// TODO: set success accordingly
_baseDataAccessor.setChildren(paths, bucketizedRecords, options);
}
} else {
success = _baseDataAccessor.set(path, value.getRecord(), options);
}
break;
default:
success = _baseDataAccessor.set(path, value.getRecord(), options);
break;
}
return success;
}
use of org.apache.helix.PropertyType in project helix by apache.
the class ZKHelixDataAccessor method getPropertyStat.
@Override
public HelixProperty.Stat getPropertyStat(PropertyKey key) {
PropertyType type = key.getType();
String path = key.getPath();
int options = constructOptions(type);
try {
Stat stat = _baseDataAccessor.getStat(path, options);
if (stat != null) {
return new HelixProperty.Stat(stat.getVersion(), stat.getCtime(), stat.getMtime());
}
} catch (ZkNoNodeException e) {
}
return null;
}
use of org.apache.helix.PropertyType in project helix by apache.
the class ZKHelixDataAccessor method getProperty.
@Override
public <T extends HelixProperty> T getProperty(PropertyKey key) {
PropertyType type = key.getType();
String path = key.getPath();
int options = constructOptions(type);
ZNRecord record = null;
try {
Stat stat = new Stat();
record = _baseDataAccessor.get(path, stat, options);
if (record != null) {
record.setCreationTime(stat.getCtime());
record.setModifiedTime(stat.getMtime());
record.setVersion(stat.getVersion());
}
} catch (ZkNoNodeException e) {
// OK
}
switch(type) {
case CURRENTSTATES:
case IDEALSTATES:
case EXTERNALVIEW:
// check if bucketized
if (record != null) {
HelixProperty property = new HelixProperty(record);
int bucketSize = property.getBucketSize();
if (bucketSize > 0) {
// @see HELIX-574
// clean up list and map fields in case we write to parent node by mistake
property.getRecord().getMapFields().clear();
property.getRecord().getListFields().clear();
List<ZNRecord> childRecords = _baseDataAccessor.getChildren(path, null, options);
ZNRecord assembledRecord = new ZNRecordAssembler().assemble(childRecords);
// merge with parent node value
if (assembledRecord != null) {
record.getSimpleFields().putAll(assembledRecord.getSimpleFields());
record.getListFields().putAll(assembledRecord.getListFields());
record.getMapFields().putAll(assembledRecord.getMapFields());
}
}
}
break;
default:
break;
}
@SuppressWarnings("unchecked") T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
return t;
}
Aggregations