Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/SimpleCatalog.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.impl;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.NoSuchElementException;
15
16 import com.sleepycat.compat.DbCompat;
17 import java.util.IdentityHashMap;
18 import com.sleepycat.persist.raw.RawObject;
19 import com.sleepycat.util.ClassResolver;
20
21 /**
22 * A static catalog containing simple types only. Once created, this catalog
23 * is immutable.
24 *
25 * For bindings accessed by a PersistComparator during recovery, the
26 * SimpleCatalog provides formats for all simple types. To reduce redundant
27 * format objects, the SimpleCatalog's formats are copied when creating a
28 * regular PersistCatalog.
29 *
30 * This class also contains utility methods for dealing with primitives.
31 *
32 * @author Mark Hayes
33 */
34 public class SimpleCatalog implements Catalog {
35
36 private static final Map<String, Class> keywordToPrimitive;
37 static {
38 keywordToPrimitive = new HashMap<String, Class>(8);
39 keywordToPrimitive.put("boolean", Boolean.TYPE);
40 keywordToPrimitive.put("char", Character.TYPE);
41 keywordToPrimitive.put("byte", Byte.TYPE);
42 keywordToPrimitive.put("short", Short.TYPE);
43 keywordToPrimitive.put("int", Integer.TYPE);
44 keywordToPrimitive.put("long", Long.TYPE);
45 keywordToPrimitive.put("float", Float.TYPE);
46 keywordToPrimitive.put("double", Double.TYPE);
47 }
48
49 private static final Map<Class, Class> primitiveTypeToWrapper;
50 static {
51 primitiveTypeToWrapper = new HashMap<Class, Class>(8);
52 primitiveTypeToWrapper.put(Boolean.TYPE, Boolean.class);
53 primitiveTypeToWrapper.put(Character.TYPE, Character.class);
54 primitiveTypeToWrapper.put(Byte.TYPE, Byte.class);
55 primitiveTypeToWrapper.put(Short.TYPE, Short.class);
56 primitiveTypeToWrapper.put(Integer.TYPE, Integer.class);
57 primitiveTypeToWrapper.put(Long.TYPE, Long.class);
58 primitiveTypeToWrapper.put(Float.TYPE, Float.class);
59 primitiveTypeToWrapper.put(Double.TYPE, Double.class);
60 }
61
62 private static final SimpleCatalog instance = new SimpleCatalog(null);
63
64 static boolean isSimpleType(Class type) {
65 return instance.formatMap.containsKey(type.getName());
66 }
67
68 static Class primitiveToWrapper(Class type) {
69 Class cls = primitiveTypeToWrapper.get(type);
70 if (cls == null) {
71 throw DbCompat.unexpectedState(type.getName());
72 }
73 return cls;
74 }
75
76 public static Class resolveClass(String className, ClassLoader loader)
77 throws ClassNotFoundException {
78
79 Class cls = keywordToPrimitive.get(className);
80 if (cls == null) {
81 cls = ClassResolver.resolveClass(className, loader);
82 }
83 return cls;
84 }
85
86 public static Class resolveKeyClass(String className, ClassLoader loader) {
87 Class cls = keywordToPrimitive.get(className);
88 if (cls != null) {
89 cls = primitiveTypeToWrapper.get(cls);
90 } else {
91 try {
92 cls = ClassResolver.resolveClass(className, loader);
93 } catch (ClassNotFoundException e) {
94 throw new IllegalArgumentException
95 ("Key class not found: " + className);
96 }
97 }
98 return cls;
99 }
100
101 public static String keyClassName(String className) {
102 Class cls = keywordToPrimitive.get(className);
103 if (cls != null) {
104 cls = primitiveTypeToWrapper.get(cls);
105 return cls.getName();
106 } else {
107 return className;
108 }
109 }
110
111 static List<Format> getAllSimpleFormats(ClassLoader loader) {
112 return new ArrayList<Format>(new SimpleCatalog(loader).formatList);
113 }
114
115 static boolean addMissingSimpleFormats(ClassLoader loader,
116 List<Format> copyToList) {
117 boolean anyCopied = false;
118 SimpleCatalog tempCatalog = null;
119 for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
120 final Format thisFormat = instance.formatList.get(i);
121 final Format otherFormat = copyToList.get(i);
122 if (thisFormat != null && otherFormat == null) {
123 assert thisFormat.getWrapperFormat() == null;
124 if (tempCatalog == null) {
125 tempCatalog = new SimpleCatalog(loader);
126 }
127 copyToList.set(i, tempCatalog.formatList.get(i));
128 anyCopied = true;
129 }
130 }
131 return anyCopied;
132 }
133
134 private final ClassLoader classLoader;
135 private final List<SimpleFormat> formatList;
136 private final Map<String, SimpleFormat> formatMap;
137
138 SimpleCatalog(final ClassLoader classLoader) {
139 this.classLoader = classLoader;
140
141 /*
142 * Reserve slots for all predefined IDs, so that that next ID assigned
143 * will be Format.ID_PREDEFINED plus one.
144 */
145 int initCapacity = Format.ID_PREDEFINED * 2;
146 formatList = new ArrayList<SimpleFormat>(initCapacity);
147 formatMap = new HashMap<String, SimpleFormat>(initCapacity);
148
149 for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
150 formatList.add(null);
151 }
152
153 /* Initialize all predefined formats. */
154 setFormat(Format.ID_BOOL, new SimpleFormat.FBool(this, true));
155 setFormat(Format.ID_BOOL_W, new SimpleFormat.FBool(this, false));
156 setFormat(Format.ID_BYTE, new SimpleFormat.FByte(this, true));
157 setFormat(Format.ID_BYTE_W, new SimpleFormat.FByte(this, false));
158 setFormat(Format.ID_SHORT, new SimpleFormat.FShort(this, true));
159 setFormat(Format.ID_SHORT_W, new SimpleFormat.FShort(this, false));
160 setFormat(Format.ID_INT, new SimpleFormat.FInt(this, true));
161 setFormat(Format.ID_INT_W, new SimpleFormat.FInt(this, false));
162 setFormat(Format.ID_LONG, new SimpleFormat.FLong(this, true));
163 setFormat(Format.ID_LONG_W, new SimpleFormat.FLong(this, false));
164 setFormat(Format.ID_FLOAT, new SimpleFormat.FFloat(this, true));
165 setFormat(Format.ID_FLOAT_W, new SimpleFormat.FFloat(this, false));
166 setFormat(Format.ID_DOUBLE, new SimpleFormat.FDouble(this, true));
167 setFormat(Format.ID_DOUBLE_W, new SimpleFormat.FDouble(this, false));
168 setFormat(Format.ID_CHAR, new SimpleFormat.FChar(this, true));
169 setFormat(Format.ID_CHAR_W, new SimpleFormat.FChar(this, false));
170 setFormat(Format.ID_STRING, new SimpleFormat.FString(this));
171 setFormat(Format.ID_BIGINT, new SimpleFormat.FBigInt(this));
172 setFormat(Format.ID_BIGDEC, new SimpleFormat.FBigDec(this));
173 setFormat(Format.ID_DATE, new SimpleFormat.FDate(this));
174
175 /* Tell primitives about their wrapper class. */
176 setWrapper(Format.ID_BOOL, Format.ID_BOOL_W);
177 setWrapper(Format.ID_BYTE, Format.ID_BYTE_W);
178 setWrapper(Format.ID_SHORT, Format.ID_SHORT_W);
179 setWrapper(Format.ID_INT, Format.ID_INT_W);
180 setWrapper(Format.ID_LONG, Format.ID_LONG_W);
181 setWrapper(Format.ID_FLOAT, Format.ID_FLOAT_W);
182 setWrapper(Format.ID_DOUBLE, Format.ID_DOUBLE_W);
183 setWrapper(Format.ID_CHAR, Format.ID_CHAR_W);
184 }
185
186 /**
187 * Sets a format for which space in the formatList has been preallocated,
188 * and makes it the current format for the class.
189 */
190 private void setFormat(int id, SimpleFormat format) {
191 format.setId(id);
192 format.initializeIfNeeded(this, null /*model*/);
193 formatList.set(id, format);
194 formatMap.put(format.getClassName(), format);
195 }
196
197 /**
198 * Tells a primitive format about the format for its corresponding
199 * primitive wrapper class.
200 */
201 private void setWrapper(int primitiveId, int wrapperId) {
202 SimpleFormat primitiveFormat = formatList.get(primitiveId);
203 SimpleFormat wrapperFormat = formatList.get(wrapperId);
204 primitiveFormat.setWrapperFormat(wrapperFormat);
205 }
206
207 public int getInitVersion(Format format, boolean forReader) {
208 return Catalog.CURRENT_VERSION;
209 }
210
211 public Format getFormat(int formatId, boolean expectStored) {
212 Format format;
213 try {
214 format = formatList.get(formatId);
215 if (format == null) {
216 throw DbCompat.unexpectedState
217 ("Not a simple type: " + formatId);
218 }
219 return format;
220 } catch (NoSuchElementException e) {
221 throw DbCompat.unexpectedState
222 ("Not a simple type: " + formatId);
223 }
224 }
225
226 public Format getFormat(Class cls, boolean checkEntitySubclassIndexes) {
227 Format format = formatMap.get(cls.getName());
228 if (format == null) {
229 throw new IllegalArgumentException
230 ("Not a simple type: " + cls.getName());
231 }
232 return format;
233 }
234
235 public Format getFormat(String className) {
236 return formatMap.get(className);
237 }
238
239 public Format createFormat(String clsName,
240 Map<String, Format> newFormats) {
241 throw DbCompat.unexpectedState();
242 }
243
244 public Format createFormat(Class type, Map<String, Format> newFormats) {
245 throw DbCompat.unexpectedState();
246 }
247
248 public boolean isRawAccess() {
249 return false;
250 }
251
252 public Object convertRawObject(RawObject o, IdentityHashMap converted) {
253 throw DbCompat.unexpectedState();
254 }
255
256 public Class resolveClass(String clsName)
257 throws ClassNotFoundException {
258
259 return SimpleCatalog.resolveClass(clsName, classLoader);
260 }
261
262 public Class resolveKeyClass(String clsName) {
263 return SimpleCatalog.resolveKeyClass(clsName, classLoader);
264 }
265
266 /* Registering proxy is not allowed for SimpleType. */
267 public static boolean allowRegisterProxy(Class type) {
268 return !isSimpleType(type);
269 }
270 }