KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > AbstractMapStore


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: AbstractMapStore.java,v 1.6 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import com.triactive.jdo.StateManager;
15 import java.sql.Connection JavaDoc;
16 import java.sql.PreparedStatement JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23 import javax.jdo.JDODataStoreException;
24 import javax.jdo.JDOHelper;
25 import javax.jdo.JDOUnsupportedOptionException;
26 import javax.jdo.JDOUserException;
27 import org.apache.log4j.Category;
28
29
30 abstract class AbstractMapStore implements MapStore
31 {
32     private static final Category LOG = Category.getInstance(AbstractMapStore.class);
33
34     protected StoreManager storeMgr;
35     protected DatabaseAdapter dba;
36     protected ColumnMapping ownerMapping;
37     protected ColumnMapping keyMapping;
38     protected ColumnMapping valueMapping;
39     protected Column ownerColumn;
40     protected Column keyColumn;
41     protected Column valueColumn;
42     protected Class JavaDoc keyType;
43     protected Class JavaDoc valueType;
44     protected boolean keysAreEmbedded;
45     protected boolean valuesAreEmbedded;
46     protected String JavaDoc loadStmt;
47     protected String JavaDoc getStmt;
48     protected String JavaDoc sizeStmt;
49     protected String JavaDoc containsValueStmt;
50     protected String JavaDoc containsEntryStmt;
51     protected String JavaDoc clearStmt;
52
53
54     public AbstractMapStore()
55     {
56     }
57
58
59     public StoreManager getStoreManager()
60     {
61         return storeMgr;
62     }
63
64
65     public Class JavaDoc getKeyType()
66     {
67         return keyType;
68     }
69
70
71     public Class JavaDoc getValueType()
72     {
73         return valueType;
74     }
75
76
77     protected boolean validateKeyForReading(StateManager sm, Object JavaDoc key)
78     {
79         if (!keysAreEmbedded)
80         {
81             PersistenceManager pm = sm.getPersistenceManager();
82
83             if (!JDOHelper.isPersistent(key) || pm != JDOHelper.getPersistenceManager(key))
84                 return false;
85         }
86
87         return true;
88     }
89
90
91     protected boolean validateValueForReading(StateManager sm, Object JavaDoc value)
92     {
93         if (!valuesAreEmbedded)
94         {
95             PersistenceManager pm = sm.getPersistenceManager();
96
97             if (!JDOHelper.isPersistent(value) || pm != JDOHelper.getPersistenceManager(value))
98                 return false;
99         }
100
101         return true;
102     }
103
104
105     protected void validateKeyForWriting(StateManager sm, Object JavaDoc key)
106     {
107         if (!keysAreEmbedded)
108         {
109             PersistenceManager pm = sm.getPersistenceManager();
110
111             if (!JDOHelper.isPersistent(key))
112                 pm.makePersistent(key);
113             else if (pm != JDOHelper.getPersistenceManager(key))
114                 throw new JDOUserException("Can't write key from a different persistence manager", JDOHelper.getObjectId(key));
115         }
116     }
117
118
119     protected void validateValueForWriting(StateManager sm, Object JavaDoc value)
120     {
121         if (!valuesAreEmbedded)
122         {
123             PersistenceManager pm = sm.getPersistenceManager();
124
125             if (!JDOHelper.isPersistent(value))
126                 pm.makePersistent(value);
127             else if (pm != JDOHelper.getPersistenceManager(value))
128                 throw new JDOUserException("Can't write value from a different persistence manager", JDOHelper.getObjectId(value));
129         }
130     }
131
132
133     public Map JavaDoc load(StateManager sm)
134     {
135         HashMap JavaDoc contents = new HashMap JavaDoc();
136         PersistenceManager pm = sm.getPersistenceManager();
137
138         try
139         {
140             Connection JavaDoc conn = pm.getConnection(false);
141
142             try
143             {
144                 PreparedStatement JavaDoc ps = conn.prepareStatement(loadStmt);
145
146                 try
147                 {
148                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
149
150                     long startTime = System.currentTimeMillis();
151
152                     ResultSet JavaDoc rs = ps.executeQuery();
153
154                     try
155                     {
156                         if (LOG.isDebugEnabled())
157                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + loadStmt);
158
159                         while (rs.next())
160                             contents.put(keyMapping.getObject(pm, rs, 1), valueMapping.getObject(pm, rs, 2));
161                     }
162                     finally
163                     {
164                         rs.close();
165                     }
166                 }
167                 finally
168                 {
169                     ps.close();
170                 }
171             }
172             finally
173             {
174                 pm.releaseConnection(conn);
175             }
176         }
177         catch (SQLException JavaDoc e)
178         {
179             throw dba.newDataStoreException("Load request failed: " + loadStmt, e);
180         }
181
182         return contents;
183     }
184
185
186     public int size(StateManager sm)
187     {
188         int numRows;
189         PersistenceManager pm = sm.getPersistenceManager();
190
191         try
192         {
193             Connection JavaDoc conn = pm.getConnection(false);
194
195             try
196             {
197                 PreparedStatement JavaDoc ps = conn.prepareStatement(sizeStmt);
198
199                 try
200                 {
201                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
202
203                     long startTime = System.currentTimeMillis();
204
205                     ResultSet JavaDoc rs = ps.executeQuery();
206
207                     try
208                     {
209                         if (LOG.isDebugEnabled())
210                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + sizeStmt);
211
212                         if (!rs.next())
213                             throw new JDODataStoreException("Size request returned no result row: " + sizeStmt);
214
215                         numRows = rs.getInt(1);
216
217                         storeMgr.logSQLWarnings(rs);
218                     }
219                     finally
220                     {
221                         rs.close();
222                     }
223                 }
224                 finally
225                 {
226                     ps.close();
227                 }
228             }
229             finally
230             {
231                 pm.releaseConnection(conn);
232             }
233         }
234         catch (SQLException JavaDoc e)
235         {
236             throw dba.newDataStoreException("Size request failed: " + sizeStmt, e);
237         }
238
239         return numRows;
240     }
241
242
243     public boolean isEmpty(StateManager sm)
244     {
245         /* There are probably more efficient ways of doing this in SQL. */
246         return size(sm) == 0;
247     }
248
249
250     protected Object JavaDoc get0(StateManager sm, Object JavaDoc key) throws NoSuchElementException JavaDoc
251     {
252         if (!validateKeyForReading(sm, key))
253             return null;
254
255         Object JavaDoc value;
256         PersistenceManager pm = sm.getPersistenceManager();
257
258         try
259         {
260             Connection JavaDoc conn = pm.getConnection(false);
261
262             try
263             {
264                 PreparedStatement JavaDoc ps = conn.prepareStatement(getStmt);
265
266                 try
267                 {
268                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
269                     keyMapping.setObject(pm, ps, 2, key);
270
271                     long startTime = System.currentTimeMillis();
272
273                     ResultSet JavaDoc rs = ps.executeQuery();
274
275                     try
276                     {
277                         if (LOG.isDebugEnabled())
278                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + getStmt);
279
280                         if (!rs.next())
281                             throw new NoSuchElementException JavaDoc();
282
283                         value = valueMapping.getObject(pm, rs, 1);
284
285                         storeMgr.logSQLWarnings(rs);
286                     }
287                     finally
288                     {
289                         rs.close();
290                     }
291                 }
292                 finally
293                 {
294                     ps.close();
295                 }
296             }
297             finally
298             {
299                 pm.releaseConnection(conn);
300             }
301         }
302         catch (SQLException JavaDoc e)
303         {
304             throw dba.newDataStoreException("Get request failed: " + getStmt, e);
305         }
306
307         return value;
308     }
309
310
311     public boolean containsKey(StateManager sm, Object JavaDoc key)
312     {
313         try
314         {
315             get0(sm, key);
316             return true;
317         }
318         catch (NoSuchElementException JavaDoc e)
319         {
320             return false;
321         }
322     }
323
324
325     public boolean containsValue(StateManager sm, Object JavaDoc value)
326     {
327         if (!validateValueForReading(sm, value))
328             return false;
329
330         boolean exists = false;
331         PersistenceManager pm = sm.getPersistenceManager();
332
333         try
334         {
335             Connection JavaDoc conn = pm.getConnection(false);
336
337             try
338             {
339                 PreparedStatement JavaDoc ps = conn.prepareStatement(containsValueStmt);
340
341                 try
342                 {
343                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
344                     valueMapping.setObject(pm, ps, 2, value);
345
346                     long startTime = System.currentTimeMillis();
347
348                     ResultSet JavaDoc rs = ps.executeQuery();
349
350                     try
351                     {
352                         if (LOG.isDebugEnabled())
353                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + containsValueStmt);
354
355                         if (rs.next())
356                             exists = true;
357
358                         storeMgr.logSQLWarnings(rs);
359                     }
360                     finally
361                     {
362                         rs.close();
363                     }
364                 }
365                 finally
366                 {
367                     ps.close();
368                 }
369             }
370             finally
371             {
372                 pm.releaseConnection(conn);
373             }
374         }
375         catch (SQLException JavaDoc e)
376         {
377             throw dba.newDataStoreException("containsValue request failed: " + containsValueStmt, e);
378         }
379
380         return exists;
381     }
382
383
384     public boolean containsEntry(StateManager sm, Object JavaDoc key, Object JavaDoc value)
385     {
386         if (!validateKeyForReading(sm, value))
387             return false;
388         if (!validateValueForReading(sm, value))
389             return false;
390
391         boolean exists = false;
392         PersistenceManager pm = sm.getPersistenceManager();
393
394         try
395         {
396             Connection JavaDoc conn = pm.getConnection(false);
397
398             try
399             {
400                 PreparedStatement JavaDoc ps = conn.prepareStatement(containsEntryStmt);
401
402                 try
403                 {
404                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
405                     keyMapping.setObject(pm, ps, 2, value);
406                     valueMapping.setObject(pm, ps, 3, value);
407
408                     long startTime = System.currentTimeMillis();
409
410                     ResultSet JavaDoc rs = ps.executeQuery();
411
412                     try
413                     {
414                         if (LOG.isDebugEnabled())
415                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + containsEntryStmt);
416
417                         if (rs.next())
418                             exists = true;
419
420                         storeMgr.logSQLWarnings(rs);
421                     }
422                     finally
423                     {
424                         rs.close();
425                     }
426                 }
427                 finally
428                 {
429                     ps.close();
430                 }
431             }
432             finally
433             {
434                 pm.releaseConnection(conn);
435             }
436         }
437         catch (SQLException JavaDoc e)
438         {
439             throw dba.newDataStoreException("containsEntry request failed: " + containsEntryStmt, e);
440         }
441
442         return exists;
443     }
444
445
446     public Object JavaDoc get(StateManager sm, Object JavaDoc key)
447     {
448         try
449         {
450             return get0(sm, key);
451         }
452         catch (NoSuchElementException JavaDoc e)
453         {
454             return null;
455         }
456     }
457
458
459     public void putAll(StateManager sm, Map JavaDoc m)
460     {
461         Iterator JavaDoc i = m.entrySet().iterator();
462
463         while (i.hasNext())
464         {
465             Map.Entry JavaDoc e = (Map.Entry JavaDoc)i.next();
466             put(sm, e.getKey(), e.getValue());
467         }
468     }
469
470
471     public void clear(StateManager sm)
472     {
473         PersistenceManager pm = sm.getPersistenceManager();
474
475         try
476         {
477             Connection JavaDoc conn = pm.getConnection(true);
478
479             try
480             {
481                 PreparedStatement JavaDoc ps = conn.prepareStatement(clearStmt);
482
483                 try
484                 {
485                     ownerMapping.setObject(pm, ps, 1, sm.getObject());
486
487                     long startTime = System.currentTimeMillis();
488
489                     ps.executeUpdate();
490
491                     if (LOG.isDebugEnabled())
492                         LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + clearStmt);
493                 }
494                 finally
495                 {
496                     ps.close();
497                 }
498             }
499             finally
500             {
501                 pm.releaseConnection(conn);
502             }
503         }
504         catch (SQLException JavaDoc e)
505         {
506             throw dba.newDataStoreException("Clear request failed: " + clearStmt, e);
507         }
508     }
509
510
511     public Queryable keySetQuery(StateManager sm)
512     {
513         throw new JDOUnsupportedOptionException("Cannot query sets obtained by Map.keySet() (yet)");
514     }
515
516
517     public Queryable valuesQuery(StateManager sm)
518     {
519         throw new JDOUnsupportedOptionException("Cannot query sets obtained by Map.values() (yet)");
520     }
521
522
523     public Queryable entrySetQuery(StateManager sm)
524     {
525         throw new JDOUnsupportedOptionException("Cannot query sets obtained by Map.entrySet() (yet)");
526     }
527 }
528
Popular Tags