Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/PersistKeyCreator.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.Collection;
11 import java.util.Set;
12
13 import com.sleepycat.bind.tuple.TupleBase;
14 import com.sleepycat.compat.DbCompat;
15 import com.sleepycat.db.DatabaseEntry;
16 import com.sleepycat.db.ForeignMultiKeyNullifier;
17 import com.sleepycat.db.SecondaryDatabase;
18 import com.sleepycat.db.SecondaryKeyCreator;
19 import com.sleepycat.db.SecondaryMultiKeyCreator;
20 import com.sleepycat.persist.model.EntityMetadata;
21 import com.sleepycat.persist.model.Relationship;
22 import com.sleepycat.persist.model.SecondaryKeyMetadata;
23 import com.sleepycat.persist.raw.RawObject;
24
25 /**
26 * A persistence secondary key creator/nullifier. This class always uses
27 * rawAccess=true to avoid depending on the presence of the proxy class.
28 *
29 * @author Mark Hayes
30 */
31 public class PersistKeyCreator implements SecondaryKeyCreator,
32 SecondaryMultiKeyCreator,
33 ForeignMultiKeyNullifier {
34
35 static boolean isManyType(Class cls) {
36 return cls.isArray() || Collection.class.isAssignableFrom(cls);
37 }
38
39 /* See Store.refresh for an explanation of the use of volatile fields. */
40 private volatile Catalog catalog;
41 private volatile Format priKeyFormat;
42 private final String keyName;
43 private volatile Format keyFormat;
44 private final boolean toMany;
45
46 /**
47 * Creates a key creator/nullifier for a given entity class and key name.
48 */
49 public PersistKeyCreator(Catalog catalogParam,
50 EntityMetadata entityMeta,
51 String keyClassName,
52 SecondaryKeyMetadata secKeyMeta,
53 boolean rawAccess) {
54 catalog = catalogParam;
55 try {
56 getFormats(entityMeta, keyClassName, secKeyMeta, rawAccess);
57 } catch (RefreshException e) {
58 /* Must assign catalog field in constructor. */
59 catalog = e.refresh();
60 try {
61 getFormats(entityMeta, keyClassName, secKeyMeta, rawAccess);
62 } catch (RefreshException e2) {
63 throw DbCompat.unexpectedException(e2);
64 }
65 }
66 keyName = secKeyMeta.getKeyName();
67 Relationship rel = secKeyMeta.getRelationship();
68 toMany = (rel == Relationship.ONE_TO_MANY ||
69 rel == Relationship.MANY_TO_MANY);
70 }
71
72 private void getFormats(EntityMetadata entityMeta,
73 String keyClassName,
74 SecondaryKeyMetadata secKeyMeta,
75 boolean rawAccess)
76 throws RefreshException {
77
78 priKeyFormat = PersistEntityBinding.getOrCreateFormat
79 (catalog, entityMeta.getPrimaryKey().getClassName(), rawAccess);
80 keyFormat = PersistEntityBinding.getOrCreateFormat
81 (catalog, keyClassName, rawAccess);
82 if (keyFormat == null) {
83 throw new IllegalArgumentException
84 ("Not a key class: " + keyClassName);
85 }
86 if (keyFormat.isPrimitive()) {
87 throw new IllegalArgumentException
88 ("Use a primitive wrapper class instead of class: " +
89 keyFormat.getClassName());
90 }
91 }
92
93 public boolean createSecondaryKey(SecondaryDatabase secondary,
94 DatabaseEntry key,
95 DatabaseEntry data,
96 DatabaseEntry result) {
97 try {
98 return createSecondaryKeyInternal(secondary, key, data, result);
99 } catch (RefreshException e) {
100 e.refresh();
101 try {
102 return createSecondaryKeyInternal(secondary, key, data,
103 result);
104 } catch (RefreshException e2) {
105 throw DbCompat.unexpectedException(e2);
106 }
107 }
108 }
109
110 private boolean createSecondaryKeyInternal(SecondaryDatabase secondary,
111 DatabaseEntry key,
112 DatabaseEntry data,
113 DatabaseEntry result)
114 throws RefreshException {
115
116 if (toMany) {
117 throw DbCompat.unexpectedState();
118 }
119 KeyLocation loc = moveToKey(key, data);
120 if (loc != null) {
121 RecordOutput output = new RecordOutput
122 (catalog, true /*rawAccess*/);
123 loc.format.copySecKey(loc.input, output);
124 TupleBase.outputToEntry(output, result);
125 return true;
126 } else {
127 /* Key field is not present or null. */
128 return false;
129 }
130 }
131
132 public void createSecondaryKeys(SecondaryDatabase secondary,
133 DatabaseEntry key,
134 DatabaseEntry data,
135 Set results) {
136 try {
137 createSecondaryKeysInternal(secondary, key, data, results);
138 } catch (RefreshException e) {
139 e.refresh();
140 try {
141 createSecondaryKeysInternal(secondary, key, data, results);
142 } catch (RefreshException e2) {
143 throw DbCompat.unexpectedException(e2);
144 }
145 }
146 }
147
148 private void createSecondaryKeysInternal(SecondaryDatabase secondary,
149 DatabaseEntry key,
150 DatabaseEntry data,
151 Set results)
152 throws RefreshException {
153
154 if (!toMany) {
155 throw DbCompat.unexpectedState();
156 }
157 KeyLocation loc = moveToKey(key, data);
158 if (loc != null) {
159 loc.format.copySecMultiKey(loc.input, keyFormat, results);
160 }
161 /* Else key field is not present or null. */
162 }
163
164 public boolean nullifyForeignKey(SecondaryDatabase secondary,
165 DatabaseEntry key,
166 DatabaseEntry data,
167 DatabaseEntry secKey) {
168 try {
169 return nullifyForeignKeyInternal(secondary, key, data, secKey);
170 } catch (RefreshException e) {
171 e.refresh();
172 try {
173 return nullifyForeignKeyInternal(secondary, key, data, secKey);
174 } catch (RefreshException e2) {
175 throw DbCompat.unexpectedException(e2);
176 }
177 }
178 }
179
180 private boolean nullifyForeignKeyInternal(SecondaryDatabase secondary,
181 DatabaseEntry key,
182 DatabaseEntry data,
183 DatabaseEntry secKey)
184 throws RefreshException {
185
186 /* Deserialize the entity and get its current class format. */
187 RawObject entity = (RawObject) PersistEntityBinding.readEntity
188 (catalog, key, null, data, true /*rawAccess*/);
189 Format entityFormat = (Format) entity.getType();
190
191 /*
192 * Set the key to null. For a TO_MANY key, pass the key object to be
193 * removed from the array/collection.
194 */
195 Object secKeyObject = null;
196 if (toMany) {
197 secKeyObject = PersistKeyBinding.readKey
198 (keyFormat, catalog, secKey.getData(), secKey.getOffset(),
199 secKey.getSize(), true /*rawAccess*/);
200 }
201 if (entityFormat.nullifySecKey
202 (catalog, entity, keyName, secKeyObject)) {
203
204 /*
205 * Using the current format for the entity, serialize the modified
206 * entity back to the data entry.
207 */
208 PersistEntityBinding.writeEntity
209 (entityFormat, catalog, entity, data, true /*rawAccess*/);
210 return true;
211 } else {
212 /* Key field is not present or null. */
213 return false;
214 }
215 }
216
217 /**
218 * Returns the location from which the secondary key field can be copied.
219 */
220 private KeyLocation moveToKey(DatabaseEntry priKey, DatabaseEntry data)
221 throws RefreshException {
222
223 RecordInput input = new RecordInput
224 (catalog, true /*rawAccess*/, priKey, priKeyFormat.getId(),
225 data.getData(), data.getOffset(), data.getSize());
226 int formatId = input.readPackedInt();
227 Format entityFormat =
228 catalog.getFormat(formatId, true /*expectStored*/);
229 input.registerEntityFormat(entityFormat);
230 Format fieldFormat = entityFormat.skipToSecKey(input, keyName);
231 if (fieldFormat != null) {
232 /* Returns null if key field is null. */
233 return input.getKeyLocation(fieldFormat);
234 } else {
235 /* Key field is not present in this class. */
236 return null;
237 }
238 }
239
240 /**
241 * See Store.refresh.
242 */
243 void refresh(final PersistCatalog newCatalog) {
244 catalog = newCatalog;
245 keyFormat = catalog.getFormat(keyFormat.getClassName());
246 priKeyFormat = catalog.getFormat(priKeyFormat.getClassName());
247 }
248 }