1 package org.apache.helix.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.TreeMap;
25
26 import org.apache.helix.HelixProperty;
27 import org.apache.helix.ZNRecord;
28 import org.apache.log4j.Logger;
29
30
31
32
33
34 public class CurrentState extends HelixProperty
35 {
36 private static Logger LOG = Logger.getLogger(CurrentState.class);
37
38 public enum CurrentStateProperty
39 {
40 SESSION_ID, CURRENT_STATE, STATE_MODEL_DEF, STATE_MODEL_FACTORY_NAME, RESOURCE
41
42 }
43
44 public CurrentState(String resourceName)
45 {
46 super(resourceName);
47 }
48
49 public CurrentState(ZNRecord record)
50 {
51 super(record);
52 }
53
54 public String getResourceName()
55 {
56 return _record.getId();
57 }
58
59 public Map<String, String> getPartitionStateMap()
60 {
61 Map<String, String> map = new HashMap<String, String>();
62 Map<String, Map<String, String>> mapFields = _record.getMapFields();
63 for (String partitionName : mapFields.keySet())
64 {
65 Map<String, String> tempMap = mapFields.get(partitionName);
66 if (tempMap != null)
67 {
68 map.put(partitionName, tempMap.get(CurrentStateProperty.CURRENT_STATE.toString()));
69 }
70 }
71 return map;
72 }
73
74 public String getSessionId()
75 {
76 return _record.getSimpleField(CurrentStateProperty.SESSION_ID.toString());
77 }
78
79 public void setSessionId(String sessionId)
80 {
81 _record.setSimpleField(CurrentStateProperty.SESSION_ID.toString(), sessionId);
82 }
83
84 public String getState(String partitionName)
85 {
86 Map<String, Map<String, String>> mapFields = _record.getMapFields();
87 Map<String, String> mapField = mapFields.get(partitionName);
88 if (mapField != null)
89 {
90 return mapField.get(CurrentStateProperty.CURRENT_STATE.toString());
91 }
92 return null;
93 }
94
95 public void setStateModelDefRef(String stateModelName)
96 {
97 _record.setSimpleField(CurrentStateProperty.STATE_MODEL_DEF.toString(),
98 stateModelName);
99 }
100
101 public String getStateModelDefRef()
102 {
103 return _record.getSimpleField(CurrentStateProperty.STATE_MODEL_DEF.toString());
104 }
105
106 public void setState(String partitionName, String state)
107 {
108 Map<String, Map<String, String>> mapFields = _record.getMapFields();
109 if (mapFields.get(partitionName) == null)
110 {
111 mapFields.put(partitionName, new TreeMap<String, String>());
112 }
113 mapFields.get(partitionName)
114 .put(CurrentStateProperty.CURRENT_STATE.toString(), state);
115 }
116
117 public void setStateModelFactoryName(String factoryName)
118 {
119 _record.setSimpleField(CurrentStateProperty.STATE_MODEL_FACTORY_NAME.toString(),
120 factoryName);
121 }
122
123 public String getStateModelFactoryName()
124 {
125 return _record.getSimpleField(CurrentStateProperty.STATE_MODEL_FACTORY_NAME.toString());
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 @Override
158 public boolean isValid()
159 {
160 if (getStateModelDefRef() == null)
161 {
162 LOG.error("Current state does not contain state model ref. id:" + getResourceName());
163 return false;
164 }
165 if (getSessionId() == null)
166 {
167 LOG.error("CurrentState does not contain session id, id : " + getResourceName());
168 return false;
169 }
170 return true;
171 }
172
173 }