From ad8bd5c3b55adc4c5052e850ad407cc04a70d246 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 21 Oct 2007 16:14:05 +0000 Subject: Assign number to properties. --- .../amazon/carbonado/gen/StorableGenerator.java | 68 ++++++---------------- 1 file changed, 19 insertions(+), 49 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/gen') diff --git a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java index e28f07c..9db49ad 100644 --- a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java @@ -113,7 +113,7 @@ public final class StorableGenerator { /** * Name prefix of protected method in generated storable that returns false * if a specific alternate key is uninitialized. The complete name is - * formed by the prefix appended with the zero-based alternate key ordinal. + * formed by the prefix appended with the zero-based alternate key number. */ public static final String IS_ALT_KEY_INITIALIZED_PREFIX = "isAltKeyInitialized$"; @@ -146,10 +146,10 @@ public final class StorableGenerator { */ public static final String PROPERTY_STATE_FIELD_NAME = "propertyState$"; - /** Adapter field names are propertyName + "$adapter$" + ordinal */ + /** Adapter field names are propertyName + "$adapter$" + number */ public static final String ADAPTER_FIELD_ELEMENT = "$adapter$"; - /** Constraint field names are propertyName + "$constraint$" + ordinal */ + /** Constraint field names are propertyName + "$constraint$" + number */ public static final String CONSTRAINT_FIELD_ELEMENT = "$constraint$"; /** Reference to TriggerSupport instance */ @@ -278,8 +278,8 @@ public final class StorableGenerator { * * Property state field names are defined by the concatenation of * {@code PROPERTY_STATE_FIELD_NAME} and a zero-based decimal - * ordinal. To determine which field holds a particular property's state, - * the field ordinal is computed as the property ordinal divided by 16. The + * number. To determine which field holds a particular property's state, + * the field number is computed as the property number divided by 16. The * specific two-bit state position is the remainder of this division times 2. * * @throws com.amazon.carbonado.MalformedTypeException if Storable type is not well-formed @@ -451,12 +451,11 @@ public final class StorableGenerator { // Also remember ordinal of optional version property for use later. int versionOrdinal = -1; { - int ordinal = -1; int maxOrdinal = mAllProperties.size() - 1; boolean requireStateField = false; for (StorableProperty property : mAllProperties.values()) { - ordinal++; + int ordinal = property.getNumber(); if (!property.isDerived() && property.isVersion()) { versionOrdinal = ordinal; @@ -853,7 +852,7 @@ public final class StorableGenerator { Label setInternalProp = b.createLabel(); // Access state of internal property directly. - int ord = findPropertyOrdinal(internal); + int ord = internal.getNumber(); b.loadThis(); b.loadField(PROPERTY_STATE_FIELD_NAME + (ord >> 4), TypeDesc.INT); b.loadConstant(PROPERTY_STATE_MASK << ((ord & 0xf) * 2)); @@ -1792,7 +1791,6 @@ public final class StorableGenerator { LocalVariable target = CodeBuilderUtil.uneraseGenericParameter(b, storableTypeDesc, 0); LocalVariable stateBits = null; - int ordinal = 0; int mask = PROPERTY_STATE_DIRTY; for (StorableProperty property : mAllProperties.values()) { @@ -1803,6 +1801,8 @@ public final class StorableGenerator { !property.isPrimaryKeyMember() && dataProperties); if (shouldCopy) { + int ordinal = property.getNumber(); + if (stateBits == null) { // Load state bits into local for quick retrieval. stateBits = b.createLocalVariable(null, TypeDesc.INT); @@ -1855,7 +1855,6 @@ public final class StorableGenerator { skipCopy.setLocation(); } - ordinal++; if ((mask <<= 2) == 0) { mask = 3; stateBits = null; @@ -2288,7 +2287,7 @@ public final class StorableGenerator { } if (properties.size() == 1) { - int ordinal = findPropertyOrdinal(properties.values().iterator().next()); + int ordinal = properties.values().iterator().next().getNumber(); b.loadThis(); b.loadField(PROPERTY_STATE_FIELD_NAME + (ordinal >> 4), TypeDesc.INT); b.loadConstant(PROPERTY_STATE_MASK << ((ordinal & 0xf) * 2)); @@ -2349,18 +2348,6 @@ public final class StorableGenerator { b.returnValue(TypeDesc.BOOLEAN); } - private int findPropertyOrdinal(StorableProperty property) { - int ordinal = 0; - for (StorableProperty p : mAllProperties.values()) { - if (p == property) { - return ordinal; - } - ordinal++; - } - throw new IllegalArgumentException - ("Unable to find property " + property + " in " + mAllProperties); - } - /** * Generates code that verifies that all primary keys are initialized. * @@ -2443,15 +2430,6 @@ public final class StorableGenerator { b.switchBranch(cases, switchLabels, noMatch); - // Gather property ordinals. - Map, Integer> ordinalMap = new HashMap, Integer>(); - { - int ordinal = 0; - for (StorableProperty prop : mAllProperties.values()) { - ordinalMap.put(prop, ordinal++); - } - } - // Params to invoke String.equals. TypeDesc[] params = {TypeDesc.OBJECT}; @@ -2502,8 +2480,7 @@ public final class StorableGenerator { } b.branch(joinMatch); } else { - int ordinal = ordinalMap.get(prop); - + int ordinal = prop.getNumber(); b.loadThis(); b.loadField(PROPERTY_STATE_FIELD_NAME + (ordinal >> 4), TypeDesc.INT); int shift = (ordinal & 0xf) * 2; @@ -2964,21 +2941,14 @@ public final class StorableGenerator { b.loadConstant(detail); invokeAppend(b, TypeDesc.STRING); - // First pass, just print primary keys. Also gather ordinals. + // First pass, just print primary keys. - Map ordinals = new IdentityHashMap(); - int ordinal = 0; - LocalVariable commaCountVar = b.createLocalVariable(null, TypeDesc.INT); b.loadConstant(-1); b.storeLocal(commaCountVar); - for (StorableProperty property : mAllProperties.values()) { - ordinals.put(property, ordinal); - if (property.isPrimaryKeyMember()) { - addPropertyAppendCall(b, property, commaCountVar, ordinal); - } - ordinal++; + for (StorableProperty property : mInfo.getPrimaryKeyProperties().values()) { + addPropertyAppendCall(b, property, commaCountVar); } // Second pass, print non-primary keys. @@ -2989,7 +2959,7 @@ public final class StorableGenerator { if (!property.isPrimaryKeyMember() && (!property.isDerived()) && (!property.isJoin())) { - addPropertyAppendCall(b, property, commaCountVar, ordinals.get(property)); + addPropertyAppendCall(b, property, commaCountVar); } } } @@ -3010,10 +2980,9 @@ public final class StorableGenerator { StorableKey key = mInfo.getAlternateKey(i); - ordinal = 0; for (OrderedProperty op : key.getProperties()) { StorableProperty property = op.getChainedProperty().getPrimeProperty(); - addPropertyAppendCall(b, property, commaCountVar, ordinals.get(property)); + addPropertyAppendCall(b, property, commaCountVar); } b.loadConstant('}'); @@ -3032,8 +3001,7 @@ public final class StorableGenerator { private void addPropertyAppendCall(CodeBuilder b, StorableProperty property, - LocalVariable commaCountVar, - int ordinal) + LocalVariable commaCountVar) { Label skipPrint = b.createLabel(); @@ -3042,6 +3010,8 @@ public final class StorableGenerator { addSkipIndependent(b, null, property, skipPrint); } + int ordinal = property.getNumber(); + // Check if property is initialized, and skip if not. b.loadThis(); b.loadField(PROPERTY_STATE_FIELD_NAME + (ordinal >> 4), TypeDesc.INT); -- cgit v1.2.3