| 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.evolve; |
| 9 |
|
| 10 |
import java.io.Serializable; |
| 11 |
|
| 12 |
import com.sleepycat.persist.model.EntityModel; |
| 13 |
import com.sleepycat.persist.raw.RawObject; |
| 14 |
import com.sleepycat.persist.raw.RawType; |
| 15 |
|
| 16 |
/** |
| 17 |
* Converts an old version of an object value to conform to the current class |
| 18 |
* or field definition. |
| 19 |
* |
| 20 |
* <p>The {@code Conversion} interface is implemented by the user. A |
| 21 |
* {@code Conversion} instance is passed to the {@link Converter#Converter} |
| 22 |
* constructor.</p> |
| 23 |
* |
| 24 |
* <p>The {@code Conversion} interface extends {@link Serializable} and the |
| 25 |
* {@code Conversion} instance is serialized for storage using standard Java |
| 26 |
* serialization. Normally, the {@code Conversion} class should only have |
| 27 |
* transient fields that are initialized in the {@link #initialize} method. |
| 28 |
* While non-transient fields are allowed, care must be taken to only include |
| 29 |
* fields that are serializable and will not pull in large amounts of data.</p> |
| 30 |
* |
| 31 |
* <p>When a class conversion is specified, two special considerations |
| 32 |
* apply:</p> |
| 33 |
* <ol> |
| 34 |
* <li>A class conversion is only applied when to instances of that class. The |
| 35 |
* conversion will not be applied when the class when it appears as a |
| 36 |
* superclass of the instance's class. In this case, a conversion for the |
| 37 |
* instance's class must also be specified.</li> |
| 38 |
* <li>Although field renaming (as well as all other changes) is handled by the |
| 39 |
* conversion method, a field Renamer is still needed when a secondary key |
| 40 |
* field is renamed and field Deleter is still needed when a secondary key |
| 41 |
* field is deleted. This is necessary for evolution of the metadata; |
| 42 |
* specifically, if the key name changes the database must be renamed and if |
| 43 |
* the key field is deleted the secondary database must be deleted.</li> |
| 44 |
* </ol> |
| 45 |
* |
| 46 |
* <p>The {@code Conversion} class must implement the standard equals method. |
| 47 |
* See {@link #equals} for more information.</p> |
| 48 |
* |
| 49 |
* <p>Conversions of simple types are generally simple. For example, a {@code |
| 50 |
* String} field that contains only integer values can be easily converted to |
| 51 |
* an {@code int} field:</p> |
| 52 |
* <pre class="code"> |
| 53 |
* // The old class. Version 0 is implied. |
| 54 |
* // |
| 55 |
* {@literal @Persistent} |
| 56 |
* class Address { |
| 57 |
* String zipCode; |
| 58 |
* ... |
| 59 |
* } |
| 60 |
* |
| 61 |
* // The new class. A new version number must be assigned. |
| 62 |
* // |
| 63 |
* {@literal @Persistent(version=1)} |
| 64 |
* class Address { |
| 65 |
* int zipCode; |
| 66 |
* ... |
| 67 |
* } |
| 68 |
* |
| 69 |
* // The conversion class. |
| 70 |
* // |
| 71 |
* class MyConversion1 implements Conversion { |
| 72 |
* |
| 73 |
* public void initialize(EntityModel model) { |
| 74 |
* // No initialization needed. |
| 75 |
* } |
| 76 |
* |
| 77 |
* public Object convert(Object fromValue) { |
| 78 |
* return Integer.valueOf((String) fromValue); |
| 79 |
* } |
| 80 |
* |
| 81 |
* {@code @Override} |
| 82 |
* public boolean equals(Object o) { |
| 83 |
* return o instanceof MyConversion1; |
| 84 |
* } |
| 85 |
* } |
| 86 |
* |
| 87 |
* // Create a field converter mutation. |
| 88 |
* // |
| 89 |
* Converter converter = new Converter(Address.class.getName(), 0, |
| 90 |
* "zipCode", new MyConversion1()); |
| 91 |
* |
| 92 |
* // Configure the converter as described {@link Mutations here}.</pre> |
| 93 |
* |
| 94 |
* <p>A conversion may perform arbitrary transformations on an object. For |
| 95 |
* example, a conversion may transform a single String address field into an |
| 96 |
* Address object containing four fields for street, city, state and zip |
| 97 |
* code.</p> |
| 98 |
* <pre class="code"> |
| 99 |
* // The old class. Version 0 is implied. |
| 100 |
* // |
| 101 |
* {@literal @Entity} |
| 102 |
* class Person { |
| 103 |
* String address; |
| 104 |
* ... |
| 105 |
* } |
| 106 |
* |
| 107 |
* // The new class. A new version number must be assigned. |
| 108 |
* // |
| 109 |
* {@literal @Entity(version=1)} |
| 110 |
* class Person { |
| 111 |
* Address address; |
| 112 |
* ... |
| 113 |
* } |
| 114 |
* |
| 115 |
* // The new address class. |
| 116 |
* // |
| 117 |
* {@literal @Persistent} |
| 118 |
* class Address { |
| 119 |
* String street; |
| 120 |
* String city; |
| 121 |
* String state; |
| 122 |
* int zipCode; |
| 123 |
* ... |
| 124 |
* } |
| 125 |
* |
| 126 |
* class MyConversion2 implements Conversion { |
| 127 |
* private transient RawType addressType; |
| 128 |
* |
| 129 |
* public void initialize(EntityModel model) { |
| 130 |
* addressType = model.getRawType(Address.class.getName()); |
| 131 |
* } |
| 132 |
* |
| 133 |
* public Object convert(Object fromValue) { |
| 134 |
* |
| 135 |
* // Parse the old address and populate the new address fields |
| 136 |
* // |
| 137 |
* String oldAddress = (String) fromValue; |
| 138 |
* {@literal Map<String, Object> addressValues = new HashMap<String, Object>();} |
| 139 |
* addressValues.put("street", parseStreet(oldAddress)); |
| 140 |
* addressValues.put("city", parseCity(oldAddress)); |
| 141 |
* addressValues.put("state", parseState(oldAddress)); |
| 142 |
* addressValues.put("zipCode", parseZipCode(oldAddress)); |
| 143 |
* |
| 144 |
* // Return new raw Address object |
| 145 |
* // |
| 146 |
* return new RawObject(addressType, addressValues, null); |
| 147 |
* } |
| 148 |
* |
| 149 |
* {@code @Override} |
| 150 |
* public boolean equals(Object o) { |
| 151 |
* return o instanceof MyConversion2; |
| 152 |
* } |
| 153 |
* |
| 154 |
* private String parseStreet(String oldAddress) { ... } |
| 155 |
* private String parseCity(String oldAddress) { ... } |
| 156 |
* private String parseState(String oldAddress) { ... } |
| 157 |
* private Integer parseZipCode(String oldAddress) { ... } |
| 158 |
* } |
| 159 |
* |
| 160 |
* // Create a field converter mutation. |
| 161 |
* // |
| 162 |
* Converter converter = new Converter(Person.class.getName(), 0, |
| 163 |
* "address", new MyConversion2()); |
| 164 |
* |
| 165 |
* // Configure the converter as described {@link Mutations here}.</pre> |
| 166 |
* |
| 167 |
* <p>Note that when a conversion returns a {@link RawObject}, it must return |
| 168 |
* it with a {@link RawType} that is current as defined by the current class |
| 169 |
* definitions. The proper types can be obtained from the {@link EntityModel} |
| 170 |
* in the conversion's {@link #initialize initialize} method.</p> |
| 171 |
* |
| 172 |
* <p>A variation on the example above is where several fields in a class |
| 173 |
* (street, city, state and zipCode) are converted to a single field (address). |
| 174 |
* In this case a class converter rather than a field converter is used.</p> |
| 175 |
* |
| 176 |
* <pre class="code"> |
| 177 |
* // The old class. Version 0 is implied. |
| 178 |
* // |
| 179 |
* {@literal @Entity} |
| 180 |
* class Person { |
| 181 |
* String street; |
| 182 |
* String city; |
| 183 |
* String state; |
| 184 |
* int zipCode; |
| 185 |
* ... |
| 186 |
* } |
| 187 |
* |
| 188 |
* // The new class. A new version number must be assigned. |
| 189 |
* // |
| 190 |
* {@literal @Entity(version=1)} |
| 191 |
* class Person { |
| 192 |
* Address address; |
| 193 |
* ... |
| 194 |
* } |
| 195 |
* |
| 196 |
* // The new address class. |
| 197 |
* // |
| 198 |
* {@literal @Persistent} |
| 199 |
* class Address { |
| 200 |
* String street; |
| 201 |
* String city; |
| 202 |
* String state; |
| 203 |
* int zipCode; |
| 204 |
* ... |
| 205 |
* } |
| 206 |
* |
| 207 |
* class MyConversion3 implements Conversion { |
| 208 |
* private transient RawType newPersonType; |
| 209 |
* private transient RawType addressType; |
| 210 |
* |
| 211 |
* public void initialize(EntityModel model) { |
| 212 |
* newPersonType = model.getRawType(Person.class.getName()); |
| 213 |
* addressType = model.getRawType(Address.class.getName()); |
| 214 |
* } |
| 215 |
* |
| 216 |
* public Object convert(Object fromValue) { |
| 217 |
* |
| 218 |
* // Get field value maps for old and new objects. |
| 219 |
* // |
| 220 |
* RawObject person = (RawObject) fromValue; |
| 221 |
* {@literal Map<String, Object> personValues = person.getValues();} |
| 222 |
* {@literal Map<String, Object> addressValues = new HashMap<String, Object>();} |
| 223 |
* RawObject address = new RawObject(addressType, addressValues, null); |
| 224 |
* |
| 225 |
* // Remove the old address fields and insert the new one. |
| 226 |
* // |
| 227 |
* addressValues.put("street", personValues.remove("street")); |
| 228 |
* addressValues.put("city", personValues.remove("city")); |
| 229 |
* addressValues.put("state", personValues.remove("state")); |
| 230 |
* addressValues.put("zipCode", personValues.remove("zipCode")); |
| 231 |
* personValues.put("address", address); |
| 232 |
* |
| 233 |
* return new RawObject(newPersonType, personValues, person.getSuper()); |
| 234 |
* } |
| 235 |
* |
| 236 |
* {@code @Override} |
| 237 |
* public boolean equals(Object o) { |
| 238 |
* return o instanceof MyConversion3; |
| 239 |
* } |
| 240 |
* } |
| 241 |
* |
| 242 |
* // Create a class converter mutation. |
| 243 |
* // |
| 244 |
* Converter converter = new Converter(Person.class.getName(), 0, |
| 245 |
* new MyConversion3()); |
| 246 |
* |
| 247 |
* // Configure the converter as described {@link Mutations here}.</pre> |
| 248 |
* |
| 249 |
* |
| 250 |
* <p>A conversion can also handle changes to class hierarchies. For example, |
| 251 |
* if a "name" field originally declared in class A is moved to its superclass |
| 252 |
* B, a conversion can move the field value accordingly:</p> |
| 253 |
* |
| 254 |
* <pre class="code"> |
| 255 |
* // The old classes. Version 0 is implied. |
| 256 |
* // |
| 257 |
* {@literal @Persistent} |
| 258 |
* class A extends B { |
| 259 |
* String name; |
| 260 |
* ... |
| 261 |
* } |
| 262 |
* {@literal @Persistent} |
| 263 |
* abstract class B { |
| 264 |
* ... |
| 265 |
* } |
| 266 |
* |
| 267 |
* // The new classes. A new version number must be assigned. |
| 268 |
* // |
| 269 |
* {@literal @Persistent(version=1)} |
| 270 |
* class A extends B { |
| 271 |
* ... |
| 272 |
* } |
| 273 |
* {@literal @Persistent(version=1)} |
| 274 |
* abstract class B { |
| 275 |
* String name; |
| 276 |
* ... |
| 277 |
* } |
| 278 |
* |
| 279 |
* class MyConversion4 implements Conversion { |
| 280 |
* private transient RawType newAType; |
| 281 |
* private transient RawType newBType; |
| 282 |
* |
| 283 |
* public void initialize(EntityModel model) { |
| 284 |
* newAType = model.getRawType(A.class.getName()); |
| 285 |
* newBType = model.getRawType(B.class.getName()); |
| 286 |
* } |
| 287 |
* |
| 288 |
* public Object convert(Object fromValue) { |
| 289 |
* RawObject oldA = (RawObject) fromValue; |
| 290 |
* RawObject oldB = oldA.getSuper(); |
| 291 |
* {@literal Map<String, Object> aValues = oldA.getValues();} |
| 292 |
* {@literal Map<String, Object> bValues = oldB.getValues();} |
| 293 |
* bValues.put("name", aValues.remove("name")); |
| 294 |
* RawObject newB = new RawObject(newBType, bValues, oldB.getSuper()); |
| 295 |
* RawObject newA = new RawObject(newAType, aValues, newB); |
| 296 |
* return newA; |
| 297 |
* } |
| 298 |
* |
| 299 |
* {@code @Override} |
| 300 |
* public boolean equals(Object o) { |
| 301 |
* return o instanceof MyConversion4; |
| 302 |
* } |
| 303 |
* } |
| 304 |
* |
| 305 |
* // Create a class converter mutation. |
| 306 |
* // |
| 307 |
* Converter converter = new Converter(A.class.getName(), 0, |
| 308 |
* new MyConversion4()); |
| 309 |
* |
| 310 |
* // Configure the converter as described {@link Mutations here}.</pre> |
| 311 |
* |
| 312 |
* <p>A conversion may return an instance of a different class entirely, as |
| 313 |
* long as it conforms to current class definitions and is the type expected |
| 314 |
* in the given context (a subtype of the old type, or a type compatible with |
| 315 |
* the new field type). For example, a field that is used to discriminate |
| 316 |
* between two types of objects could be removed and replaced by two new |
| 317 |
* subclasses:</p> <pre class="code"> |
| 318 |
* // The old class. Version 0 is implied. |
| 319 |
* // |
| 320 |
* {@literal @Persistent} |
| 321 |
* class Pet { |
| 322 |
* boolean isCatNotDog; |
| 323 |
* ... |
| 324 |
* } |
| 325 |
* |
| 326 |
* // The new classes. A new version number must be assigned to the Pet class. |
| 327 |
* // |
| 328 |
* {@literal @Persistent(version=1)} |
| 329 |
* class Pet { |
| 330 |
* ... |
| 331 |
* } |
| 332 |
* {@literal @Persistent} |
| 333 |
* class Cat extends Pet { |
| 334 |
* ... |
| 335 |
* } |
| 336 |
* {@literal @Persistent} |
| 337 |
* class Dog extends Pet { |
| 338 |
* ... |
| 339 |
* } |
| 340 |
* |
| 341 |
* class MyConversion5 implements Conversion { |
| 342 |
* private transient RawType newPetType; |
| 343 |
* private transient RawType dogType; |
| 344 |
* private transient RawType catType; |
| 345 |
* |
| 346 |
* public void initialize(EntityModel model) { |
| 347 |
* newPetType = model.getRawType(Pet.class.getName()); |
| 348 |
* dogType = model.getRawType(Dog.class.getName()); |
| 349 |
* catType = model.getRawType(Cat.class.getName()); |
| 350 |
* } |
| 351 |
* |
| 352 |
* public Object convert(Object fromValue) { |
| 353 |
* RawObject pet = (RawObject) fromValue; |
| 354 |
* {@literal Map<String, Object> petValues = pet.getValues();} |
| 355 |
* Boolean isCat = (Boolean) petValues.remove("isCatNotDog"); |
| 356 |
* RawObject newPet = new RawObject(newPetType, petValues, |
| 357 |
* pet.getSuper()); |
| 358 |
* RawType newSubType = isCat ? catType : dogType; |
| 359 |
* return new RawObject(newSubType, Collections.emptyMap(), newPet); |
| 360 |
* } |
| 361 |
* |
| 362 |
* {@code @Override} |
| 363 |
* public boolean equals(Object o) { |
| 364 |
* return o instanceof MyConversion5; |
| 365 |
* } |
| 366 |
* } |
| 367 |
* |
| 368 |
* // Create a class converter mutation. |
| 369 |
* // |
| 370 |
* Converter converter = new Converter(Pet.class.getName(), 0, |
| 371 |
* new MyConversion5()); |
| 372 |
* |
| 373 |
* // Configure the converter as described {@link Mutations here}.</pre> |
| 374 |
* |
| 375 |
* <p>The primary limitation of a conversion is that it may access at most a |
| 376 |
* single entity instance at one time. Conversions involving multiple entities |
| 377 |
* at once may be made by performing a <a |
| 378 |
* href="package-summary.html#storeConversion">store conversion</a>.</p> |
| 379 |
* |
| 380 |
* @see com.sleepycat.persist.evolve Class Evolution |
| 381 |
* @author Mark Hayes |
| 382 |
*/ |
| 383 |
public interface Conversion extends Serializable { |
| 384 |
|
| 385 |
/** |
| 386 |
* Initializes the conversion, allowing it to obtain raw type information |
| 387 |
* from the entity model. |
| 388 |
*/ |
| 389 |
void initialize(EntityModel model); |
| 390 |
|
| 391 |
/** |
| 392 |
* Converts an old version of an object value to conform to the current |
| 393 |
* class or field definition. |
| 394 |
* |
| 395 |
* <p>If a {@link RuntimeException} is thrown by this method, it will be |
| 396 |
* thrown to the original caller. Similarly, a {@link |
| 397 |
* IllegalArgumentException} will be thrown to the original caller if the |
| 398 |
* object returned by this method does not conform to current class |
| 399 |
* definitions.</p> |
| 400 |
* |
| 401 |
* <p>The class of the input and output object may be one of the simple |
| 402 |
* types or {@link RawObject}. For primitive types, the primitive wrapper |
| 403 |
* class is used.</p> |
| 404 |
* |
| 405 |
* @param fromValue the object value being converted. The type of this |
| 406 |
* value is defined by the old class version that is being converted. |
| 407 |
* |
| 408 |
* @return the converted object. The type of this value must conform to |
| 409 |
* a current class definition. If this is a class conversion, it must |
| 410 |
* be the current version of the class. If this is a field conversion, it |
| 411 |
* must be of a type compatible with the current declared type of the |
| 412 |
* field. |
| 413 |
*/ |
| 414 |
Object convert(Object fromValue); |
| 415 |
|
| 416 |
/** |
| 417 |
* The standard {@code equals} method that must be implemented by |
| 418 |
* conversion class. |
| 419 |
* |
| 420 |
* <p>When mutations are specified when opening a store, the specified and |
| 421 |
* previously stored mutations are compared for equality. If they are |
| 422 |
* equal, there is no need to replace the existing mutations in the stored |
| 423 |
* catalog. To accurately determine equality, the conversion class must |
| 424 |
* implement the {@code equals} method.</p> |
| 425 |
* |
| 426 |
* <p>If the {@code equals} method is not explicitly implemented by the |
| 427 |
* conversion class or a superclass other than {@code Object}, {@code |
| 428 |
* IllegalArgumentException} will be thrown when the store is opened.</p> |
| 429 |
* |
| 430 |
* <p>Normally whenever {@code equals} is implemented the {@code hashCode} |
| 431 |
* method should also be implemented to support hash sets and maps. |
| 432 |
* However, hash sets and maps containing <code>Conversion</code> objects |
| 433 |
* are not used by the DPL and therefore the DPL does not require |
| 434 |
* {@code hashCode} to be implemented.</p> |
| 435 |
*/ |
| 436 |
boolean equals(Object other); |
| 437 |
} |