1 21 package com.db4o; 22 23 import com.db4o.inside.marshall.*; 24 import com.db4o.reflect.*; 25 26 30 public final class YapArrayN extends YapArray { 31 32 33 public YapArrayN(YapStream stream, TypeHandler4 a_handler, boolean a_isPrimitive) { 34 super(stream, a_handler, a_isPrimitive); 35 } 36 37 public final Object [] allElements(Object a_array) { 38 int[] dim = _reflectArray.dimensions(a_array); 39 Object [] flat = new Object [elementCount(dim)]; 40 _reflectArray.flatten(a_array, dim, 0, flat, 0); 41 return flat; 42 } 43 44 public final int elementCount(Transaction a_trans, YapReader a_bytes) { 45 return elementCount(readDimensions(a_trans, a_bytes, new ReflectClass[1])); 46 } 47 48 private final int elementCount(int[] a_dim) { 49 int elements = a_dim[0]; 50 for (int i = 1; i < a_dim.length; i++) { 51 elements = elements * a_dim[i]; 52 } 53 return elements; 54 } 55 56 public final byte identifier() { 57 return YapConst.YAPARRAYN; 58 } 59 60 public final int objectLength(Object a_object) { 61 int[] dim = _reflectArray.dimensions(a_object); 62 return YapConst.OBJECT_LENGTH 63 + (YapConst.INT_LENGTH * (2 + dim.length)) 64 + (elementCount(dim) * i_handler.linkLength()); 65 } 66 67 public int ownLength(Object obj){ 68 int[] dim = _reflectArray.dimensions(obj); 69 return YapConst.OBJECT_LENGTH 70 + (YapConst.INT_LENGTH * (2 + dim.length)); 71 } 72 73 public final Object read1(MarshallerFamily mf, YapWriter reader) throws CorruptionException { 74 75 if (Deploy.debug) { 76 reader.readBegin(identifier()); 77 } 78 79 Object [] ret = new Object [1]; 80 int[] dim = read1Create(reader.getTransaction(), reader, ret); 81 if(ret[0] != null){ 82 Object [] objects = new Object [elementCount(dim)]; 83 for (int i = 0; i < objects.length; i++) { 84 objects[i] = i_handler.read(mf, reader, true); 85 } 86 _reflectArray.shape(objects, 0, ret[0], dim, 0); 87 } 88 89 if (Deploy.debug) { 90 reader.readEnd(); 91 } 92 93 return ret[0]; 94 } 95 96 protected int readElementsDefrag(ReaderPair readers) { 97 int numDimensions=super.readElementsDefrag(readers); 98 int [] dimensions=new int[numDimensions]; 99 for (int i = 0; i < numDimensions; i++) { 100 dimensions[i]=readers.readInt(); 101 } 102 return elementCount(dimensions); 103 } 104 105 public final void read1Candidates(MarshallerFamily mf, YapReader reader, QCandidates candidates) { 106 if(Deploy.debug){ 107 reader.readBegin(identifier()); 108 } 109 110 Object [] ret = new Object [1]; 111 int[] dim = read1Create(candidates.i_trans, reader, ret); 112 if(ret[0] != null){ 113 int count = elementCount(dim); 114 for (int i = 0; i < count; i++) { 115 QCandidate qc = i_handler.readSubCandidate(mf, reader, candidates, true); 116 if(qc != null){ 117 candidates.addByIdentity(qc); 118 } 119 } 120 } 121 122 if (Deploy.debug) { 123 reader.readEnd(); 124 } 125 } 126 127 public final Object read1Query(Transaction a_trans, MarshallerFamily mf, YapReader a_bytes) throws CorruptionException { 128 129 if(Deploy.debug){ 130 a_bytes.readBegin(identifier()); 131 } 132 133 Object [] ret = new Object [1]; 134 int[] dim = read1Create(a_trans, a_bytes, ret); 135 if(ret[0] != null){ 136 Object [] objects = new Object [elementCount(dim)]; 137 for (int i = 0; i < objects.length; i++) { 138 objects[i] = i_handler.readQuery(a_trans, mf, true, a_bytes, true); 139 } 140 _reflectArray.shape(objects, 0, ret[0], dim, 0); 141 } 142 143 if (Deploy.debug) { 144 a_bytes.readEnd(); 145 } 146 147 return ret[0]; 148 } 149 150 private int[] read1Create(Transaction a_trans, YapReader a_bytes, Object [] obj) { 151 ReflectClass[] clazz = new ReflectClass[1]; 152 int[] dim = readDimensions(a_trans, a_bytes, clazz); 153 if (i_isPrimitive) { 154 obj[0] = a_trans.reflector().array().newInstance(i_handler.primitiveClassReflector(), dim); 155 } else { 156 if (clazz[0] != null) { 157 obj[0] = a_trans.reflector().array().newInstance(clazz[0], dim); 158 } 159 } 160 return dim; 161 } 162 163 private final int[] readDimensions(Transaction a_trans, YapReader a_bytes, ReflectClass[] clazz) { 164 int[] dim = new int[readElementsAndClass(a_trans, a_bytes, clazz)]; 165 for (int i = 0; i < dim.length; i++) { 166 dim[i] = a_bytes.readInt(); 167 } 168 return dim; 169 } 170 171 public final void writeNew1(Object obj, YapWriter writer) { 172 173 if (Deploy.debug) { 174 writer.writeBegin(identifier()); 175 } 176 177 int[] dim = _reflectArray.dimensions(obj); 178 writeClass(obj, writer); 179 writer.writeInt(dim.length); 180 for (int i = 0; i < dim.length; i++) { 181 writer.writeInt(dim[i]); 182 } 183 Object [] objects = allElements(obj); 184 185 MarshallerFamily mf = MarshallerFamily.current(); 186 187 for (int i = 0; i < objects.length; i++) { 188 i_handler.writeNew(mf, element(objects, i), false, writer, true, true); 189 } 190 191 if (Deploy.debug) { 192 writer.writeEnd(); 193 } 194 195 } 196 197 private Object element(Object a_array, int a_position) { 198 try { 199 return _reflectArray.get(a_array, a_position); 200 } catch (Exception e) { 201 return null; 202 } 203 } 204 } 205 | Popular Tags |