From dc375a085d7689ba541d573181d31816c5d1dd9e Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Thu, 11 Mar 2010 01:24:57 +0000 Subject: Support partition key when loading by alternate key. --- .../amazon/carbonado/gen/CommonMethodNames.java | 1 + .../amazon/carbonado/gen/StorableGenerator.java | 74 ++++++++++++++++------ 2 files changed, 57 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main/java/com/amazon/carbonado/gen/CommonMethodNames.java b/src/main/java/com/amazon/carbonado/gen/CommonMethodNames.java index ba20e31..5356fba 100644 --- a/src/main/java/com/amazon/carbonado/gen/CommonMethodNames.java +++ b/src/main/java/com/amazon/carbonado/gen/CommonMethodNames.java @@ -73,6 +73,7 @@ public class CommonMethodNames { public static final String LOAD_ONE_METHOD_NAME = "loadOne", TRY_LOAD_ONE_METHOD_NAME = "tryLoadOne", + AND_METHOD_NAME = "and", WITH_METHOD_NAME = "with", FETCH_METHOD_NAME = "fetch"; diff --git a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java index 0aeb541..6fd19fc 100644 --- a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java @@ -1021,12 +1021,15 @@ public final class StorableGenerator { CodeBuilder b = new CodeBuilder(mi); - // Add empty method that will be overrriden if there is a partition key to check that it is initialized + // Add empty method that will be overrriden if there is a partition + // key to check that it is initialized b.loadThis(); - b.invokeVirtual(CHECK_PK_FOR_LOAD_METHOD_NAME, null, null); - CodeBuilder b1 = new CodeBuilder(mClassFile.addMethod(Modifiers.PROTECTED, CHECK_PK_FOR_LOAD_METHOD_NAME, null, null)); - b1.loadThis(); - b1.returnVoid(); + b.invokeVirtual(CHECK_PK_FOR_LOAD_METHOD_NAME, null, null); + CodeBuilder b1 = new CodeBuilder + (mClassFile.addMethod + (Modifiers.PROTECTED, CHECK_PK_FOR_LOAD_METHOD_NAME, null, null)); + b1.loadThis(); + b1.returnVoid(); // Check that primary key is initialized. b.loadThis(); @@ -1083,6 +1086,41 @@ public final class StorableGenerator { new TypeDesc[]{bindType}); } + StorableKey parKey = mInfo.getPartitionKey(); + if (parKey != null) { + // Transfer set partition key properties to query. + for (OrderedProperty op : parKey.getProperties()) { + if (altKey.getProperties().contains(op)) { + // Was already supplied to query. + continue; + } + + StorableProperty prop = op.getChainedProperty().getPrimeProperty(); + + Label skip = b.createLabel(); + + if (!prop.isDerived()) { + int num = prop.getNumber(); + b.loadThis(); + b.loadField(PROPERTY_STATE_FIELD_NAME + (num >> 4), TypeDesc.INT); + b.loadConstant(PROPERTY_STATE_MASK << ((num & 0xf) * 2)); + b.math(Opcode.IAND); + b.ifZeroComparisonBranch(skip, "=="); + } + + b.loadConstant(prop.getName() + " = ?"); + b.invokeInterface(queryType, AND_METHOD_NAME, queryType, + new TypeDesc[]{TypeDesc.STRING}); + loadThisProperty(b, prop); + TypeDesc bindType = CodeBuilderUtil.bindQueryParam(prop.getType()); + CodeBuilderUtil.convertValue(b, prop.getType(), bindType.toClass()); + b.invokeInterface(queryType, WITH_METHOD_NAME, queryType, + new TypeDesc[]{bindType}); + + skip.setLocation(); + } + } + b.branch(runQuery); noAltKey.setLocation(); @@ -1714,19 +1752,19 @@ public final class StorableGenerator { addIsInitializedMethod (IS_PK_INITIALIZED_METHOD_NAME, mInfo.getPrimaryKeyProperties()); - { - // Define protected isPartitionKeyInitialized method - // It will return true if there are no Partition keys - final Map> partitionProperties = new LinkedHashMap>(); - for (StorableProperty property : mAllProperties.values()) { - if (!property.isDerived() && property.isPartitionKeyMember()) { - partitionProperties.put(property.getName(), property); - } - } - - // Add methods to check that the partition key is defined - addIsInitializedMethod(IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, partitionProperties); - } + { + // Define protected isPartitionKeyInitialized method + // It will return true if there are no Partition keys + final Map> partitionProperties = new LinkedHashMap>(); + for (StorableProperty property : mAllProperties.values()) { + if (!property.isDerived() && property.isPartitionKeyMember()) { + partitionProperties.put(property.getName(), property); + } + } + + // Add methods to check that the partition key is defined + addIsInitializedMethod(IS_PARTITION_KEY_INITIALIZED_METHOD_NAME, partitionProperties); + } // Define protected methods to check if alternate key is initialized. addAltKeyMethods: -- cgit v1.2.3