if (pHashKeyName == null) {
throw new IllegalArgumentException("The name of the primary hash key must be specified.");
}
if (lsiNameToLsiDefinition.containsKey(lsiName)) {
LocalSecondaryIndex existingLsi = lsiNameToLsiDefinition.get(lsiName);
if ( !lsiName.equals(existingLsi.getIndexName())
|| existingLsi.getKeySchema() == null
|| existingLsi.getKeySchema().size() != 2 // the hash key element should be already added
|| !KeyType.RANGE.toString().equals(existingLsi.getKeySchema().get(1).getKeyType()) ) {
throw new IllegalStateException("Found invalid state of an existing LocalSecondaryIndex object " +
"associated with the LSI [" + lsiName + "].");
}
String existingLsiRangeKeyName = existingLsi.getKeySchema().get(1).getAttributeName();
if ( !existingLsiRangeKeyName.equals(lsiRangeKeyName) ) {
throw new DynamoDBMappingException("Multiple range keys [" + existingLsiRangeKeyName + ", " + lsiRangeKeyName +
"] are found for the LSI [" + lsiName + "]. " +
"Each index allows at most one range key attribute.");
}
} else {
lsiNameToLsiDefinition.put(
lsiName,
new LocalSecondaryIndex()
.withIndexName(lsiName)
.withKeySchema(
new KeySchemaElement(pHashKeyName, KeyType.HASH),
new KeySchemaElement(lsiRangeKeyName, KeyType.RANGE))
.withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)));