KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > server > PersistGraph


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.server;
13
14 import com.versant.core.common.Debug;
15 import com.versant.core.common.OID;
16 import com.versant.core.common.State;
17 import com.versant.core.metadata.ModelMetaData;
18
19 import java.util.Date JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  * A graph of OID's and State's used to represent a collection of objects
25  * to be persisted. This class implements algorithms to sort the graph
26  * for persisting in the correct order and to detect cycles. This class
27  * only does a partial sort of the graph and is suitable for graphs that
28  * do not contain any new instances with post-insert key generators.
29  */

30 public class PersistGraph extends OIDGraph {
31
32     protected ModelMetaData jmd;
33     protected OID[] oids;
34     protected State[] oldStates;
35     protected State[] newStates;
36     protected Map JavaDoc oidIndexMap;
37     
38     public boolean optimistic; // Carries this flag for VDS transaction
39

40     public PersistGraph(ModelMetaData jmd, int maxSize) {
41         this.jmd = jmd;
42         oids = new OID[maxSize];
43         oldStates = new State[maxSize];
44         newStates = new State[maxSize];
45         size = 0;
46         oidIndexMap = new HashMap JavaDoc();
47     }
48
49     /**
50      * Empty the graph so it can be reused.
51      */

52     public void clear() {
53         for (int i = size - 1; i >= 0; i--) {
54             oids[i] = null;
55             oldStates[i] = null;
56             newStates[i] = null;
57         }
58         size = 0;
59         oidIndexMap.clear();
60     }
61
62     /**
63      * Add a node to the graph.
64      */

65     public void add(OID oid, State oldState, State newState) {
66         oids[size] = oid;
67         oldStates[size] = oldState;
68         newStates[size] = newState;
69         oidIndexMap.put(oid, new Integer JavaDoc(size++));
70     }
71
72     /**
73      * How many nodes are in the graph?
74      */

75     public int size() {
76         return size;
77     }
78
79     /**
80      * Get the OID at index.
81      */

82     public OID getOID(int index) {
83         return oids[index];
84     }
85
86     /**
87      * Get the old State at index.
88      */

89     public State getOldState(int index) {
90         return oldStates[index];
91     }
92
93     /**
94      * Get the new State at index.
95      */

96     public State getNewState(int index) {
97         return newStates[index];
98     }
99
100     /**
101      * Dump the graph to System.out for debugging.
102      */

103     public void dump() {
104         for (int i = 0; i < size; i++) dump(i);
105     }
106
107     /**
108      * Dump entries at index to System.out for debugging.
109      */

110     public void dump(int index) {
111         if (Debug.DEBUG) {
112             System.out.println("[" + index + "] " + oids[index] + " " +
113                     newStates[index]);
114         }
115     }
116
117     /**
118      * Find the index of OID in the graph or less than 0 if not found.
119      * Calling this method after persist has been called will return incorrect
120      * results.
121      *
122      * @see #indexOfAfterPersist
123      */

124     public int indexOf(OID oid) {
125         Object JavaDoc o = oidIndexMap.get(oid);
126         return o == null ? -1 : ((Integer JavaDoc)o).intValue();
127     }
128
129     /**
130      * Find the index of OID in the graph or less than 0 if not found.
131      * This method may be called after persist has been called. It will
132      * build the OID to index map the first time it is called.
133      *
134      * @see #indexOf
135      */

136     public int indexOfAfterPersist(OID oid) {
137         if (oidIndexMap.size() == 0) {
138             OID[] oids = this.oids;
139             for (int i = size - 1; i >= 0; i--) {
140                 oidIndexMap.put(oids[i], new Integer JavaDoc(i));
141             }
142         }
143         Object JavaDoc o = oidIndexMap.get(oid);
144         return o == null ? -1 : ((Integer JavaDoc)o).intValue();
145     }
146
147     /**
148      * Bump up timestamps etc.
149      */

150     public void doAutoSets() {
151         Date JavaDoc now = new Date JavaDoc();
152         for (int i = 0; i < size; i++) {
153             OID oid = oids[i];
154             State ns = newStates[i];
155             if (ns == null) break;
156             ns.setClassMetaData(oid.getClassMetaData());
157             if (oid.isNew()) {
158                 ns.updateAutoSetFieldsCreated(now);
159             } else {
160                 ns.updateAutoSetFieldsModified(now, oldStates[i]);
161             }
162         }
163     }
164
165     /**
166      * Compare graph entries at and a and b. Return 0 if equal, less than 0
167      * if a is less than b or greater than 0 if a is greater than b. This
168      * orders entries by class referenceGraphIndex, by new objects first,
169      * by field numbers.
170      */

171     protected int compare(int a, int b) {
172         final OID oidA = oids[a];
173         final OID oidB = oids[b];
174
175         int diff = oidB.getClassMetaData().referenceGraphIndex
176                 - oidA.getClassMetaData().referenceGraphIndex;
177         if (diff != 0) return diff;
178
179         // by new objects
180
boolean newA = oidA.isNew();
181         boolean newB = oidB.isNew();
182         if (newA && !newB) return -1;
183         if (!newA && newB) return +1;
184
185         // by field numbers
186
return newStates[a].compareToPass1(newStates[b]);
187     }
188
189     /**
190      * Swap entries.
191      */

192     protected void swap(int index1, int index2) {
193         OID tempStr = oids[index1];
194         State tempOldState = oldStates[index1];
195         State tempNewState = newStates[index1];
196
197         oids[index1] = oids[index2];
198         oldStates[index1] = oldStates[index2];
199         newStates[index1] = newStates[index2];
200
201         oids[index2] = tempStr;
202         oldStates[index2] = tempOldState;
203         newStates[index2] = tempNewState;
204     }
205 }
206
Popular Tags