1 2 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 ; 22 23 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; private boolean fullSort; 42 43 public boolean epcAll; public OID[] epcOids; public int[] epcClasses; public int epcClassCount; 50 51 public StatesToStore() { 52 } 53 54 public StatesToStore(ModelMetaData jmd) { 55 this.jmd = jmd; 56 } 57 58 66 public boolean isFullSortRequired() { 67 return fullSort; 68 } 69 70 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 87 public int size() { 88 return size; 89 } 90 91 94 public boolean isEmpty() { 95 return size == 0; 96 } 97 98 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 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 sb = new StringBuffer ("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 { 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 { 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 270 public Iterator iteratorForOIDs() { 271 return new Iter(); 272 } 273 274 public class Iter implements Iterator { 275 276 private int pos; 277 278 public boolean hasNext() { 279 return pos < size - 1; 280 } 281 282 public Object next() { 283 return oids[pos++]; 284 } 285 286 public void remove() { 287 throw new UnsupportedOperationException (); 288 } 289 290 } 291 292 } 293 | Popular Tags |