Debian Codegraph @

jdbm1/src/main/java/jdbm/recman/CacheRecordManager.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Copyright 2000-2001 (C) Alex Boisvert. All Rights Reserved.
44 * Contributions are Copyright (C) 2000 by their associated contributors.
45 *
46 * $Id: CacheRecordManager.java,v 1.9 2005/06/25 23:12:32 doomdark Exp $
47 */
48 package jdbm.recman;
49
50
51 import java.io.IOException;
52 import java.util.Enumeration;
53
54 import jdbm.I18n;
55 import jdbm.RecordManager;
56 import jdbm.helper.CacheEvictionException;
57 import jdbm.helper.CachePolicy;
58 import jdbm.helper.CachePolicyListener;
59 import jdbm.helper.DefaultSerializer;
60 import jdbm.helper.Serializer;
61 import jdbm.helper.WrappedRuntimeException;
62
63
64 /**
65 * A RecordManager wrapping and caching another RecordManager.
66 *
67 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
68 * @author <a href="[email protected]">Cees de Groot</a>
69 */
70 public class CacheRecordManager implements RecordManager
71 {
72 /** Wrapped RecordManager */
73 protected RecordManager recordManager;
74
75 /** Cache for underlying RecordManager */
76 protected CachePolicy<Long, CacheEntry> cache;
77
78
79 /**
80 * Construct a CacheRecordManager wrapping another RecordManager and
81 * using a given cache policy.
82 *
83 * @param recordManager Wrapped RecordManager
84 * @param cache Cache policy
85 */
86 public CacheRecordManager( RecordManager recordManager, CachePolicy<Long, CacheEntry> cache )
87 {
88 if ( recordManager == null )
89 {
90 throw new IllegalArgumentException( I18n.err( I18n.ERR_517 ) );
91 }
92
93 if ( cache == null )
94 {
95 throw new IllegalArgumentException( I18n.err( I18n.ERR_542 ) );
96 }
97
98 this.recordManager = recordManager;
99 this.cache = cache;
100 this.cache.addListener( new CacheListener() );
101 }
102
103
104 /**
105 * Get the underlying Record Manager.
106 *
107 * @return underlying RecordManager or null if CacheRecordManager has
108 * been closed.
109 */
110 public RecordManager getRecordManager()
111 {
112 return recordManager;
113 }
114
115
116 /**
117 * Get the underlying cache policy
118 *
119 * @return underlying CachePolicy or null if CacheRecordManager has
120 * been closed.
121 */
122 public CachePolicy<Long, CacheEntry> getCachePolicy()
123 {
124 return cache;
125 }
126
127
128 /**
129 * Inserts a new record using a custom serializer.
130 *
131 * @param obj the object for the new record.
132 * @return the rowid for the new record.
133 * @throws IOException when one of the underlying I/O operations fails.
134 */
135 public long insert( Object obj ) throws IOException
136 {
137 return insert( obj, DefaultSerializer.INSTANCE );
138 }
139
140
141 /**
142 * Inserts a new record using a custom serializer.
143 *
144 * @param obj the object for the new record.
145 * @param serializer a custom serializer
146 * @return the rowid for the new record.
147 * @throws IOException when one of the underlying I/O operations fails.
148 */
149 public synchronized long insert( Object obj, Serializer serializer ) throws IOException
150 {
151 checkIfClosed();
152
153 long recid = recordManager.insert( obj, serializer );
154
155 try
156 {
157 cache.put( recid, new CacheEntry( recid, obj, serializer, false ) );
158 }
159 catch ( CacheEvictionException except )
160 {
161 throw new WrappedRuntimeException( except );
162 }
163
164 return recid;
165 }
166
167
168 /**
169 * Deletes a record.
170 *
171 * @param recid the rowid for the record that should be deleted.
172 * @throws IOException when one of the underlying I/O operations fails.
173 */
174 public synchronized void delete( long recid ) throws IOException
175 {
176 checkIfClosed();
177
178 // Remove the entry from the underlying storage
179 recordManager.delete( recid );
180
181 // And now update the cache
182 cache.remove( recid );
183 }
184
185
186 /**
187 * Updates a record using standard Java serialization.
188 *
189 * @param recid the recid for the record that is to be updated.
190 * @param obj the new object for the record.
191 * @throws IOException when one of the underlying I/O operations fails.
192 */
193 public void update( long recid, Object obj ) throws IOException
194 {
195 update( recid, obj, DefaultSerializer.INSTANCE );
196 }
197
198
199 /**
200 * Updates a record using a custom serializer.
201 *
202 * @param recid the recid for the record that is to be updated.
203 * @param obj the new object for the record.
204 * @param serializer a custom serializer
205 * @throws IOException when one of the underlying I/O operations fails.
206 */
207 public synchronized void update( long recid, Object obj, Serializer serializer ) throws IOException
208 {
209 checkIfClosed();
210
211 try
212 {
213 CacheEntry entry = cache.get( recid );
214
215 if ( entry != null )
216 {
217 // reuse existing cache entry
218 entry.obj = obj;
219 entry.serializer = serializer;
220 entry.isDirty = true;
221 }
222 else
223 {
224 cache.put( recid, new CacheEntry( recid, obj, serializer, true ) );
225 }
226 }
227 catch ( CacheEvictionException except )
228 {
229 throw new IOException( except.getLocalizedMessage() );
230 }
231 }
232
233
234 /**
235 * Fetches a record using standard Java serialization.
236 *
237 * @param recid the recid for the record that must be fetched.
238 * @return the object contained in the record.
239 * @throws IOException when one of the underlying I/O operations fails.
240 */
241 public Object fetch( long recid ) throws IOException
242 {
243 return fetch( recid, DefaultSerializer.INSTANCE );
244 }
245
246
247 /**
248 * Fetches a record using a custom serializer.
249 *
250 * @param recid the recid for the record that must be fetched.
251 * @param serializer a custom serializer
252 * @return the object contained in the record.
253 * @throws IOException when one of the underlying I/O operations fails.
254 */
255 public synchronized Object fetch( long recid, Serializer serializer ) throws IOException
256 {
257 checkIfClosed();
258
259 CacheEntry entry = cache.get( recid );
260
261 if ( entry == null )
262 {
263 entry = new CacheEntry( recid, null, serializer, false );
264 entry.obj = recordManager.fetch( recid, serializer );
265
266 try
267 {
268 cache.put( recid, entry );
269 }
270 catch ( CacheEvictionException except )
271 {
272 throw new WrappedRuntimeException( except );
273 }
274 }
275
276 if ( entry.obj instanceof byte[] )
277 {
278 byte[] copy = new byte[( ( byte[] ) entry.obj ).length];
279 System.arraycopy( entry.obj, 0, copy, 0, ( ( byte[] ) entry.obj ).length );
280 return copy;
281 }
282
283 return entry.obj;
284 }
285
286
287 /**
288 * Closes the record manager.
289 *
290 * @throws IOException when one of the underlying I/O operations fails.
291 */
292 public synchronized void close() throws IOException
293 {
294 checkIfClosed();
295
296 updateCacheEntries();
297 recordManager.close();
298 recordManager = null;
299 cache = null;
300 }
301
302
303 /**
304 * Returns the number of slots available for "root" rowids. These slots
305 * can be used to store special rowids, like rowids that point to
306 * other rowids. Root rowids are useful for bootstrapping access to
307 * a set of data.
308 */
309 public synchronized int getRootCount()
310 {
311 checkIfClosed();
312
313 return recordManager.getRootCount();
314 }
315
316
317 /**
318 * Returns the indicated root rowid.
319 *
320 * @see #getRootCount
321 */
322 public synchronized long getRoot( int id ) throws IOException
323 {
324 checkIfClosed();
325
326 return recordManager.getRoot( id );
327 }
328
329
330 /**
331 * Sets the indicated root rowid.
332 *
333 * @see #getRootCount
334 */
335 public synchronized void setRoot( int id, long rowid ) throws IOException
336 {
337 checkIfClosed();
338
339 recordManager.setRoot( id, rowid );
340 }
341
342
343 /**
344 * Commit (make persistent) all changes since beginning of transaction.
345 */
346 public synchronized void commit() throws IOException
347 {
348 checkIfClosed();
349 updateCacheEntries();
350 recordManager.commit();
351 }
352
353
354 /**
355 * Rollback (cancel) all changes since beginning of transaction.
356 */
357 public synchronized void rollback() throws IOException
358 {
359 checkIfClosed();
360
361 recordManager.rollback();
362
363 // discard all cache entries since we don't know which entries
364 // where part of the transaction
365 cache.removeAll();
366 }
367
368
369 /**
370 * Obtain the record id of a named object. Returns 0 if named object
371 * doesn't exist.
372 */
373 public synchronized long getNamedObject( String name ) throws IOException
374 {
375 checkIfClosed();
376
377 return recordManager.getNamedObject( name );
378 }
379
380
381 /**
382 * Set the record id of a named object.
383 */
384 public synchronized void setNamedObject( String name, long recid ) throws IOException
385 {
386 checkIfClosed();
387
388 recordManager.setNamedObject( name, recid );
389 }
390
391
392 /**
393 * Check if RecordManager has been closed. If so, throw an IllegalStateException
394 */
395 private void checkIfClosed() throws IllegalStateException
396 {
397 if ( recordManager == null )
398 {
399 throw new IllegalStateException( I18n.err( I18n.ERR_538 ) );
400 }
401 }
402
403
404 /**
405 * Update all dirty cache objects to the underlying RecordManager.
406 */
407 protected void updateCacheEntries() throws IOException
408 {
409 Enumeration<CacheEntry> enume = cache.elements();
410
411 while ( enume.hasMoreElements() )
412 {
413 CacheEntry entry = enume.nextElement();
414
415 if ( entry.isDirty )
416 {
417 recordManager.update( entry.recid, entry.obj, entry.serializer );
418 entry.isDirty = false;
419 }
420 }
421 }
422
423 /**
424 * A class to store a cached entry.
425 */
426 private class CacheEntry
427 {
428 long recid;
429 Object obj;
430 Serializer serializer;
431 boolean isDirty;
432
433
434 CacheEntry( long recid, Object obj, Serializer serializer, boolean isDirty )
435 {
436 this.recid = recid;
437 this.obj = obj;
438 this.serializer = serializer;
439 this.isDirty = isDirty;
440 }
441
442 } // class CacheEntry
443
444 private class CacheListener implements CachePolicyListener<CacheEntry>
445 {
446
447 /**
448 * Notification that cache is evicting an object
449 *
450 * @param obj object evicted from cache
451 */
452 public void cacheObjectEvicted( CacheEntry obj ) throws CacheEvictionException
453 {
454 CacheEntry entry = obj;
455 if ( entry.isDirty )
456 {
457 try
458 {
459 recordManager.update( entry.recid, entry.obj, entry.serializer );
460 }
461 catch ( IOException except )
462 {
463 throw new CacheEvictionException( except );
464 }
465 }
466 }
467 }
468 }