Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/PersistEntityBinding.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 com.sleepycat.bind.EntityBinding;
11 import com.sleepycat.bind.tuple.TupleBase;
12 import com.sleepycat.compat.DbCompat;
13 import com.sleepycat.db.DatabaseEntry;
14 import com.sleepycat.persist.raw.RawObject;
15
16 /**
17 * A persistence entity binding for a given entity class.
18 *
19 * @author Mark Hayes
20 */
21 public class PersistEntityBinding implements EntityBinding {
22
23 /* See Store.refresh for an explanation of the use of volatile fields. */
24 volatile PersistCatalog catalog;
25 volatile Format entityFormat;
26 final boolean rawAccess;
27 PersistKeyAssigner keyAssigner;
28
29 /**
30 * Creates a key binding for a given entity class.
31 */
32 public PersistEntityBinding(final PersistCatalog catalogParam,
33 final String entityClassName,
34 final boolean rawAccess) {
35 catalog = catalogParam;
36
37 try {
38 entityFormat = getOrCreateFormat(catalog, entityClassName,
39 rawAccess);
40 } catch (RefreshException e) {
41 /* Must assign catalog field in constructor. */
42 catalog = e.refresh();
43 try {
44 entityFormat = getOrCreateFormat(catalog, entityClassName,
45 rawAccess);
46 } catch (RefreshException e2) {
47 throw DbCompat.unexpectedException(e2);
48 }
49 }
50 if (!entityFormat.isEntity()) {
51 throw new IllegalArgumentException
52 ("Not an entity class: " + entityClassName);
53 }
54 this.rawAccess = rawAccess;
55 }
56
57 public PersistKeyAssigner getKeyAssigner() {
58 return keyAssigner;
59 }
60
61 public Object entryToObject(final DatabaseEntry key,
62 final DatabaseEntry data) {
63 try {
64 return entryToObjectInternal(key, null, data);
65 } catch (RefreshException e) {
66 e.refresh();
67 try {
68 return entryToObjectInternal(key, null, data);
69 } catch (RefreshException e2) {
70 throw DbCompat.unexpectedException(e2);
71 }
72 }
73 }
74
75 /**
76 * This method will be used in PrimaryIndex.get, where the primary key is
77 * known to DPL. This method will force to call readEntityWithPriKey to
78 * directly assign primary key to the de-serialized object.
79 */
80 public Object entryToObjectWithPriKey(final Object priKey,
81 final DatabaseEntry data) {
82 try {
83 if (priKey == null) {
84 throw new
85 IllegalArgumentException("Primary key cannot be null.");
86 }
87 return entryToObjectInternal(null, priKey, data);
88 } catch (RefreshException e) {
89 e.refresh();
90 try {
91 return entryToObjectInternal(null, priKey, data);
92 } catch (RefreshException e2) {
93 throw DbCompat.unexpectedException(e2);
94 }
95 }
96 }
97
98 /**
99 * This method is always called after reading an entity. If a refresh is
100 * needed, we detect that in PersistCatalog.getFormat(int).
101 */
102 private Object entryToObjectInternal(final DatabaseEntry key,
103 final Object priKey,
104 final DatabaseEntry data)
105 throws RefreshException {
106
107 return readEntity(catalog, key, priKey, data, rawAccess);
108 }
109
110 /**
111 * Creates the instance, reads the entity key first to track visited
112 * entities correctly, then reads the data and returns the entity.
113 *
114 * This is a special case of EntityInput.readObject for a top level entity.
115 * Special treatments are:
116 * - The formatId must be >= 0; since this is the top level instance, it
117 * cannot refer to a visited object nor be a null reference.
118 * - The entity is not checked for existence in the visited object set;
119 * entities cannot be referenced by another entity.
120 * - Reader.readPriKey must be called prior to calling Reader.readObject.
121 */
122 static Object readEntity(Catalog useCatalog,
123 DatabaseEntry key,
124 Object priKey,
125 DatabaseEntry data,
126 boolean rawAccess)
127 throws RefreshException {
128
129 RecordInput dataInput = new RecordInput
130 (useCatalog, rawAccess, null, 0,
131 data.getData(), data.getOffset(), data.getSize());
132 int initialOffset = dataInput.getBufferOffset();
133 int formatId = dataInput.readPackedInt();
134 Format format = useCatalog.getFormat(formatId, true /*expectStored*/);
135 dataInput.registerEntityFormat(format);
136 Reader reader = format.getReader();
137 Object entity = reader.newInstance(dataInput, rawAccess);
138 if (priKey == null) {
139 /* If priKey is null, need to deserialize the primary key. */
140 RecordInput keyInput = new RecordInput
141 (useCatalog, rawAccess, null, 0,
142 key.getData(), key.getOffset(), key.getSize());
143 reader.readPriKey(entity, keyInput, rawAccess);
144 } else {
145
146 /*
147 * If priKey is not null, directly assign it to the primary key
148 * field. [#19248]
149 */
150 Accessor accessor =
151 reader.getAccessor(entity instanceof RawObject ?
152 true :
153 rawAccess);
154 if (accessor == null) {
155 accessor = format.getLatestVersion().getReader().
156 getAccessor(entity instanceof RawObject ?
157 true :
158 rawAccess);
159 }
160 accessor.setPriField(entity, priKey);
161 }
162 dataInput.registerEntity(entity, initialOffset);
163 entity = reader.readObject(entity, dataInput, rawAccess);
164 return entity;
165 }
166
167 public void objectToData(final Object entity, final DatabaseEntry data) {
168 try {
169 objectToDataInternal(entity, data);
170 } catch (RefreshException e) {
171 e.refresh();
172 try {
173 objectToDataInternal(entity, data);
174 } catch (RefreshException e2) {
175 throw DbCompat.unexpectedException(e2);
176 }
177 }
178 }
179
180 /**
181 * This method is always called before writing an entity. If a refresh is
182 * needed, we detect that here.
183 */
184 private void objectToDataInternal(final Object entity,
185 final DatabaseEntry data)
186 throws RefreshException {
187
188 Format format = getValidFormat(entity);
189 /* Before a write, check whether a refresh is needed. [#16655] */
190 catalog.checkWriteInReplicaUpgradeMode();
191 writeEntity(format, catalog, entity, data, rawAccess);
192 }
193
194 /**
195 * Writes the formatId and object, and returns the bytes.
196 *
197 * This is a special case of EntityOutput.writeObject for a top level
198 * entity. Special treatments are:
199 * - The entity may not be null.
200 * - The entity is not checked for existence in the visited object set;
201 * entities cannot be referenced by another entity.
202 */
203 static void writeEntity(Format format,
204 Catalog catalog,
205 Object entity,
206 DatabaseEntry data,
207 boolean rawAccess)
208 throws RefreshException {
209
210 RecordOutput output = new RecordOutput(catalog, rawAccess);
211 output.registerEntity(entity);
212 output.writePackedInt(format.getId());
213 format.writeObject(entity, output, rawAccess);
214 TupleBase.outputToEntry(output, data);
215 }
216
217 public void objectToKey(final Object entity, final DatabaseEntry key) {
218 try {
219 objectToKeyInternal(entity, key);
220 } catch (RefreshException e) {
221 e.refresh();
222 try {
223 objectToKeyInternal(entity, key);
224 } catch (RefreshException e2) {
225 throw DbCompat.unexpectedException(e2);
226 }
227 }
228 }
229
230 private void objectToKeyInternal(final Object entity,
231 final DatabaseEntry key)
232 throws RefreshException {
233
234 /*
235 * Write the primary key field as a special case since the output
236 * format is for a key binding, not entity data.
237 */
238 Format format = getValidFormat(entity);
239 RecordOutput output = new RecordOutput(catalog, rawAccess);
240
241 /* Write the primary key and return the bytes. */
242 format.writePriKey(entity, output, rawAccess);
243 TupleBase.outputToEntry(output, key);
244 }
245
246 /**
247 * Returns the format for the given entity and validates it, throwing an
248 * exception if it is invalid for this binding.
249 */
250 private Format getValidFormat(Object entity)
251 throws RefreshException {
252
253 /* A null entity is not allowed. */
254 if (entity == null) {
255 throw new IllegalArgumentException("An entity may not be null");
256 }
257
258 /*
259 * Get the format. getFormat throws IllegalArgumentException if the
260 * class is not persistent.
261 */
262 Format format;
263 if (rawAccess) {
264 if (!(entity instanceof RawObject)) {
265 throw new IllegalArgumentException
266 ("Entity must be a RawObject");
267 }
268 format = (Format) ((RawObject) entity).getType();
269 } else {
270 format = catalog.getFormat
271 (entity.getClass(), true /*checkEntitySubclassIndexes*/);
272 }
273
274 /* Check that the entity class/subclass is valid for this binding. */
275 if (format.getEntityFormat() != entityFormat) {
276 throw new IllegalArgumentException
277 ("The entity class (" + format.getClassName() +
278 ") must be this entity class or a subclass of it: " +
279 entityFormat.getClassName());
280 }
281
282 return format;
283 }
284
285 /**
286 * Utility method for getting or creating a format as appropriate for
287 * bindings and key creators.
288 */
289 static Format getOrCreateFormat(Catalog useCatalog,
290 String clsName,
291 boolean rawAccess)
292 throws RefreshException {
293
294 if (rawAccess) {
295 Format format = useCatalog.getFormat(clsName);
296 if (format == null) {
297 throw new IllegalArgumentException
298 ("Not a persistent class: " + clsName);
299 }
300 return format;
301 } else {
302 Class cls = useCatalog.resolveKeyClass(clsName);
303 return useCatalog.getFormat(cls,
304 true /*checkEntitySubclassIndexes*/);
305 }
306 }
307
308 /**
309 * See Store.refresh.
310 */
311 void refresh(final PersistCatalog newCatalog) {
312 catalog = newCatalog;
313 entityFormat = newCatalog.getFormat(entityFormat.getClassName());
314 if (keyAssigner != null) {
315 keyAssigner.refresh(newCatalog);
316 }
317 }
318 }