|
| 1 | +package com.xxdb.data; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Objects; |
| 8 | +import com.xxdb.io.ExtendedDataInput; |
| 9 | +import com.xxdb.io.ExtendedDataOutput; |
| 10 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 11 | + |
| 12 | +public class BasicIotAnyVector extends AbstractVector { |
| 13 | + |
| 14 | + private Map<Integer, Entity> subVector; |
| 15 | + private BasicIntVector indexsDataType; |
| 16 | + private BasicIntVector indexs; |
| 17 | + |
| 18 | + protected BasicIotAnyVector(Entity[] array) { |
| 19 | + super(DATA_FORM.DF_VECTOR); |
| 20 | + subVector = new HashMap<>(); |
| 21 | + indexsDataType = new BasicIntVector(0); |
| 22 | + indexs = new BasicIntVector(0); |
| 23 | + for (int i = 0; i < array.length; i++) { |
| 24 | + subVector.put(array[i].getDataType().getValue(), array[i]); |
| 25 | + for (int j = 0; j < array[i].rows(); j++ ) { |
| 26 | + indexsDataType.add(array[i].getDataType().getValue()); |
| 27 | + indexs.add(j); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + protected BasicIotAnyVector(ExtendedDataInput in) throws IOException { |
| 33 | + super(DATA_FORM.DF_VECTOR); |
| 34 | + int rows = in.readInt(); |
| 35 | + int cols = in.readInt(); // 1 |
| 36 | + long size = in.readLong(); |
| 37 | + |
| 38 | + indexsDataType = new BasicIntVector(0); |
| 39 | + indexs = new BasicIntVector(0); |
| 40 | + for(long i = 0; i < size; i++){ |
| 41 | + int type = in.readInt(); |
| 42 | + int idx = in.readInt(); |
| 43 | + indexsDataType.add(type); |
| 44 | + indexs.add(idx); |
| 45 | + } |
| 46 | + |
| 47 | + int typeSize = in.readInt(); |
| 48 | + subVector = new HashMap<>(); |
| 49 | + for (int i = 0; i < typeSize; i++) { |
| 50 | + short flag = in.readShort(); |
| 51 | + int form = flag>>8; |
| 52 | + int type = flag & 0xff; |
| 53 | + boolean extended = type >= 128; |
| 54 | + if(type >= 128) |
| 55 | + type -= 128; |
| 56 | + Entity obj = BasicEntityFactory.instance().createEntity(DATA_FORM.values()[form], DATA_TYPE.valueOf(type), in, extended); |
| 57 | + subVector.put(type, obj); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public Entity get(int index){ |
| 62 | + BasicInt dataType = (BasicInt) indexsDataType.get(index); |
| 63 | + return subVector.get(dataType.getInt()); |
| 64 | + } |
| 65 | + |
| 66 | + public Vector getSubVector(int[] indices){ |
| 67 | + throw new RuntimeException("BasicIotAnyVector.getSubVector not supported."); |
| 68 | + } |
| 69 | + |
| 70 | + public void set(int index, Entity value) throws Exception { |
| 71 | + throw new RuntimeException("BasicIotAnyVector.set not supported."); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Vector combine(Vector vector) { |
| 76 | + throw new UnsupportedOperationException(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean isNull(int index) { |
| 81 | + throw new RuntimeException("BasicIotAnyVector.isNull not supported."); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void setNull(int index) { |
| 86 | + throw new RuntimeException("BasicIotAnyVector.setNull not supported."); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public DATA_CATEGORY getDataCategory() { |
| 91 | + return Entity.DATA_CATEGORY.MIXED; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public DATA_TYPE getDataType() { |
| 96 | + return Entity.DATA_TYPE.DT_ANYIOT; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int rows() { |
| 101 | + return indexs.rows(); |
| 102 | + } |
| 103 | + |
| 104 | + @JsonIgnore |
| 105 | + @Override |
| 106 | + public int getUnitLength(){ |
| 107 | + throw new RuntimeException("IotAnyVector.getUnitLength not supported."); |
| 108 | + } |
| 109 | + |
| 110 | + public void addRange(Object[] valueList) { |
| 111 | + throw new RuntimeException("IotAnyVector not support addRange"); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void Append(Scalar value) { |
| 116 | + throw new RuntimeException("IotAnyVector not support append"); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void Append(Vector value) { |
| 121 | + throw new RuntimeException("IotAnyVector not support append"); |
| 122 | + } |
| 123 | + |
| 124 | + public String getString(){ |
| 125 | + StringBuilder sb = new StringBuilder("["); |
| 126 | + for (Entity value : subVector.values()) |
| 127 | + sb.append(value.getString()).append(","); |
| 128 | + |
| 129 | + sb.setLength(sb.length() - 1); |
| 130 | + sb.append("]"); |
| 131 | + |
| 132 | + return sb.toString(); |
| 133 | + } |
| 134 | + |
| 135 | + public Class<?> getElementClass(){ |
| 136 | + return Entity.class; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void serialize(int start, int count, ExtendedDataOutput out) throws IOException { |
| 141 | + throw new RuntimeException("BasicIotAnyVector.serialize not supported."); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int serialize(int indexStart, int offect, int targetNumElement, AbstractVector.NumElementAndPartial numElementAndPartial, ByteBuffer out) throws IOException{ |
| 146 | + throw new RuntimeException("BasicIotAnyVector.serialize not supported."); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void writeVectorToOutputStream(ExtendedDataOutput out) throws IOException { |
| 151 | + long size = indexs.rows(); |
| 152 | + out.writeLong(size); |
| 153 | + |
| 154 | + for (int i = 0; i < size; i++) { |
| 155 | + out.writeInt(indexsDataType.getInt(i)); |
| 156 | + out.writeInt(indexs.getInt(i)); |
| 157 | + } |
| 158 | + |
| 159 | + out.writeInt(subVector.size()); |
| 160 | + |
| 161 | + for (Entity value : subVector.values()) { |
| 162 | + if (Objects.nonNull(value)) |
| 163 | + value.write(out); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public int asof(Scalar value) { |
| 169 | + throw new RuntimeException("BasicAnyVector.asof not supported."); |
| 170 | + } |
| 171 | +} |
0 commit comments