1 2 12 package com.versant.core.common; 13 14 import com.versant.core.metadata.FetchGroup; 15 import com.versant.core.metadata.ModelMetaData; 16 import com.versant.core.metadata.ClassMetaData; 17 import com.versant.core.server.*; 18 19 import java.io.*; 20 import java.util.*; 21 22 import com.versant.core.storagemanager.ApplicationContext; 23 import com.versant.core.util.OIDObjectInput; 24 import com.versant.core.util.OIDObjectOutput; 25 import com.versant.core.util.FastExternalizable; 26 27 31 public final class StatesReturned implements StateContainer, FastExternalizable { 32 33 private EntrySet data = new EntrySet(); 34 private transient ApplicationContext context; 35 private OID directOid; 37 38 public transient StatesReturned next; 39 40 public StatesReturned() { 41 } 42 43 public StatesReturned(ApplicationContext context) { 44 setContext(context); 45 } 46 47 public ApplicationContext getContext() { 48 return context; 49 } 50 51 public void setContext(ApplicationContext context) { 52 this.context = context; 53 } 54 55 public int size() { 56 return data.size(); 57 } 58 59 63 public Iterator iterator() { 64 return data.iterator(); 65 } 66 67 70 public Iterator iteratorForOIDs() { 71 return data.newKeyIterator(); 72 } 73 74 78 public void visited(OID oid) { 79 addImp(oid, null, false); 80 } 81 82 public void ensureDirect(Object [] oids) { 83 if (oids == null) return; 84 for (int i = 0; i < oids.length; i++) { 85 if (oids[i] == null) break; 86 addImp((OID)oids[i], null, true); 87 } 88 } 89 90 97 public boolean isStateRequired(OID oid, FetchGroup fetchGroup) { 98 if (data.contains(oid)) { 99 EntrySet.Entry e = data.get(oid); 100 State s = (State) e.getValue(); 101 if (s == null) { 102 return false; 103 } 104 return !s.containsFetchGroup(fetchGroup); 105 } 106 return context == null || context.isStateRequired(oid, fetchGroup); 107 } 108 109 112 public void addState(OID oid, State state) { 113 if (Debug.DEBUG) { 114 if (state == null) { 115 throw BindingSupportImpl.getInstance().internal("Null state not allowed"); 116 } 117 } 118 addIndirect(oid, state); 119 } 120 121 125 public State add(OID key, State value) { 126 addImp(key, value, true); 127 return value; 128 } 129 130 private void addImp(OID oid, State state, boolean directFlag) { 131 if (oid == null) { 132 throw BindingSupportImpl.getInstance().illegalArgument( 133 "null oid not supported"); 134 } 135 if (directFlag && directOid == null) { 136 directOid = oid; 137 } 138 139 EntrySet.Entry e = data.get(oid); 140 if (e == null) { 141 data.add(oid, state, directFlag); 142 } else { 143 State current = (State) e.getValue(); 144 if (current == null) { 145 e.setValue(state); 146 } else if (state != null && current != state) { 147 current.updateNonFilled(state); 148 } 149 if (directFlag) e.setDirect(true); 150 } 151 } 152 153 public void addIndirect(OID key, State value) { 154 addImp(key, value, false); 155 } 156 157 161 public boolean containsKey(Object key) { 162 return data.contains((OID) key); 163 } 164 165 168 public State get(Object key) { 169 EntrySet.Entry e = data.get((OID) key); 170 if (e == null) return null; 171 return (State) e.getValue(); 172 } 173 174 public EntrySet.Entry getEntry(Object key) { 175 return data.get((OID) key); 176 } 177 178 public void clear() { 179 data.clear(); 180 directOid = null; 181 } 182 183 186 public OID getDirectOID() { 187 return directOid; 188 } 189 190 public void addIndirectOIDs(CachedQueryResult cacheContainer) { 191 if (cacheContainer.indirectOIDs == null) { 192 cacheContainer.indirectOIDs = new OIDArray(); 193 } 194 OIDArray indirectOIDs = cacheContainer.indirectOIDs; 195 196 for (Iterator i = iteratorForOIDs(); i.hasNext(); ) { 197 OID oid = (OID)i.next(); 198 indirectOIDs.add(oid); 199 } 200 } 201 202 public void writeExternal(OIDObjectOutput out) throws IOException { 203 int n = data.size(); 204 out.writeInt(n); 205 for (Iterator i = iterator(); i.hasNext(); ) { 206 EntrySet.Entry e = (EntrySet.Entry)i.next(); 207 OID oid = (OID)e.getKey(); 208 State state = (State)e.getValue(); 209 if (state == null) { 210 out.writeShort(-1); 211 out.write(oid); 212 } else if (state == NULLState.NULL_STATE) { 213 out.writeShort(-2); 214 out.write(oid); 215 } else { 216 out.writeShort(state.getClassIndex()); 217 oid.resolve(state); 218 out.writeWithoutCMD(oid); 219 state.writeExternal(out); 220 } 221 out.writeBoolean(e.direct); 222 } 223 out.write(directOid); 224 } 225 226 public void readExternal(OIDObjectInput in) throws IOException, 227 ClassNotFoundException { 228 ModelMetaData jmd = in.getModelMetaData(); 229 int n = in.readInt(); 230 for (int i = 0; i < n; i++) { 231 OID oid; 232 State state; 233 int ci = in.readShort(); 234 switch (ci) { 235 case -1: 236 oid = in.readOID(); 237 state = null; 238 break; 239 case -2: 240 oid = in.readOID(); 241 state = NULLState.NULL_STATE; 242 break; 243 default: 244 ClassMetaData cmd = jmd.classes[ci]; 245 oid = in.readOID(cmd); 246 state = cmd.createState(); 247 state.readExternal(in); 248 }; 249 data.add(oid, state, in.readBoolean()); 250 } 251 directOid = in.readOID(); 252 } 253 254 public void dump() { 255 System.out.println("-- StatesReturned"); 256 System.out.println("Map:"); 257 for (Iterator i = iterator(); i.hasNext(); ) { 258 EntrySet.Entry e = (EntrySet.Entry)i.next(); 259 OID oid = (OID)e.getKey(); 260 State state = (State)e.getValue(); 261 System.out.println(oid.toStringImp() + " -> " + state + " direct " + e.isDirect()); 262 } 263 System.out.println("---"); 264 } 265 266 } 267 | Popular Tags |