summaryrefslogtreecommitdiff
path: root/lang/java/src/com/sleepycat/persist/impl/SimpleCatalog.java
blob: a2a9a4a11b3577eecc630b3d91d63fc429c814a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

package com.sleepycat.persist.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import com.sleepycat.compat.DbCompat;
import java.util.IdentityHashMap;
import com.sleepycat.persist.raw.RawObject;
import com.sleepycat.util.ClassResolver;

/**
 * A static catalog containing simple types only.  Once created, this catalog
 * is immutable.
 *
 * For bindings accessed by a PersistComparator during recovery, the
 * SimpleCatalog provides formats for all simple types.  To reduce redundant
 * format objects, the SimpleCatalog's formats are copied when creating a
 * regular PersistCatalog.
 *
 * This class also contains utility methods for dealing with primitives.
 *
 * @author Mark Hayes
 */
public class SimpleCatalog implements Catalog {

    private static final Map<String, Class> keywordToPrimitive;
    static {
        keywordToPrimitive = new HashMap<String, Class>(8);
        keywordToPrimitive.put("boolean", Boolean.TYPE);
        keywordToPrimitive.put("char", Character.TYPE);
        keywordToPrimitive.put("byte", Byte.TYPE);
        keywordToPrimitive.put("short", Short.TYPE);
        keywordToPrimitive.put("int", Integer.TYPE);
        keywordToPrimitive.put("long", Long.TYPE);
        keywordToPrimitive.put("float", Float.TYPE);
        keywordToPrimitive.put("double", Double.TYPE);
    }

    private static final Map<Class, Class> primitiveTypeToWrapper;
    static {
        primitiveTypeToWrapper = new HashMap<Class, Class>(8);
        primitiveTypeToWrapper.put(Boolean.TYPE, Boolean.class);
        primitiveTypeToWrapper.put(Character.TYPE, Character.class);
        primitiveTypeToWrapper.put(Byte.TYPE, Byte.class);
        primitiveTypeToWrapper.put(Short.TYPE, Short.class);
        primitiveTypeToWrapper.put(Integer.TYPE, Integer.class);
        primitiveTypeToWrapper.put(Long.TYPE, Long.class);
        primitiveTypeToWrapper.put(Float.TYPE, Float.class);
        primitiveTypeToWrapper.put(Double.TYPE, Double.class);
    }

    private static final SimpleCatalog instance = new SimpleCatalog(null);

    static boolean isSimpleType(Class type) {
        return instance.formatMap.containsKey(type.getName());
    }

    static Class primitiveToWrapper(Class type) {
        Class cls = primitiveTypeToWrapper.get(type);
        if (cls == null) {
            throw DbCompat.unexpectedState(type.getName());
        }
        return cls;
    }

    public static Class resolveClass(String className, ClassLoader loader)
        throws ClassNotFoundException {

        Class cls = keywordToPrimitive.get(className);
        if (cls == null) {
            cls = ClassResolver.resolveClass(className, loader);
        }
        return cls;
    }

    public static Class resolveKeyClass(String className, ClassLoader loader) {
        Class cls = keywordToPrimitive.get(className);
        if (cls != null) {
            cls = primitiveTypeToWrapper.get(cls);
        } else {
            try {
                cls = ClassResolver.resolveClass(className, loader);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException
                    ("Key class not found: " + className);
            }
        }
        return cls;
    }

    public static String keyClassName(String className) {
        Class cls = keywordToPrimitive.get(className);
        if (cls != null) {
            cls = primitiveTypeToWrapper.get(cls);
            return cls.getName();
        } else {
            return className;
        }
    }

    static List<Format> getAllSimpleFormats(ClassLoader loader) {
        return new ArrayList<Format>(new SimpleCatalog(loader).formatList);
    }

    static boolean addMissingSimpleFormats(ClassLoader loader,
                                           List<Format> copyToList) {
        boolean anyCopied = false;
        SimpleCatalog tempCatalog = null;
        for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
            final Format thisFormat = instance.formatList.get(i);
            final Format otherFormat = copyToList.get(i);
            if (thisFormat != null && otherFormat == null) {
                assert thisFormat.getWrapperFormat() == null;
                if (tempCatalog == null) {
                    tempCatalog = new SimpleCatalog(loader);
                }
                copyToList.set(i, tempCatalog.formatList.get(i));
                anyCopied = true;
            }
        }
        return anyCopied;
    }

    private final ClassLoader classLoader;
    private final List<SimpleFormat> formatList;
    private final Map<String, SimpleFormat> formatMap;

    SimpleCatalog(final ClassLoader classLoader) {
        this.classLoader = classLoader;

        /*
         * Reserve slots for all predefined IDs, so that that next ID assigned
         * will be Format.ID_PREDEFINED plus one.
         */
        int initCapacity = Format.ID_PREDEFINED * 2;
        formatList = new ArrayList<SimpleFormat>(initCapacity);
        formatMap = new HashMap<String, SimpleFormat>(initCapacity);

        for (int i = 0; i <= Format.ID_PREDEFINED; i += 1) {
            formatList.add(null);
        }

        /* Initialize all predefined formats.  */
        setFormat(Format.ID_BOOL,     new SimpleFormat.FBool(this, true));
        setFormat(Format.ID_BOOL_W,   new SimpleFormat.FBool(this, false));
        setFormat(Format.ID_BYTE,     new SimpleFormat.FByte(this, true));
        setFormat(Format.ID_BYTE_W,   new SimpleFormat.FByte(this, false));
        setFormat(Format.ID_SHORT,    new SimpleFormat.FShort(this, true));
        setFormat(Format.ID_SHORT_W,  new SimpleFormat.FShort(this, false));
        setFormat(Format.ID_INT,      new SimpleFormat.FInt(this, true));
        setFormat(Format.ID_INT_W,    new SimpleFormat.FInt(this, false));
        setFormat(Format.ID_LONG,     new SimpleFormat.FLong(this, true));
        setFormat(Format.ID_LONG_W,   new SimpleFormat.FLong(this, false));
        setFormat(Format.ID_FLOAT,    new SimpleFormat.FFloat(this, true));
        setFormat(Format.ID_FLOAT_W,  new SimpleFormat.FFloat(this, false));
        setFormat(Format.ID_DOUBLE,   new SimpleFormat.FDouble(this, true));
        setFormat(Format.ID_DOUBLE_W, new SimpleFormat.FDouble(this, false));
        setFormat(Format.ID_CHAR,     new SimpleFormat.FChar(this, true));
        setFormat(Format.ID_CHAR_W,   new SimpleFormat.FChar(this, false));
        setFormat(Format.ID_STRING,   new SimpleFormat.FString(this));
        setFormat(Format.ID_BIGINT,   new SimpleFormat.FBigInt(this));
        setFormat(Format.ID_BIGDEC,   new SimpleFormat.FBigDec(this));
        setFormat(Format.ID_DATE,     new SimpleFormat.FDate(this));

        /* Tell primitives about their wrapper class. */
        setWrapper(Format.ID_BOOL, Format.ID_BOOL_W);
        setWrapper(Format.ID_BYTE, Format.ID_BYTE_W);
        setWrapper(Format.ID_SHORT, Format.ID_SHORT_W);
        setWrapper(Format.ID_INT, Format.ID_INT_W);
        setWrapper(Format.ID_LONG, Format.ID_LONG_W);
        setWrapper(Format.ID_FLOAT, Format.ID_FLOAT_W);
        setWrapper(Format.ID_DOUBLE, Format.ID_DOUBLE_W);
        setWrapper(Format.ID_CHAR, Format.ID_CHAR_W);
    }

    /**
     * Sets a format for which space in the formatList has been preallocated,
     * and makes it the current format for the class.
     */
    private void setFormat(int id, SimpleFormat format) {
        format.setId(id);
        format.initializeIfNeeded(this, null /*model*/);
        formatList.set(id, format);
        formatMap.put(format.getClassName(), format);
    }

    /**
     * Tells a primitive format about the format for its corresponding
     * primitive wrapper class.
     */
    private void setWrapper(int primitiveId, int wrapperId) {
        SimpleFormat primitiveFormat = formatList.get(primitiveId);
        SimpleFormat wrapperFormat = formatList.get(wrapperId);
        primitiveFormat.setWrapperFormat(wrapperFormat);
    }

    public int getInitVersion(Format format, boolean forReader) {
        return Catalog.CURRENT_VERSION;
    }

    public Format getFormat(int formatId, boolean expectStored) {
        Format format;
        try {
            format = formatList.get(formatId);
            if (format == null) {
                throw DbCompat.unexpectedState
                    ("Not a simple type: " + formatId);
            }
            return format;
        } catch (NoSuchElementException e) {
            throw DbCompat.unexpectedState
                ("Not a simple type: " + formatId);
        }
    }

    public Format getFormat(Class cls, boolean checkEntitySubclassIndexes) {
        Format format = formatMap.get(cls.getName());
        if (format == null) {
            throw new IllegalArgumentException
                ("Not a simple type: " + cls.getName());
        }
        return format;
    }

    public Format getFormat(String className) {
        return formatMap.get(className);
    }

    public Format createFormat(String clsName,
                               Map<String, Format> newFormats) {
        throw DbCompat.unexpectedState();
    }

    public Format createFormat(Class type, Map<String, Format> newFormats) {
        throw DbCompat.unexpectedState();
    }

    public boolean isRawAccess() {
        return false;
    }

    public Object convertRawObject(RawObject o, IdentityHashMap converted) {
        throw DbCompat.unexpectedState();
    }

    public Class resolveClass(String clsName)
        throws ClassNotFoundException {

        return SimpleCatalog.resolveClass(clsName, classLoader);
    }

    public Class resolveKeyClass(String clsName) {
        return SimpleCatalog.resolveKeyClass(clsName, classLoader);
    }
    
    /* Registering proxy is not allowed for SimpleType. */
    public static boolean allowRegisterProxy(Class type) {
        return !isSimpleType(type);
    }
}