WritableUtils.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "lib/commons.h"
  19. #include "util/StringUtil.h"
  20. #include "util/WritableUtils.h"
  21. namespace NativeTask {
  22. KeyValueType JavaClassToKeyValueType(const std::string & clazz) {
  23. if (clazz == "org.apache.hadoop.io.Text") {
  24. return TextType;
  25. }
  26. if (clazz == "org.apache.hadoop.io.BytesWritable") {
  27. return BytesType;
  28. }
  29. if (clazz == "org.apache.hadoop.io.ByteWritable") {
  30. return ByteType;
  31. }
  32. if (clazz == "org.apache.hadoop.io.BooleanWritable") {
  33. return BoolType;
  34. }
  35. if (clazz == "org.apache.hadoop.io.IntWritable") {
  36. return IntType;
  37. }
  38. if (clazz == "org.apache.hadoop.io.LongWritable") {
  39. return LongType;
  40. }
  41. if (clazz == "org.apache.hadoop.io.FloatWritable") {
  42. return FloatType;
  43. }
  44. if (clazz == "org.apache.hadoop.io.DoubleWritable") {
  45. return DoubleType;
  46. }
  47. if (clazz == "org.apache.hadoop.io.MD5Hash") {
  48. return MD5HashType;
  49. }
  50. if (clazz == "org.apache.hadoop.io.VIntWritable") {
  51. return VIntType;
  52. }
  53. if (clazz == "org.apache.hadoop.io.VLongWritable") {
  54. return VLongType;
  55. }
  56. return UnknownType;
  57. }
  58. int64_t WritableUtils::ReadVLongInner(const char * pos, uint32_t & len) {
  59. bool neg = *pos < -120;
  60. len = neg ? (-119 - *pos) : (-111 - *pos);
  61. const char * end = pos + len;
  62. int64_t value = 0;
  63. while (++pos < end) {
  64. value = (value << 8) | *(uint8_t*)pos;
  65. }
  66. return neg ? (value ^ -1LL) : value;
  67. }
  68. uint32_t WritableUtils::GetVLongSizeInner(int64_t value) {
  69. if (value < 0) {
  70. value ^= -1L; // take one's complement'
  71. }
  72. if (value < (1LL << 8)) {
  73. return 2;
  74. } else if (value < (1LL << 16)) {
  75. return 3;
  76. } else if (value < (1LL << 24)) {
  77. return 4;
  78. } else if (value < (1LL << 32)) {
  79. return 5;
  80. } else if (value < (1LL << 40)) {
  81. return 6;
  82. } else if (value < (1LL << 48)) {
  83. return 7;
  84. } else if (value < (1LL << 56)) {
  85. return 8;
  86. } else {
  87. return 9;
  88. }
  89. }
  90. void WritableUtils::WriteVLongInner(int64_t v, char * pos, uint32_t & len) {
  91. char base;
  92. if (v >= 0) {
  93. base = -113;
  94. } else {
  95. v ^= -1L; // take one's complement
  96. base = -121;
  97. }
  98. uint64_t value = v;
  99. if (value < (1 << 8)) {
  100. *(pos++) = base;
  101. *(uint8_t*)(pos) = value;
  102. len = 2;
  103. } else if (value < (1 << 16)) {
  104. *(pos++) = base - 1;
  105. *(uint8_t*)(pos++) = value >> 8;
  106. *(uint8_t*)(pos) = value;
  107. len = 3;
  108. } else if (value < (1 << 24)) {
  109. *(pos++) = base - 2;
  110. *(uint8_t*)(pos++) = value >> 16;
  111. *(uint8_t*)(pos++) = value >> 8;
  112. *(uint8_t*)(pos) = value;
  113. len = 4;
  114. } else if (value < (1ULL << 32)) {
  115. *(pos++) = base - 3;
  116. *(uint32_t*)(pos) = hadoop_be32toh((uint32_t)value);
  117. len = 5;
  118. } else if (value < (1ULL << 40)) {
  119. *(pos++) = base - 4;
  120. *(uint32_t*)(pos) = hadoop_be32toh((uint32_t)(value >> 8));
  121. *(uint8_t*)(pos + 4) = value;
  122. len = 6;
  123. } else if (value < (1ULL << 48)) {
  124. *(pos++) = base - 5;
  125. *(uint32_t*)(pos) = hadoop_be32toh((uint32_t)(value >> 16));
  126. *(uint8_t*)(pos + 4) = value >> 8;
  127. *(uint8_t*)(pos + 5) = value;
  128. len = 7;
  129. } else if (value < (1ULL << 56)) {
  130. *(pos++) = base - 6;
  131. *(uint32_t*)(pos) = hadoop_be32toh((uint32_t)(value >> 24));
  132. *(uint8_t*)(pos + 4) = value >> 16;
  133. *(uint8_t*)(pos + 5) = value >> 8;
  134. *(uint8_t*)(pos + 6) = value;
  135. len = 8;
  136. } else {
  137. *(pos++) = base - 7;
  138. *(uint64_t*)pos = hadoop_be64toh(value);
  139. len = 9;
  140. }
  141. }
  142. // Stream interfaces
  143. int64_t WritableUtils::ReadVLong(InputStream * stream) {
  144. char buff[10];
  145. if (stream->read(buff, 1) != 1) {
  146. THROW_EXCEPTION(IOException, "ReadVLong reach EOF");
  147. }
  148. uint32_t len = DecodeVLongSize(buff);
  149. if (len > 1) {
  150. len--;
  151. if (stream->readFully(buff + 1, len) != (int32_t)len) {
  152. THROW_EXCEPTION(IOException, "ReadVLong reach EOF");
  153. }
  154. }
  155. return ReadVLong(buff, len);
  156. }
  157. int64_t WritableUtils::ReadLong(InputStream * stream) {
  158. int64_t ret;
  159. if (stream->readFully(&ret, 8) != 8) {
  160. THROW_EXCEPTION(IOException, "ReadLong reach EOF");
  161. }
  162. return (int64_t)hadoop_be64toh(ret);
  163. }
  164. int32_t WritableUtils::ReadInt(InputStream * stream) {
  165. int32_t ret;
  166. if (stream->readFully(&ret, 4) != 4) {
  167. THROW_EXCEPTION(IOException, "ReadInt reach EOF");
  168. }
  169. return (int32_t)hadoop_be32toh(ret);
  170. }
  171. int16_t WritableUtils::ReadShort(InputStream * stream) {
  172. uint16_t ret;
  173. if (stream->readFully(&ret, 2) != 2) {
  174. THROW_EXCEPTION(IOException, "ReadShort reach EOF");
  175. }
  176. return (int16_t)((ret >> 8) | (ret << 8));
  177. }
  178. float WritableUtils::ReadFloat(InputStream * stream) {
  179. uint32_t ret;
  180. if (stream->readFully(&ret, 4) != 4) {
  181. THROW_EXCEPTION(IOException, "ReadFloat reach EOF");
  182. }
  183. ret = hadoop_be32toh(ret);
  184. return *(float*)&ret;
  185. }
  186. string WritableUtils::ReadText(InputStream * stream) {
  187. int64_t len = ReadVLong(stream);
  188. string ret = string(len, '\0');
  189. if (stream->readFully((void *)ret.data(), len) != len) {
  190. THROW_EXCEPTION_EX(IOException, "ReadString reach EOF, need %d", len);
  191. }
  192. return ret;
  193. }
  194. string WritableUtils::ReadBytes(InputStream * stream) {
  195. int32_t len = ReadInt(stream);
  196. string ret = string(len, '\0');
  197. if (stream->readFully((void *)ret.data(), len) != len) {
  198. THROW_EXCEPTION_EX(IOException, "ReadString reach EOF, need %d", len);
  199. }
  200. return ret;
  201. }
  202. string WritableUtils::ReadUTF8(InputStream * stream) {
  203. int16_t len = ReadShort(stream);
  204. string ret = string(len, '\0');
  205. if (stream->readFully((void *)ret.data(), len) != len) {
  206. THROW_EXCEPTION_EX(IOException, "ReadString reach EOF, need %d", len);
  207. }
  208. return ret;
  209. }
  210. void WritableUtils::WriteVLong(OutputStream * stream, int64_t v) {
  211. char buff[10];
  212. uint32_t len;
  213. WriteVLong(v, buff, len);
  214. stream->write(buff, len);
  215. }
  216. void WritableUtils::WriteLong(OutputStream * stream, int64_t v) {
  217. uint64_t be = hadoop_be64toh((uint64_t)v);
  218. stream->write(&be, 8);
  219. }
  220. void WritableUtils::WriteInt(OutputStream * stream, int32_t v) {
  221. uint32_t be = hadoop_be32toh((uint32_t)v);
  222. stream->write(&be, 4);
  223. }
  224. void WritableUtils::WriteShort(OutputStream * stream, int16_t v) {
  225. uint16_t be = v;
  226. be = ((be >> 8) | (be << 8));
  227. stream->write(&be, 2);
  228. }
  229. void WritableUtils::WriteFloat(OutputStream * stream, float v) {
  230. uint32_t intv = *(uint32_t*)&v;
  231. intv = hadoop_be32toh(intv);
  232. stream->write(&intv, 4);
  233. }
  234. void WritableUtils::WriteText(OutputStream * stream, const string & v) {
  235. WriteVLong(stream, v.length());
  236. stream->write(v.c_str(), (uint32_t)v.length());
  237. }
  238. void WritableUtils::WriteBytes(OutputStream * stream, const string & v) {
  239. WriteInt(stream, (int32_t)v.length());
  240. stream->write(v.c_str(), (uint32_t)v.length());
  241. }
  242. void WritableUtils::WriteUTF8(OutputStream * stream, const string & v) {
  243. if (v.length() > 65535) {
  244. THROW_EXCEPTION_EX(IOException, "string too long (%lu) for WriteUTF8", v.length());
  245. }
  246. WriteShort(stream, (int16_t)v.length());
  247. stream->write(v.c_str(), (uint32_t)v.length());
  248. }
  249. void WritableUtils::toString(string & dest, KeyValueType type, const void * data, uint32_t length) {
  250. switch (type) {
  251. case TextType:
  252. dest.append((const char*)data, length);
  253. break;
  254. case BytesType:
  255. dest.append((const char*)data, length);
  256. break;
  257. case ByteType:
  258. dest.append(1, *(char*)data);
  259. break;
  260. case BoolType:
  261. dest.append(*(uint8_t*)data ? "true" : "false");
  262. break;
  263. case IntType:
  264. dest.append(StringUtil::ToString((int32_t)hadoop_be32toh(*(uint32_t*)data)));
  265. break;
  266. case LongType:
  267. dest.append(StringUtil::ToString((int64_t)hadoop_be64toh(*(uint64_t*)data)));
  268. break;
  269. case FloatType:
  270. dest.append(StringUtil::ToString(*(float*)data));
  271. break;
  272. case DoubleType:
  273. dest.append(StringUtil::ToString(*(double*)data));
  274. break;
  275. case MD5HashType:
  276. dest.append(StringUtil::ToHexString(data, length));
  277. break;
  278. default:
  279. dest.append((const char*)data, length);
  280. break;
  281. }
  282. }
  283. } // namespace NativeTask