| 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.List; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.HashSet; |
| 14 |
import java.util.Map; |
| 15 |
import java.util.Set; |
| 16 |
|
| 17 |
import com.sleepycat.compat.DbCompat; |
| 18 |
import com.sleepycat.db.DatabaseException; |
| 19 |
import com.sleepycat.db.Transaction; |
| 20 |
import com.sleepycat.persist.evolve.Converter; |
| 21 |
import com.sleepycat.persist.evolve.Deleter; |
| 22 |
import com.sleepycat.persist.evolve.Mutation; |
| 23 |
import com.sleepycat.persist.evolve.Mutations; |
| 24 |
import com.sleepycat.persist.evolve.Renamer; |
| 25 |
import com.sleepycat.persist.model.SecondaryKeyMetadata; |
| 26 |
import java.util.IdentityHashMap; |
| 27 |
|
| 28 |
/** |
| 29 |
* Evolves each old format that is still relevant if necessary, using Mutations |
| 30 |
* to configure deleters, renamers, and converters. |
| 31 |
* |
| 32 |
* @author Mark Hayes |
| 33 |
*/ |
| 34 |
class Evolver { |
| 35 |
|
| 36 |
static final int EVOLVE_NONE = 0; |
| 37 |
static final int EVOLVE_NEEDED = 1; |
| 38 |
static final int EVOLVE_FAILURE = 2; |
| 39 |
|
| 40 |
private PersistCatalog catalog; |
| 41 |
private String storePrefix; |
| 42 |
private Mutations mutations; |
| 43 |
private Map<String, Format> newFormats; |
| 44 |
private boolean forceEvolution; |
| 45 |
private boolean disallowClassChanges; |
| 46 |
private boolean nestedFormatsChanged; |
| 47 |
private Map<Format, Format> changedFormats; |
| 48 |
private StringBuilder errors; |
| 49 |
private Set<String> deleteDbs; |
| 50 |
private Map<String, String> renameDbs; |
| 51 |
private Map<Format, Format> renameFormats; |
| 52 |
private Map<Integer, Boolean> evolvedFormats; |
| 53 |
private List<Format> unprocessedFormats; |
| 54 |
private Map<Format, Set<Format>> subclassMap; |
| 55 |
|
| 56 |
Evolver(PersistCatalog catalog, |
| 57 |
String storePrefix, |
| 58 |
Mutations mutations, |
| 59 |
Map<String, Format> newFormats, |
| 60 |
boolean forceEvolution, |
| 61 |
boolean disallowClassChanges) { |
| 62 |
this.catalog = catalog; |
| 63 |
this.storePrefix = storePrefix; |
| 64 |
this.mutations = mutations; |
| 65 |
this.newFormats = newFormats; |
| 66 |
this.forceEvolution = forceEvolution; |
| 67 |
this.disallowClassChanges = disallowClassChanges; |
| 68 |
changedFormats = new IdentityHashMap<Format, Format>(); |
| 69 |
errors = new StringBuilder(); |
| 70 |
deleteDbs = new HashSet<String>(); |
| 71 |
renameDbs = new HashMap<String, String>(); |
| 72 |
renameFormats = new IdentityHashMap<Format, Format>(); |
| 73 |
evolvedFormats = new HashMap<Integer, Boolean>(); |
| 74 |
unprocessedFormats = new ArrayList<Format>(); |
| 75 |
subclassMap = catalog.getSubclassMap(); |
| 76 |
} |
| 77 |
|
| 78 |
final Mutations getMutations() { |
| 79 |
return mutations; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns whether any formats were changed during evolution, and therefore |
| 84 |
* need to be stored in the catalog. |
| 85 |
*/ |
| 86 |
boolean areFormatsChanged() { |
| 87 |
return !changedFormats.isEmpty(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns whether the given format was changed during evolution. |
| 92 |
*/ |
| 93 |
boolean isFormatChanged(Format format) { |
| 94 |
return changedFormats.containsKey(format); |
| 95 |
} |
| 96 |
|
| 97 |
private void setFormatsChanged(Format oldFormat) { |
| 98 |
checkClassChangesAllowed(oldFormat); |
| 99 |
changedFormats.put(oldFormat, oldFormat); |
| 100 |
nestedFormatsChanged = true; |
| 101 |
/* PersistCatalog.expectNoClassChanges is true in unit tests only. */ |
| 102 |
if (PersistCatalog.expectNoClassChanges) { |
| 103 |
throw new IllegalStateException("expectNoClassChanges"); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private void checkClassChangesAllowed(Format oldFormat) { |
| 108 |
if (disallowClassChanges) { |
| 109 |
throw new IllegalStateException |
| 110 |
("When performing an upgrade changes are not allowed " + |
| 111 |
"but were made to: " + oldFormat.getClassName()); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns the set of formats for a specific superclass format, or null if |
| 117 |
* the superclass is not a complex type or has not subclasses. |
| 118 |
*/ |
| 119 |
Set<Format> getSubclassFormats(Format superFormat) { |
| 120 |
return subclassMap.get(superFormat); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns an error string if any mutations are invalid or missing, or |
| 125 |
* returns null otherwise. If non-null is returned, the store may not be |
| 126 |
* opened. |
| 127 |
*/ |
| 128 |
String getErrors() { |
| 129 |
if (errors.length() > 0) { |
| 130 |
return errors.toString() + "\n---\n" + |
| 131 |
"(Note that when upgrading an application in a replicated " + |
| 132 |
"environment, this exception may indicate that the Master " + |
| 133 |
"was mistakenly upgraded before this Replica could be " + |
| 134 |
"upgraded, and the solution is to upgrade this Replica.)"; |
| 135 |
} else { |
| 136 |
return null; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Adds a newline and the given error. |
| 142 |
*/ |
| 143 |
private void addError(String error) { |
| 144 |
if (errors.length() > 0) { |
| 145 |
errors.append("\n---\n"); |
| 146 |
} |
| 147 |
errors.append(error); |
| 148 |
} |
| 149 |
|
| 150 |
private String getClassVersionLabel(Format format, String prefix) { |
| 151 |
if (format != null) { |
| 152 |
return prefix + |
| 153 |
" class: " + format.getClassName() + |
| 154 |
" version: " + format.getVersion(); |
| 155 |
} else { |
| 156 |
return ""; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Adds a specified error when no specific mutation is involved. |
| 162 |
*/ |
| 163 |
void addEvolveError(Format oldFormat, |
| 164 |
Format newFormat, |
| 165 |
String scenario, |
| 166 |
String error) { |
| 167 |
checkClassChangesAllowed(oldFormat); |
| 168 |
if (scenario == null) { |
| 169 |
scenario = "Error"; |
| 170 |
} |
| 171 |
addError(scenario + " when evolving" + |
| 172 |
getClassVersionLabel(oldFormat, "") + |
| 173 |
getClassVersionLabel(newFormat, " to") + |
| 174 |
" Error: " + error); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Adds an error for an invalid mutation. |
| 179 |
*/ |
| 180 |
void addInvalidMutation(Format oldFormat, |
| 181 |
Format newFormat, |
| 182 |
Mutation mutation, |
| 183 |
String error) { |
| 184 |
checkClassChangesAllowed(oldFormat); |
| 185 |
addError("Invalid mutation: " + mutation + |
| 186 |
getClassVersionLabel(oldFormat, " For") + |
| 187 |
getClassVersionLabel(newFormat, " New") + |
| 188 |
" Error: " + error); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Adds an error for a missing mutation. |
| 193 |
*/ |
| 194 |
void addMissingMutation(Format oldFormat, |
| 195 |
Format newFormat, |
| 196 |
String error) { |
| 197 |
checkClassChangesAllowed(oldFormat); |
| 198 |
addError("Mutation is missing to evolve" + |
| 199 |
getClassVersionLabel(oldFormat, "") + |
| 200 |
getClassVersionLabel(newFormat, " to") + |
| 201 |
" Error: " + error); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Called by PersistCatalog for all non-entity formats. |
| 206 |
*/ |
| 207 |
void addNonEntityFormat(Format oldFormat) { |
| 208 |
unprocessedFormats.add(oldFormat); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Called by PersistCatalog after calling evolveFormat or |
| 213 |
* addNonEntityFormat for all old formats. |
| 214 |
* |
| 215 |
* We do not require deletion of an unreferenced class for two |
| 216 |
* reasons: 1) built-in proxy classes may not be referenced, 2) the |
| 217 |
* user may wish to declare persistent classes that are not yet used. |
| 218 |
*/ |
| 219 |
void finishEvolution() { |
| 220 |
/* Process unreferenced classes. */ |
| 221 |
for (Format oldFormat : unprocessedFormats) { |
| 222 |
oldFormat.setUnused(true); |
| 223 |
evolveFormat(oldFormat); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Called by PersistCatalog for all entity formats, and by Format.evolve |
| 229 |
* methods for all potentially referenced non-entity formats. |
| 230 |
*/ |
| 231 |
boolean evolveFormat(Format oldFormat) { |
| 232 |
if (oldFormat.isNew()) { |
| 233 |
return true; |
| 234 |
} |
| 235 |
boolean result; |
| 236 |
Format oldEntityFormat = oldFormat.getEntityFormat(); |
| 237 |
boolean trackEntityChanges = oldEntityFormat != null; |
| 238 |
boolean saveNestedFormatsChanged = nestedFormatsChanged; |
| 239 |
if (trackEntityChanges) { |
| 240 |
nestedFormatsChanged = false; |
| 241 |
} |
| 242 |
Integer oldFormatId = oldFormat.getId(); |
| 243 |
if (evolvedFormats.containsKey(oldFormatId)) { |
| 244 |
result = evolvedFormats.get(oldFormatId); |
| 245 |
} else { |
| 246 |
evolvedFormats.put(oldFormatId, true); |
| 247 |
result = evolveFormatInternal(oldFormat); |
| 248 |
evolvedFormats.put(oldFormatId, result); |
| 249 |
} |
| 250 |
if (oldFormat.getLatestVersion().isNew()) { |
| 251 |
nestedFormatsChanged = true; |
| 252 |
} |
| 253 |
if (trackEntityChanges) { |
| 254 |
if (nestedFormatsChanged) { |
| 255 |
Format latest = oldEntityFormat.getLatestVersion(); |
| 256 |
if (latest != null) { |
| 257 |
latest.setEvolveNeeded(true); |
| 258 |
} |
| 259 |
} |
| 260 |
nestedFormatsChanged = saveNestedFormatsChanged; |
| 261 |
} |
| 262 |
return result; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Tries to evolve a given existing format to the current version of the |
| 267 |
* class and returns false if an invalid mutation is encountered or the |
| 268 |
* configured mutations are not sufficient. |
| 269 |
*/ |
| 270 |
private boolean evolveFormatInternal(Format oldFormat) { |
| 271 |
|
| 272 |
/* Predefined formats and deleted classes never need evolving. */ |
| 273 |
if (Format.isPredefined(oldFormat) || oldFormat.isDeleted()) { |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
/* Get class mutations. */ |
| 278 |
String oldName = oldFormat.getClassName(); |
| 279 |
int oldVersion = oldFormat.getVersion(); |
| 280 |
Renamer renamer = mutations.getRenamer(oldName, oldVersion, null); |
| 281 |
Deleter deleter = mutations.getDeleter(oldName, oldVersion, null); |
| 282 |
Converter converter = |
| 283 |
mutations.getConverter(oldName, oldVersion, null); |
| 284 |
if (deleter != null && (converter != null || renamer != null)) { |
| 285 |
addInvalidMutation |
| 286 |
(oldFormat, null, deleter, |
| 287 |
"Class Deleter not allowed along with a Renamer or " + |
| 288 |
"Converter for the same class"); |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
/* |
| 293 |
* For determining the new name, arrays get special treatment. The |
| 294 |
* component format is evolved in the process, and we disallow muations |
| 295 |
* for arrays. |
| 296 |
*/ |
| 297 |
String newName; |
| 298 |
if (oldFormat.isArray()) { |
| 299 |
if (deleter != null || converter != null || renamer != null) { |
| 300 |
Mutation mutation = (deleter != null) ? deleter : |
| 301 |
((converter != null) ? converter : renamer); |
| 302 |
addInvalidMutation |
| 303 |
(oldFormat, null, mutation, |
| 304 |
"Mutations not allowed for an array"); |
| 305 |
return false; |
| 306 |
} |
| 307 |
Format compFormat = oldFormat.getComponentType(); |
| 308 |
if (!evolveFormat(compFormat)) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
Format latest = compFormat.getLatestVersion(); |
| 312 |
if (latest != compFormat) { |
| 313 |
newName = (latest.isArray() ? "[" : "[L") + |
| 314 |
latest.getClassName() + ';'; |
| 315 |
} else { |
| 316 |
newName = oldName; |
| 317 |
} |
| 318 |
} else { |
| 319 |
newName = (renamer != null) ? renamer.getNewName() : oldName; |
| 320 |
} |
| 321 |
|
| 322 |
/* Try to get the new class format. Save exception for later. */ |
| 323 |
Format newFormat; |
| 324 |
String newFormatException; |
| 325 |
try { |
| 326 |
Class newClass = catalog.resolveClass(newName); |
| 327 |
try { |
| 328 |
newFormat = catalog.createFormat(newClass, newFormats); |
| 329 |
assert newFormat != oldFormat : newFormat.getClassName(); |
| 330 |
newFormatException = null; |
| 331 |
} catch (Exception e) { |
| 332 |
newFormat = null; |
| 333 |
newFormatException = e.toString(); |
| 334 |
} |
| 335 |
} catch (ClassNotFoundException e) { |
| 336 |
newFormat = null; |
| 337 |
newFormatException = e.toString(); |
| 338 |
} |
| 339 |
|
| 340 |
if (newFormat != null) { |
| 341 |
|
| 342 |
/* |
| 343 |
* If the old format is not the existing latest version and the new |
| 344 |
* format is not an existing format, then we must evolve the latest |
| 345 |
* old version to the new format first. We cannot evolve old |
| 346 |
* format to a new format that may be discarded because it is equal |
| 347 |
* to the latest existing format (which will remain the current |
| 348 |
* version). |
| 349 |
*/ |
| 350 |
if (oldFormat != oldFormat.getLatestVersion() && |
| 351 |
newFormat.getPreviousVersion() == null) { |
| 352 |
assert newFormats.containsValue(newFormat); |
| 353 |
Format oldLatestFormat = oldFormat.getLatestVersion(); |
| 354 |
if (!evolveFormat(oldLatestFormat)) { |
| 355 |
return false; |
| 356 |
} |
| 357 |
if (oldLatestFormat == oldLatestFormat.getLatestVersion()) { |
| 358 |
assert !newFormats.containsValue(newFormat) : newFormat; |
| 359 |
/* newFormat equals oldLatestFormat and was discarded. */ |
| 360 |
newFormat = oldLatestFormat; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/* |
| 365 |
* If the old format was previously evolved to the new format |
| 366 |
* (which means the new format is actually an existing format), |
| 367 |
* then there is nothing to do. This is the case where no class |
| 368 |
* changes were made. |
| 369 |
* |
| 370 |
* However, if mutations were specified when opening the catalog |
| 371 |
* that are different than the mutations last used, then we must |
| 372 |
* force the re-evolution of all old formats. |
| 373 |
*/ |
| 374 |
if (!forceEvolution && |
| 375 |
newFormat == oldFormat.getLatestVersion()) { |
| 376 |
return true; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
/* Apply class Renamer and continue if successful. */ |
| 381 |
if (renamer != null) { |
| 382 |
if (!applyClassRenamer(renamer, oldFormat, newFormat)) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/* Apply class Converter and return. */ |
| 388 |
if (converter != null) { |
| 389 |
if (oldFormat.isEntity()) { |
| 390 |
if (newFormat == null || !newFormat.isEntity()) { |
| 391 |
addInvalidMutation |
| 392 |
(oldFormat, newFormat, converter, |
| 393 |
"Class converter not allowed for an entity class " + |
| 394 |
"that is no longer present or not having an " + |
| 395 |
"@Entity annotation"); |
| 396 |
return false; |
| 397 |
} |
| 398 |
if (!oldFormat.evolveMetadata(newFormat, converter, this)) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
} |
| 402 |
return applyConverter(converter, oldFormat, newFormat); |
| 403 |
} |
| 404 |
|
| 405 |
/* Apply class Deleter and return. */ |
| 406 |
boolean needDeleter = |
| 407 |
(newFormat == null) || |
| 408 |
(newFormat.isEntity() != oldFormat.isEntity()); |
| 409 |
if (deleter != null) { |
| 410 |
if (!needDeleter) { |
| 411 |
addInvalidMutation |
| 412 |
(oldFormat, newFormat, deleter, |
| 413 |
"Class deleter not allowed when the class and its " + |
| 414 |
"@Entity or @Persistent annotation is still present"); |
| 415 |
return false; |
| 416 |
} |
| 417 |
return applyClassDeleter(deleter, oldFormat, newFormat); |
| 418 |
} else { |
| 419 |
if (needDeleter) { |
| 420 |
if (newFormat == null) { |
| 421 |
assert newFormatException != null; |
| 422 |
/* FindBugs newFormat known to be null excluded. */ |
| 423 |
addMissingMutation |
| 424 |
(oldFormat, newFormat, newFormatException); |
| 425 |
} else { |
| 426 |
addMissingMutation |
| 427 |
(oldFormat, newFormat, |
| 428 |
"@Entity switched to/from @Persistent"); |
| 429 |
} |
| 430 |
return false; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/* |
| 435 |
* Class-level mutations have been applied. Now apply field mutations |
| 436 |
* (for complex classes) or special conversions (enum conversions, for |
| 437 |
* example) by calling the old format's evolve method. |
| 438 |
*/ |
| 439 |
return oldFormat.evolve(newFormat, this); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Use the old format and discard the new format. Called by |
| 444 |
* Format.evolve when the old and new formats are identical. |
| 445 |
*/ |
| 446 |
void useOldFormat(Format oldFormat, Format newFormat) { |
| 447 |
Format renamedFormat = renameFormats.get(oldFormat); |
| 448 |
if (renamedFormat != null) { |
| 449 |
|
| 450 |
/* |
| 451 |
* The format was renamed but, because this method is called, we |
| 452 |
* know that no other class changes were made. Use the new/renamed |
| 453 |
* format as the reader. |
| 454 |
*/ |
| 455 |
assert renamedFormat == newFormat; |
| 456 |
useEvolvedFormat(oldFormat, renamedFormat, renamedFormat); |
| 457 |
} else if (newFormat != null && |
| 458 |
(oldFormat.getVersion() != newFormat.getVersion() || |
| 459 |
!oldFormat.isCurrentVersion())) { |
| 460 |
|
| 461 |
/* |
| 462 |
* If the user wants a new version number, but ther are no other |
| 463 |
* changes, we will oblige. Or, if an attempt is being made to |
| 464 |
* use an old version, then the following events happened and we |
| 465 |
* must evolve the old format: |
| 466 |
* 1) The (previously) latest version of the format was evolved |
| 467 |
* because it is not equal to the live class version. Note that |
| 468 |
* evolveFormatInternal always evolves the latest version first. |
| 469 |
* 2) We are now attempting to evolve an older version of the same |
| 470 |
* format, and it happens to be equal to the live class version. |
| 471 |
* However, we're already committed to the new format, and we must |
| 472 |
* evolve all versions. |
| 473 |
* [#16467] |
| 474 |
*/ |
| 475 |
useEvolvedFormat(oldFormat, newFormat, newFormat); |
| 476 |
} else { |
| 477 |
/* The new format is discarded. */ |
| 478 |
catalog.useExistingFormat(oldFormat); |
| 479 |
if (newFormat != null) { |
| 480 |
newFormats.remove(newFormat.getClassName()); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Install an evolver Reader in the old format. Called by Format.evolve |
| 487 |
* when the old and new formats are not identical. |
| 488 |
*/ |
| 489 |
void useEvolvedFormat(Format oldFormat, |
| 490 |
Reader evolveReader, |
| 491 |
Format newFormat) { |
| 492 |
oldFormat.setReader(evolveReader); |
| 493 |
if (newFormat != null) { |
| 494 |
oldFormat.setLatestVersion(newFormat); |
| 495 |
} |
| 496 |
setFormatsChanged(oldFormat); |
| 497 |
} |
| 498 |
|
| 499 |
private boolean applyClassRenamer(Renamer renamer, |
| 500 |
Format oldFormat, |
| 501 |
Format newFormat) { |
| 502 |
if (!checkUpdatedVersion(renamer, oldFormat, newFormat)) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
if (oldFormat.isEntity() && oldFormat.isCurrentVersion()) { |
| 506 |
String newClassName = newFormat.getClassName(); |
| 507 |
String oldClassName = oldFormat.getClassName(); |
| 508 |
/* Queue the renaming of the primary and secondary databases. */ |
| 509 |
renameDbs.put |
| 510 |
(Store.makePriDbName(storePrefix, oldClassName), |
| 511 |
Store.makePriDbName(storePrefix, newClassName)); |
| 512 |
for (SecondaryKeyMetadata keyMeta : |
| 513 |
oldFormat.getEntityMetadata().getSecondaryKeys().values()) { |
| 514 |
String keyName = keyMeta.getKeyName(); |
| 515 |
renameDbs.put |
| 516 |
(Store.makeSecDbName(storePrefix, oldClassName, keyName), |
| 517 |
Store.makeSecDbName(storePrefix, newClassName, keyName)); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/* |
| 522 |
* Link the old format to the renamed format so that we can detect the |
| 523 |
* rename in useOldFormat. |
| 524 |
*/ |
| 525 |
renameFormats.put(oldFormat, newFormat); |
| 526 |
|
| 527 |
setFormatsChanged(oldFormat); |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Called by ComplexFormat when a secondary key name is changed. |
| 533 |
*/ |
| 534 |
void renameSecondaryDatabase(String oldEntityClass, |
| 535 |
String newEntityClass, |
| 536 |
String oldKeyName, |
| 537 |
String newKeyName) { |
| 538 |
renameDbs.put |
| 539 |
(Store.makeSecDbName(storePrefix, oldEntityClass, oldKeyName), |
| 540 |
Store.makeSecDbName(storePrefix, newEntityClass, newKeyName)); |
| 541 |
} |
| 542 |
|
| 543 |
private boolean applyClassDeleter(Deleter deleter, |
| 544 |
Format oldFormat, |
| 545 |
Format newFormat) { |
| 546 |
if (!checkUpdatedVersion(deleter, oldFormat, newFormat)) { |
| 547 |
return false; |
| 548 |
} |
| 549 |
if (oldFormat.isEntity() && oldFormat.isCurrentVersion()) { |
| 550 |
/* Queue the deletion of the primary and secondary databases. */ |
| 551 |
String className = oldFormat.getClassName(); |
| 552 |
deleteDbs.add(Store.makePriDbName(storePrefix, className)); |
| 553 |
for (SecondaryKeyMetadata keyMeta : |
| 554 |
oldFormat.getEntityMetadata().getSecondaryKeys().values()) { |
| 555 |
deleteDbs.add(Store.makeSecDbName |
| 556 |
(storePrefix, className, keyMeta.getKeyName())); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
/* |
| 561 |
* Set the format to deleted last, so that the above test using |
| 562 |
* isCurrentVersion works properly. |
| 563 |
*/ |
| 564 |
oldFormat.setDeleted(true); |
| 565 |
if (newFormat != null) { |
| 566 |
oldFormat.setLatestVersion(newFormat); |
| 567 |
} |
| 568 |
|
| 569 |
setFormatsChanged(oldFormat); |
| 570 |
return true; |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Called by ComplexFormat when a secondary key is dropped. |
| 575 |
*/ |
| 576 |
void deleteSecondaryDatabase(String oldEntityClass, String keyName) { |
| 577 |
deleteDbs.add(Store.makeSecDbName |
| 578 |
(storePrefix, oldEntityClass, keyName)); |
| 579 |
} |
| 580 |
|
| 581 |
private boolean applyConverter(Converter converter, |
| 582 |
Format oldFormat, |
| 583 |
Format newFormat) { |
| 584 |
if (!checkUpdatedVersion(converter, oldFormat, newFormat)) { |
| 585 |
return false; |
| 586 |
} |
| 587 |
Reader reader = new ConverterReader(converter); |
| 588 |
useEvolvedFormat(oldFormat, reader, newFormat); |
| 589 |
return true; |
| 590 |
} |
| 591 |
|
| 592 |
boolean isClassConverted(Format format) { |
| 593 |
return format.getReader() instanceof ConverterReader; |
| 594 |
} |
| 595 |
|
| 596 |
private boolean checkUpdatedVersion(Mutation mutation, |
| 597 |
Format oldFormat, |
| 598 |
Format newFormat) { |
| 599 |
if (newFormat != null && |
| 600 |
!oldFormat.isEnum() && |
| 601 |
newFormat.getVersion() <= oldFormat.getVersion()) { |
| 602 |
addInvalidMutation |
| 603 |
(oldFormat, newFormat, mutation, |
| 604 |
"A new higher version number must be assigned"); |
| 605 |
return false; |
| 606 |
} else { |
| 607 |
return true; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
boolean checkUpdatedVersion(String scenario, |
| 612 |
Format oldFormat, |
| 613 |
Format newFormat) { |
| 614 |
if (newFormat != null && |
| 615 |
!oldFormat.isEnum() && |
| 616 |
newFormat.getVersion() <= oldFormat.getVersion()) { |
| 617 |
addEvolveError |
| 618 |
(oldFormat, newFormat, scenario, |
| 619 |
"A new higher version number must be assigned"); |
| 620 |
return false; |
| 621 |
} else { |
| 622 |
return true; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
void renameAndRemoveDatabases(Store store, Transaction txn) |
| 627 |
throws DatabaseException { |
| 628 |
|
| 629 |
for (String dbName : deleteDbs) { |
| 630 |
String[] fileAndDbNames = store.parseDbName(dbName); |
| 631 |
/* Do nothing if database does not exist. */ |
| 632 |
DbCompat.removeDatabase |
| 633 |
(store.getEnvironment(), txn, |
| 634 |
fileAndDbNames[0], fileAndDbNames[1]); |
| 635 |
} |
| 636 |
|
| 637 |
/* |
| 638 |
* An importunate locker must be used to rename databases, since rename |
| 639 |
* with Database handles open is not currently possible. This is the |
| 640 |
* same sort of operation as performed by the HA replayer. If the |
| 641 |
* evolution (and rename here) occurs in a replication group upgrade, |
| 642 |
* this will cause DatabasePreemptedException the next time the |
| 643 |
* database is accessed (via the store or otherwise). In a standalone |
| 644 |
* environment, this won't happen because the database won't be open; |
| 645 |
* evolve occurs before opening the database in this case. [#16655] |
| 646 |
*/ |
| 647 |
boolean saveImportunate = false; |
| 648 |
if (txn != null) { |
| 649 |
saveImportunate = DbCompat.setImportunate(txn, true); |
| 650 |
} |
| 651 |
try { |
| 652 |
for (Map.Entry<String, String> entry : renameDbs.entrySet()) { |
| 653 |
String oldName = entry.getKey(); |
| 654 |
String newName = entry.getValue(); |
| 655 |
String[] oldFileAndDbNames = store.parseDbName(oldName); |
| 656 |
String[] newFileAndDbNames = store.parseDbName(newName); |
| 657 |
/* Do nothing if database does not exist. */ |
| 658 |
DbCompat.renameDatabase |
| 659 |
(store.getEnvironment(), txn, |
| 660 |
oldFileAndDbNames[0], oldFileAndDbNames[1], |
| 661 |
newFileAndDbNames[0], newFileAndDbNames[1]); |
| 662 |
} |
| 663 |
} finally { |
| 664 |
if (txn != null) { |
| 665 |
DbCompat.setImportunate(txn, saveImportunate); |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Evolves a primary key field or composite key field. |
| 672 |
*/ |
| 673 |
int evolveRequiredKeyField(Format oldParent, |
| 674 |
Format newParent, |
| 675 |
FieldInfo oldField, |
| 676 |
FieldInfo newField) { |
| 677 |
int result = EVOLVE_NONE; |
| 678 |
String oldName = oldField.getName(); |
| 679 |
final String FIELD_KIND = |
| 680 |
"primary key field or composite key class field"; |
| 681 |
final String FIELD_LABEL = |
| 682 |
FIELD_KIND + ": " + oldName; |
| 683 |
|
| 684 |
if (newField == null) { |
| 685 |
addMissingMutation |
| 686 |
(oldParent, newParent, |
| 687 |
"Field is missing and deletion is not allowed for a " + |
| 688 |
FIELD_LABEL); |
| 689 |
return EVOLVE_FAILURE; |
| 690 |
} |
| 691 |
|
| 692 |
/* Check field mutations. Only a Renamer is allowed. */ |
| 693 |
Deleter deleter = mutations.getDeleter |
| 694 |
(oldParent.getClassName(), oldParent.getVersion(), oldName); |
| 695 |
if (deleter != null) { |
| 696 |
addInvalidMutation |
| 697 |
(oldParent, newParent, deleter, |
| 698 |
"Deleter is not allowed for a " + FIELD_LABEL); |
| 699 |
return EVOLVE_FAILURE; |
| 700 |
} |
| 701 |
Converter converter = mutations.getConverter |
| 702 |
(oldParent.getClassName(), oldParent.getVersion(), oldName); |
| 703 |
if (converter != null) { |
| 704 |
addInvalidMutation |
| 705 |
(oldParent, newParent, converter, |
| 706 |
"Converter is not allowed for a " + FIELD_LABEL); |
| 707 |
return EVOLVE_FAILURE; |
| 708 |
} |
| 709 |
Renamer renamer = mutations.getRenamer |
| 710 |
(oldParent.getClassName(), oldParent.getVersion(), oldName); |
| 711 |
String newName = newField.getName(); |
| 712 |
if (renamer != null) { |
| 713 |
if (!renamer.getNewName().equals(newName)) { |
| 714 |
addInvalidMutation |
| 715 |
(oldParent, newParent, converter, |
| 716 |
"Converter is not allowed for a " + FIELD_LABEL); |
| 717 |
return EVOLVE_FAILURE; |
| 718 |
} |
| 719 |
result = EVOLVE_NEEDED; |
| 720 |
} else { |
| 721 |
if (!oldName.equals(newName)) { |
| 722 |
addMissingMutation |
| 723 |
(oldParent, newParent, |
| 724 |
"Renamer is required when field name is changed from: " + |
| 725 |
oldName + " to: " + newName); |
| 726 |
return EVOLVE_FAILURE; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
/* |
| 731 |
* Evolve the declared version of the field format. |
| 732 |
*/ |
| 733 |
Format oldFieldFormat = oldField.getType(); |
| 734 |
if (!evolveFormat(oldFieldFormat)) { |
| 735 |
return EVOLVE_FAILURE; |
| 736 |
} |
| 737 |
Format oldLatestFormat = oldFieldFormat.getLatestVersion(); |
| 738 |
if (oldFieldFormat != oldLatestFormat) { |
| 739 |
result = EVOLVE_NEEDED; |
| 740 |
} |
| 741 |
Format newFieldFormat = newField.getType(); |
| 742 |
|
| 743 |
if (oldLatestFormat.getClassName().equals |
| 744 |
(newFieldFormat.getClassName())) { |
| 745 |
/* Formats are identical. */ |
| 746 |
return result; |
| 747 |
} else if ((oldLatestFormat.getWrapperFormat() != null && |
| 748 |
oldLatestFormat.getWrapperFormat().getId() == |
| 749 |
newFieldFormat.getId()) || |
| 750 |
(newFieldFormat.getWrapperFormat() != null && |
| 751 |
newFieldFormat.getWrapperFormat().getId() == |
| 752 |
oldLatestFormat.getId())) { |
| 753 |
/* Primitive <-> primitive wrapper type change. */ |
| 754 |
return EVOLVE_NEEDED; |
| 755 |
} else { |
| 756 |
/* Type was changed incompatibly. */ |
| 757 |
addEvolveError |
| 758 |
(oldParent, newParent, |
| 759 |
"Type may not be changed for a " + FIELD_KIND, |
| 760 |
"Old field type: " + oldLatestFormat.getClassName() + |
| 761 |
" is not compatible with the new type: " + |
| 762 |
newFieldFormat.getClassName() + |
| 763 |
" for field: " + oldName); |
| 764 |
return EVOLVE_FAILURE; |
| 765 |
} |
| 766 |
} |
| 767 |
} |