csvarchive.hh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef CSVARCHIVE_HH_
  19. #define CSVARCHIVE_HH_
  20. #include "recordio.hh"
  21. namespace hadoop {
  22. class PushBackInStream {
  23. private:
  24. InStream* stream;
  25. bool isAvail;
  26. char pbchar;
  27. public:
  28. void setStream(InStream* stream_) {
  29. stream = stream_;
  30. isAvail = false;
  31. pbchar = 0;
  32. }
  33. ssize_t read(void* buf, size_t len) {
  34. if (len > 0 && isAvail) {
  35. char* p = (char*) buf;
  36. *p = pbchar;
  37. isAvail = false;
  38. if (len > 1) {
  39. ssize_t ret = stream->read((char*)buf + 1, len - 1);
  40. return ret + 1;
  41. } else {
  42. return 1;
  43. }
  44. } else {
  45. return stream->read(buf, len);
  46. }
  47. }
  48. void pushBack(char c) {
  49. pbchar = c;
  50. isAvail = true;
  51. }
  52. };
  53. class CsvIndex : public Index {
  54. private:
  55. PushBackInStream& stream;
  56. public:
  57. CsvIndex(PushBackInStream& _stream) : stream(_stream) {}
  58. bool done() {
  59. char c;
  60. stream.read(&c, 1);
  61. if (c != ',') {
  62. stream.pushBack(c);
  63. }
  64. return (c == '}') ? true : false;
  65. }
  66. void incr() {}
  67. ~CsvIndex() {}
  68. };
  69. class ICsvArchive : public IArchive {
  70. private:
  71. PushBackInStream stream;
  72. public:
  73. ICsvArchive(InStream& _stream) { stream.setStream(&_stream); }
  74. virtual void deserialize(int8_t& t, const char* tag);
  75. virtual void deserialize(bool& t, const char* tag);
  76. virtual void deserialize(int32_t& t, const char* tag);
  77. virtual void deserialize(int64_t& t, const char* tag);
  78. virtual void deserialize(float& t, const char* tag);
  79. virtual void deserialize(double& t, const char* tag);
  80. virtual void deserialize(std::string& t, const char* tag);
  81. virtual void deserialize(std::string& t, size_t& len, const char* tag);
  82. virtual void startRecord(Record& s, const char* tag);
  83. virtual void endRecord(Record& s, const char* tag);
  84. virtual Index* startVector(const char* tag);
  85. virtual void endVector(Index* idx, const char* tag);
  86. virtual Index* startMap(const char* tag);
  87. virtual void endMap(Index* idx, const char* tag);
  88. virtual ~ICsvArchive();
  89. };
  90. class OCsvArchive : public OArchive {
  91. private:
  92. OutStream& stream;
  93. bool isFirst;
  94. void printCommaUnlessFirst() {
  95. if (!isFirst) {
  96. stream.write(",",1);
  97. }
  98. isFirst = false;
  99. }
  100. public:
  101. OCsvArchive(OutStream& _stream) : stream(_stream) {isFirst = true;}
  102. virtual void serialize(int8_t t, const char* tag);
  103. virtual void serialize(bool t, const char* tag);
  104. virtual void serialize(int32_t t, const char* tag);
  105. virtual void serialize(int64_t t, const char* tag);
  106. virtual void serialize(float t, const char* tag);
  107. virtual void serialize(double t, const char* tag);
  108. virtual void serialize(const std::string& t, const char* tag);
  109. virtual void serialize(const std::string& t, size_t len, const char* tag);
  110. virtual void startRecord(const Record& s, const char* tag);
  111. virtual void endRecord(const Record& s, const char* tag);
  112. virtual void startVector(size_t len, const char* tag);
  113. virtual void endVector(size_t len, const char* tag);
  114. virtual void startMap(size_t len, const char* tag);
  115. virtual void endMap(size_t len, const char* tag);
  116. virtual ~OCsvArchive();
  117. };
  118. }
  119. #endif /*CSVARCHIVE_HH_*/