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