Joe Hopkins commented on Bug HSHARDS-64

With formatting and new code section commented...

Original code section that incorrectly throws exception if id is not ShardEncodingIdentifierGenerator.

IdentifierGenerator idGenerator = shard.getSessionFactoryImplementor().getIdentifierGenerator(className);
if (idGenerator instanceof ShardEncodingIdentifierGenerator) { 
  return ((ShardEncodingIdentifierGenerator)idGenerator).extractShardId(getIdentifier(obj)); 
} else {
  // TODO(tomislav): also use shard resolution strategy if it returns only 1 shard; throw this error in config instead of here  
  throw new HibernateException("Can not use virtual sharding with non-shard resolving id gen"); 
}

Updated complete method with added code to handle user generated id types.

public ShardId getShardIdForObject(Object obj, List<Shard> shardsToConsider) {
  // TODO(maxr) optimize this by keeping an identity map of objects to shardId
  Shard shard = getShardForObject(obj, shardsToConsider);
  if(shard == null) { 
    return null; 
  } else if (shard.getShardIds().size() == 1) { 
    return shard.getShardIds().iterator().next(); 
  } else {
    String className;
    if (obj instanceof HibernateProxy) { 
     className = ((HibernateProxy)obj).getHibernateLazyInitializer().getPersistentClass().getName();   
    }     
    else   { 
      className = obj.getClass().getName(); 
    }
    IdentifierGenerator idGenerator = shard.getSessionFactoryImplementor().getIdentifierGenerator(className);
    if (idGenerator instanceof ShardEncodingIdentifierGenerator) { 
      return ((ShardEncodingIdentifierGenerator)idGenerator).extractShardId(getIdentifier(obj)); 
    } else {
      // ======== New ===========
      List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
        ShardResolutionStrategyDataImpl(obj.getClass(), getIdentifier(obj)));
      if (shardIds!=null && shardIds.size() > 0) { 
        return shardIds.get(0); 
      }
      // ========================
      throw new HibernateException("Can not resolve shard id using virtual sharding");
    }
  }
}
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira