| 1 | <html> |
| 2 | <body> |
| 3 | Utilities for managing class evolution of persistent objects. |
| 4 | |
| 5 | <h1>Class Evolution</h1> |
| 6 | |
| 7 | <p>For persistent data that is not short lived, changes to persistent classes |
| 8 | are almost inevitable. Some changes are compatible with existing types, and |
| 9 | data conversion for these changes is performed automatically and transparently. |
| 10 | Other changes are not compatible with existing types. Mutations can be used to |
| 11 | explicitly manage many types of incompatible changes.</p> |
| 12 | |
| 13 | <p>Not all incompatible class changes can be handled via mutations. For |
| 14 | example, complex refactoring may require a transformation that manipulates |
| 15 | multiple entity instances at once. Such changes are not possible with |
| 16 | mutations but can be made by performing a <a href="#storeConversion">store |
| 17 | conversion</a>.</p> |
| 18 | |
| 19 | <p>The different categories of type changes are described below.</p> |
| 20 | |
| 21 | <h2>Key Field Changes</h2> |
| 22 | |
| 23 | <p>Unlike entity data, key data is not versioned. Therefore, the physical key |
| 24 | format for an index is fixed once the index has been opened, and the changes |
| 25 | allowed for key fields are very limited. The only changes allowed for key |
| 26 | fields are:</p> |
| 27 | <ul> |
| 28 | <li>The name of a key field may be changed, as long as this change is |
| 29 | accompanied by a {@link com.sleepycat.persist.evolve.Renamer} mutation.</li> |
| 30 | <li>A primitive type may be changed to its corresponding primitive wrapper |
| 31 | type. This is a compatible change.</li> |
| 32 | <li>For primary key fields and fields of a composite key class, a primitive |
| 33 | wrapper type may be changed to its corresponding primitive type. This is |
| 34 | allowed because these key fields with reference types may never have null |
| 35 | values. This is a compatible change.</li> |
| 36 | </ul> |
| 37 | |
| 38 | <p>Any other changes to a key field are incompatible and may be made only by |
| 39 | performing a <a href="#storeConversion">store conversion</a>.</p> |
| 40 | |
| 41 | <p>Key ordering, including the behavior of a custom {@link |
| 42 | java.lang.Comparable}, is also fixed, since keys are stored in order in the |
| 43 | index. The specifications for key ordering may not be changed, and the |
| 44 | developer is responsible for not changing the behavior of a {@code Comparable} |
| 45 | key class. <strong>WARNING:</strong>: Changing the behavior of a {@code |
| 46 | Comparable} key class is likely to make the index unusable.</p> |
| 47 | |
| 48 | <h2>Compatible Type Changes</h2> |
| 49 | |
| 50 | <p>Entity data, unlike key data, is versioned. Therefore, some changes can be |
| 51 | made compatibly and other changes can be handled via mutations. Compatible |
| 52 | changes are defined below. To make a compatible class change, a mutation is |
| 53 | not required; however, the class version must be assigned a new (greater) |
| 54 | integer value.</p> |
| 55 | |
| 56 | <p>Changes to a class hierarchy are compatible in some cases. A new class may |
| 57 | be inserted in the hierarchy. A class may be deleted from the hierarchy as |
| 58 | long as one of the following is true: 1) it contains no persistent fields, 2) |
| 59 | any persistent fields are deleted with field Deleter mutations, or 3) the class |
| 60 | is deleted with a class Deleter mutation. Classes in an existing hierarchy may |
| 61 | not be reordered compatibly, and fields may not moved from one class to another |
| 62 | compatibly; for such changes a class Converter mutation is required.</p> |
| 63 | |
| 64 | <p>Changes to field types in entity class definitions are compatible when they |
| 65 | conform to the Java Language Specification definitions for <a |
| 66 | href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2">Widening |
| 67 | Primitive Conversions</a> and <a |
| 68 | href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.5">Widening |
| 69 | Reference Conversions</a>. For example, a smaller integer |
| 70 | type may be changed to a larger integer type, and a reference type may be |
| 71 | changed to one of its supertypes. Automatic widening conversions are performed |
| 72 | as described in the Java Language Specification.</p> |
| 73 | |
| 74 | <p>Primitive types may also be compatibly changed to their corresponding |
| 75 | primitive wrapper types, or to the wrapper type for a widened primitive type. |
| 76 | However, changing from a primitive wrapper type to a primitive type is not a |
| 77 | compatible change since existing null values could not be represented.</p> |
| 78 | |
| 79 | <p>Integer primitive types (byte, short, char, int, long) and their primitive |
| 80 | wrapper types may be compatibly changed to the BigInteger type.</p> |
| 81 | |
| 82 | <p>Enum values may be added compatibly, but may not be deleted or renamed. As |
| 83 | long as new values are declared after existing values, the default sort order |
| 84 | for enum key fields will match the declaration order, i.e, the default sort |
| 85 | order will match the enum ordinal order. If a new value is inserted (declared |
| 86 | before an existing value), it will be sorted after all existing values but |
| 87 | before newly added values. However, these ordering rules are only guaranteed |
| 88 | for enums containing up to 631 values and only if existing values are not |
| 89 | reordered. If more than 631 values are declared or the declarations of |
| 90 | existing values are reordered, then the default sort order will be arbitrary |
| 91 | and will not match the declaration (ordinal) order.</p> |
| 92 | |
| 93 | <p>In addition, adding fields to a class is a compatible change. When a |
| 94 | persistent instance of a class is read that does not contain the new field, the |
| 95 | new field is initialized by the default constructor.</p> |
| 96 | |
| 97 | <p>All other changes to instance fields are considered incompatible. |
| 98 | Incompatible changes may be handled via mutations, as described next.</p> |
| 99 | |
| 100 | <p>Note that whenever a class is changed, either compatibly or incompatibly, a |
| 101 | new (higher) class version number must be assigned. See {@link |
| 102 | com.sleepycat.persist.model.Entity#version} and {@link |
| 103 | com.sleepycat.persist.model.Persistent#version} for information on assigning |
| 104 | class version numbers.</p> |
| 105 | |
| 106 | <h2>Mutations</h2> |
| 107 | |
| 108 | <p>There are three types of mutations: {@link |
| 109 | com.sleepycat.persist.evolve.Renamer}, {@link |
| 110 | com.sleepycat.persist.evolve.Deleter} and {@link |
| 111 | com.sleepycat.persist.evolve.Converter}.</p> |
| 112 | |
| 113 | <p>A class or field can be renamed using a {@link |
| 114 | com.sleepycat.persist.evolve.Renamer}. Renaming is not expensive, since it |
| 115 | does not involve conversion of instance data.</p> |
| 116 | |
| 117 | <p>A class or field can be deleted using a {@link |
| 118 | com.sleepycat.persist.evolve.Deleter}.</p> |
| 119 | <ul> |
| 120 | <li>Deleting an entity class causes removal of the primary and secondary |
| 121 | indices for the store, on other words, removal of all store entities for that |
| 122 | class and its subclasses. Removal is performed when the store is opened. A |
| 123 | {@link com.sleepycat.persist.evolve.Deleter} should be used for an entity class |
| 124 | in all of the following circumstances: |
| 125 | <ul> |
| 126 | <li>When removing the entity class itself.</li> |
| 127 | <li>When removing {@link com.sleepycat.persist.model.Entity} from the class |
| 128 | to make it non-persistent.</li> |
| 129 | <li>When removing {@link com.sleepycat.persist.model.Entity} from the class |
| 130 | and adding {@link com.sleepycat.persist.model.Persistent}, to use it as an |
| 131 | embedded persistent class but not an entity class. The version of the class |
| 132 | must be incremented in this case.</li> |
| 133 | </ul> |
| 134 | </li> |
| 135 | |
| 136 | <li>Deleting a non-entity class does not itself cause deletion of instance |
| 137 | data, but is needed to inform DPL that the deleted class will not be used. |
| 138 | Instances of the deleted class must be handled (discarded or converted to |
| 139 | another class) by {@link com.sleepycat.persist.evolve.Deleter} or {@link |
| 140 | com.sleepycat.persist.evolve.Converter} mutations for the field or enclosing |
| 141 | class that contain embedded instances of the deleted class. A {@link |
| 142 | com.sleepycat.persist.evolve.Deleter} should be used for a non-entity class in |
| 143 | all of the following circumstances: |
| 144 | <ul> |
| 145 | <li>When removing the persistent class itself.</li> |
| 146 | <li>When removing {@link com.sleepycat.persist.model.Persistent} from the |
| 147 | class to make it non-persistent.</li> |
| 148 | <li>When removing {@link com.sleepycat.persist.model.Persistent} from the |
| 149 | class and adding {@link com.sleepycat.persist.model.Entity}, to use it as an |
| 150 | entity class but not an embedded persistent class. The version of the class |
| 151 | must be incremented in this case.</li> |
| 152 | </ul> |
| 153 | </li> |
| 154 | |
| 155 | <li>Deleting a field causes automatic conversion of the instances containing |
| 156 | that field, in order to discard the field values.</li> |
| 157 | </ul> |
| 158 | |
| 159 | <p>Other incompatible changes are handled by creating a {@link |
| 160 | com.sleepycat.persist.evolve.Converter} mutation and implementing a {@link |
| 161 | com.sleepycat.persist.evolve.Conversion#convert Conversion.convert} method that |
| 162 | manipulates the raw objects and/or simple values directly. The {@code convert} |
| 163 | method is passed an object of the old incompatible type and it returns an |
| 164 | object of a current type.</p> |
| 165 | |
| 166 | <p>Conversions can be specified in two ways: for specific fields or for all |
| 167 | instances of a class. A different {@link |
| 168 | com.sleepycat.persist.evolve.Converter} constructor is used in each case. |
| 169 | Field-specific conversions are used instead of class conversions when both are |
| 170 | applicable.</p> |
| 171 | |
| 172 | <p>Note that a class conversion may be not specified for an enum class. A |
| 173 | field conversion, or a class conversion for the class declaring the field, may |
| 174 | be used.</p> |
| 175 | |
| 176 | <p>Note that each mutation is applied to a specific class version number. The |
| 177 | class version must be explicitly specified in a mutation for two reasons:</p> |
| 178 | <ol> |
| 179 | <li>This provides safety in the face of multiple unconverted versions of a |
| 180 | given type. Without a version, a single conversion method would have to handle |
| 181 | multiple input types, and would have to distinguish between them by examining |
| 182 | the data or type information.</li> |
| 183 | <li>This allows arbitrary changes to be made. For example, a series of name |
| 184 | changes may reuse a given name for more than one version. To identify the |
| 185 | specific type being converted or renamed, a version number is needed.</li> |
| 186 | </ol> |
| 187 | <p>See {@link com.sleepycat.persist.model.Entity#version} and {@link |
| 188 | com.sleepycat.persist.model.Persistent#version} for information on assigning |
| 189 | class version numbers.</p> |
| 190 | |
| 191 | <p>Mutations are therefore responsible for converting each existing |
| 192 | incompatible class version to the current version as defined by a current class |
| 193 | definition. For example, consider that class-version A-1 is initially changed |
| 194 | to A-2 and a mutation is added for converting A-1 to A-2. If later changes in |
| 195 | version A-3 occur before converting all A-1 instances to version A-2, the |
| 196 | converter for A-1 will have to be changed. Instead of converting from A-1 to |
| 197 | A-2 it will need to convert from A-1 to A-3. In addition, a mutation |
| 198 | converting A-2 to A-3 will be needed.</p> |
| 199 | |
| 200 | <p>When a {@link com.sleepycat.persist.evolve.Converter} mutation applies to a |
| 201 | given object, other mutations that may apply to that object are not |
| 202 | automatically performed. It is the responsibility of the {@link |
| 203 | com.sleepycat.persist.evolve.Converter} to return an object that conforms to |
| 204 | the current class definition, including renaming fields and classes. If the |
| 205 | input object has nested objects or superclasses that also need conversion, the |
| 206 | converter must perform these nested conversions before returning the final |
| 207 | converted object. This rule avoids the complexity and potential errors that |
| 208 | could result if a converter mutation were automatically combined with other |
| 209 | mutations in an arbitrary manner.</p> |
| 210 | |
| 211 | <p>The {@link com.sleepycat.persist.EntityStore#evolve EntityStore.evolve} |
| 212 | method may optionally be used to ensure that all instances of an old class |
| 213 | version are converted to the current version.</p> |
| 214 | |
| 215 | <h2>Other Metadata Changes</h2> |
| 216 | |
| 217 | <p>When a class that happens to be an entity class is renamed, it remains an |
| 218 | entity class. When a field that happens to be a primary or |
| 219 | secondary key field is renamed, its metadata remains intact as well.</p> |
| 220 | |
| 221 | <p>When the {@link com.sleepycat.persist.model.SecondaryKey} annotation is |
| 222 | added to an <em>existing</em> field, a new index is created automatically. The |
| 223 | new index will be populated by reading the entire primary index when the |
| 224 | primary index is opened.</p> |
| 225 | |
| 226 | <p>When the {@link com.sleepycat.persist.model.SecondaryKey} annotation is |
| 227 | included with a <em>new</em> field, a new index is created automatically. The |
| 228 | new field is required to be a reference type (not a primitive) and must be |
| 229 | initialized to null (the default behavior) in the default constructor. |
| 230 | Entities will be indexed by the field when they are stored with a non-null key |
| 231 | value.</p> |
| 232 | |
| 233 | <p>When a field with the {@link com.sleepycat.persist.model.SecondaryKey} |
| 234 | annotation is deleted, or when the {@link |
| 235 | com.sleepycat.persist.model.SecondaryKey} annotation is removed from a field |
| 236 | without deleting it, the secondary index is removed (dropped). Removal occurs |
| 237 | when the store is opened.</p> |
| 238 | |
| 239 | <p>The {@link com.sleepycat.persist.model.SecondaryKey#relate |
| 240 | SecondaryKey.relate} property may NOT be changed. All other properties of a |
| 241 | {@link com.sleepycat.persist.model.SecondaryKey} may be changed, although |
| 242 | avoiding changes that cause foreign key integrity errors is the responsibility |
| 243 | of the application developer. For example, if the {@link |
| 244 | com.sleepycat.persist.model.SecondaryKey#relatedEntity} property is added but |
| 245 | not all existing secondary keys reference existing primary keys for the related |
| 246 | entity, foreign key integrity errors may occur.</p> |
| 247 | |
| 248 | <p>The {@link com.sleepycat.persist.model.PrimaryKey} annotation may NOT be |
| 249 | removed from a field in an entity class.</p> |
| 250 | |
| 251 | <p>The {@link com.sleepycat.persist.model.PrimaryKey#sequence} property may be |
| 252 | added, removed, or changed to a different name.</p> |
| 253 | |
| 254 | <p>The {@link com.sleepycat.persist.model.Persistent#proxyFor} property may NOT |
| 255 | be added, removed, or changed to a different class.</p> |
| 256 | |
| 257 | <h2>Warnings on Testing and Backups</h2> |
| 258 | |
| 259 | <p>The application developer is responsible for verifying that class evolution |
| 260 | works properly before deploying with a changed set of persistent classes. The |
| 261 | DPL will report errors when old class definitions cannot be evolved, for |
| 262 | example, when a mutation is missing. To test that no such errors will occur, |
| 263 | application test cases must include instances of all persistent classes.</p> |
| 264 | |
| 265 | <p>Converter mutations require special testing. Since the application |
| 266 | conversion method is allowed to return instances of any type, the DPL cannot |
| 267 | check that the proper type is returned until the data is accessed. To avoid |
| 268 | data access errors, application test cases must cover converter mutations for |
| 269 | all potential input and output types.</p> |
| 270 | |
| 271 | <p>When secondary keys are dropped or entity classes are deleted, the |
| 272 | underlying databases are deleted and cannot be recovered from the store. This |
| 273 | takes place when the store is opened. It is strongly recommended that a backup |
| 274 | of the entire store is made before opening the store and causing class |
| 275 | evolution to proceed.</p> |
| 276 | |
| 277 | <h2><a name="storeConversion">Store Conversion</a></h2> |
| 278 | |
| 279 | <p>When mutations are not sufficient for handling class changes, a full store |
| 280 | conversion may be performed. This is necessary for two particular types of |
| 281 | class changes:</p> |
| 282 | <ul> |
| 283 | <li>A change to a physical key format, for example, a change from type |
| 284 | {@code int} to type {@code long}.</li> |
| 285 | <li>A conversion that involves multiple entities at once, for example, |
| 286 | combining two separate entity classes into a new single entity class.</li> |
| 287 | </ul> |
| 288 | |
| 289 | <p>To perform a full store conversion, a program is written that performs the |
| 290 | following steps to copy the data from the old store to a new converted |
| 291 | store:</p> |
| 292 | <ol> |
| 293 | <li>The old store is opened as a {@link com.sleepycat.persist.raw.RawStore} and |
| 294 | the new store is opened as an {@link com.sleepycat.persist.EntityStore}.</li> |
| 295 | <li>All entities are read from the old store. Entities are read using a {@link |
| 296 | com.sleepycat.persist.raw.RawStore} to allow access to entities for which no |
| 297 | compatible class exists.</li> |
| 298 | <li>The {@link com.sleepycat.persist.raw.RawObject} entities are then converted |
| 299 | to the format desired. Raw objects can be arbitrarily manipulated as needed. |
| 300 | The updated raw objects must conform to the new evolved class definitions.</li> |
| 301 | <li>The updated raw entities are converted to live objects by calling the |
| 302 | {@link com.sleepycat.persist.model.EntityModel#convertRawObject |
| 303 | EntityModel.convertRawObject} method of the new store. This method converts |
| 304 | raw objects obtained from a different store, as long as they conform to the new |
| 305 | evolved class definitions.</li> |
| 306 | <li>The new live objects are written to the new {@link |
| 307 | com.sleepycat.persist.EntityStore} using a {@link |
| 308 | com.sleepycat.persist.PrimaryIndex} as usual.</li> |
| 309 | </ol> |
| 310 | |
| 311 | <p>To perform such a conversion, two separate stores must be open at once. |
| 312 | Both stores may be in the same {@link com.sleepycat.db.Environment}, if |
| 313 | desired, by giving them different store names. But since all data is being |
| 314 | rewritten, there are performance advantages to creating the new store in a new |
| 315 | fresh environment: the data will be compacted as it is written, and the old |
| 316 | store can be removed very quickly by deleting the old environment directory |
| 317 | after the conversion is complete.</p> |
| 318 | |
| 319 | |
| 320 | </body> |
| 321 | </html> |