38
38
import java .util .Map ;
39
39
import java .util .Map .Entry ;
40
40
41
- import org .jboss .logging .Logger ;
42
-
43
41
import org .hibernate .AssertionFailure ;
44
42
import org .hibernate .Hibernate ;
45
43
import org .hibernate .HibernateException ;
52
50
import org .hibernate .cache .spi .NaturalIdCacheKey ;
53
51
import org .hibernate .cache .spi .access .NaturalIdRegionAccessStrategy ;
54
52
import org .hibernate .cache .spi .access .SoftLock ;
53
+ import org .hibernate .cfg .AvailableSettings ;
55
54
import org .hibernate .collection .spi .PersistentCollection ;
56
55
import org .hibernate .engine .loading .internal .LoadContexts ;
57
56
import org .hibernate .engine .spi .AssociationKey ;
76
75
import org .hibernate .pretty .MessageHelper ;
77
76
import org .hibernate .proxy .HibernateProxy ;
78
77
import org .hibernate .proxy .LazyInitializer ;
78
+ import org .hibernate .service .config .spi .ConfigurationService ;
79
79
import org .hibernate .tuple .ElementWrapper ;
80
80
import org .hibernate .type .CollectionType ;
81
+ import org .jboss .logging .Logger ;
81
82
82
83
/**
83
84
* A <strong>stateful</strong> implementation of the {@link PersistenceContext} contract meaning that we maintain this
@@ -93,11 +94,12 @@ public class StatefulPersistenceContext implements PersistenceContext {
93
94
94
95
private static final CoreMessageLogger LOG = Logger .getMessageLogger ( CoreMessageLogger .class , StatefulPersistenceContext .class .getName () );
95
96
96
- private static final boolean tracing = LOG .isTraceEnabled ();
97
+ private static final boolean tracing = LOG .isTraceEnabled ();
97
98
98
99
public static final Object NO_ROW = new MarkerObject ( "NO_ROW" );
99
100
100
- public static final int INIT_COLL_SIZE = 8 ;
101
+ private static final int DEFAULT_INITIAL_CAPACITY = 8 ;
102
+ private final int initialCapacity ;
101
103
102
104
private SessionImplementor session ;
103
105
@@ -162,29 +164,32 @@ public class StatefulPersistenceContext implements PersistenceContext {
162
164
*/
163
165
public StatefulPersistenceContext (SessionImplementor session ) {
164
166
this .session = session ;
167
+
168
+ initialCapacity = session .getFactory ().getServiceRegistry ().getService ( ConfigurationService .class )
169
+ .getSetting ( AvailableSettings .SESSION_INITIAL_CAPACITY , Integer .class , DEFAULT_INITIAL_CAPACITY );
165
170
166
- entitiesByKey = new HashMap <EntityKey , Object >( INIT_COLL_SIZE );
167
- entitiesByUniqueKey = new HashMap <EntityUniqueKey , Object >( INIT_COLL_SIZE );
171
+ entitiesByKey = new HashMap <EntityKey , Object >( initialCapacity );
172
+ entitiesByUniqueKey = new HashMap <EntityUniqueKey , Object >( initialCapacity );
168
173
//noinspection unchecked
169
- proxiesByKey = new ConcurrentReferenceHashMap <EntityKey , Object >( INIT_COLL_SIZE , .75f , 1 , ConcurrentReferenceHashMap .ReferenceType .STRONG , ConcurrentReferenceHashMap .ReferenceType .WEAK , null );
170
- entitySnapshotsByKey = new HashMap <EntityKey , Object >( INIT_COLL_SIZE );
174
+ proxiesByKey = new ConcurrentReferenceHashMap <EntityKey , Object >( initialCapacity , .75f , 1 , ConcurrentReferenceHashMap .ReferenceType .STRONG , ConcurrentReferenceHashMap .ReferenceType .WEAK , null );
175
+ entitySnapshotsByKey = new HashMap <EntityKey , Object >( initialCapacity );
171
176
172
177
entityEntryContext = new EntityEntryContext ();
173
- // entityEntries = IdentityMap.instantiateSequenced( INIT_COLL_SIZE );
174
- collectionEntries = IdentityMap .instantiateSequenced ( INIT_COLL_SIZE );
175
- parentsByChild = new IdentityHashMap <Object ,Object >( INIT_COLL_SIZE );
178
+ // entityEntries = IdentityMap.instantiateSequenced( initialCapacity );
179
+ collectionEntries = IdentityMap .instantiateSequenced ( initialCapacity );
180
+ parentsByChild = new IdentityHashMap <Object ,Object >( initialCapacity );
176
181
177
- collectionsByKey = new HashMap <CollectionKey , PersistentCollection >( INIT_COLL_SIZE );
178
- arrayHolders = new IdentityHashMap <Object , PersistentCollection >( INIT_COLL_SIZE );
182
+ collectionsByKey = new HashMap <CollectionKey , PersistentCollection >( initialCapacity );
183
+ arrayHolders = new IdentityHashMap <Object , PersistentCollection >( initialCapacity );
179
184
180
185
nullifiableEntityKeys = new HashSet <EntityKey >();
181
186
182
187
initTransientState ();
183
188
}
184
189
185
190
private void initTransientState () {
186
- nullAssociations = new HashSet <AssociationKey >( INIT_COLL_SIZE );
187
- nonlazyCollections = new ArrayList <PersistentCollection >( INIT_COLL_SIZE );
191
+ nullAssociations = new HashSet <AssociationKey >( initialCapacity );
192
+ nonlazyCollections = new ArrayList <PersistentCollection >( initialCapacity );
188
193
}
189
194
190
195
@ Override
@@ -208,7 +213,7 @@ public LoadContexts getLoadContexts() {
208
213
@ Override
209
214
public void addUnownedCollection (CollectionKey key , PersistentCollection collection ) {
210
215
if (unownedCollections ==null ) {
211
- unownedCollections = new HashMap <CollectionKey ,PersistentCollection >(INIT_COLL_SIZE );
216
+ unownedCollections = new HashMap <CollectionKey ,PersistentCollection >(initialCapacity );
212
217
}
213
218
unownedCollections .put ( key , collection );
214
219
}
@@ -1659,6 +1664,9 @@ public static StatefulPersistenceContext deserialize(
1659
1664
}
1660
1665
final StatefulPersistenceContext rtn = new StatefulPersistenceContext ( session );
1661
1666
SessionFactoryImplementor sfi = session .getFactory ();
1667
+
1668
+ int initialCapacity = sfi .getServiceRegistry ().getService ( ConfigurationService .class )
1669
+ .getSetting ( AvailableSettings .SESSION_INITIAL_CAPACITY , Integer .class , DEFAULT_INITIAL_CAPACITY );
1662
1670
1663
1671
// during deserialization, we need to reconnect all proxies and
1664
1672
// collections to this session, as well as the EntityEntry and
@@ -1672,14 +1680,14 @@ public static StatefulPersistenceContext deserialize(
1672
1680
1673
1681
int count = ois .readInt ();
1674
1682
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] entitiesByKey entries" );
1675
- rtn .entitiesByKey = new HashMap <EntityKey ,Object >( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1683
+ rtn .entitiesByKey = new HashMap <EntityKey ,Object >( count < initialCapacity ? initialCapacity : count );
1676
1684
for ( int i = 0 ; i < count ; i ++ ) {
1677
1685
rtn .entitiesByKey .put ( EntityKey .deserialize ( ois , sfi ), ois .readObject () );
1678
1686
}
1679
1687
1680
1688
count = ois .readInt ();
1681
1689
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] entitiesByUniqueKey entries" );
1682
- rtn .entitiesByUniqueKey = new HashMap <EntityUniqueKey ,Object >( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1690
+ rtn .entitiesByUniqueKey = new HashMap <EntityUniqueKey ,Object >( count < initialCapacity ? initialCapacity : count );
1683
1691
for ( int i = 0 ; i < count ; i ++ ) {
1684
1692
rtn .entitiesByUniqueKey .put ( EntityUniqueKey .deserialize ( ois , session ), ois .readObject () );
1685
1693
}
@@ -1688,7 +1696,7 @@ public static StatefulPersistenceContext deserialize(
1688
1696
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] proxiesByKey entries" );
1689
1697
//noinspection unchecked
1690
1698
rtn .proxiesByKey = new ConcurrentReferenceHashMap <EntityKey , Object >(
1691
- count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ,
1699
+ count < initialCapacity ? initialCapacity : count ,
1692
1700
.75f ,
1693
1701
1 ,
1694
1702
ConcurrentReferenceHashMap .ReferenceType .STRONG ,
@@ -1709,15 +1717,15 @@ public static StatefulPersistenceContext deserialize(
1709
1717
1710
1718
count = ois .readInt ();
1711
1719
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] entitySnapshotsByKey entries" );
1712
- rtn .entitySnapshotsByKey = new HashMap <EntityKey ,Object >( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1720
+ rtn .entitySnapshotsByKey = new HashMap <EntityKey ,Object >( count < initialCapacity ? initialCapacity : count );
1713
1721
for ( int i = 0 ; i < count ; i ++ ) {
1714
1722
rtn .entitySnapshotsByKey .put ( EntityKey .deserialize ( ois , sfi ), ois .readObject () );
1715
1723
}
1716
1724
1717
1725
rtn .entityEntryContext = EntityEntryContext .deserialize ( ois , rtn );
1718
1726
// count = ois.readInt();
1719
1727
// if ( tracing ) LOG.trace("Starting deserialization of [" + count + "] entityEntries entries");
1720
- // rtn.entityEntries = IdentityMap.instantiateSequenced( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1728
+ // rtn.entityEntries = IdentityMap.instantiateSequenced( count < initialCapacity ? initialCapacity : count );
1721
1729
// for ( int i = 0; i < count; i++ ) {
1722
1730
// Object entity = ois.readObject();
1723
1731
// EntityEntry entry = EntityEntry.deserialize( ois, rtn );
@@ -1726,14 +1734,14 @@ public static StatefulPersistenceContext deserialize(
1726
1734
1727
1735
count = ois .readInt ();
1728
1736
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] collectionsByKey entries" );
1729
- rtn .collectionsByKey = new HashMap <CollectionKey ,PersistentCollection >( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1737
+ rtn .collectionsByKey = new HashMap <CollectionKey ,PersistentCollection >( count < initialCapacity ? initialCapacity : count );
1730
1738
for ( int i = 0 ; i < count ; i ++ ) {
1731
1739
rtn .collectionsByKey .put ( CollectionKey .deserialize ( ois , session ), (PersistentCollection ) ois .readObject () );
1732
1740
}
1733
1741
1734
1742
count = ois .readInt ();
1735
1743
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] collectionEntries entries" );
1736
- rtn .collectionEntries = IdentityMap .instantiateSequenced ( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1744
+ rtn .collectionEntries = IdentityMap .instantiateSequenced ( count < initialCapacity ? initialCapacity : count );
1737
1745
for ( int i = 0 ; i < count ; i ++ ) {
1738
1746
final PersistentCollection pc = ( PersistentCollection ) ois .readObject ();
1739
1747
final CollectionEntry ce = CollectionEntry .deserialize ( ois , session );
@@ -1743,7 +1751,7 @@ public static StatefulPersistenceContext deserialize(
1743
1751
1744
1752
count = ois .readInt ();
1745
1753
if ( tracing ) LOG .trace ("Starting deserialization of [" + count + "] arrayHolders entries" );
1746
- rtn .arrayHolders = new IdentityHashMap <Object , PersistentCollection >( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
1754
+ rtn .arrayHolders = new IdentityHashMap <Object , PersistentCollection >( count < initialCapacity ? initialCapacity : count );
1747
1755
for ( int i = 0 ; i < count ; i ++ ) {
1748
1756
rtn .arrayHolders .put ( ois .readObject (), (PersistentCollection ) ois .readObject () );
1749
1757
}
0 commit comments