Search in sources :

Example 1 with PropertyType

use of org.apache.helix.PropertyType in project helix by apache.

the class ZKHelixDataAccessor method removeProperty.

@Override
public boolean removeProperty(PropertyKey key) {
    PropertyType type = key.getType();
    String path = key.getPath();
    int options = constructOptions(type);
    return _baseDataAccessor.remove(path, options);
}
Also used : PropertyType(org.apache.helix.PropertyType)

Example 2 with PropertyType

use of org.apache.helix.PropertyType in project helix by apache.

the class ZKHelixDataAccessor method getChildNames.

@Override
public List<String> getChildNames(PropertyKey key) {
    PropertyType type = key.getType();
    String parentPath = key.getPath();
    int options = constructOptions(type);
    List<String> childNames = _baseDataAccessor.getChildNames(parentPath, options);
    if (childNames == null) {
        childNames = Collections.emptyList();
    }
    return childNames;
}
Also used : PropertyType(org.apache.helix.PropertyType)

Example 3 with PropertyType

use of org.apache.helix.PropertyType in project helix by apache.

the class ZKHelixDataAccessor method setChildren.

@Override
public <T extends HelixProperty> boolean[] setChildren(List<PropertyKey> keys, List<T> children) {
    int options = -1;
    List<String> paths = new ArrayList<String>();
    List<ZNRecord> records = new ArrayList<ZNRecord>();
    List<List<String>> bucketizedPaths = new ArrayList<List<String>>(Collections.<List<String>>nCopies(keys.size(), null));
    List<List<ZNRecord>> bucketizedRecords = new ArrayList<List<ZNRecord>>(Collections.<List<ZNRecord>>nCopies(keys.size(), null));
    for (int i = 0; i < keys.size(); i++) {
        PropertyKey key = keys.get(i);
        PropertyType type = key.getType();
        String path = key.getPath();
        paths.add(path);
        options = constructOptions(type);
        HelixProperty value = children.get(i);
        switch(type) {
            case EXTERNALVIEW:
                if (value.getBucketSize() == 0) {
                    records.add(value.getRecord());
                } else {
                    ZNRecord metaRecord = new ZNRecord(value.getId());
                    metaRecord.setSimpleFields(value.getRecord().getSimpleFields());
                    records.add(metaRecord);
                    ZNRecordBucketizer bucketizer = new ZNRecordBucketizer(value.getBucketSize());
                    Map<String, ZNRecord> map = bucketizer.bucketize(value.getRecord());
                    List<String> childBucketizedPaths = new ArrayList<String>();
                    List<ZNRecord> childBucketizedRecords = new ArrayList<ZNRecord>();
                    for (String bucketName : map.keySet()) {
                        childBucketizedPaths.add(path + "/" + bucketName);
                        childBucketizedRecords.add(map.get(bucketName));
                    }
                    bucketizedPaths.set(i, childBucketizedPaths);
                    bucketizedRecords.set(i, childBucketizedRecords);
                }
                break;
            case STATEMODELDEFS:
                if (value.isValid()) {
                    records.add(value.getRecord());
                }
                break;
            default:
                records.add(value.getRecord());
                break;
        }
    }
    // set non-bucketized nodes or parent nodes of bucketized nodes
    boolean[] success = _baseDataAccessor.setChildren(paths, records, options);
    // set bucketized nodes
    List<String> allBucketizedPaths = new ArrayList<String>();
    List<ZNRecord> allBucketizedRecords = new ArrayList<ZNRecord>();
    for (int i = 0; i < keys.size(); i++) {
        if (success[i] && bucketizedPaths.get(i) != null) {
            allBucketizedPaths.addAll(bucketizedPaths.get(i));
            allBucketizedRecords.addAll(bucketizedRecords.get(i));
        }
    }
    // TODO: set success accordingly
    _baseDataAccessor.setChildren(allBucketizedPaths, allBucketizedRecords, options);
    return success;
}
Also used : ZNRecordBucketizer(org.apache.helix.ZNRecordBucketizer) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) HelixProperty(org.apache.helix.HelixProperty) ArrayList(java.util.ArrayList) List(java.util.List) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey)

Example 4 with PropertyType

use of org.apache.helix.PropertyType in project helix by apache.

the class ZKHelixDataAccessor method getProperty.

@Override
public <T extends HelixProperty> List<T> getProperty(List<PropertyKey> keys, boolean throwException) throws HelixMetaDataAccessException {
    if (keys == null || keys.size() == 0) {
        return Collections.emptyList();
    }
    List<T> childValues = new ArrayList<T>();
    // read all records
    List<String> paths = new ArrayList<>();
    List<Stat> stats = new ArrayList<>();
    for (PropertyKey key : keys) {
        paths.add(key.getPath());
        stats.add(new Stat());
    }
    List<ZNRecord> children = _baseDataAccessor.get(paths, stats, 0, throwException);
    // check if bucketized
    for (int i = 0; i < keys.size(); i++) {
        PropertyKey key = keys.get(i);
        ZNRecord record = children.get(i);
        Stat stat = stats.get(i);
        PropertyType type = key.getType();
        String path = key.getPath();
        int options = constructOptions(type);
        if (record != null) {
            record.setCreationTime(stat.getCtime());
            record.setModifiedTime(stat.getMtime());
            record.setVersion(stat.getVersion());
        }
        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, 1, 0);
                        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);
        childValues.add(t);
    }
    return childValues;
}
Also used : ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) Stat(org.apache.zookeeper.data.Stat) HelixProperty(org.apache.helix.HelixProperty) ZNRecordAssembler(org.apache.helix.ZNRecordAssembler) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 5 with PropertyType

use of org.apache.helix.PropertyType in project helix by apache.

the class ZKHelixDataAccessor method updateProperty.

@Override
public <T extends HelixProperty> boolean updateProperty(PropertyKey key, DataUpdater<ZNRecord> updater, T value) {
    PropertyType type = key.getType();
    String path = key.getPath();
    int options = constructOptions(type);
    boolean success = false;
    switch(type) {
        case CURRENTSTATES:
            success = _groupCommit.commit(_baseDataAccessor, options, path, value.getRecord(), true);
            break;
        case STATUSUPDATES:
            if (LOG.isTraceEnabled()) {
                LOG.trace("Update status. path: " + key.getPath() + ", record: " + value.getRecord());
            }
            break;
        default:
            success = _baseDataAccessor.update(path, updater, options);
            break;
    }
    return success;
}
Also used : PropertyType(org.apache.helix.PropertyType)

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