From 35783466fcf6f964be62bf65eed06ab2b647d7ba Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 3 Oct 2011 21:56:28 +0000 Subject: Automatically update layout generation for deserialized layouts. --- .../com/amazon/carbonado/layout/LayoutFactory.java | 58 ++++++++++++---------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java index 67d4348..71d1ec5 100644 --- a/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java +++ b/src/main/java/com/amazon/carbonado/layout/LayoutFactory.java @@ -269,27 +269,12 @@ public class LayoutFactory implements LayoutCapability { // exception, which prevents the mistake from persisting. assert(newLayout != null); - int generation = 0; - - Cursor cursor = mLayoutStorage - .query("storableTypeName = ?") - .with(info.getStorableType().getName()) - .orderBy("-generation") - .fetch(); - - boolean newGen; - try { - if (newGen = cursor.hasNext()) { - generation = cursor.next().getGeneration() + 1; - } - } finally { - cursor.close(); - } + int generation = nextGeneration(info.getStorableType().getName()); newLayout.insert(readOnly, generation); layout = newLayout; - if (newGen) { + if (generation == 0) { LogFactory.getLog(getClass()) .debug("New schema layout inserted: " + layout); } @@ -368,6 +353,7 @@ public class LayoutFactory implements LayoutCapability { Transaction txn = mRepository.enterTransaction(); try { txn.setForUpdate(true); + StoredLayout storedLayout = mLayoutStorage.prepare(); storedLayout.readFrom(in); try { @@ -375,17 +361,20 @@ public class LayoutFactory implements LayoutCapability { } catch (UniqueConstraintException e) { StoredLayout existing = mLayoutStorage.prepare(); storedLayout.copyPrimaryKeyProperties(existing); - if (!existing.tryLoad()) { - throw e; - } - // Only check subset of primary and alternate keys. The check - // of layout properties is more important. - if (!(existing.getLayoutID() == storedLayout.getLayoutID() && - existing.getStorableTypeName().equals(storedLayout.getStorableTypeName()))) - { - throw e; + if (existing.tryLoad()) { + // Only check subset of primary and alternate keys. The check + // of layout properties is more important. + if (!existing.getStorableTypeName().equals(storedLayout.getStorableTypeName())) + { + throw e; + } + storedLayout = existing; + } else { + // Assume alternate key constraint, so increment the generation. + storedLayout.setGeneration + (nextGeneration(storedLayout.getStorableTypeName())); + storedLayout.insert(); } - storedLayout = existing; } int op; @@ -424,6 +413,21 @@ public class LayoutFactory implements LayoutCapability { mReconstructed.put(reconstructed, layout); } + private int nextGeneration(String typeName) throws FetchException { + Cursor cursor = mLayoutStorage + .query("storableTypeName = ?").with(typeName).orderBy("-generation").fetch(); + + try { + if (cursor.hasNext()) { + return cursor.next().getGeneration() + 1; + } + } finally { + cursor.close(); + } + + return 0; + } + /** * Creates a long hash code that attempts to mix in all relevant layout * elements. -- cgit v1.2.3