KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > QueryCacheItemImpl


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  */

20 package org.enhydra.dods.cache;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import org.enhydra.dods.cache.Condition;
26 import org.enhydra.dods.cache.QueryCacheItem;
27 import com.lutris.appserver.server.sql.DatabaseManagerException;
28 import com.lutris.dods.builder.generator.dataobject.GenericDO;
29 import com.lutris.appserver.server.sql.CoreDataStruct;
30
31 /**
32  * This class stores one query and its necessary data, for query array.
33  *
34  * @author Tanja Jovanovic
35  * @author Nenad Vico
36  * @version 2.0 15.06.2003.
37  */

38 public class QueryCacheItemImpl implements QueryCacheItem {
39
40     /**
41      * Query id: String "query_database_name.String_presentation_of_query".
42      */

43     protected String JavaDoc queryId;
44
45     /**
46      * HashMap of data (and DataStruct) object IDs which are results of the
47      * query.
48      */

49     protected HashMap JavaDoc OIds;
50
51     /**
52      * Number of cached query results.
53      */

54     protected int resultNum = 0;
55
56     /**
57      * True if all query results are cached, otherwise false.
58      */

59     protected boolean completeRes = false;
60
61     /**
62      * Head of object ID list in which are query results.
63      */

64     protected ListItem head;
65
66     /**
67      * Tail of object ID list in which are query results.
68      */

69     protected ListItem tail;
70     
71     /**
72      * This attribute indicates whether there have been performed inserts,
73      * updates or deletes on results of this query.
74      *
75      */

76     protected boolean modifiedQuery = false;
77
78     /**
79      * Time needed for query execution.
80      */

81     protected int time;
82
83     /**
84      * Array of conditions declared in WHERE part of the query
85      * (array of org.enhydra.dods.cache.Condition objects).
86      */

87     protected ArrayList JavaDoc conds;
88
89     /**
90      * Database of the query.
91      */

92     protected String JavaDoc originDatabase;
93
94     /**
95      * Constructor (String).
96      *
97      * @param origDb Query database.
98      */

99     public QueryCacheItemImpl(String JavaDoc origDb) {
100         OIds = new HashMap JavaDoc();
101         resultNum = 0;
102         completeRes = false;
103         modifiedQuery = false;
104         this.time = 0;
105         conds = new ArrayList JavaDoc();
106         originDatabase = origDb;
107     }
108
109     /**
110      * Constructor (String, LinkedHashSet, int, ArrayList, String).
111      *
112      * @param qId Query id.
113      * @param OIds Array of object IDs which are results of the Query.
114      * @param time Query execution time.
115      * @param conditions Conditions (WHERE part of the query).
116      * @param origDb Query database.
117      */

118     public QueryCacheItemImpl(String JavaDoc qId, HashMap JavaDoc OIds, int time, ArrayList JavaDoc conditions, String JavaDoc origDb) {
119         queryId = qId;
120         this.OIds = OIds;
121         resultNum = 0;
122         completeRes = false;
123         modifiedQuery = false;
124         this.time = time;
125         conds = conditions;
126         originDatabase = origDb;
127     }
128
129     /**
130      * Returns query id (String it the form:
131      * query_database_name.String_presentation_of_query).
132      *
133      * @return Query id.
134      */

135     public String JavaDoc getQueryId() {
136         return queryId;
137     }
138
139     /**
140      * Sets query id (String it the form:
141      * query_database_name.String_presentation_of_query).
142      *
143      * @param queryId Query id.
144      */

145     public void setQueryId(String JavaDoc queryId) {
146         this.queryId = queryId;
147     }
148
149     /**
150      * Returns OIds (Collection of object IDs which are results of the query).
151      *
152      * @return Collection of object IDs which are results of the query.
153      */

154     public Collection JavaDoc getOIds() {
155         ArrayList JavaDoc list = new ArrayList JavaDoc();
156         ListItem iter = head;
157
158         while (iter != null) {
159             list.add(iter.handle);
160             iter = iter.next;
161         }
162         return list;
163     }
164
165     /**
166      * Returns number of cached query results.
167      *
168      * @return Number of cached query results.
169      */

170     public int getResultNum() {
171         return resultNum;
172     }
173
174     /**
175      * Returns true if all query results are cached, otherwise false.
176      *
177      * @return true if all query results are cached, otherwise false.
178      */

179     public boolean isCompleteResult() {
180         return completeRes;
181     }
182
183     /**
184      * Sets new boolean value about the cached query results (true if all query
185      * results are cached, otherwise false).
186      *
187      * @param newCompleteRes true if all query results are cached, otherwise
188      * false.
189      */

190     public void setCompleteResult(boolean newCompleteRes) {
191         completeRes = newCompleteRes;
192     }
193   
194     /**
195      * Returns true if there have been performed inserts, updates or deletes
196      * concerning results of this query, otherwise false.
197      *
198      * @return true if there have been performed inserts, updates or deletes
199      * concerning results of this query, otherwise false.
200      */

201     public boolean isModifiedQuery() {
202         return modifiedQuery;
203     }
204
205     /**
206      * Sets modifiedQuery attribute.
207      *
208      * @param mod New value of attribute modifiedQuery.
209      * concerning results of this query, otherwise false.
210      */

211     public void setModifiedQuery(boolean mod) {
212         modifiedQuery = mod;
213     }
214
215     /**
216      * Returns time needed for query execution.
217      *
218      * @return Time needed for query execution.
219      */

220     public int getTime() {
221         return time;
222     }
223
224     /**
225      * Sets time needed for query execution.
226      *
227      * @param time Time needed for query execution.
228      */

229     public void setTime(int time) {
230         this.time = time;
231     }
232
233     /**
234      * Returns array of query conditions conds.
235      *
236      * @return Array of query conditions.
237      */

238     public ArrayList JavaDoc getConds() {
239         return conds;
240     }
241
242     /**
243      * Sets array of query conditions.
244      *
245      * @param conds Array of query conditions.
246      */

247     public void setConds(ArrayList JavaDoc conds) {
248         this.conds = conds;
249     }
250
251     /**
252      * Adds condition to query.
253      *
254      * @param cond <code>Condition</code> that will be added to query.
255      */

256     public void addCond(Condition cond) {
257         conds.add(cond);
258     }
259
260     /**
261      * Returns query database.
262      *
263      * @deprecated Use get_OriginDatabase()
264      * @return Query database.
265      */

266     public String JavaDoc getOriginDatabase() {
267         return get_OriginDatabase();
268     }
269
270     /**
271      * Returns query database.
272      *
273      * @return Query database.
274      */

275     public String JavaDoc get_OriginDatabase() {
276         return originDatabase;
277     }
278
279     /**
280      * Checks whether data object obj satisfies conditions of this query.
281      *
282      * @param obj Data object for which are checked conditions of this query.
283      * @return true if data object obj satisfies conditions of this query,
284      * otherwise false.
285      */

286     public boolean checkConditions(GenericDO obj) {
287         for (int i = 0; i < conds.size(); i++) {
288             if (!obj.compareCond((Condition) conds.get(i))) {
289                 return false;
290             }
291         }
292         return true;
293     }
294
295     /**
296      * Checks whether DataStruct object obj satisfies conditions of this query.
297      *
298      * @param obj DataStruct object for which are checked conditions of this
299      * query.
300      * @return true if DataStruct object obj satisfies conditions of this query,
301      * otherwise false.
302      */

303     public boolean checkConditions(CoreDataStruct obj) {
304         for (int i = 0; i < conds.size(); i++) {
305             if (!obj.compareCond((Condition) conds.get(i))) {
306                 return false;
307             }
308         }
309         return true;
310     }
311
312     /**
313      * Inserts data object obj (or updates it if already exists) in array DOs,
314      * if it satisfies this query.
315      *
316      * @param obj Data object which may be inserted (or updated) in array DOs.
317      */

318     public void update(GenericDO obj) {
319         if (obj.get_OriginDatabase().equals(originDatabase)) {
320             try {
321                 String JavaDoc key = obj.get_Handle();
322
323                 if (checkConditions(obj)) {
324                     ListItem tmp = null;
325
326                     tmp = (ListItem) OIds.get(key);
327                     if (tmp == null) {
328                         addHandle(key);
329                     }
330                 } else {
331                     removeHandle(key);
332                 }
333             } catch (DatabaseManagerException e) {}
334         }
335     }
336
337     /**
338      * Inserts DataStruct object obj (or updates it if already exists) in array
339      * DOs, if it satisfies this query.
340      *
341      * @param obj DataStruct object which may be inserted (or updated) in array
342      * DOs.
343      */

344     public void update(CoreDataStruct obj) {
345         if (obj.get_Database().equals(originDatabase)) {
346             try {
347                 String JavaDoc key = obj.get_Handle();
348
349                 if (checkConditions(obj)) {
350                     ListItem tmp = null;
351
352                     tmp = (ListItem) OIds.get(key);
353                     if (tmp == null) {
354                         addHandle(key);
355                     }
356                 } else {
357                     removeHandle(key);
358                 }
359             } catch (DatabaseManagerException e) {}
360         }
361     }
362
363     /**
364      * Removes data object obj from array DOs, if present.
365      *
366      * @param obj Data object which will be removed from array DOs.
367      */

368     public void delete(GenericDO obj) {
369         if (obj.get_OriginDatabase().equals(originDatabase)) {
370             try {
371                 String JavaDoc key = null;
372
373                 key = obj.get_Handle();
374                 if (key != null) {
375                     removeHandle(key);
376                 }
377             } catch (DatabaseManagerException e) {}
378         }
379     }
380
381     /**
382      * Removes DataStruct object obj from array DOs, if present.
383      *
384      * @param obj DataStruct object which will be removed from array DOs.
385      */

386     public void delete(CoreDataStruct obj) {
387         if (obj.get_Database().equals(originDatabase)) {
388             try {
389                 String JavaDoc key = null;
390
391                 key = obj.get_Handle();
392                 if (key != null) {
393                     removeHandle(key);
394                 }
395             } catch (DatabaseManagerException e) {}
396         }
397     }
398
399     /**
400      * Adds data object obj to array DOs.
401      *
402      * @param obj Data object which will be added to array DOs.
403      */

404     public void add(GenericDO obj) {
405         if (obj.get_OriginDatabase().equals(originDatabase)) {
406             try {
407                 String JavaDoc key = null;
408
409                 key = obj.get_Handle();
410                 if (key != null) {
411                     addHandle(key);
412                 }
413             } catch (DatabaseManagerException e) {}
414         }
415     }
416
417     /**
418      * Adds DataStruct object obj to array DOs.
419      *
420      * @param obj DataStruct object which will be added to array DOs.
421      */

422     public void add(CoreDataStruct obj) {
423         if (obj != null && obj.get_Database() != null) {
424             if (obj.get_Database().equals(originDatabase)) {
425                 try {
426            
427                     String JavaDoc key = null;
428
429                     key = obj.get_Handle();
430                     if (key != null) {
431                         addHandle(key);
432                     }
433                 } catch (DatabaseManagerException e) {}
434             }
435         }
436     }
437
438     /**
439      * Shows content of this class.
440      * Can be used for debugging.
441      */

442     public String JavaDoc toString() {
443         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
444         GenericDO DO = null;
445
446         ret.append("\n QueryCacheItemImpl: ");
447         ret.append("\n queryId: " + queryId);
448         ret.append("\n OIds : " + OIds);
449         if (OIds != null) {
450             for (ListItem iter = head; iter != null; iter = iter.next) {
451                 ret.append(" " + iter.handle);
452             }
453         }
454         ret.append("\n time : " + time);
455         ret.append("\n conds : " + conds);
456         ret.append("\n originDatabase : " + originDatabase);
457         return ret.toString();
458     }
459
460     protected void addHandle(String JavaDoc handle) {
461         ListItem item = new ListItem(handle);
462
463         item.prev = tail;
464         if (tail != null) {
465             tail.next = item;
466         }
467         if (head == null) {
468             head = item;
469         }
470         tail = item;
471         ListItem value = (ListItem) OIds.get(handle);
472
473         if (value == null) {
474             OIds.put(handle, item);
475         } else {
476             item.sameNext = value;
477             OIds.put(handle, item);
478         }
479         resultNum++;
480     }
481
482     protected void removeHandle(String JavaDoc handle) {
483         ListItem curr = (ListItem) OIds.get(handle);
484
485         if (curr != null) {
486             if (head == curr) {
487                 head = curr.next;
488             }
489             if (tail == curr) {
490                 tail = curr.prev;
491             }
492         }
493         ListItem temp;
494
495         while (curr != null) {
496             temp = curr;
497             curr = curr.unlink().sameNext;
498             temp.sameNext = null;
499             resultNum--;
500         }
501     }
502     static class ListItem {
503         public String JavaDoc handle;
504         public ListItem prev = null;
505         public ListItem next = null;
506         public ListItem sameNext = null;
507         ListItem() {}
508
509         ListItem(String JavaDoc hnd) {
510             handle = hnd;
511         }
512
513         ListItem unlink() {
514             if (prev != null) {
515                 prev.next = next;
516             }
517             if (next != null) {
518                 next.prev = prev;
519             }
520             return this;
521         }
522     }
523 }
524
Popular Tags