View Javadoc

1   package org.apache.helix;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Constructor;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * A wrapper class for ZNRecord. Used as a base class for IdealState, CurrentState, etc.
32   */
33  public class HelixProperty
34  {
35    public enum HelixPropertyAttribute
36    {
37      BUCKET_SIZE,
38      BATCH_MESSAGE_MODE
39    }
40  
41    protected final ZNRecord _record;
42  
43    public HelixProperty(String id)
44    {
45      _record = new ZNRecord(id);
46    }
47  
48    public HelixProperty(ZNRecord record)
49    {
50      _record = new ZNRecord(record);
51    }
52  
53    public final String getId()
54    {
55      return _record.getId();
56    }
57  
58    public final ZNRecord getRecord()
59    {
60      return _record;
61    }
62  
63    public final void setDeltaList(List<ZNRecordDelta> deltaList)
64    {
65      _record.setDeltaList(deltaList);
66    }
67  
68    @Override
69    public String toString()
70    {
71      return _record.toString();
72    }
73  
74    public int getBucketSize()
75    {
76      String bucketSizeStr =
77          _record.getSimpleField(HelixPropertyAttribute.BUCKET_SIZE.toString());
78      int bucketSize = 0;
79      if (bucketSizeStr != null)
80      {
81        try
82        {
83          bucketSize = Integer.parseInt(bucketSizeStr);
84        }
85        catch (NumberFormatException e)
86        {
87          // OK
88        }
89      }
90      return bucketSize;
91    }
92  
93    public void setBucketSize(int bucketSize)
94    {
95      if (bucketSize <= 0)
96        bucketSize = 0;
97  
98      _record.setSimpleField(HelixPropertyAttribute.BUCKET_SIZE.toString(), "" + bucketSize);
99    }
100 
101   /**
102    * static method that convert ZNRecord to an instance that subclasses HelixProperty
103    * 
104    * @param clazz
105    * @param record
106    * @return
107    */
108   public static <T extends HelixProperty> T convertToTypedInstance(Class<T> clazz,
109                                                                    ZNRecord record)
110   {
111     if (record == null)
112     {
113       return null;
114     }
115 
116     try
117     {
118       Constructor<T> constructor = clazz.getConstructor(new Class[] { ZNRecord.class });
119       return constructor.newInstance(record);
120     }
121     catch (Exception e)
122     {
123       // TODO Auto-generated catch block
124       e.printStackTrace();
125     }
126 
127     return null;
128   }
129 
130   public static <T extends HelixProperty> List<T> convertToTypedList(Class<T> clazz,
131                                                                      Collection<ZNRecord> records)
132   {
133     if (records == null)
134     {
135       return null;
136     }
137 
138     List<T> decorators = new ArrayList<T>();
139     for (ZNRecord record : records)
140     {
141       T decorator = HelixProperty.convertToTypedInstance(clazz, record);
142       if (decorator != null)
143       {
144         decorators.add(decorator);
145       }
146     }
147     return decorators;
148   }
149 
150   public static <T extends HelixProperty> Map<String, T> convertListToMap(List<T> records)
151   {
152     if (records == null)
153     {
154       return Collections.emptyMap();
155     }
156 
157     Map<String, T> decorators = new HashMap<String, T>();
158     for (T record : records)
159     {
160       decorators.put(record.getId(), record);
161     }
162     return decorators;
163   }
164 
165   public static <T extends HelixProperty> List<ZNRecord> convertToList(List<T> typedInstances)
166   {
167     if (typedInstances == null)
168     {
169       return Collections.emptyList();
170     }
171 
172     List<ZNRecord> records = new ArrayList<ZNRecord>();
173     for (T typedInstance : typedInstances)
174     {
175       records.add(typedInstance.getRecord());
176     }
177 
178     return records;
179   }
180 
181   public void setBatchMessageMode(boolean enable)
182   {
183     _record.setSimpleField(HelixPropertyAttribute.BATCH_MESSAGE_MODE.toString(), ""
184         + enable);
185   }
186 
187   public boolean getBatchMessageMode()
188   {
189     String enableStr =
190         _record.getSimpleField(HelixPropertyAttribute.BATCH_MESSAGE_MODE.toString());
191     if (enableStr == null)
192     {
193       return false;
194     }
195 
196     try
197     {
198       return Boolean.parseBoolean(enableStr.toLowerCase());
199     }
200     catch (Exception e)
201     {
202       return false;
203     }
204   }
205   
206   public boolean isValid()
207   {
208     return true;
209   }
210 
211   @Override
212   public boolean equals(Object obj)
213   {
214     if (obj == null)
215     {
216       return false;
217     }
218     if (obj instanceof HelixProperty)
219     {
220       HelixProperty that = (HelixProperty) obj;
221       if (that.getRecord() != null)
222       {
223         return that.getRecord().equals(this.getRecord());
224       }
225     }
226     return false;
227   }
228 }