Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/RecordInput.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.HashMap;
11 import java.util.Map;
12
13 import com.sleepycat.bind.tuple.TupleInput;
14 import com.sleepycat.db.DatabaseEntry;
15
16 /**
17 * Implements EntityInput to read record key-data pairs. Extends TupleInput to
18 * implement the subset of TupleInput methods that are defined in the
19 * EntityInput interface.
20 *
21 * @author Mark Hayes
22 */
23 class RecordInput extends TupleInput implements EntityInput {
24
25 /* Initial size of visited map. */
26 static final int VISITED_INIT_SIZE = 50;
27
28 /*
29 * Offset to indicate that the visited object is stored in the primary key
30 * byte array.
31 */
32 static final int PRI_KEY_VISITED_OFFSET = Integer.MAX_VALUE - 1;
33
34 /* Used by RecordOutput to prevent illegal nested references. */
35 static final int PROHIBIT_REF_OFFSET = Integer.MAX_VALUE - 2;
36
37 /* Used by RecordInput to prevent illegal nested references. */
38 static final Object PROHIBIT_REF_OBJECT = new Object();
39
40 static final String PROHIBIT_NESTED_REF_MSG =
41 "Cannot embed a reference to a proxied object in the proxy; for " +
42 "example, a collection may not be an element of the collection " +
43 "because collections are proxied";
44
45 private Catalog catalog;
46 private boolean rawAccess;
47 private Map<Integer, Object> visited;
48 private DatabaseEntry priKeyEntry;
49 private int priKeyFormatId;
50 private boolean newStringFormat;
51
52 /**
53 * Creates a new input with a empty/null visited map.
54 */
55 RecordInput(Catalog catalog,
56 boolean rawAccess,
57 DatabaseEntry priKeyEntry,
58 int priKeyFormatId,
59 byte[] buffer,
60 int offset,
61 int length) {
62 super(buffer, offset, length);
63 this.catalog = catalog;
64 this.rawAccess = rawAccess;
65 this.priKeyEntry = priKeyEntry;
66 this.priKeyFormatId = priKeyFormatId;
67 this.visited = new HashMap<Integer, Object>(VISITED_INIT_SIZE);
68 }
69
70 /**
71 * Copy contructor where a new offset can be specified.
72 */
73 private RecordInput(RecordInput other, int offset) {
74 this(other.catalog, other.rawAccess, other.priKeyEntry,
75 other.priKeyFormatId, other.buf, offset, other.len);
76 visited = other.visited;
77 }
78
79 /**
80 * Copy contructor where a DatabaseEntry can be specified.
81 */
82 private RecordInput(RecordInput other, DatabaseEntry entry) {
83 this(other.catalog, other.rawAccess, other.priKeyEntry,
84 other.priKeyFormatId, entry.getData(), entry.getOffset(),
85 entry.getSize());
86 visited = other.visited;
87 }
88
89 /**
90 * @see EntityInput#getCatalog
91 */
92 public Catalog getCatalog() {
93 return catalog;
94 }
95
96 /**
97 * @see EntityInput#isRawAccess
98 */
99 public boolean isRawAccess() {
100 return rawAccess;
101 }
102
103 /**
104 * @see EntityInput#setRawAccess
105 */
106 public boolean setRawAccess(boolean rawAccessParam) {
107 boolean original = rawAccess;
108 rawAccess = rawAccessParam;
109 return original;
110 }
111
112 /**
113 * @see EntityInput#readObject
114 */
115 public Object readObject()
116 throws RefreshException {
117
118 /* Save the current offset before reading the format ID. */
119 Integer visitedOffset = off;
120 RecordInput useInput = this;
121 int formatId = readPackedInt();
122 Object o = null;
123
124 /* For a zero format ID, return a null instance. */
125 if (formatId == Format.ID_NULL) {
126 return null;
127 }
128
129 /* For a negative format ID, lookup an already visited instance. */
130 if (formatId < 0) {
131 int offset = (-(formatId + 1));
132 o = visited.get(offset);
133 if (o == RecordInput.PROHIBIT_REF_OBJECT) {
134 throw new IllegalArgumentException
135 (RecordInput.PROHIBIT_NESTED_REF_MSG);
136 }
137 if (o != null) {
138 /* Return a previously visited object. */
139 return o;
140 } else {
141
142 /*
143 * When reading starts from a non-zero offset, we may have to
144 * go back in the stream and read the referenced object. This
145 * happens when reading secondary key fields.
146 */
147 visitedOffset = offset;
148 if (offset == RecordInput.PRI_KEY_VISITED_OFFSET) {
149 assert priKeyEntry != null && priKeyFormatId > 0;
150 useInput = new RecordInput(this, priKeyEntry);
151 formatId = priKeyFormatId;
152 } else {
153 useInput = new RecordInput(this, offset);
154 formatId = useInput.readPackedInt();
155 }
156 }
157 }
158
159 /*
160 * Add a visted object slot that prohibits nested references to this
161 * object during the call to Reader.newInstance below. The newInstance
162 * method is allowed to read nested fields (in which case
163 * Reader.readObject further below does nothing) under certain
164 * conditions, but under these conditions we do not support nested
165 * references to the parent object. [#15815]
166 */
167 visited.put(visitedOffset, RecordInput.PROHIBIT_REF_OBJECT);
168
169 /* Create the object using the format indicated. */
170 Format format = catalog.getFormat(formatId, true /*expectStored*/);
171 Reader reader = format.getReader();
172 o = reader.newInstance(useInput, rawAccess);
173
174 /*
175 * Set the newly created object in the map of visited objects. This
176 * must be done before calling Reader.readObject, which allows the
177 * object to contain a reference to itself.
178 */
179 visited.put(visitedOffset, o);
180
181 /*
182 * Finish reading the object. Then replace it in the visited map in
183 * case a converted object is returned by readObject.
184 */
185 Object o2 = reader.readObject(o, useInput, rawAccess);
186 if (o != o2) {
187 visited.put(visitedOffset, o2);
188 }
189 return o2;
190 }
191
192 /**
193 * @see EntityInput#readKeyObject
194 */
195 public Object readKeyObject(Format format)
196 throws RefreshException {
197
198 /* Create and read the object using the given key format. */
199 Reader reader = format.getReader();
200 Object o = reader.newInstance(this, rawAccess);
201 return reader.readObject(o, this, rawAccess);
202 }
203
204 /**
205 * @see EntityInput#readStringObject
206 */
207 public Object readStringObject()
208 throws RefreshException {
209
210 if (!newStringFormat) {
211 return readObject();
212 }
213 return readString();
214 }
215
216 /**
217 * Called when copying secondary keys, for an input that is positioned on
218 * the secondary key field. Handles references to previously occurring
219 * objects, returning a different RecordInput than this one if appropriate.
220 */
221 KeyLocation getKeyLocation(Format fieldFormat)
222 throws RefreshException {
223
224 RecordInput input = this;
225 if (fieldFormat.getId() == Format.ID_STRING && newStringFormat) {
226
227 /*
228 * In new JE version, we do not store format ID for String data,
229 * So we have to read the real String data to see if the String
230 * data is null or not. [#19247]
231 */
232 int len = input.getStringByteLength();
233 String strKey = input.readString();
234 input.skipFast(0 - len);
235 if(strKey == null) {
236 /* String key field is null. */
237 return null;
238 }
239 } else if (!fieldFormat.isPrimitive()) {
240 int formatId = input.readPackedInt();
241 if (formatId == Format.ID_NULL) {
242 /* Key field is null. */
243 return null;
244 }
245 if (formatId < 0) {
246 int offset = (-(formatId + 1));
247 if (offset == RecordInput.PRI_KEY_VISITED_OFFSET) {
248 assert priKeyEntry != null && priKeyFormatId > 0;
249 input = new RecordInput(this, priKeyEntry);
250 formatId = priKeyFormatId;
251 } else {
252 input = new RecordInput(this, offset);
253 formatId = input.readPackedInt();
254 }
255 }
256 fieldFormat = catalog.getFormat(formatId, true /*expectStored*/);
257 }
258 /* Key field is non-null. */
259 return new KeyLocation(input, fieldFormat);
260 }
261
262 /**
263 * @see EntityInput#registerPriKeyObject
264 */
265 public void registerPriKeyObject(Object o) {
266
267 /*
268 * PRI_KEY_VISITED_OFFSET is used as the visited offset to indicate
269 * that the visited object is stored in the primary key byte array.
270 */
271 visited.put(RecordInput.PRI_KEY_VISITED_OFFSET, o);
272 }
273
274 /**
275 * @see EntityInput#registerPriKeyObject
276 */
277 public void registerPriStringKeyObject(Object o) {
278
279 /*
280 * In JE 5.0 and later, String is treated as a primitive type, so a
281 * String object does not need to be registered. But in earlier
282 * versions, Strings are treated as any other object and must be
283 * registered. [#19247]
284 */
285 if (!newStringFormat) {
286 visited.put(RecordInput.PRI_KEY_VISITED_OFFSET, o);
287 }
288 }
289
290 /**
291 * Registers the top level entity before reading it, to allow nested fields
292 * to reference their parent entity. [#17525]
293 */
294 public void registerEntity(Object entity, int initialOffset) {
295 visited.put(initialOffset, entity);
296 }
297
298 /**
299 * Registers the entity format before reading it, so that old-format String
300 * fields can be read properly. [#19247]
301 */
302 public void registerEntityFormat(Format entityFormat) {
303 newStringFormat = entityFormat.getNewStringFormat();
304 }
305
306 /**
307 * @see EntityInput#skipField
308 */
309 public void skipField(Format declaredFormat)
310 throws RefreshException {
311
312 if (declaredFormat != null && declaredFormat.isPrimitive()) {
313 declaredFormat.skipContents(this);
314 } else if (declaredFormat != null &&
315 declaredFormat.getId() == Format.ID_STRING &&
316 newStringFormat) {
317
318 /*
319 * In the new JE version, we treat String as primitive, and will
320 * not store format ID for String data. [#19247]
321 */
322 declaredFormat.skipContents(this);
323 } else {
324 int formatId = readPackedInt();
325 if (formatId > 0) {
326 Format format =
327 catalog.getFormat(formatId, true /*expectStored*/);
328 format.skipContents(this);
329 }
330 }
331 }
332
333 public int readArrayLength() {
334 return readPackedInt();
335 }
336
337 public int readEnumConstant(String[] names) {
338 return readPackedInt();
339 }
340 }