From 06d5dbc5313f2c1686cff1664460f1eced7510f2 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 9 May 2008 06:03:46 +0000 Subject: JDBC repository now correctly works in non-master mode. --- .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 4 +- .../carbonado/repo/jdbc/JDBCStorableGenerator.java | 62 ++++++++++++++-------- .../amazon/carbonado/repo/jdbc/JDBCStorage.java | 4 +- 3 files changed, 45 insertions(+), 25 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo/jdbc') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java index 6849ca8..cd539f6 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java @@ -164,7 +164,7 @@ class JDBCRepository extends AbstractRepository private final Log mLog = LogFactory.getLog(getClass()); - final boolean mIsMaster; + private final boolean mIsMaster; final Iterable mTriggerFactories; private final AtomicReference mRootRef; private final String mDatabaseProductName; @@ -699,7 +699,7 @@ class JDBCRepository extends AbstractRepository } } - return new JDBCStorage(this, info, autoVersioning, suppressReload); + return new JDBCStorage(this, info, mIsMaster, autoVersioning, suppressReload); } @Override diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java index 993b95c..8e6e468 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorableGenerator.java @@ -90,18 +90,21 @@ class JDBCStorableGenerator { } static Class getGeneratedClass(JDBCStorableInfo info, + boolean isMaster, boolean autoVersioning, boolean suppressReload) throws SupportException { - Object key = KeyFactory.createKey(new Object[] {info, autoVersioning, suppressReload}); + Object key = KeyFactory.createKey(new Object[] { + info, isMaster, autoVersioning, suppressReload}); synchronized (cCache) { Class generatedClass = (Class) cCache.get(key); if (generatedClass != null) { return generatedClass; } - generatedClass = new JDBCStorableGenerator(info, autoVersioning, suppressReload) + generatedClass = new JDBCStorableGenerator + (info, isMaster, autoVersioning, suppressReload) .generateAndInjectClass(); cCache.put(key, generatedClass); return generatedClass; @@ -110,7 +113,13 @@ class JDBCStorableGenerator { private final Class mStorableType; private final JDBCStorableInfo mInfo; - private final boolean mAutoVersioning; + + private static enum Versioning { + NONE, EXTERNAL, AUTO + } + + private final Versioning mVersioning; + private final boolean mSuppressReload; private final Map> mAllProperties; @@ -119,22 +128,31 @@ class JDBCStorableGenerator { private final ClassFile mClassFile; private JDBCStorableGenerator(JDBCStorableInfo info, - boolean autoVersioning, boolean suppressReload) + boolean isMaster, boolean autoVersioning, boolean suppressReload) throws SupportException { mStorableType = info.getStorableType(); mInfo = info; - mAutoVersioning = autoVersioning; mAllProperties = mInfo.getAllProperties(); EnumSet features = EnumSet - .of(MasterFeature.INSERT_SEQUENCES, - MasterFeature.INSERT_CHECK_REQUIRED, // Must use @Automatic to override. - MasterFeature.INSERT_TXN, // Required because of reload after insert. + .of(MasterFeature.INSERT_TXN, // Required because of reload after insert. MasterFeature.UPDATE_TXN); // Required because of reload after update. - if (info.getVersionProperty() != null && info.getVersionProperty().isDerived()) { - features.add(MasterFeature.VERSIONING); + if (!isMaster) { + mVersioning = Versioning.NONE; + } else { + features.add(MasterFeature.INSERT_SEQUENCES); + // Must use @Automatic to override. + features.add(MasterFeature.INSERT_CHECK_REQUIRED); + + if (info.getVersionProperty() != null && info.getVersionProperty().isDerived()) { + features.add(MasterFeature.VERSIONING); + // Say none because master storable takes care of it. + mVersioning = Versioning.NONE; + } else { + mVersioning = autoVersioning ? Versioning.AUTO : Versioning.EXTERNAL; + } } if (suppressReload) { @@ -155,7 +173,7 @@ class JDBCStorableGenerator { suppressReload = false; break honorSuppression; } - if (prop.isVersion() && !mAutoVersioning) { + if (prop.isVersion() && mVersioning == Versioning.EXTERNAL) { // Always need to reload for version. suppressReload = false; break honorSuppression; @@ -706,7 +724,7 @@ class JDBCStorableGenerator { } Label setNormally = b.createLabel(); - if (property.isVersion() && mAutoVersioning) { + if (property.isVersion() && mVersioning == Versioning.AUTO) { // Automatically supply initial value unless manually supplied. branchIfDirty(b, propNumber, setNormally, true); setPreparedStatementValue @@ -835,14 +853,14 @@ class JDBCStorableGenerator { propNumber++; if (property.isSelectable() && !property.isPrimaryKeyMember()) { - if (property.isVersion() && !mAutoVersioning) { + if (property.isVersion() && mVersioning == Versioning.EXTERNAL) { // Assume database trigger manages version. continue; } Label isNotDirty = null; - if (!property.isVersion()) { - // Version must always be updated, but all other + if (!property.isVersion() || mVersioning != Versioning.AUTO) { + // Auto version must always be updated, but all other // properties are updated only if dirty. isNotDirty = b.createLabel(); branchIfDirty(b, propNumber, isNotDirty, false); @@ -873,7 +891,7 @@ class JDBCStorableGenerator { JDBCStorableProperty versionProperty = mInfo.getVersionProperty(); if (versionProperty != null) { - if (!versionProperty.isSelectable()) { + if (!versionProperty.isSelectable() || mVersioning == Versioning.NONE) { versionProperty = null; } else { // Include version property in WHERE clause to support optimistic locking. @@ -990,14 +1008,14 @@ class JDBCStorableGenerator { propNumber++; if (property.isSelectable() && !property.isPrimaryKeyMember()) { - if (property.isVersion() && !mAutoVersioning) { + if (property.isVersion() && mVersioning == Versioning.EXTERNAL) { // Assume database trigger manages version. continue; } Label isNotDirty = null; - if (!property.isVersion()) { - // Version must always be updated, but all other + if (!property.isVersion() || mVersioning != Versioning.AUTO) { + // Auto version must always be updated, but all other // properties are updated only if dirty. isNotDirty = b.createLabel(); branchIfDirty(b, propNumber, isNotDirty, false); @@ -1005,8 +1023,10 @@ class JDBCStorableGenerator { b.loadLocal(psVar); b.loadLocal(indexVar); + int mode = (property.isVersion() && mVersioning == Versioning.AUTO) ? + INCREMENT_VERSION : NORMAL; setPreparedStatementValue - (b, property, property.isVersion() ? INCREMENT_VERSION : NORMAL, + (b, property, mode, null, lobArrayVar, lobIndexMap.get(property)); b.integerIncrement(indexVar, 1); @@ -1196,7 +1216,7 @@ class JDBCStorableGenerator { * Returns true if property value is always part of insert statement. */ private boolean isAlwaysInserted(JDBCStorableProperty property) { - return property.isVersion() ? mAutoVersioning : !property.isAutomatic(); + return property.isVersion() ? (mVersioning == Versioning.AUTO) : !property.isAutomatic(); } /** diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java index dd81f90..cc9bef9 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCStorage.java @@ -89,7 +89,7 @@ class JDBCStorage extends StandardQueryFactory final TriggerManager mTriggerManager; JDBCStorage(JDBCRepository repository, JDBCStorableInfo info, - boolean autoVersioning, boolean suppressReload) + boolean isMaster, boolean autoVersioning, boolean suppressReload) throws SupportException, RepositoryException { super(info.getStorableType()); @@ -98,7 +98,7 @@ class JDBCStorage extends StandardQueryFactory mInfo = info; Class generatedStorableClass = JDBCStorableGenerator - .getGeneratedClass(info, autoVersioning, suppressReload); + .getGeneratedClass(info, isMaster, autoVersioning, suppressReload); mInstanceFactory = QuickConstructorGenerator .getInstance(generatedStorableClass, InstanceFactory.class); -- cgit v1.2.3