Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/RecordOutput.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.Map;
11
12 import com.sleepycat.bind.tuple.TupleOutput;
13 import com.sleepycat.persist.raw.RawObject;
14 import java.util.IdentityHashMap;
15
16 /**
17 * Implements EntityOutput to write record key-data pairs. Extends TupleOutput
18 * to implement the subset of TupleOutput methods that are defined in the
19 * EntityOutput interface.
20 *
21 * @author Mark Hayes
22 */
23 class RecordOutput extends TupleOutput implements EntityOutput {
24
25 private Catalog catalog;
26 private boolean rawAccess;
27 private Map<Object, Integer> visited;
28
29 /**
30 * Creates a new output with an empty/null visited map.
31 */
32 RecordOutput(Catalog catalog, boolean rawAccess) {
33
34 super();
35 this.catalog = catalog;
36 this.rawAccess = rawAccess;
37 this.visited = new IdentityHashMap<Object, Integer>();
38 }
39
40 /**
41 * @see EntityOutput#writeObject
42 */
43 public void writeObject(Object o, Format fieldFormat)
44 throws RefreshException {
45
46 /* For a null instance, write a zero format ID. */
47 if (o == null) {
48 writePackedInt(Format.ID_NULL);
49 return;
50 }
51
52 /*
53 * For an already visited instance, output a reference to it. The
54 * reference is the negation of the visited offset minus one.
55 */
56 Integer offset = visited.get(o);
57 if (offset != null) {
58 if (offset == RecordInput.PROHIBIT_REF_OFFSET) {
59 throw new IllegalArgumentException
60 (RecordInput.PROHIBIT_NESTED_REF_MSG);
61 } else {
62 writePackedInt(-(offset + 1));
63 return;
64 }
65 }
66
67 /*
68 * Get and validate the format. Catalog.getFormat(Class) throws
69 * IllegalArgumentException if the class is not persistent. We don't
70 * need to check the fieldFormat (and it will be null) for non-raw
71 * access because field type checking is enforced by Java.
72 */
73 Format format;
74 if (rawAccess) {
75 format = RawAbstractInput.checkRawType(catalog, o, fieldFormat);
76 } else {
77
78 /*
79 * Do not attempt to open subclass indexes in case this is an
80 * embedded entity. We will detect that error below, but we must
81 * not fail first when attempting to open the secondaries.
82 */
83 format = catalog.getFormat
84 (o.getClass(), false /*checkEntitySubclassIndexes*/);
85 }
86 if (format.getProxiedFormat() != null) {
87 throw new IllegalArgumentException
88 ("May not store proxy classes directly: " +
89 format.getClassName());
90 }
91 /* Check for embedded entity classes and subclasses. */
92 if (format.getEntityFormat() != null) {
93 throw new IllegalArgumentException
94 ("References to entities are not allowed: " +
95 o.getClass().getName());
96 }
97
98 /*
99 * Remember that we visited this instance. Certain formats
100 * (ProxiedFormat for example) prohibit nested fields that reference
101 * the parent object. [#15815]
102 */
103 boolean prohibitNestedRefs = format.areNestedRefsProhibited();
104 Integer visitedOffset = size();
105 visited.put(o, prohibitNestedRefs ? RecordInput.PROHIBIT_REF_OFFSET :
106 visitedOffset);
107
108 /* Finally, write the formatId and object value. */
109 writePackedInt(format.getId());
110 format.writeObject(o, this, rawAccess);
111
112 /* Always allow references from siblings that follow. */
113 if (prohibitNestedRefs) {
114 visited.put(o, visitedOffset);
115 }
116 }
117
118 /**
119 * @see EntityOutput#writeKeyObject
120 */
121 public void writeKeyObject(Object o, Format fieldFormat)
122 throws RefreshException {
123
124 /* Key objects must not be null and must be of the declared class. */
125 if (o == null) {
126 throw new IllegalArgumentException
127 ("Key field object may not be null");
128 }
129 Format format;
130 if (rawAccess) {
131 if (o instanceof RawObject) {
132 format = (Format) ((RawObject) o).getType();
133 } else {
134 format = catalog.getFormat
135 (o.getClass(), false /*checkEntitySubclassIndexes*/);
136 /* Expect primitive wrapper class in raw mode. */
137 if (fieldFormat.isPrimitive()) {
138 fieldFormat = fieldFormat.getWrapperFormat();
139 }
140 }
141 } else {
142 format = catalog.getFormat(o.getClass(),
143 false /*checkEntitySubclassIndexes*/);
144 }
145 if (fieldFormat != format) {
146 throw new IllegalArgumentException
147 ("The key field object class (" + o.getClass().getName() +
148 ") must be the field's declared class: " +
149 fieldFormat.getClassName());
150 }
151
152 /* Write the object value (no formatId is written for keys). */
153 fieldFormat.writeObject(o, this, rawAccess);
154 }
155
156 /**
157 * @see EntityOutput#registerPriKeyObject
158 */
159 public void registerPriKeyObject(Object o) {
160
161 /*
162 * PRI_KEY_VISITED_OFFSET is used as the visited offset to indicate
163 * that the visited object is stored in the primary key byte array.
164 */
165 visited.put(o, RecordInput.PRI_KEY_VISITED_OFFSET);
166 }
167
168 /**
169 * Registers the top level entity before writing it, to allow nested fields
170 * to reference their parent entity. [#17525]
171 */
172 public void registerEntity(Object entity) {
173 assert size() == 0;
174 visited.put(entity, size());
175 }
176
177 /**
178 * @see EntityOutput#writeArrayLength
179 */
180 public void writeArrayLength(int length) {
181 writePackedInt(length);
182 }
183
184 /**
185 * @see EntityOutput#writeEnumConstant
186 */
187 public void writeEnumConstant(String[] names, int index) {
188 writePackedInt(index);
189 }
190 }