[hibernate-commits] Hibernate SVN: r11478 - in trunk/HibernateExt/shards/src: java/org/hibernate/shards/query and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon May 7 20:58:54 EDT 2007


Author: buurzgoth
Date: 2007-05-07 20:58:54 -0400 (Mon, 07 May 2007)
New Revision: 11478

Added:
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/stat/
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/stat/ShardedSessionStatistics.java
Modified:
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/Shard.java
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardImpl.java
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/query/QueryResult.java
   trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
   trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardDefaultMock.java
   trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java
Log:
Implement a bunch of methods in ShardedSessionImpl

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/Shard.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/Shard.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/Shard.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -43,6 +43,7 @@
  * itself?
  *
  * @author Max Ross <maxr at google.com>
+ *         tomislav at google.com (Tomislav Nad)
  */
 public interface Shard {
 
@@ -114,6 +115,41 @@
   List<Object> list(CriteriaId criteriaId);
 
   /**
+   * @see Session#load(Class, Serializable)
+   */
+  Object load(Class<?> clazz, Serializable id);
+
+  /**
+   * @see Session#load(Class, Serializable, LockMode)
+   */
+  Object load(Class<?> clazz, Serializable id, LockMode lockMode);
+
+  /**
+   * @see Session#load(String, Serializable)
+   */
+  Object load(String entityName, Serializable id);
+
+  /**
+   * @see Session#load(String, Serializable, LockMode)
+   */
+  Object load(String entityName, Serializable id, LockMode lockMode);
+
+  /**
+   * @see Session#load(Object, Serializable)
+   */
+  void load(Object object, Serializable id);
+
+  /**
+   * @see Session#lock(Object, LockMode)
+   */
+  void lock(Object object, LockMode lockMode);
+
+  /**
+   * @see Session#lock(String, Object, LockMode)
+   */
+  void lock(String entityName, Object object, LockMode lockMode);
+
+  /**
    * @see Criteria#uniqueResult()
    */
   Object uniqueResult(CriteriaId criteriaId);
@@ -124,6 +160,21 @@
   Serializable save(String entityName, Object obj);
 
   /**
+   * @see Session#persist(String, Object)
+   */
+  void persist(String entityName, Object obj);
+
+  /**
+   * @see Session#refresh(Object)
+   */
+  void refresh(Object object);
+
+  /**
+   * @see Session#refresh(Object, LockMode)
+   */
+  void refresh(Object object, LockMode lockMode);
+
+  /**
    * @see Session#saveOrUpdate(Object)
    */
   void saveOrUpdate(Object obj);
@@ -154,6 +205,16 @@
   void delete(String entityName, Object object);
 
   /**
+   * @see Session#getCurrentLockMode(Object)
+   */
+  LockMode getCurrentLockMode(Object object);
+
+  /**
+   * @see Session#getEntityName(Object)
+   */
+  String getEntityName(Object object);
+
+  /**
    * @return the ids of the virtual shards that are mapped to this physical shard.
    * The returned Set is unmodifiable.
    */

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardImpl.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardImpl.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/ShardImpl.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -37,11 +37,7 @@
 import org.hibernate.shards.util.Sets;
 
 import java.io.Serializable;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Concrete implementation of the {@link Shard} interface.
@@ -265,6 +261,34 @@
     return establishSession().get(entityName, id, lockMode);
   }
 
+  public Object load(Class<?> clazz, Serializable id) {
+    return establishSession().load(clazz, id);
+  }
+
+  public Object load(Class<?> clazz, Serializable id, LockMode lockMode) {
+    return establishSession().load(clazz, id, lockMode);
+  }
+
+  public Object load(String entityName, Serializable id) {
+    return establishSession().load(entityName, id);
+  }
+
+  public Object load(String entityName, Serializable id, LockMode lockMode) {
+    return establishSession().load(entityName, id, lockMode);
+  }
+
+  public void load(Object object, Serializable id) {
+    establishSession().load(object, id);
+  }
+
+  public void lock(Object object, LockMode lockMode) {
+    establishSession().lock(object, lockMode);
+  }
+
+  public void lock(String entityName, Object object, LockMode lockMode) {
+    establishSession().lock(entityName, object, lockMode);
+  }
+
   @SuppressWarnings("unchecked")
   public List<Object> list(CriteriaId criteriaId) {
     return criteriaMap.get(criteriaId).list();
@@ -312,6 +336,18 @@
     return establishSession().save(entityName, obj);
   }
 
+  public void persist(String entityName, Object obj) {
+    establishSession().persist(entityName, obj);
+  }
+
+  public void refresh(Object object) {
+    establishSession().refresh(object);
+  }
+
+  public void refresh(Object object, LockMode lockMode) {
+    establishSession().refresh(object, lockMode);
+  }
+
   public void saveOrUpdate(Object obj) {
     establishSession().saveOrUpdate(obj);
   }
@@ -336,6 +372,14 @@
     establishSession().delete(entityName, object);
   }
 
+  public LockMode getCurrentLockMode(Object object) {
+    return establishSession().getCurrentLockMode(object);
+  }
+
+  public String getEntityName(Object object) {
+    return establishSession().getEntityName(object);
+  }
+
   @Override
   public String toString() {
     return getSessionFactoryImplementor().getSettings().getSessionFactoryName();

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/query/QueryResult.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/query/QueryResult.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/query/QueryResult.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -22,9 +22,9 @@
 import org.hibernate.shards.util.Lists;
 import org.hibernate.shards.util.Maps;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.Collections;
 
 /**
  * @author maulik at google.com (Maulik Shah)
@@ -33,7 +33,7 @@
 
   private final Map<Shard, List> resultMap = Maps.newHashMap();
 
-  private final List<?> entityList = Lists.newArrayList();
+  private final List entityList = Lists.newArrayList();
 
   public Map<Shard, List> getResultMap() {
     return Collections.unmodifiableMap(resultMap);

Modified: trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/session/ShardedSessionImpl.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -20,64 +20,33 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hibernate.CacheMode;
-import org.hibernate.Criteria;
-import org.hibernate.EntityMode;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Interceptor;
-import org.hibernate.LockMode;
-import org.hibernate.Query;
-import org.hibernate.ReplicationMode;
-import org.hibernate.SQLQuery;
-import org.hibernate.SessionException;
-import org.hibernate.Transaction;
-import org.hibernate.TransientObjectException;
+import org.hibernate.*;
 import org.hibernate.classic.Session;
 import org.hibernate.engine.SessionFactoryImplementor;
 import org.hibernate.id.IdentifierGenerator;
 import org.hibernate.metadata.ClassMetadata;
 import org.hibernate.proxy.HibernateProxy;
-import org.hibernate.shards.CrossShardAssociationException;
-import org.hibernate.shards.Shard;
-import org.hibernate.shards.ShardId;
-import org.hibernate.shards.ShardImpl;
-import org.hibernate.shards.ShardOperation;
-import org.hibernate.shards.ShardedTransaction;
+import org.hibernate.shards.*;
 import org.hibernate.shards.criteria.CriteriaFactoryImpl;
 import org.hibernate.shards.criteria.CriteriaId;
 import org.hibernate.shards.criteria.ShardedCriteriaImpl;
 import org.hibernate.shards.engine.ShardedSessionFactoryImplementor;
 import org.hibernate.shards.engine.ShardedSessionImplementor;
 import org.hibernate.shards.id.ShardEncodingIdentifierGenerator;
-import org.hibernate.shards.query.AdHocQueryFactoryImpl;
-import org.hibernate.shards.query.ExitOperationsQueryCollector;
-import org.hibernate.shards.query.NamedQueryFactoryImpl;
-import org.hibernate.shards.query.QueryId;
-import org.hibernate.shards.query.ShardedQueryImpl;
+import org.hibernate.shards.query.*;
+import org.hibernate.shards.stat.ShardedSessionStatistics;
 import org.hibernate.shards.strategy.ShardStrategy;
 import org.hibernate.shards.strategy.exit.FirstNonNullResultExitStrategy;
 import org.hibernate.shards.strategy.selection.ShardResolutionStrategyData;
 import org.hibernate.shards.strategy.selection.ShardResolutionStrategyDataImpl;
 import org.hibernate.shards.transaction.ShardedTransactionImpl;
-import org.hibernate.shards.util.Iterables;
-import org.hibernate.shards.util.Lists;
-import org.hibernate.shards.util.Maps;
-import org.hibernate.shards.util.Pair;
-import org.hibernate.shards.util.Preconditions;
-import org.hibernate.shards.util.Sets;
+import org.hibernate.shards.util.*;
 import org.hibernate.stat.SessionStatistics;
 import org.hibernate.type.Type;
 
 import java.io.Serializable;
 import java.sql.Connection;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 /**
  * Concrete implementation of a ShardedSession, and also the central component of
@@ -86,6 +55,7 @@
  * other components of Hibernate Shards. This class is not threadsafe.
  *
  * @author Max Ross <maxr at google.com>
+ *         tomislav at google.com (Tomislav Nad)
  */
 public class ShardedSessionImpl implements ShardedSession, ShardedSessionImplementor,
     ShardIdResolver {
@@ -404,7 +374,8 @@
   }
 
   /**
-   * Unsupported.  This is a scope decision, not a technical decision.
+   * We do not have sharded Connections as that would mean pushing sharding to
+   * connection-level.
    */
   public Connection connection() throws HibernateException {
     throw new UnsupportedOperationException();
@@ -521,43 +492,81 @@
     }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
-  public Object load(Class theClass, Serializable id, LockMode lockMode)
+  public Object load(Class clazz, Serializable id, LockMode lockMode)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                 ShardResolutionStrategyDataImpl(clazz, id));
+    if (shardIds.size() == 1) {
+      return shardIdsToShards.get(shardIds.get(0)).load(clazz, id, lockMode);
+    } else {
+      Object result = get(clazz, id, lockMode);
+      if (result == null) {
+        shardedSessionFactory.getEntityNotFoundDelegate().handleEntityNotFound(clazz.getName(), id);
+      }
+      return result;
+    }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public Object load(String entityName, Serializable id, LockMode lockMode)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                 ShardResolutionStrategyDataImpl(entityName, id));
+    if (shardIds.size() == 1) {
+      return shardIdsToShards.get(shardIds.get(0)).load(entityName, id, lockMode);
+    } else {
+      Object result = get(entityName, id, lockMode);
+      if (result == null) {
+        shardedSessionFactory.getEntityNotFoundDelegate().handleEntityNotFound(entityName, id);
+      }
+      return result;
+    }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
-  public Object load(Class theClass, Serializable id)
+  public Object load(Class clazz, Serializable id)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                 ShardResolutionStrategyDataImpl(clazz, id));
+    if (shardIds.size() == 1) {
+      return shardIdsToShards.get(shardIds.get(0)).load(clazz, id);
+    } else {
+      Object result = get(clazz, id);
+      if (result == null) {
+        shardedSessionFactory.getEntityNotFoundDelegate().handleEntityNotFound(clazz.getName(), id);
+      }
+      return result;
+    }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public Object load(String entityName, Serializable id)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                 ShardResolutionStrategyDataImpl(entityName, id));
+    if (shardIds.size() == 1) {
+      return shardIdsToShards.get(shardIds.get(0)).load(entityName, id);
+    } else {
+      Object result = get(entityName, id);
+      if (result == null) {
+        shardedSessionFactory.getEntityNotFoundDelegate().handleEntityNotFound(entityName, id);
+      }
+      return result;
+    }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public void load(Object object, Serializable id) throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                 ShardResolutionStrategyDataImpl(object.getClass(), id));
+    if (shardIds.size() == 1) {
+      shardIdsToShards.get(shardIds.get(0)).load(object, id);
+    } else {
+      Object result = get(object.getClass(), id);
+      if (result == null) {
+        shardedSessionFactory.getEntityNotFoundDelegate().handleEntityNotFound(object.getClass().getName(), id);
+      } else {
+        Shard objectShard = getShardForObject(result, shardIdListToShardList(shardIds));
+        evict(result);
+        objectShard.load(object, id);
+      }
+    }
   }
 
   /**
@@ -577,6 +586,7 @@
   }
 
   public Serializable save(String entityName, Object object) throws HibernateException {
+    // TODO(tomislav): what if we have detached instance?
     ShardId shardId = getShardIdForObject(object);
     if(shardId == null) {
       shardId = selectShardIdForNewObject(object);
@@ -863,19 +873,21 @@
     throw new UnsupportedOperationException();
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public void persist(Object object) throws HibernateException {
-    throw new UnsupportedOperationException();
+    persist(null, object);
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public void persist(String entityName, Object object)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    // TODO(tomislav): what if we have detached object?
+    ShardId shardId = getShardIdForObject(object);
+    if(shardId == null) {
+      shardId = selectShardIdForNewObject(object);
+    }
+    Preconditions.checkNotNull(shardId);
+    setCurrentSubgraphShardId(shardId);
+    log.debug(String.format("Persisting object of type %s to shard %s", object.getClass(), shardId));
+    shardIdsToShards.get(shardId).persist(entityName, object);
   }
 
   private interface DeleteOperation {
@@ -938,48 +950,97 @@
   }
 
   public void lock(final Object object, final LockMode lockMode) throws HibernateException {
-    SessionOperation<Void> op = new SessionOperation<Void>() {
-      public Void apply(Session session) {
-        session.lock(object, lockMode);
+    ShardOperation<Void> op = new ShardOperation<Void>() {
+      public Void execute(Shard s) {
+        s.lock(object, lockMode);
         return null;
       }
+      public String getOperationName() {
+        return "lock(Object object, LockMode lockMode)";
+      }
     };
-    invokeSessionOperationOnSessionWithObject(op, object);
+    invokeOnShardWithObject(op, object);
   }
 
   public void lock(final String entityName, final Object object, final LockMode lockMode)
       throws HibernateException {
-    SessionOperation<Void> op = new SessionOperation<Void>() {
-      public Void apply(Session session) {
-        session.lock(entityName, object, lockMode);
+    ShardOperation<Void> op = new ShardOperation<Void>() {
+      public Void execute(Shard s) {
+        s.lock(entityName, object, lockMode);
         return null;
       }
+      public String getOperationName() {
+        return "lock(String entityName, Object object, LockMode lockMode)";
+      }
     };
-    invokeSessionOperationOnSessionWithObject(op, object);
+    invokeOnShardWithObject(op, object);
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public void refresh(Object object) throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<Shard> candidateShards;
+    Serializable id = null;
+    try {
+      id = getIdentifier(object);
+      List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                   ShardResolutionStrategyDataImpl(object.getClass(), id));
+      candidateShards = shardIdListToShardList(shardIds);
+    } catch (TransientObjectException toe) {
+      // detached instance
+      candidateShards = shards;
+    }
+    if (candidateShards.size() == 1) {
+      candidateShards.get(0).refresh(object);
+    } else {
+      for (Shard shard : candidateShards) {
+        try {
+          shard.refresh(object);
+          return;
+        } catch (UnresolvableObjectException uoe) {
+          // ignore
+        }
+      }
+      throw new UnresolvableObjectException(id, object.getClass().getName());
+    }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public void refresh(Object object, LockMode lockMode)
       throws HibernateException {
-    throw new UnsupportedOperationException();
+    List<Shard> candidateShards;
+    Serializable id = null;
+    try {
+      id = getIdentifier(object);
+      List<ShardId> shardIds = selectShardIdsFromShardResolutionStrategyData(new
+                                   ShardResolutionStrategyDataImpl(object.getClass(), id));
+      candidateShards = shardIdListToShardList(shardIds);
+    } catch (TransientObjectException toe) {
+      // detached instance
+      candidateShards = shards;
+    }
+    if (candidateShards.size() == 1) {
+      candidateShards.get(0).refresh(object, lockMode);
+    } else {
+      for (Shard shard : candidateShards) {
+        try {
+          shard.refresh(object, lockMode);
+          return;
+        } catch (UnresolvableObjectException uoe) {
+          // ignore
+        }
+      }
+      throw new UnresolvableObjectException(id, object.getClass().getName());
+    }
   }
 
   public LockMode getCurrentLockMode(final Object object) throws HibernateException {
-    SessionOperation<LockMode> invoker = new SessionOperation<LockMode>() {
-      public LockMode apply(Session s) {
+    ShardOperation<LockMode> invoker = new ShardOperation<LockMode>() {
+      public LockMode execute(Shard s) {
         return s.getCurrentLockMode(object);
       }
+      public String getOperationName() {
+        return "getCurrentLockmode(Object object)";
+      }
     };
-    return invokeSessionOperationOnSessionWithObject(invoker, object);
+    return invokeOnShardWithObject(invoker, object);
   }
 
   public Transaction beginTransaction() throws HibernateException {
@@ -1068,12 +1129,15 @@
   }
 
   public String getEntityName(final Object object) throws HibernateException {
-    SessionOperation<String> invoker = new SessionOperation<String>() {
-      public String apply(Session s) {
+    ShardOperation<String> invoker = new ShardOperation<String>() {
+      public String execute(Shard s) {
         return s.getEntityName(object);
       }
+      public String getOperationName() {
+        return "getEntityName(Object object)";
+      }
     };
-    return invokeSessionOperationOnSessionWithObject(invoker, object);
+    return invokeOnShardWithObject(invoker, object);
   }
 
   public Filter enableFilter(String filterName) {
@@ -1116,11 +1180,8 @@
     }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public SessionStatistics getStatistics() {
-    throw new UnsupportedOperationException();
+    return new ShardedSessionStatistics(this);
   }
 
   public void setReadOnly(Object entity, boolean readOnly) {
@@ -1135,15 +1196,17 @@
     }
   }
 
-  /**
-   * Unsupported.  This is a scope decision, not a technical decision.
-   */
   public Connection disconnect() throws HibernateException {
-    throw new UnsupportedOperationException();
+    for (Shard s : getShards()) {
+      s.getSession().disconnect();
+    }
+    // we do not allow application-supplied connections, so we can always return
+    // null
+    return null;
   }
 
   /**
-   * Unsupported.  This is a scope decision, not a technical decision.
+   * @deprecated
    */
   public void reconnect() throws HibernateException {
     throw new UnsupportedOperationException();
@@ -1153,7 +1216,8 @@
    * Unsupported.  This is a technical decision.
    */
   public void reconnect(Connection connection) throws HibernateException {
-    throw new UnsupportedOperationException();
+    throw new UnsupportedOperationException(
+        "Cannot reconnect a sharded session");
   }
 
   /**
@@ -1442,29 +1506,24 @@
     currentSubgraphShardId.set(shardId);
   }
 
-  private interface SessionOperation<T> {
-    T apply(Session session);
-  }
-
   /**
-   * Helper method we can use when we need to find the Session with which a
-   * specified object is associated and invoke the method on that Session.
+   * Helper method we can use when we need to find the Shard with which a
+   * specified object is associated and invoke the method on that Shard.
    * If the object isn't associated with a Session we just invoke it on a
    * random Session with the expectation that this will cause an error.
    */
-  <T> T invokeSessionOperationOnSessionWithObject(SessionOperation<T> sessionOperation, Object object) throws HibernateException {
+  <T> T invokeOnShardWithObject(ShardOperation<T> so, Object object) throws HibernateException {
     ShardId shardId = getShardIdForObject(object);
-    Session sessionToUse;
+    Shard shardToUse;
     if (shardId == null) {
       // just ask this question of a random shard so we get the proper error
-      sessionToUse = getSomeSession();
-      if (sessionToUse == null) {
-        sessionToUse = shards.get(0).establishSession();
-      }
+      shardToUse = shards.get(0);
     } else {
-      sessionToUse = shardIdsToShards.get(shardId).establishSession();
+      shardToUse = shardIdsToShards.get(shardId);
     }
-    return sessionOperation.apply(sessionToUse);
+    return so.execute(shardToUse);
   }
 
+
+
 }

Added: trunk/HibernateExt/shards/src/java/org/hibernate/shards/stat/ShardedSessionStatistics.java
===================================================================
--- trunk/HibernateExt/shards/src/java/org/hibernate/shards/stat/ShardedSessionStatistics.java	                        (rev 0)
+++ trunk/HibernateExt/shards/src/java/org/hibernate/shards/stat/ShardedSessionStatistics.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -0,0 +1,77 @@
+/**
+ * Copyright (C) 2007 Google Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+package org.hibernate.shards.stat;
+
+import org.hibernate.shards.Shard;
+import org.hibernate.shards.engine.ShardedSessionImplementor;
+import org.hibernate.shards.util.Sets;
+import org.hibernate.stat.SessionStatistics;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Sharded implementation of the SessionStatistics that aggregates the
+ * statistics of all underlying individual SessionStatistics.
+ *
+ * @author tomislav at google.com (Tomislav Nad)
+ */
+public class ShardedSessionStatistics implements SessionStatistics {
+
+  private final Set<SessionStatistics> sessionStatistics;
+
+  public ShardedSessionStatistics(ShardedSessionImplementor session) {
+    sessionStatistics = Sets.newHashSet();
+    for (Shard s : session.getShards()) {
+      sessionStatistics.add(s.getSession().getStatistics());
+    }
+  }
+
+  public int getEntityCount() {
+    int count = 0;
+    for (SessionStatistics s : sessionStatistics) {
+      count += s.getEntityCount();
+    }
+    return count;
+  }
+
+  public int getCollectionCount() {
+    int count = 0;
+    for (SessionStatistics s : sessionStatistics) {
+      count += s.getCollectionCount();
+    }
+    return count;
+  }
+
+  public Set getEntityKeys() {
+    Set entityKeys = Sets.newHashSet();
+    for (SessionStatistics s : sessionStatistics) {
+      entityKeys.addAll(s.getEntityKeys());
+    }
+    return Collections.unmodifiableSet(entityKeys);
+  }
+
+  public Set getCollectionKeys() {
+    Set collectionKeys = Sets.newHashSet();
+    for (SessionStatistics s : sessionStatistics) {
+      collectionKeys.addAll(s.getCollectionKeys());
+    }
+    return Collections.unmodifiableSet(collectionKeys);
+  }
+}

Modified: trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardDefaultMock.java
===================================================================
--- trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardDefaultMock.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/test/org/hibernate/shards/ShardDefaultMock.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -37,6 +37,7 @@
 
 /**
  * @author Max Ross <maxr at google.com>
+ *         tomislav at google.com (Tomislav Nad)
  */
 public class ShardDefaultMock implements Shard {
 
@@ -84,6 +85,35 @@
     throw new UnsupportedOperationException();
   }
 
+
+  public Object load(Class<?> clazz, Serializable id) {
+    throw new UnsupportedOperationException();
+  }
+
+  public Object load(Class<?> clazz, Serializable id, LockMode lockMode) {
+    throw new UnsupportedOperationException();
+  }
+
+  public Object load(String entityName, Serializable id) {
+    throw new UnsupportedOperationException();
+  }
+
+  public Object load(String entityName, Serializable id, LockMode lockMode) {
+    throw new UnsupportedOperationException();
+  }
+
+  public void load(Object object, Serializable id) {
+    throw new UnsupportedOperationException();
+  }
+
+  public void lock(Object object, LockMode lockMode) {
+    throw new UnsupportedOperationException();
+  }
+
+  public void lock(String entityName, Object object, LockMode lockMode) {
+    throw new UnsupportedOperationException();
+  }
+
   public List<Object> list(CriteriaId criteriaId) {
     throw new UnsupportedOperationException();
   }
@@ -100,6 +130,18 @@
     throw new UnsupportedOperationException();
   }
 
+  public void persist(String entityName, Object obj) {
+    throw new UnsupportedOperationException();
+  }
+
+  public void refresh(Object object) {
+    throw new UnsupportedOperationException();
+  }
+
+  public void refresh(Object object, LockMode lockMode) {
+    throw new UnsupportedOperationException();
+  }
+
   public void saveOrUpdate(Object obj) {
     throw new UnsupportedOperationException();
   }
@@ -124,6 +166,14 @@
     throw new UnsupportedOperationException();
   }
 
+  public LockMode getCurrentLockMode(Object object) {
+    throw new UnsupportedOperationException();
+  }
+
+  public String getEntityName(Object object) {
+    throw new UnsupportedOperationException();
+  }
+
   public Set<ShardId> getShardIds() {
     throw new UnsupportedOperationException();
   }

Modified: trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java
===================================================================
--- trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java	2007-05-08 00:51:04 UTC (rev 11477)
+++ trunk/HibernateExt/shards/src/test/org/hibernate/shards/integration/model/ModelPermutedIntegrationTest.java	2007-05-08 00:58:54 UTC (rev 11478)
@@ -28,21 +28,8 @@
 import org.hibernate.shards.ShardId;
 import org.hibernate.shards.integration.BaseShardingIntegrationTestCase;
 import org.hibernate.shards.integration.MemoryLeakPlugger;
-import static org.hibernate.shards.integration.model.ModelDataFactory.building;
-import static org.hibernate.shards.integration.model.ModelDataFactory.elevator;
-import static org.hibernate.shards.integration.model.ModelDataFactory.escalator;
-import static org.hibernate.shards.integration.model.ModelDataFactory.floor;
-import static org.hibernate.shards.integration.model.ModelDataFactory.office;
-import static org.hibernate.shards.integration.model.ModelDataFactory.person;
-import static org.hibernate.shards.integration.model.ModelDataFactory.tenant;
-import static org.hibernate.shards.integration.model.ModelDataFactory.window;
-import org.hibernate.shards.model.Building;
-import org.hibernate.shards.model.Escalator;
-import org.hibernate.shards.model.Floor;
-import org.hibernate.shards.model.Office;
-import org.hibernate.shards.model.Person;
-import org.hibernate.shards.model.Tenant;
-import org.hibernate.shards.model.Window;
+import static org.hibernate.shards.integration.model.ModelDataFactory.*;
+import org.hibernate.shards.model.*;
 import org.hibernate.shards.session.ShardedSessionFactory;
 import org.hibernate.shards.session.ShardedSessionImpl;
 import org.hibernate.shards.session.SubsetShardedSessionFactoryImpl;
@@ -182,7 +169,6 @@
     floor(b, 23);
     session.saveOrUpdate(b);
     commitAndResetSession();
-    session.beginTransaction();
     b = reload(b);
     b.setName("b2");
     session.saveOrUpdate(b);
@@ -878,6 +864,68 @@
     assertEquals("a different name", b.getName());
   }
 
+  public void testLoad() {
+    session.beginTransaction();
+    Building b = building("b1");
+    session.save(b);
+    commitAndResetSession();
+    Building loadedB = (Building) session.load(b.getClass(), b.getBuildingId());
+    assertNotNull(loadedB);
+    assertEquals("b1", loadedB.getName());
+  }
+
+  public void testLoadIntoObject() {
+    session.beginTransaction();
+    Building b = building("b1");
+    session.save(b);
+    commitAndResetSession();
+    Building loadedB = new Building();
+    session.load(loadedB, b.getBuildingId());
+    assertNotNull(loadedB);
+    assertEquals("b1", loadedB.getName());
+  }
+
+   public void testLoadNonexisting() {
+     session.beginTransaction();
+     Building b = building("b1");
+     session.save(b);
+     commitAndResetSession();
+     b = reload(b);
+     Serializable id = b.getBuildingId();
+     session.delete(b);
+     commitAndResetSession();
+     try {
+       Building loadedB = (Building)session.load(Building.class, id);
+       loadedB.getName();
+       fail();
+     } catch (HibernateException he) {
+       // good
+     }
+   }
+
+  public void testPersist() {
+    session.beginTransaction();
+    Building b = building("b1");
+    session.persist(b);
+    commitAndResetSession();
+    Building returnedB = (Building) session.get(b.getClass(), b.getBuildingId());
+    assertNotNull(returnedB);
+    assertEquals("b1", returnedB.getName());
+  }
+
+  public void testRefresh() {
+    session.beginTransaction();
+    Building b = building("b1");
+    session.save(b);
+    commitAndResetSession();
+    Building reloadedB = reload(b);
+    reloadedB.setName("b2");
+    commitAndResetSession();
+    assertEquals("b1", b.getName());
+    session.refresh(b);
+    assertEquals("b2", b.getName());
+  }
+
   // calling update on a nonexistent entity should result in an exception
   public void testUpdateOfNonexistentEntity() {
     session.beginTransaction();




More information about the hibernate-commits mailing list