Search in sources :

Example 6 with PropertyType

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);
}
Also used : HelixProperty(org.apache.helix.HelixProperty) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey)

Example 7 with PropertyType

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;
}
Also used : HelixProperty(org.apache.helix.HelixProperty) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) ZNRecordAssembler(org.apache.helix.ZNRecordAssembler) ZNRecord(org.apache.helix.ZNRecord)

Example 8 with PropertyType

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;
}
Also used : HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) ZNRecordBucketizer(org.apache.helix.ZNRecordBucketizer) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) ZNRecord(org.apache.helix.ZNRecord)

Example 9 with PropertyType

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;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) PropertyType(org.apache.helix.PropertyType)

Example 10 with PropertyType

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;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) HelixProperty(org.apache.helix.HelixProperty) PropertyType(org.apache.helix.PropertyType) ZNRecordAssembler(org.apache.helix.ZNRecordAssembler) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

PropertyType (org.apache.helix.PropertyType)11 ZNRecord (org.apache.helix.ZNRecord)6 ArrayList (java.util.ArrayList)5 HelixProperty (org.apache.helix.HelixProperty)5 PropertyKey (org.apache.helix.PropertyKey)3 ZNRecordAssembler (org.apache.helix.ZNRecordAssembler)3 Stat (org.apache.zookeeper.data.Stat)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)2 ZNRecordBucketizer (org.apache.helix.ZNRecordBucketizer)2 HashMap (java.util.HashMap)1 List (java.util.List)1 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)1