Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.helix.model;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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.
  */
 
 import java.util.Map;
 
Current states of partitions in a resource for an instance.
 
 public class CurrentState extends HelixProperty {
   private static Logger LOG = Logger.getLogger(CurrentState.class);

  
Lookup keys for the current state
 
   public enum CurrentStateProperty {
     SESSION_ID,
     CURRENT_STATE,
     STATE_MODEL_DEF,
     STATE_MODEL_FACTORY_NAME,
     RESOURCE // ,
              // BUCKET_SIZE
   }

  
Instantiate a current state with a resource

Parameters:
resourceName name identifying the resource
 
   public CurrentState(String resourceName) {
     super(resourceName);
   }

  
Instantiate a current state with a resource

Parameters:
resourceId identifier for the resource
 
   public CurrentState(ResourceId resourceId) {
     super(resourceId.stringify());
   }

  
Instantiate a current state with a pre-populated ZNRecord

Parameters:
record a ZNRecord corresponding to the current state
 
   public CurrentState(ZNRecord record) {
     super(record);
   }

  
Get the name of the resource

Returns:
String resource identifier
 
   public String getResourceName() {
     return .getId();
   }

  
Get the resource id

Returns:
ResourceId
 
   public ResourceId getResourceId() {
     return ResourceId.from(getResourceName());
   }

  
Get the partitions on this instance and the state that each partition is currently in.

Returns:
(partition, state) pairs
 
   public Map<StringStringgetPartitionStateMap() {
     Map<StringStringmap = new HashMap<StringString>();
    Map<StringMap<StringString>> mapFields = .getMapFields();
    for (String partitionName : mapFields.keySet()) {
      Map<StringStringtempMap = mapFields.get(partitionName);
      if (tempMap != null) {
        map.put(partitionNametempMap.get(..toString()));
      }
    }
    return map;
  }

  
Get the partitions on this instance and the state that each partition is currently in

Returns:
(partition id, state) pairs
    Map<PartitionIdStatemap = new HashMap<PartitionIdState>();
    for (String partitionName : .getMapFields().keySet()) {
      Map<StringStringstateMap = .getMapField(partitionName);
      if (stateMap != null) {
        map.put(PartitionId.from(partitionName),
            State.from(stateMap.get(..toString())));
      }
    }
    return map;
  }

  
Get the session that this current state corresponds to

Returns:
session identifier
    return SessionId.from(getSessionId());
  }

  
Get the session that this current state corresponds to

Returns:
session identifier
  public String getSessionId() {
  }

  
Set the session that this current state corresponds to

Parameters:
sessionId session identifier
  public void setSessionId(SessionId sessionId) {
    setSessionId(sessionId.stringify());
  }

  
Set the session that this current state corresponds to

Parameters:
sessionId session identifier
  public void setSessionId(String sessionId) {
  }

  
Get the state of a partition on this instance

Parameters:
partitionName the name of the partition
Returns:
the state, or null if the partition is not present
  public String getState(String partitionName) {
    Map<StringMap<StringString>> mapFields = .getMapFields();
    Map<StringStringmapField = mapFields.get(partitionName);
    if (mapField != null) {
      return mapField.get(..toString());
    }
    return null;
  }

  
Get the state of a partition on this instance

Parameters:
partitionId partition id
Returns:
State
  public State getState(PartitionId partitionId) {
    return State.from(getState(partitionId.stringify()));
  }

  
Set the state model that the resource follows

Parameters:
stateModelName an identifier of the state model
  public void setStateModelDefRef(String stateModelName) {
  }

  
Get the state model that the resource follows

Returns:
an identifier of the state model
  public String getStateModelDefRef() {
  }

  
Set the state model that the resource follows

Parameters:
stateModelName an identifier of the state model
  public void setStateModelDefId(StateModelDefId stateModelId) {
        stateModelId.stringify());
  }

  
Get the state model that the resource follows

Returns:
an identifier of the state model
    return StateModelDefId.from(getStateModelDefRef());
  }

  
Set the state that a partition is currently in on this instance

Parameters:
partitionId the id of the partition
state the state of the partition
  public void setState(PartitionId partitionIdState state) {
    Map<StringMap<StringString>> mapFields = .getMapFields();
    if (mapFields.get(partitionId.stringify()) == null) {
      mapFields.put(partitionId.stringify(), new TreeMap<StringString>());
    }
    mapFields.get(partitionId.stringify()).put(..toString(),
        state.toString());
  }

  
Set the state that a partition is currently in on this instance

Parameters:
partitionName the name of the partition
state the state of the partition
  public void setState(String partitionNameString state) {
    Map<StringMap<StringString>> mapFields = .getMapFields();
    if (mapFields.get(partitionName) == null) {
      mapFields.put(partitionNamenew TreeMap<StringString>());
    }
    mapFields.get(partitionName).put(..toString(), state);
  }

  
Set the state model factory

Parameters:
factoryName the name of the factory
  public void setStateModelFactoryName(String factoryName) {
  }

  
Get the state model factory

Returns:
a name that identifies the state model factory
  }
  // @Override
  // public int getBucketSize()
  // {
  // String bucketSizeStr =
  // _record.getSimpleField(CurrentStateProperty.BUCKET_SIZE.toString());
  // int bucketSize = 0;
  // if (bucketSizeStr != null)
  // {
  // try
  // {
  // bucketSize = Integer.parseInt(bucketSizeStr);
  // }
  // catch (NumberFormatException e)
  // {
  // // OK
  // }
  // }
  // return bucketSize;
  // }
  //
  // @Override
  // public void setBucketSize(int bucketSize)
  // {
  // if (bucketSize > 0)
  // {
  // _record.setSimpleField(CurrentStateProperty.BUCKET_SIZE.toString(), "" + bucketSize);
  // }
  // }
  public boolean isValid() {
    if (getStateModelDefRef() == null) {
      .error("Current state does not contain state model ref. id:" + getResourceName());
      return false;
    }
    if (getSessionId() == null) {
      .error("CurrentState does not contain session id, id : " + getResourceName());
      return false;
    }
    return true;
  }

  
Convert a string map to a concrete partition map

Parameters:
rawMap map of partition name to state name
Returns:
map of partition id to state
  public static Map<PartitionIdStatepartitionStateMapFromStringMap(Map<StringStringrawMap) {
    if (rawMap == null) {
      return Collections.emptyMap();
    }
    Map<PartitionIdStatepartitionStateMap = new HashMap<PartitionIdState>();
    for (String partitionId : rawMap.keySet()) {
      partitionStateMap.put(PartitionId.from(partitionId), State.from(rawMap.get(partitionId)));
    }
    return partitionStateMap;
  }

  
Convert a partition map to a string map

Parameters:
partitionStateMap map of partition id to state
Returns:
map of partition name to state name
      Map<PartitionIdStatepartitionStateMap) {
    if (partitionStateMap == null) {
      return Collections.emptyMap();
    }
    Map<StringStringrawMap = new HashMap<StringString>();
    for (PartitionId partitionId : partitionStateMap.keySet()) {
      rawMap.put(partitionId.stringify(), partitionStateMap.get(partitionId).toString());
    }
    return rawMap;
  }
New to GrepCode? Check out our FAQ X