26
26
import java .io .Serializable ;
27
27
import java .util .Properties ;
28
28
import java .util .Set ;
29
- import java .util .concurrent .locks .ReentrantReadWriteLock ;
30
29
31
30
import org .jboss .logging .Logger ;
32
31
@@ -51,100 +50,96 @@ public class UpdateTimestampsCache {
51
50
public static final String REGION_NAME = UpdateTimestampsCache .class .getName ();
52
51
private static final CoreMessageLogger LOG = Logger .getMessageLogger ( CoreMessageLogger .class , UpdateTimestampsCache .class .getName () );
53
52
54
- private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock ();
55
- private final TimestampsRegion region ;
56
53
private final SessionFactoryImplementor factory ;
54
+ private final TimestampsRegion region ;
57
55
58
56
public UpdateTimestampsCache (Settings settings , Properties props , final SessionFactoryImplementor factory ) throws HibernateException {
59
57
this .factory = factory ;
60
- String prefix = settings .getCacheRegionPrefix ();
61
- String regionName = prefix == null ? REGION_NAME : prefix + '.' + REGION_NAME ;
58
+ final String prefix = settings .getCacheRegionPrefix ();
59
+ final String regionName = prefix == null ? REGION_NAME : prefix + '.' + REGION_NAME ;
60
+
62
61
LOG .startingUpdateTimestampsCache ( regionName );
63
62
this .region = settings .getRegionFactory ().buildTimestampsRegion ( regionName , props );
64
63
}
64
+
65
65
@ SuppressWarnings ({"UnusedDeclaration" })
66
- public UpdateTimestampsCache (Settings settings , Properties props )
67
- throws HibernateException {
68
- this (settings , props , null );
66
+ public UpdateTimestampsCache (Settings settings , Properties props ) throws HibernateException {
67
+ this ( settings , props , null );
69
68
}
70
69
71
70
@ SuppressWarnings ({"UnnecessaryBoxing" })
72
71
public void preinvalidate (Serializable [] spaces ) throws CacheException {
73
- readWriteLock .writeLock ().lock ();
72
+ final boolean debug = LOG .isDebugEnabled ();
73
+ final boolean stats = factory != null && factory .getStatistics ().isStatisticsEnabled ();
74
74
75
- try {
76
- Long ts = region .nextTimestamp () + region .getTimeout ();
77
- for ( Serializable space : spaces ) {
75
+ final Long ts = region .nextTimestamp () + region .getTimeout ();
76
+
77
+ for ( Serializable space : spaces ) {
78
+ if ( debug ) {
78
79
LOG .debugf ( "Pre-invalidating space [%s], timestamp: %s" , space , ts );
79
- //put() has nowait semantics, is this really appropriate?
80
- //note that it needs to be async replication, never local or sync
81
- region .put ( space , ts );
82
- if ( factory != null && factory .getStatistics ().isStatisticsEnabled () ) {
83
- factory .getStatisticsImplementor ().updateTimestampsCachePut ();
84
- }
85
80
}
86
- }
87
- finally {
88
- readWriteLock .writeLock ().unlock ();
81
+ //put() has nowait semantics, is this really appropriate?
82
+ //note that it needs to be async replication, never local or sync
83
+ region .put ( space , ts );
84
+ if ( stats ) {
85
+ factory .getStatisticsImplementor ().updateTimestampsCachePut ();
86
+ }
89
87
}
90
88
}
91
89
92
- @ SuppressWarnings ({"UnnecessaryBoxing" })
90
+ @ SuppressWarnings ({"UnnecessaryBoxing" })
93
91
public void invalidate (Serializable [] spaces ) throws CacheException {
94
- readWriteLock .writeLock ().lock ();
92
+ final boolean debug = LOG .isDebugEnabled ();
93
+ final boolean stats = factory != null && factory .getStatistics ().isStatisticsEnabled ();
95
94
96
- try {
97
- Long ts = region .nextTimestamp ();
98
- for (Serializable space : spaces ) {
95
+ final Long ts = region .nextTimestamp ();
96
+
97
+ for (Serializable space : spaces ) {
98
+ if ( debug ) {
99
99
LOG .debugf ( "Invalidating space [%s], timestamp: %s" , space , ts );
100
- //put() has nowait semantics, is this really appropriate?
101
- //note that it needs to be async replication, never local or sync
102
- region .put ( space , ts );
103
- if ( factory != null && factory .getStatistics ().isStatisticsEnabled () ) {
104
- factory .getStatisticsImplementor ().updateTimestampsCachePut ();
105
- }
106
100
}
107
- }
108
- finally {
109
- readWriteLock .writeLock ().unlock ();
101
+ //put() has nowait semantics, is this really appropriate?
102
+ //note that it needs to be async replication, never local or sync
103
+ region .put ( space , ts );
104
+ if ( stats ) {
105
+ factory .getStatisticsImplementor ().updateTimestampsCachePut ();
106
+ }
110
107
}
111
108
}
112
109
113
110
@ SuppressWarnings ({"unchecked" , "UnnecessaryUnboxing" })
114
111
public boolean isUpToDate (Set spaces , Long timestamp ) throws HibernateException {
115
- readWriteLock .readLock ().lock ();
116
-
117
- try {
118
- for ( Serializable space : (Set <Serializable >) spaces ) {
119
- Long lastUpdate = (Long ) region .get ( space );
120
- if ( lastUpdate == null ) {
121
- if ( factory != null && factory .getStatistics ().isStatisticsEnabled () ) {
122
- factory .getStatisticsImplementor ().updateTimestampsCacheMiss ();
123
- }
124
- //the last update timestamp was lost from the cache
125
- //(or there were no updates since startup!)
126
- //updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );
127
- //result = false; // safer
112
+ final boolean debug = LOG .isDebugEnabled ();
113
+ final boolean stats = factory != null && factory .getStatistics ().isStatisticsEnabled ();
114
+
115
+ for ( Serializable space : (Set <Serializable >) spaces ) {
116
+ Long lastUpdate = (Long ) region .get ( space );
117
+ if ( lastUpdate == null ) {
118
+ if ( stats ) {
119
+ factory .getStatisticsImplementor ().updateTimestampsCacheMiss ();
120
+ }
121
+ //the last update timestamp was lost from the cache
122
+ //(or there were no updates since startup!)
123
+ //updateTimestamps.put( space, new Long( updateTimestamps.nextTimestamp() ) );
124
+ //result = false; // safer
125
+ }
126
+ else {
127
+ if ( debug ) {
128
+ LOG .debugf (
129
+ "[%s] last update timestamp: %s" ,
130
+ space ,
131
+ lastUpdate + ", result set timestamp: " + timestamp
132
+ );
128
133
}
129
- else {
130
- if ( LOG .isDebugEnabled () ) {
131
- LOG .debugf (
132
- "[%s] last update timestamp: %s" ,
133
- space ,
134
- lastUpdate + ", result set timestamp: " + timestamp
135
- );
136
- }
137
- if ( factory != null && factory .getStatistics ().isStatisticsEnabled () ) {
138
- factory .getStatisticsImplementor ().updateTimestampsCacheHit ();
139
- }
140
- if ( lastUpdate >= timestamp ) return false ;
134
+ if ( stats ) {
135
+ factory .getStatisticsImplementor ().updateTimestampsCacheHit ();
136
+ }
137
+ if ( lastUpdate >= timestamp ) {
138
+ return false ;
141
139
}
142
140
}
143
- return true ;
144
- }
145
- finally {
146
- readWriteLock .readLock ().unlock ();
147
141
}
142
+ return true ;
148
143
}
149
144
150
145
public void clear () throws CacheException {
0 commit comments