| 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.ArrayList; |
| 11 |
import java.util.Arrays; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.List; |
| 14 |
import java.util.Map; |
| 15 |
|
| 16 |
import com.sleepycat.compat.DbCompat; |
| 17 |
import com.sleepycat.persist.model.ClassMetadata; |
| 18 |
import com.sleepycat.persist.model.EntityModel; |
| 19 |
import com.sleepycat.persist.model.FieldMetadata; |
| 20 |
import com.sleepycat.persist.raw.RawField; |
| 21 |
import com.sleepycat.persist.raw.RawObject; |
| 22 |
import java.util.IdentityHashMap; |
| 23 |
|
| 24 |
/** |
| 25 |
* Format for a composite key class. |
| 26 |
* |
| 27 |
* This class is similar to ComplexFormat in that a composite key class and |
| 28 |
* other complex classes have fields, and the Accessor interface is used to |
| 29 |
* access those fields. Composite key classes are different in the following |
| 30 |
* ways: |
| 31 |
* |
| 32 |
* - The superclass must be Object. No inheritance is allowed. |
| 33 |
* |
| 34 |
* - All instance fields must be annotated with @KeyField, which determines |
| 35 |
* their order in the data bytes. |
| 36 |
* |
| 37 |
* - Although fields may be reference types (primitive wrappers or other simple |
| 38 |
* reference types), they are stored as if they were primitives. No object |
| 39 |
* format ID is stored, and the class of the object must be the declared |
| 40 |
* classs of the field; i.e., no polymorphism is allowed for key fields. |
| 41 |
* In other words, a composite key is stored as an ordinary tuple as defined |
| 42 |
* in the com.sleepycat.bind.tuple package. This keeps the key small and |
| 43 |
* gives it a well defined sort order. |
| 44 |
* |
| 45 |
* - If the key class implements Comparable, it is called by the Database |
| 46 |
* btree comparator. It must therefore be available during JE recovery, |
| 47 |
* before the store and catalog have been opened. To support this, this |
| 48 |
* format can be constructed during recovery. A SimpleCatalog singleton |
| 49 |
* instance is used to provide a catalog of simple types that is used by |
| 50 |
* the composite key format. |
| 51 |
* |
| 52 |
* - When interacting with the Accessor, the composite key format treats the |
| 53 |
* Accessor's non-key fields as its key fields. The Accessor's key fields |
| 54 |
* are secondary keys, while the composite format's key fields are the |
| 55 |
* component parts of a single key. |
| 56 |
* |
| 57 |
* @author Mark Hayes |
| 58 |
*/ |
| 59 |
public class CompositeKeyFormat extends Format { |
| 60 |
|
| 61 |
private static final long serialVersionUID = 306843428409314630L; |
| 62 |
|
| 63 |
private ClassMetadata metadata; |
| 64 |
private List<FieldInfo> fields; |
| 65 |
private transient Accessor objAccessor; |
| 66 |
private transient Accessor rawAccessor; |
| 67 |
private transient volatile Map<String, RawField> rawFields; |
| 68 |
private transient volatile FieldInfo[] rawInputFields; |
| 69 |
|
| 70 |
static String[] getFieldNameArray(List<FieldMetadata> list) { |
| 71 |
int index = 0; |
| 72 |
String[] a = new String[list.size()]; |
| 73 |
for (FieldMetadata f : list) { |
| 74 |
a[index] = f.getName(); |
| 75 |
index += 1; |
| 76 |
} |
| 77 |
return a; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Creates a new composite key format. |
| 82 |
*/ |
| 83 |
CompositeKeyFormat(Catalog catalog, |
| 84 |
Class cls, |
| 85 |
ClassMetadata metadata, |
| 86 |
List<FieldMetadata> fieldMeta) { |
| 87 |
this(catalog, cls, metadata, getFieldNameArray(fieldMeta)); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Reconsistitues a composite key format after a PersistComparator is |
| 92 |
* deserialized. |
| 93 |
*/ |
| 94 |
CompositeKeyFormat(Catalog catalog, Class cls, String[] fieldNames) { |
| 95 |
this(catalog, cls, null /*metadata*/, fieldNames); |
| 96 |
} |
| 97 |
|
| 98 |
private CompositeKeyFormat(Catalog catalog, |
| 99 |
Class cls, |
| 100 |
ClassMetadata metadata, |
| 101 |
String[] fieldNames) { |
| 102 |
super(catalog, cls); |
| 103 |
this.metadata = metadata; |
| 104 |
|
| 105 |
/* Check that the superclass is Object. */ |
| 106 |
Class superCls = cls.getSuperclass(); |
| 107 |
if (superCls != Object.class) { |
| 108 |
throw new IllegalArgumentException |
| 109 |
("Composite key class must be derived from Object: " + |
| 110 |
cls.getName()); |
| 111 |
} |
| 112 |
|
| 113 |
/* Populate fields list in fieldNames order. */ |
| 114 |
List<FieldInfo> instanceFields = |
| 115 |
FieldInfo.getInstanceFields(cls, metadata); |
| 116 |
fields = new ArrayList<FieldInfo>(instanceFields.size()); |
| 117 |
for (String fieldName : fieldNames) { |
| 118 |
FieldInfo field = null; |
| 119 |
for (FieldInfo tryField : instanceFields) { |
| 120 |
if (fieldName.equals(tryField.getName())) { |
| 121 |
field = tryField; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
if (field == null) { |
| 126 |
throw new IllegalArgumentException |
| 127 |
("Composite key field is not an instance field: " + |
| 128 |
getClassName() + '.' + fieldName); |
| 129 |
} |
| 130 |
fields.add(field); |
| 131 |
instanceFields.remove(field); |
| 132 |
Class fieldCls = field.getFieldClass(getCatalog()); |
| 133 |
if (!SimpleCatalog.isSimpleType(fieldCls) && |
| 134 |
!fieldCls.isEnum()) { |
| 135 |
throw new IllegalArgumentException |
| 136 |
("Composite key field is not a simple type or enum: " + |
| 137 |
getClassName() + '.' + fieldName); |
| 138 |
} |
| 139 |
} |
| 140 |
if (instanceFields.size() > 0) { |
| 141 |
throw new IllegalArgumentException |
| 142 |
("All composite key instance fields must be key fields: " + |
| 143 |
getClassName() + '.' + instanceFields.get(0).getName()); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
List<FieldInfo> getFieldInfo() { |
| 148 |
return fields; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
void migrateFromBeta(Map<String, Format> formatMap) { |
| 153 |
super.migrateFromBeta(formatMap); |
| 154 |
for (FieldInfo field : fields) { |
| 155 |
field.migrateFromBeta(formatMap); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
boolean isModelClass() { |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public ClassMetadata getClassMetadata() { |
| 166 |
if (metadata == null) { |
| 167 |
throw DbCompat.unexpectedState(getClassName()); |
| 168 |
} |
| 169 |
return metadata; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public Map<String, RawField> getFields() { |
| 174 |
|
| 175 |
/* |
| 176 |
* Lazily create the raw type information. Synchronization is not |
| 177 |
* required since this object is immutable. If by chance we create two |
| 178 |
* maps when two threads execute this block, no harm is done. But be |
| 179 |
* sure to assign the rawFields field only after the map is fully |
| 180 |
* populated. |
| 181 |
*/ |
| 182 |
if (rawFields == null) { |
| 183 |
Map<String, RawField> map = new HashMap<String, RawField>(); |
| 184 |
for (RawField field : fields) { |
| 185 |
map.put(field.getName(), field); |
| 186 |
} |
| 187 |
rawFields = map; |
| 188 |
} |
| 189 |
return rawFields; |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
void collectRelatedFormats(Catalog catalog, |
| 194 |
Map<String, Format> newFormats) { |
| 195 |
/* Collect field formats. */ |
| 196 |
for (FieldInfo field : fields) { |
| 197 |
field.collectRelatedFormats(catalog, newFormats); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
void initialize(Catalog catalog, EntityModel model, int initVersion) { |
| 203 |
/* Initialize all fields. */ |
| 204 |
for (FieldInfo field : fields) { |
| 205 |
field.initialize(catalog, model, initVersion); |
| 206 |
} |
| 207 |
/* Create the accessor. */ |
| 208 |
Class type = getType(); |
| 209 |
if (type != null) { |
| 210 |
if (EnhancedAccessor.isEnhanced(type)) { |
| 211 |
objAccessor = new EnhancedAccessor(catalog, type, fields); |
| 212 |
} else { |
| 213 |
objAccessor = new ReflectionAccessor(catalog, type, fields); |
| 214 |
} |
| 215 |
} |
| 216 |
rawAccessor = new RawAccessor(this, fields); |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
Object newArray(int len) { |
| 221 |
return objAccessor.newArray(len); |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public Object newInstance(EntityInput input, boolean rawAccess) { |
| 226 |
Accessor accessor = rawAccess ? rawAccessor : objAccessor; |
| 227 |
return accessor.newInstance(); |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public Object readObject(Object o, EntityInput input, boolean rawAccess) |
| 232 |
throws RefreshException { |
| 233 |
|
| 234 |
Accessor accessor = rawAccess ? rawAccessor : objAccessor; |
| 235 |
accessor.readCompositeKeyFields(o, input); |
| 236 |
return o; |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
void writeObject(Object o, EntityOutput output, boolean rawAccess) |
| 241 |
throws RefreshException { |
| 242 |
|
| 243 |
Accessor accessor = rawAccess ? rawAccessor : objAccessor; |
| 244 |
accessor.writeCompositeKeyFields(o, output); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
Object convertRawObject(Catalog catalog, |
| 249 |
boolean rawAccess, |
| 250 |
RawObject rawObject, |
| 251 |
IdentityHashMap converted) |
| 252 |
throws RefreshException { |
| 253 |
|
| 254 |
/* |
| 255 |
* Synchronization is not required since rawInputFields is immutable. |
| 256 |
* If by chance we create duplicate values when two threads execute |
| 257 |
* this block, no harm is done. But be sure to assign the field only |
| 258 |
* after the values are fully populated. |
| 259 |
*/ |
| 260 |
FieldInfo[] myFields = rawInputFields; |
| 261 |
if (myFields == null) { |
| 262 |
myFields = new FieldInfo[fields.size()]; |
| 263 |
fields.toArray(myFields); |
| 264 |
rawInputFields = myFields; |
| 265 |
} |
| 266 |
if (rawObject.getSuper() != null) { |
| 267 |
throw new IllegalArgumentException |
| 268 |
("RawObject has too many superclasses: " + |
| 269 |
rawObject.getType().getClassName()); |
| 270 |
} |
| 271 |
RawObject[] objects = new RawObject[myFields.length]; |
| 272 |
Arrays.fill(objects, rawObject); |
| 273 |
EntityInput in = new RawComplexInput |
| 274 |
(catalog, rawAccess, converted, myFields, objects); |
| 275 |
Object o = newInstance(in, rawAccess); |
| 276 |
converted.put(rawObject, o); |
| 277 |
return readObject(o, in, rawAccess); |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
void skipContents(RecordInput input) |
| 282 |
throws RefreshException { |
| 283 |
|
| 284 |
int maxNum = fields.size(); |
| 285 |
for (int i = 0; i < maxNum; i += 1) { |
| 286 |
fields.get(i).getType().skipContents(input); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
void copySecKey(RecordInput input, RecordOutput output) { |
| 292 |
int maxNum = fields.size(); |
| 293 |
for (int i = 0; i < maxNum; i += 1) { |
| 294 |
fields.get(i).getType().copySecKey(input, output); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
Format getSequenceKeyFormat() { |
| 300 |
if (fields.size() != 1) { |
| 301 |
throw new IllegalArgumentException |
| 302 |
("A composite key class used with a sequence may contain " + |
| 303 |
"only a single key field: " + getClassName()); |
| 304 |
} |
| 305 |
return fields.get(0).getType().getSequenceKeyFormat(); |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
boolean evolve(Format newFormatParam, Evolver evolver) { |
| 310 |
|
| 311 |
/* Disallow evolution to a non-composite format. */ |
| 312 |
if (!(newFormatParam instanceof CompositeKeyFormat)) { |
| 313 |
evolver.addEvolveError |
| 314 |
(this, newFormatParam, null, |
| 315 |
"A composite key class may not be changed to a different " + |
| 316 |
"type"); |
| 317 |
return false; |
| 318 |
} |
| 319 |
CompositeKeyFormat newFormat = (CompositeKeyFormat) newFormatParam; |
| 320 |
|
| 321 |
/* Check for added or removed key fields. */ |
| 322 |
if (fields.size() != newFormat.fields.size()) { |
| 323 |
evolver.addEvolveError |
| 324 |
(this, newFormat, |
| 325 |
"Composite key class fields were added or removed ", |
| 326 |
"Old fields: " + fields + |
| 327 |
" new fields: " + newFormat.fields); |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
/* Check for modified key fields. */ |
| 332 |
boolean newVersion = false; |
| 333 |
for (int i = 0; i < fields.size(); i += 1) { |
| 334 |
int result = evolver.evolveRequiredKeyField |
| 335 |
(this, newFormat, fields.get(i), |
| 336 |
newFormat.fields.get(i)); |
| 337 |
if (result == Evolver.EVOLVE_FAILURE) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
if (result == Evolver.EVOLVE_NEEDED) { |
| 341 |
newVersion = true; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
* We never need to use a custom reader because the physical key field |
| 347 |
* formats never change. But we do create a new evolved format when |
| 348 |
* a type changes (primitive <-> primitive wrapper) so that the new |
| 349 |
* type information is correct. |
| 350 |
*/ |
| 351 |
if (newVersion) { |
| 352 |
evolver.useEvolvedFormat(this, newFormat, newFormat); |
| 353 |
} else { |
| 354 |
evolver.useOldFormat(this, newFormat); |
| 355 |
} |
| 356 |
return true; |
| 357 |
} |
| 358 |
} |