David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ |
| 21 | #define _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ 1 |
| 22 | |
| 23 | #include "TBinaryProtocol.h" |
| 24 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 25 | namespace apache { namespace thrift { namespace protocol { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 28 | * !!!WARNING!!! |
| 29 | * This class is still highly experimental. Incompatible changes |
| 30 | * WILL be made to it without notice. DO NOT USE IT YET unless |
| 31 | * you are coordinating your testing with the author. |
| 32 | * |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 33 | * The dense protocol is designed to use as little space as possible. |
| 34 | * |
| 35 | * There are two types of dense protocol instances. Standalone instances |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 36 | * are not used for RPC and just encoded and decode structures of |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 37 | * a predetermined type. Non-standalone instances are used for RPC. |
| 38 | * Currently, only standalone instances exist. |
| 39 | * |
| 40 | * To use a standalone dense protocol object, you must set the type_spec |
| 41 | * property (either in the constructor, or with setTypeSpec) to the local |
| 42 | * reflection TypeSpec of the structures you will write to (or read from) the |
| 43 | * protocol instance. |
| 44 | * |
| 45 | * BEST PRACTICES: |
| 46 | * - Never use optional for primitives or containers. |
| 47 | * - Only use optional for structures if they are very big and very rarely set. |
| 48 | * - All integers are variable-length, so you can use i64 without bloating. |
| 49 | * - NEVER EVER change the struct definitions IN ANY WAY without either |
| 50 | * changing your cache keys or talking to dreiss. |
| 51 | * |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 52 | * TODO(dreiss): New class write with old meta. |
| 53 | * |
| 54 | * We override all of TBinaryProtocol's methods. |
| 55 | * We inherit so that we can can explicitly call TBPs's primitive-writing |
| 56 | * methods within our versions. |
| 57 | * |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 58 | */ |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 59 | class TDenseProtocol |
| 60 | : public TVirtualProtocol<TDenseProtocol, TBinaryProtocol> { |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 61 | protected: |
| 62 | static const int32_t VERSION_MASK = 0xffff0000; |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 63 | // VERSION_1 (0x80010000) is taken by TBinaryProtocol. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 64 | static const int32_t VERSION_2 = 0x80020000; |
| 65 | |
| 66 | public: |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 67 | typedef apache::thrift::reflection::local::TypeSpec TypeSpec; |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 68 | static const int FP_PREFIX_LEN; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 69 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 70 | /** |
| 71 | * @param tran The transport to use. |
| 72 | * @param type_spec The TypeSpec of the structures using this protocol. |
| 73 | */ |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 74 | TDenseProtocol(boost::shared_ptr<TTransport> trans, |
| 75 | TypeSpec* type_spec = NULL) : |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 76 | TVirtualProtocol<TDenseProtocol, TBinaryProtocol>(trans), |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 77 | type_spec_(type_spec), |
| 78 | standalone_(true) |
| 79 | {} |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 80 | |
| 81 | void setTypeSpec(TypeSpec* type_spec) { |
| 82 | type_spec_ = type_spec; |
| 83 | } |
| 84 | TypeSpec* getTypeSpec() { |
| 85 | return type_spec_; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Writing functions. |
| 91 | */ |
| 92 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 93 | uint32_t writeMessageBegin(const std::string& name, |
| 94 | const TMessageType messageType, |
| 95 | const int32_t seqid); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 96 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 97 | uint32_t writeMessageEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 98 | |
| 99 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 100 | uint32_t writeStructBegin(const char* name); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 101 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 102 | uint32_t writeStructEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 103 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 104 | uint32_t writeFieldBegin(const char* name, |
| 105 | const TType fieldType, |
| 106 | const int16_t fieldId); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 107 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 108 | uint32_t writeFieldEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 109 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 110 | uint32_t writeFieldStop(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 111 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 112 | uint32_t writeMapBegin(const TType keyType, |
| 113 | const TType valType, |
| 114 | const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 115 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 116 | uint32_t writeMapEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 117 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 118 | uint32_t writeListBegin(const TType elemType, const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 119 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 120 | uint32_t writeListEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 121 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 122 | uint32_t writeSetBegin(const TType elemType, const uint32_t size); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 123 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 124 | uint32_t writeSetEnd(); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 125 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 126 | uint32_t writeBool(const bool value); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 127 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 128 | uint32_t writeByte(const int8_t byte); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 129 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 130 | uint32_t writeI16(const int16_t i16); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 131 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 132 | uint32_t writeI32(const int32_t i32); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 133 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 134 | uint32_t writeI64(const int64_t i64); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 135 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 136 | uint32_t writeDouble(const double dub); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 137 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 138 | uint32_t writeString(const std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 139 | |
David Reiss | 6806fb8 | 2010-10-06 17:09:52 +0000 | [diff] [blame] | 140 | uint32_t writeBinary(const std::string& str); |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 141 | |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * Helper writing functions (don't do state transitions). |
| 145 | */ |
| 146 | inline uint32_t subWriteI32(const int32_t i32); |
| 147 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 148 | inline uint32_t subWriteString(const std::string& str); |
| 149 | |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 150 | uint32_t subWriteBool(const bool value) { |
| 151 | return TBinaryProtocol::writeBool(value); |
| 152 | } |
| 153 | |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * Reading functions |
| 157 | */ |
| 158 | |
| 159 | uint32_t readMessageBegin(std::string& name, |
| 160 | TMessageType& messageType, |
| 161 | int32_t& seqid); |
| 162 | |
| 163 | uint32_t readMessageEnd(); |
| 164 | |
| 165 | uint32_t readStructBegin(std::string& name); |
| 166 | |
| 167 | uint32_t readStructEnd(); |
| 168 | |
| 169 | uint32_t readFieldBegin(std::string& name, |
| 170 | TType& fieldType, |
| 171 | int16_t& fieldId); |
| 172 | |
| 173 | uint32_t readFieldEnd(); |
| 174 | |
| 175 | uint32_t readMapBegin(TType& keyType, |
| 176 | TType& valType, |
| 177 | uint32_t& size); |
| 178 | |
| 179 | uint32_t readMapEnd(); |
| 180 | |
| 181 | uint32_t readListBegin(TType& elemType, |
| 182 | uint32_t& size); |
| 183 | |
| 184 | uint32_t readListEnd(); |
| 185 | |
| 186 | uint32_t readSetBegin(TType& elemType, |
| 187 | uint32_t& size); |
| 188 | |
| 189 | uint32_t readSetEnd(); |
| 190 | |
| 191 | uint32_t readBool(bool& value); |
David Reiss | 8dfc732 | 2010-10-06 17:09:58 +0000 | [diff] [blame] | 192 | // Provide the default readBool() implementation for std::vector<bool> |
| 193 | using TVirtualProtocol<TDenseProtocol, TBinaryProtocol>::readBool; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 194 | |
| 195 | uint32_t readByte(int8_t& byte); |
| 196 | |
| 197 | uint32_t readI16(int16_t& i16); |
| 198 | |
| 199 | uint32_t readI32(int32_t& i32); |
| 200 | |
| 201 | uint32_t readI64(int64_t& i64); |
| 202 | |
| 203 | uint32_t readDouble(double& dub); |
| 204 | |
| 205 | uint32_t readString(std::string& str); |
| 206 | |
David Reiss | c005b1b | 2008-02-15 01:38:18 +0000 | [diff] [blame] | 207 | uint32_t readBinary(std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Helper reading functions (don't do state transitions). |
| 211 | */ |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 212 | inline uint32_t subReadI32(int32_t& i32); |
| 213 | |
| 214 | inline uint32_t subReadString(std::string& str); |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 215 | |
| 216 | uint32_t subReadBool(bool& value) { |
| 217 | return TBinaryProtocol::readBool(value); |
| 218 | } |
| 219 | |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 220 | |
| 221 | private: |
| 222 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 223 | // Implementation functions, documented in the .cpp. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 224 | inline void checkTType(const TType ttype); |
| 225 | inline void stateTransition(); |
| 226 | |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 227 | // Read and write variable-length integers. |
| 228 | // Uses the same technique as the MIDI file format. |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 229 | inline uint32_t vlqRead(uint64_t& vlq); |
| 230 | inline uint32_t vlqWrite(uint64_t vlq); |
David Reiss | e67c0e6 | 2007-09-07 01:34:12 +0000 | [diff] [blame] | 231 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 232 | // Called before throwing an exception to make the object reusable. |
| 233 | void resetState() { |
| 234 | ts_stack_.clear(); |
| 235 | idx_stack_.clear(); |
| 236 | mkv_stack_.clear(); |
| 237 | } |
| 238 | |
| 239 | // TypeSpec of the top-level structure to write, |
| 240 | // for standalone protocol objects. |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 241 | TypeSpec* type_spec_; |
| 242 | |
| 243 | std::vector<TypeSpec*> ts_stack_; // TypeSpec stack. |
| 244 | std::vector<int> idx_stack_; // InDeX stack. |
| 245 | std::vector<bool> mkv_stack_; // Map Key/Vlue stack. |
| 246 | // True = key, False = value. |
| 247 | |
David Reiss | ce161a9 | 2007-09-11 22:09:42 +0000 | [diff] [blame] | 248 | // True iff this is a standalone instance (no RPC). |
| 249 | bool standalone_; |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 252 | }}} // apache::thrift::protocol |
David Reiss | 4e7530d | 2007-09-04 21:49:53 +0000 | [diff] [blame] | 253 | |
| 254 | #endif // #ifndef _THRIFT_PROTOCOL_TDENSEPROTOCOL_H_ |