-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp_back
More file actions
245 lines (195 loc) · 6.54 KB
/
Copy pathmain.cpp_back
File metadata and controls
245 lines (195 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "demo.pb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <istream>
#include <ostream>
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/io/gzip_stream.h"
using namespace ::google::protobuf::io;
#define PROTOBUFFER_MAX_BUFFER_SIZE 10240
typedef protocol::recsys::deldup::RecItemArr PbRecItemArr;
typedef protocol::recsys::deldup::RecItem PbRecItem;
// 解压文件中的gzip压缩的pb内容
void ungzippb(PbRecItemArr& itemarr, std::string file_name) {
std::ifstream input(file_name.c_str(), std::ifstream::in | std::ifstream::binary);
IstreamInputStream inputFileStream(&input);
GzipInputStream gzipInputStream(&inputFileStream);
if (!itemarr.ParseFromZeroCopyStream(&gzipInputStream)) {
std::cerr << "failed to parse recitemarr" << std::endl;
}
return;
}
// 压缩pb结构并写入文件
void gzippb(PbRecItemArr& itemarr, std::string str_name) {
std::ofstream output(str_name.c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
OstreamOutputStream outputFileStream(&output);
GzipOutputStream::Options options;
options.format = GzipOutputStream::GZIP;
options.compression_level = 6;
GzipOutputStream gzipOutputStream(&outputFileStream, options);
if (!itemarr.SerializeToZeroCopyStream(&gzipOutputStream)) {
std::cerr << "Failed to write scene!" << std::endl;
}
return;
}
void dump_recitemarr(PbRecItemArr& itemarr) {
::google::protobuf::RepeatedPtrField<PbRecItem>* p_recitem = itemarr.mutable_recitem();
::google::protobuf::RepeatedPtrField<PbRecItem>::iterator recitem_it = p_recitem->begin();
for (; recitem_it != p_recitem->end(); ++recitem_it) {
std::cout << \
"vid=" << recitem_it->videoid() << ", " \
"type=" << recitem_it->type() << ", " \
"timestamp=" << recitem_it->timestamp() \
<< std::endl;
}
return;
}
// 解压
class CCustomStringInputStream: public google::protobuf::io::ZeroCopyInputStream
{
public:
CCustomStringInputStream(std::string str_zipdata) {
m_idx = 0;
m_size = str_zipdata.length();
memcpy(m_szBuffer, str_zipdata.c_str(), str_zipdata.length());
}
virtual ~CCustomStringInputStream() {
}
virtual bool Next(const void** data, int* size) {
*data = m_szBuffer + m_idx;
*size = m_size - m_idx;
m_idx += m_size;
return true;
}
virtual void BackUp(int count) {
m_idx -= count;
}
virtual bool Skip(int count) {
if (m_idx + count == m_size) {
m_idx += count;
return false;
} else if (m_idx + count > m_size) {
return false;
}
m_idx += count;
return true;
}
virtual int64_t ByteCount() const {
return m_idx;
}
private:
int64_t m_idx;
int64_t m_size;
char m_szBuffer[PROTOBUFFER_MAX_BUFFER_SIZE];
};
// 压缩
class CCustomStringOutputStream: public google::protobuf::io::ZeroCopyOutputStream
{
public:
CCustomStringOutputStream() {
}
~CCustomStringOutputStream() {
}
void Reset() {
m_idx=0;
}
std::string ToString() {
return std::string(m_szBuffer, m_idx);
}
virtual bool Next(void** data, int* size) {
*data = m_szBuffer + m_idx;
*size = sizeof(m_szBuffer) - m_idx;
m_idx += *size;
return true;
}
virtual void BackUp(int count) {
m_idx -= count;
}
virtual int64_t ByteCount() const {
return m_idx;
}
private:
int64_t m_idx;
char m_szBuffer[PROTOBUFFER_MAX_BUFFER_SIZE];
};
void ungzip_pb(const std::string& str_zipdata, std::string& str_outdata) {
PbRecItemArr out_recitemarr;
CCustomStringInputStream stream_input(str_zipdata);
GzipInputStream gzipInputStream(&stream_input);
out_recitemarr.ParseFromZeroCopyStream(&gzipInputStream);
out_recitemarr.SerializeToString(&str_outdata);
return;
}
int main(int argc, char* argv[]) {
PbRecItemArr itemarr;
ungzippb(itemarr, "my.data");
dump_recitemarr(itemarr);
PbRecItem item1, item2;
item1.set_timestamp(1111);
item1.set_videoid(1);
item1.set_type(1);
item2.set_timestamp(2222);
item2.set_videoid(2);
item2.set_type(2);
std::vector<PbRecItem> vec_recitem;
vec_recitem.push_back(item1);
vec_recitem.push_back(item2);
PbRecItemArr items1;
for (int idx = 0; idx < vec_recitem.size(); idx++) {
PbRecItem* item = items1.add_recitem();
*item = vec_recitem[idx];
}
std::string str_src;
items1.SerializeToString(&str_src);
std::cout << "src_length: " << str_src.length() << std::endl;
CCustomStringOutputStream stream_output;
GzipOutputStream::Options options;
options.format = GzipOutputStream::GZIP;
options.compression_level = 6;
GzipOutputStream m_zip(&stream_output, options);
items1.SerializeToZeroCopyStream(&m_zip);
m_zip.Close();
std::string str_zip;
str_zip = stream_output.ToString();
std::cout << "fixed_length: " << str_zip.length() << std::endl;
PbRecItemArr out_recitemarr;
CCustomStringInputStream stream_input(str_zip);
GzipInputStream gzipInputStream(&stream_input);
if (!out_recitemarr.ParseFromZeroCopyStream(&gzipInputStream)) {
std::cerr << "failed to parse recitemarr" << std::endl;
}
// dump_recitemarr(out_recitemarr);
std::string str_outdata;
ungzip_pb(str_zip, str_outdata);
PbRecItemArr test;
test.ParseFromString(str_outdata);
std::cout << "dump test" << std::endl;
dump_recitemarr(test);
/*
PbRecItemArr itemarr;
ungzippb(itemarr, "my.data");
dump_recitemarr(itemarr);
PbRecItem item1, item2;
item1.set_timestamp(1111);
item1.set_videoid(1);
item1.set_type(1);
item2.set_timestamp(2222);
item2.set_videoid(2);
item2.set_type(2);
std::vector<PbRecItem> vec_recitem;
vec_recitem.push_back(item1);
vec_recitem.push_back(item2);
PbRecItemArr items1;
for (int idx = 0; idx < vec_recitem.size(); idx++) {
PbRecItem* item = items1.add_recitem();
*item = vec_recitem[idx];
}
gzippb(items1, "my.data");
*/
return 0;
}