KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > StatesToStore


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.common;
13
14 import com.versant.core.metadata.ModelMetaData;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.util.OIDObjectOutput;
17 import com.versant.core.util.OIDObjectInput;
18 import com.versant.core.util.FastExternalizable;
19
20 import java.io.*;
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * Collection of OIDs and States to be persisted.
25  */

26 public final class StatesToStore implements FastExternalizable {
27
28     private static final int breakPoint = 100;
29     private static final int initialSize = 50;
30
31     private ModelMetaData jmd;
32
33     private static final int INITIAL_CAPACITY = 20;
34     private static final int GROW_SIZE = 2;
35
36     public OID[] oids = new OID[INITIAL_CAPACITY];
37     public State[] states = new State[INITIAL_CAPACITY];
38     public State[] origStates = new State[INITIAL_CAPACITY];
39
40     private int size; // the number of entries
41
private boolean fullSort;
42
43     // These fields keep track of OIDs/instances of objectid-class'es and
44
// classes to evict from l2 cache if the transaction commits (epc = Evict
45
// Post Commit).
46
public boolean epcAll; // evict everything from l2 cache on commit
47
public OID[] epcOids; // may be null
48
public int[] epcClasses; // of class index, may be null
49
public int epcClassCount;
50
51     public StatesToStore() {
52     }
53
54     public StatesToStore(ModelMetaData jmd) {
55         this.jmd = jmd;
56     }
57
58     /**
59      * Is a full topological sort required to persist the states in this
60      * graph? This will be true if any of the states are for new objects
61      * and are using a post-insert key generator.
62      *
63      * @see com.versant.core.server.PersistGraph
64      * @see com.versant.core.server.PersistGraphFullSort
65      */

66     public boolean isFullSortRequired() {
67         return fullSort;
68     }
69
70     /**
71      * Add a state to the container to be persisted.
72      * @param state The state containing the changed fields
73      * @param origState The state containing original field values
74      * @param fullSort If true then the fullSort flag is set on the container
75      */

76     public void add(OID oid, State state, State origState, boolean fullSort) {
77         resize();
78         oids[size] = oid;
79         states[size] = state;
80         origStates[size++] = origState;
81         if (fullSort) this.fullSort = true;
82     }
83
84     /**
85      * The number of entries in the container.
86      */

87     public int size() {
88         return size;
89     }
90
91     /**
92      * Is the container empty?
93      */

94     public boolean isEmpty() {
95         return size == 0;
96     }
97
98     /**
99      * Check if the arrays needs to grow some more.
100      */

101     private void resize() {
102         final int length = oids.length;
103         if (length == size) {
104             final int growTo = (length + 1) * GROW_SIZE;
105             final OID[] tmpOIDs = new OID[growTo];
106             final State[] tmpStates = new State[growTo];
107             final State[] tmpOStates = new State[growTo];
108
109             for (int i = 0; i < length; i++) {
110                 tmpOIDs[i] = oids[i];
111                 tmpStates[i] = states[i];
112                 tmpOStates[i] = origStates[i];
113             }
114
115             oids = tmpOIDs;
116             states = tmpStates;
117             origStates = tmpOStates;
118         }
119     }
120
121     /**
122      * Clear the container for reuse.
123      */

124     public void clear() {
125         if (size > breakPoint) {
126             oids = new OID[initialSize];
127             states = new State[initialSize];
128             origStates = new State[initialSize];
129         } else {
130             final int n = size;
131             for (int i = 0; i < n; i++) {
132                 oids[i] = null;
133                 states[i] = null;
134                 origStates[i] = null;
135             }
136         }
137         size = 0;
138         fullSort = false;
139         epcAll = false;
140         epcClassCount = 0;
141         epcClasses = null;
142         epcOids = null;
143     }
144
145     public void dump() {
146         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("StoreOIDStateContainer: ");
147         for (int i = 0; i < oids.length; i++) {
148             OID oid = oids[i];
149             if (oid == null) break;
150             sb.append("\nOID = " + oid.toSString());
151             sb.append("\nState = " + states[i]);
152             sb.append("\nOrigState = " + origStates[i]);
153             sb.append("\nNext");
154         }
155         System.out.println(sb.toString());
156     }
157
158     public void dumpEPC() {
159         System.out.println("StatesToStore.dumpEPC");
160         System.out.println("epcAll = " + epcAll);
161         System.out.println("epcOids = " + epcOids);
162         if (epcOids != null) {
163             System.out.println("epcOids.length = " + epcOids.length);
164         }
165         System.out.println("epcClasses = " + epcClasses);
166         if (epcClasses != null) {
167             System.out.println("epcClasses.length = " + epcClasses.length);
168         }
169         System.out.println("epcClassCount = " + epcClassCount);
170
171     }
172
173     public void writeExternal(OIDObjectOutput out) throws IOException {
174         out.writeBoolean(fullSort);
175         out.writeInt(size);
176         for (int i = 0; i < size; i++) {
177             State state = states[i];
178             OID oid = oids[i];
179             oid.resolve(state);
180             out.writeShort(state.getClassIndex());
181             out.writeWithoutCMD(oid);
182             state.writeExternal(out);
183             if (origStates[i] == null) {
184                 out.writeBoolean(false);
185             } else {
186                 out.writeBoolean(true);
187                 origStates[i].writeExternal(out);
188             }
189         }
190         writeExternalEpc(out);
191     }
192
193     public void readExternal(OIDObjectInput in) throws IOException,
194                 ClassNotFoundException JavaDoc {
195         jmd = in.getModelMetaData();
196         fullSort = in.readBoolean();
197         size = in.readInt();
198         oids = new OID[size];
199         states = new State[size];
200         origStates = new State[size];
201         for (int i = 0; i < size; i++) {
202             ClassMetaData cmd = jmd.classes[in.readShort()];
203             oids[i] = in.readOID(cmd);
204             states[i] = cmd.createState();
205             states[i].readExternal(in);
206             if (in.readBoolean()) {
207                 origStates[i] = cmd.createState();
208                 origStates[i].readExternal(in);
209             }
210         }
211         readExternalEpc(in);
212     }
213
214     private void writeExternalEpc(OIDObjectOutput out) throws IOException {
215         out.writeBoolean(epcAll);
216         if (epcOids == null) {
217             out.writeByte(0);
218         } else {
219             out.writeByte(1);
220             int n = 0;
221             for (; n < epcOids.length; ) {
222                 if (epcOids[n] == null) break;
223                 n++;
224             }
225             out.writeInt(n);
226             for (int i = 0; i < n; i++) {
227                 OID oid = epcOids[i];
228                 if (oid.isNew()) {
229                     throw BindingSupportImpl.getInstance().internal(
230                             "New oids should not be included for eviction");
231                 }
232                 out.write(oid);
233             }
234         }
235         if (epcClasses == null) {
236             out.writeByte(0);
237         } else {
238             out.writeByte(1);
239             out.writeByte(epcClasses.length);
240             for (int i = 0; i < epcClasses.length; i++) {
241                 out.writeInt(epcClasses[i]);
242             }
243         }
244         out.writeInt(epcClassCount);
245     }
246
247     private void readExternalEpc(OIDObjectInput in) throws IOException,
248             ClassNotFoundException JavaDoc {
249         epcAll = in.readBoolean();
250         if (in.readByte() != 0) {
251             int n = in.readInt();
252             OID[] oids = epcOids = new OID[n];
253             for (int i = 0; i < n; i++) {
254                 oids[i] = in.readOID();
255             }
256         }
257         if (in.readByte() != 0) {
258             int n = in.readInt();
259             int[] ia = epcClasses = new int[n];
260             for (int i = 0; i < n; i++) {
261                 ia[i] = in.readInt();
262             }
263         }
264         epcClassCount = in.readInt();
265     }
266
267     /**
268      * Iterate over the OIDs.
269      */

270     public Iterator JavaDoc iteratorForOIDs() {
271         return new Iter();
272     }
273
274     public class Iter implements Iterator JavaDoc {
275
276         private int pos;
277
278         public boolean hasNext() {
279             return pos < size - 1;
280         }
281
282         public Object JavaDoc next() {
283             return oids[pos++];
284         }
285
286         public void remove() {
287             throw new UnsupportedOperationException JavaDoc();
288         }
289
290     }
291
292 }
293
Popular Tags