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 ARCHIVE_HH_ |
---|
20 | #define ARCHIVE_HH_ |
---|
21 | #include "recordio.hh" |
---|
22 | |
---|
23 | namespace hadoop { |
---|
24 | |
---|
25 | class Index { |
---|
26 | public: |
---|
27 | virtual bool done() = 0; |
---|
28 | virtual void incr() = 0; |
---|
29 | virtual ~Index() {} |
---|
30 | }; |
---|
31 | |
---|
32 | class IArchive { |
---|
33 | public: |
---|
34 | virtual void deserialize(int8_t& t, const char* tag) = 0; |
---|
35 | virtual void deserialize(bool& t, const char* tag) = 0; |
---|
36 | virtual void deserialize(int32_t& t, const char* tag) = 0; |
---|
37 | virtual void deserialize(int64_t& t, const char* tag) = 0; |
---|
38 | virtual void deserialize(float& t, const char* tag) = 0; |
---|
39 | virtual void deserialize(double& t, const char* tag) = 0; |
---|
40 | virtual void deserialize(std::string& t, const char* tag) = 0; |
---|
41 | virtual void deserialize(std::string& t, size_t& len, const char* tag) = 0; |
---|
42 | virtual void startRecord(hadoop::Record& s, const char* tag) = 0; |
---|
43 | virtual void endRecord(hadoop::Record& s, const char* tag) = 0; |
---|
44 | virtual Index* startVector(const char* tag) = 0; |
---|
45 | virtual void endVector(Index* idx, const char* tag) = 0; |
---|
46 | virtual Index* startMap(const char* tag) = 0; |
---|
47 | virtual void endMap(Index* idx, const char* tag) = 0; |
---|
48 | virtual void deserialize(hadoop::Record& s, const char* tag) { |
---|
49 | s.deserialize(*this, tag); |
---|
50 | } |
---|
51 | template <typename T> |
---|
52 | void deserialize(std::vector<T>& v, const char* tag) { |
---|
53 | Index* idx = startVector(tag); |
---|
54 | while (!idx->done()) { |
---|
55 | T t; |
---|
56 | deserialize(t, tag); |
---|
57 | v.push_back(t); |
---|
58 | idx->incr(); |
---|
59 | } |
---|
60 | endVector(idx, tag); |
---|
61 | } |
---|
62 | template <typename K, typename V> |
---|
63 | void deserialize(std::map<K,V>& v, const char* tag) { |
---|
64 | Index* idx = startMap(tag); |
---|
65 | while (!idx->done()) { |
---|
66 | K key; |
---|
67 | deserialize(key, tag); |
---|
68 | V value; |
---|
69 | deserialize(value, tag); |
---|
70 | v[key] = value; |
---|
71 | idx->incr(); |
---|
72 | } |
---|
73 | endMap(idx, tag); |
---|
74 | } |
---|
75 | virtual ~IArchive() {} |
---|
76 | }; |
---|
77 | |
---|
78 | class OArchive { |
---|
79 | public: |
---|
80 | virtual void serialize(int8_t t, const char* tag) = 0; |
---|
81 | virtual void serialize(bool t, const char* tag) = 0; |
---|
82 | virtual void serialize(int32_t t, const char* tag) = 0; |
---|
83 | virtual void serialize(int64_t t, const char* tag) = 0; |
---|
84 | virtual void serialize(float t, const char* tag) = 0; |
---|
85 | virtual void serialize(double t, const char* tag) = 0; |
---|
86 | virtual void serialize(const std::string& t, const char* tag) = 0; |
---|
87 | virtual void serialize(const std::string& t, size_t len, const char* tag) = 0; |
---|
88 | virtual void startRecord(const hadoop::Record& s, const char* tag) = 0; |
---|
89 | virtual void endRecord(const hadoop::Record& s, const char* tag) = 0; |
---|
90 | virtual void startVector(size_t len, const char* tag) = 0; |
---|
91 | virtual void endVector(size_t len, const char* tag) = 0; |
---|
92 | virtual void startMap(size_t len, const char* tag) = 0; |
---|
93 | virtual void endMap(size_t len, const char* tag) = 0; |
---|
94 | virtual void serialize(const hadoop::Record& s, const char* tag) { |
---|
95 | s.serialize(*this, tag); |
---|
96 | } |
---|
97 | template <typename T> |
---|
98 | void serialize(const std::vector<T>& v, const char* tag) { |
---|
99 | startVector(v.size(), tag); |
---|
100 | if (v.size()>0) { |
---|
101 | for (size_t cur = 0; cur<v.size(); cur++) { |
---|
102 | serialize(v[cur], tag); |
---|
103 | } |
---|
104 | } |
---|
105 | endVector(v.size(), tag); |
---|
106 | } |
---|
107 | template <typename K, typename V> |
---|
108 | void serialize(const std::map<K,V>& v, const char* tag) { |
---|
109 | startMap(v.size(), tag); |
---|
110 | if (v.size()>0) { |
---|
111 | typedef typename std::map<K,V>::const_iterator CI; |
---|
112 | for (CI cur = v.begin(); cur!=v.end(); cur++) { |
---|
113 | serialize(cur->first, tag); |
---|
114 | serialize(cur->second, tag); |
---|
115 | } |
---|
116 | } |
---|
117 | endMap(v.size(), tag); |
---|
118 | } |
---|
119 | virtual ~OArchive() {} |
---|
120 | }; |
---|
121 | }; // end namespace hadoop |
---|
122 | #endif /*ARCHIVE_HH_*/ |
---|