[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "typeIDs.hh" |
---|
| 20 | |
---|
| 21 | using namespace hadoop; |
---|
| 22 | |
---|
| 23 | void TypeID::serialize(::hadoop::OArchive& a_, const char* tag) const |
---|
| 24 | { |
---|
| 25 | a_.serialize(typeVal, tag); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | bool TypeID::operator==(const TypeID& peer_) const |
---|
| 29 | { |
---|
| 30 | return (this->typeVal == peer_.typeVal); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void TypeID::print(int space) const |
---|
| 34 | { |
---|
| 35 | for (int i=0; i<space; i++) { |
---|
| 36 | printf(" "); |
---|
| 37 | } |
---|
| 38 | printf("typeID(%lx) = %d\n", (long)this, typeVal); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | /*StructTypeID::StructTypeID(const char *p): TypeID(RIOTYPE_STRUCT) |
---|
| 43 | { |
---|
| 44 | pName = new std::string(p); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | StructTypeID::StructTypeID(std::string* p): TypeID(RIOTYPE_STRUCT) |
---|
| 48 | { |
---|
| 49 | this->pName = p; |
---|
| 50 | }*/ |
---|
| 51 | |
---|
| 52 | StructTypeID::StructTypeID(const std::vector<FieldTypeInfo*>& vec) : |
---|
| 53 | TypeID(RIOTYPE_STRUCT) |
---|
| 54 | { |
---|
| 55 | // we need to copy object clones into our own vector |
---|
| 56 | for (unsigned int i=0; i<vec.size(); i++) { |
---|
| 57 | typeInfos.push_back(vec[i]->clone()); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /*StructTypeID::StructTypeID(const StructTypeID& ti) : |
---|
| 62 | TypeID(RIOTYPE_STRUCT) |
---|
| 63 | { |
---|
| 64 | // we need to copy object clones into our own vector |
---|
| 65 | for (unsigned int i=0; i<ti.typeInfos.size(); i++) { |
---|
| 66 | typeInfos.push_back(ti.typeInfos[i]->clone()); |
---|
| 67 | } |
---|
| 68 | } */ |
---|
| 69 | |
---|
| 70 | StructTypeID::~StructTypeID() |
---|
| 71 | { |
---|
| 72 | for (unsigned int i=0; i<typeInfos.size(); i++) { |
---|
| 73 | delete typeInfos[i]; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void StructTypeID::add(FieldTypeInfo *pti) |
---|
| 78 | { |
---|
| 79 | typeInfos.push_back(pti); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // return the StructTypeiD, if any, of the given field |
---|
| 83 | StructTypeID* StructTypeID::findStruct(const char *pStructName) |
---|
| 84 | { |
---|
| 85 | // walk through the list, searching. Not the most efficient way, but this |
---|
| 86 | // in intended to be used rarely, so we keep it simple. |
---|
| 87 | // As an optimization, we can keep a hashmap of record name to its RTI, for later. |
---|
| 88 | for (unsigned int i=0; i<typeInfos.size(); i++) { |
---|
| 89 | if ((0 == typeInfos[i]->getFieldID()->compare(pStructName)) && |
---|
| 90 | (typeInfos[i]->getTypeID()->getTypeVal()==RIOTYPE_STRUCT)) { |
---|
| 91 | return (StructTypeID*)(typeInfos[i]->getTypeID()->clone()); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void StructTypeID::serialize(::hadoop::OArchive& a_, const char* tag) const |
---|
| 98 | { |
---|
| 99 | a_.serialize(typeVal, tag); |
---|
| 100 | serializeRest(a_, tag); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /* |
---|
| 104 | * Writes rest of the struct (excluding type value). |
---|
| 105 | * As an optimization, this method is directly called by RTI |
---|
| 106 | * for the top level record so that we don't write out the byte |
---|
| 107 | * indicating that this is a struct (since top level records are |
---|
| 108 | * always structs). |
---|
| 109 | */ |
---|
| 110 | void StructTypeID::serializeRest(::hadoop::OArchive& a_, const char* tag) const |
---|
| 111 | { |
---|
| 112 | a_.serialize((int32_t)typeInfos.size(), tag); |
---|
| 113 | for (unsigned int i=0; i<typeInfos.size(); i++) { |
---|
| 114 | typeInfos[i]->serialize(a_, tag); |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | /* |
---|
| 120 | * deserialize ourselves. Called by RTI. |
---|
| 121 | */ |
---|
| 122 | void StructTypeID::deserialize(::hadoop::IArchive& a_, const char* tag) |
---|
| 123 | { |
---|
| 124 | // number of elements |
---|
| 125 | int numElems; |
---|
| 126 | a_.deserialize(numElems, tag); |
---|
| 127 | for (int i=0; i<numElems; i++) { |
---|
| 128 | typeInfos.push_back(genericReadTypeInfo(a_, tag)); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // generic reader: reads the next TypeInfo object from stream and returns it |
---|
| 133 | FieldTypeInfo* StructTypeID::genericReadTypeInfo(::hadoop::IArchive& a_, const char* tag) |
---|
| 134 | { |
---|
| 135 | // read name of field |
---|
| 136 | std::string* pName = new std::string(); |
---|
| 137 | a_.deserialize(*pName, tag); |
---|
| 138 | TypeID* pti = genericReadTypeID(a_, tag); |
---|
| 139 | return new FieldTypeInfo(pName, pti); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | // generic reader: reads the next TypeID object from stream and returns it |
---|
| 143 | TypeID* StructTypeID::genericReadTypeID(::hadoop::IArchive& a_, const char* tag) |
---|
| 144 | { |
---|
| 145 | int8_t typeVal; |
---|
| 146 | a_.deserialize(typeVal, tag); |
---|
| 147 | switch(typeVal) { |
---|
| 148 | case RIOTYPE_BOOL: |
---|
| 149 | case RIOTYPE_BUFFER: |
---|
| 150 | case RIOTYPE_BYTE: |
---|
| 151 | case RIOTYPE_DOUBLE: |
---|
| 152 | case RIOTYPE_FLOAT: |
---|
| 153 | case RIOTYPE_INT: |
---|
| 154 | case RIOTYPE_LONG: |
---|
| 155 | case RIOTYPE_STRING: |
---|
| 156 | return new TypeID(typeVal); |
---|
| 157 | case RIOTYPE_STRUCT: |
---|
| 158 | { |
---|
| 159 | StructTypeID* pstID = new StructTypeID(); |
---|
| 160 | int numElems; |
---|
| 161 | a_.deserialize(numElems, tag); |
---|
| 162 | for (int i=0; i<numElems; i++) { |
---|
| 163 | pstID->add(genericReadTypeInfo(a_, tag)); |
---|
| 164 | } |
---|
| 165 | return pstID; |
---|
| 166 | } |
---|
| 167 | case RIOTYPE_VECTOR: |
---|
| 168 | { |
---|
| 169 | TypeID* pti = genericReadTypeID(a_, tag); |
---|
| 170 | return new VectorTypeID(pti); |
---|
| 171 | } |
---|
| 172 | case RIOTYPE_MAP: |
---|
| 173 | { |
---|
| 174 | TypeID* ptiKey = genericReadTypeID(a_, tag); |
---|
| 175 | TypeID* ptiValue = genericReadTypeID(a_, tag); |
---|
| 176 | return new MapTypeID(ptiKey, ptiValue); |
---|
| 177 | } |
---|
| 178 | default: |
---|
| 179 | // shouldn't be here |
---|
| 180 | return NULL; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | void StructTypeID::print(int space) const |
---|
| 185 | { |
---|
| 186 | TypeID::print(space); |
---|
| 187 | for (int i=0; i<space; i++) { |
---|
| 188 | printf(" "); |
---|
| 189 | } |
---|
| 190 | printf("StructTypeInfo(%lx): \n", (long)&typeInfos); |
---|
| 191 | for (unsigned int i=0; i<typeInfos.size(); i++) { |
---|
| 192 | typeInfos[i]->print(space+2); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | VectorTypeID::~VectorTypeID() |
---|
| 198 | { |
---|
| 199 | delete ptiElement; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | VectorTypeID::VectorTypeID(const VectorTypeID& ti): TypeID(RIOTYPE_VECTOR) |
---|
| 203 | { |
---|
| 204 | ptiElement = ti.ptiElement->clone(); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | void VectorTypeID::serialize(::hadoop::OArchive& a_, const char* tag) const |
---|
| 208 | { |
---|
| 209 | a_.serialize(typeVal, tag); |
---|
| 210 | ptiElement->serialize(a_, tag); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | bool VectorTypeID::operator==(const TypeID& peer_) const |
---|
| 214 | { |
---|
| 215 | if (typeVal != peer_.getTypeVal()) { |
---|
| 216 | return false; |
---|
| 217 | } |
---|
| 218 | // this must be a vector type id |
---|
| 219 | return (*ptiElement) == (*((VectorTypeID&)peer_).ptiElement); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void VectorTypeID::print(int space) const |
---|
| 223 | { |
---|
| 224 | TypeID::print(space); |
---|
| 225 | for (int i=0; i<space; i++) { |
---|
| 226 | printf(" "); |
---|
| 227 | } |
---|
| 228 | printf("VectorTypeInfo(%lx): \n", (long)this); |
---|
| 229 | ptiElement->print(space+2); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | MapTypeID::~MapTypeID() |
---|
| 234 | { |
---|
| 235 | delete ptiKey; |
---|
| 236 | delete ptiValue; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | MapTypeID::MapTypeID(const MapTypeID& ti): TypeID(RIOTYPE_MAP) |
---|
| 240 | { |
---|
| 241 | ptiKey = ti.ptiKey->clone(); |
---|
| 242 | ptiValue = ti.ptiValue->clone(); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void MapTypeID::serialize(::hadoop::OArchive& a_, const char* tag) const |
---|
| 246 | { |
---|
| 247 | a_.serialize(typeVal, tag); |
---|
| 248 | ptiKey->serialize(a_, tag); |
---|
| 249 | ptiValue->serialize(a_, tag); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | bool MapTypeID::operator==(const TypeID& peer_) const |
---|
| 253 | { |
---|
| 254 | if (typeVal != peer_.getTypeVal()) { |
---|
| 255 | return false; |
---|
| 256 | } |
---|
| 257 | // this must be a map type id |
---|
| 258 | MapTypeID& mti = (MapTypeID&) peer_; |
---|
| 259 | if (!(*ptiKey == *(mti.ptiKey))) { |
---|
| 260 | return false; |
---|
| 261 | } |
---|
| 262 | return ((*ptiValue == *(mti.ptiValue))); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | void MapTypeID::print(int space) const |
---|
| 266 | { |
---|
| 267 | TypeID::print(space); |
---|
| 268 | for (int i=0; i<space; i++) { |
---|
| 269 | printf(" "); |
---|
| 270 | } |
---|
| 271 | printf("MapTypeInfo(%lx): \n", (long)this); |
---|
| 272 | ptiKey->print(space+2); |
---|
| 273 | ptiValue->print(space+2); |
---|
| 274 | } |
---|