1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.conf;
18
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import org.apache.accumulo.core.client.AccumuloException;
25 import org.apache.accumulo.core.client.Connector;
26 import org.apache.accumulo.core.client.TableNotFoundException;
27 import org.apache.accumulo.core.client.impl.Tables;
28 import org.apache.log4j.Logger;
29
30 public abstract class AccumuloConfiguration implements Iterable<Entry<String,String>> {
31 private static final Logger log = Logger.getLogger(AccumuloConfiguration.class);
32
33 public abstract String get(Property property);
34
35 public abstract Iterator<Entry<String,String>> iterator();
36
37 private void checkType(Property property, PropertyType type) {
38 if (!property.getType().equals(type)) {
39 String msg = "Configuration method intended for type " + type + " called with a " + property.getType() + " argument (" + property.getKey() + ")";
40 IllegalArgumentException err = new IllegalArgumentException(msg);
41 log.error(msg, err);
42 throw err;
43 }
44 }
45
46 /**
47 * This method returns all properties in a map of string->string under the given prefix property.
48 * @param property the prefix property, and must be of type PropertyType.PREFIX
49 * @return a map of strings to strings of the resulting properties
50 */
51 public Map<String, String> getAllPropertiesWithPrefix(Property property) {
52 checkType(property, PropertyType.PREFIX);
53
54 Map<String, String> propMap = new HashMap<String, String>();
55
56 for (Entry<String, String> entry : this) {
57 if (entry.getKey().startsWith(property.getKey())) {
58 propMap.put(entry.getKey(), entry.getValue());
59 }
60 }
61
62 return propMap;
63 }
64
65 public long getMemoryInBytes(Property property) {
66 checkType(property, PropertyType.MEMORY);
67
68 String memString = get(property);
69 return getMemoryInBytes(memString);
70 }
71
72 static public long getMemoryInBytes(String str) {
73 int multiplier = 0;
74 switch (str.charAt(str.length() - 1)) {
75 case 'G':
76 multiplier += 10;
77 case 'M':
78 multiplier += 10;
79 case 'K':
80 multiplier += 10;
81 case 'B':
82 return Long.parseLong(str.substring(0, str.length() - 1)) << multiplier;
83 default:
84 return Long.parseLong(str);
85 }
86 }
87
88 public long getTimeInMillis(Property property) {
89 checkType(property, PropertyType.TIMEDURATION);
90
91 return getTimeInMillis(get(property));
92 }
93
94 static public long getTimeInMillis(String str) {
95 int multiplier = 1;
96 switch (str.charAt(str.length() - 1)) {
97 case 'd':
98 multiplier *= 24;
99 case 'h':
100 multiplier *= 60;
101 case 'm':
102 multiplier *= 60;
103 case 's':
104 multiplier *= 1000;
105 if (str.length() > 1 && str.endsWith("ms"))
106
107 return Long.parseLong(str.substring(0, str.length() - 2));
108 return Long.parseLong(str.substring(0, str.length() - 1)) * multiplier;
109 default:
110 return Long.parseLong(str) * 1000;
111 }
112 }
113
114 public boolean getBoolean(Property property) {
115 checkType(property, PropertyType.BOOLEAN);
116 return Boolean.parseBoolean(get(property));
117 }
118
119 public double getFraction(Property property) {
120 checkType(property, PropertyType.FRACTION);
121
122 return getFraction(get(property));
123 }
124
125 public double getFraction(String str) {
126 if (str.charAt(str.length() - 1) == '%')
127 return Double.parseDouble(str.substring(0, str.length() - 1)) / 100.0;
128 return Double.parseDouble(str);
129 }
130
131 public int getPort(Property property) {
132 checkType(property, PropertyType.PORT);
133
134 String portString = get(property);
135 int port = Integer.parseInt(portString);
136 if (port != 0) {
137 if (port < 1024 || port > 65535) {
138 log.error("Invalid port number " + port + "; Using default " + property.getDefaultValue());
139 port = Integer.parseInt(property.getDefaultValue());
140 }
141 }
142 return port;
143 }
144
145 public int getCount(Property property) {
146 checkType(property, PropertyType.COUNT);
147
148 String countString = get(property);
149 return Integer.parseInt(countString);
150 }
151
152 public static synchronized DefaultConfiguration getDefaultConfiguration() {
153 return DefaultConfiguration.getInstance();
154 }
155
156
157 /**
158 *
159 * @deprecated not for client use
160 */
161 @Deprecated
162 public static synchronized AccumuloConfiguration getSiteConfiguration() {
163 return SiteConfiguration.getInstance(getDefaultConfiguration());
164 }
165
166 public static AccumuloConfiguration getTableConfiguration(Connector conn, String tableId) throws TableNotFoundException, AccumuloException {
167 String tableName = Tables.getTableName(conn.getInstance(), tableId);
168 return new ConfigurationCopy(conn.tableOperations().getProperties(tableName));
169 }
170
171 public int getMaxFilesPerTablet() {
172 int maxFilesPerTablet = getCount(Property.TABLE_FILE_MAX);
173 if (maxFilesPerTablet <= 0) {
174 maxFilesPerTablet = getCount(Property.TSERV_SCAN_MAX_OPENFILES) - 1;
175 log.debug("Max files per tablet " + maxFilesPerTablet);
176 }
177
178 return maxFilesPerTablet;
179 }
180 }