forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.cc
More file actions
198 lines (175 loc) · 6.94 KB
/
Schema.cc
File metadata and controls
198 lines (175 loc) · 6.94 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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <pulsar/Schema.h>
#include <pulsar/defines.h>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <iostream>
#include <map>
#include <memory>
#include "JsonUtils.h"
#include "SchemaUtils.h"
using boost::property_tree::ptree;
PULSAR_PUBLIC std::ostream &operator<<(std::ostream &s, pulsar::SchemaType schemaType) {
return s << strSchemaType(schemaType);
}
PULSAR_PUBLIC std::ostream &operator<<(std::ostream &s, pulsar::KeyValueEncodingType encodingType) {
return s << strEncodingType(encodingType);
}
namespace pulsar {
PULSAR_PUBLIC const char *strEncodingType(KeyValueEncodingType encodingType) {
switch (encodingType) {
case KeyValueEncodingType::INLINE:
return "INLINE";
case KeyValueEncodingType::SEPARATED:
return "SEPARATED";
};
// NOTE : Do not add default case in the switch above. In future if we get new cases for
// Schema and miss them in the switch above we would like to get notified. Adding
// return here to make the compiler happy.
return "UnknownSchemaType";
}
PULSAR_PUBLIC KeyValueEncodingType enumEncodingType(std::string encodingTypeStr) {
if (encodingTypeStr == "INLINE") {
return KeyValueEncodingType::INLINE;
} else if (encodingTypeStr == "SEPARATED") {
return KeyValueEncodingType::SEPARATED;
} else {
throw std::invalid_argument("No match encoding type: " + encodingTypeStr);
}
}
PULSAR_PUBLIC const char *strSchemaType(SchemaType schemaType) {
switch (schemaType) {
case NONE:
return "NONE";
case STRING:
return "STRING";
case INT8:
return "INT8";
case INT16:
return "INT16";
case INT32:
return "INT32";
case INT64:
return "INT64";
case FLOAT:
return "FLOAT";
case DOUBLE:
return "DOUBLE";
case BYTES:
return "BYTES";
case JSON:
return "JSON";
case PROTOBUF:
return "PROTOBUF";
case AVRO:
return "AVRO";
case AUTO_CONSUME:
return "AUTO_CONSUME";
case AUTO_PUBLISH:
return "AUTO_PUBLISH";
case KEY_VALUE:
return "KEY_VALUE";
case PROTOBUF_NATIVE:
return "PROTOBUF_NATIVE";
};
// NOTE : Do not add default case in the switch above. In future if we get new cases for
// Schema and miss them in the switch above we would like to get notified. Adding
// return here to make the compiler happy.
return "UnknownSchemaType";
}
PULSAR_PUBLIC SchemaType enumSchemaType(std::string schemaTypeStr) {
if (schemaTypeStr == "NONE") {
return NONE;
} else if (schemaTypeStr == "STRING") {
return STRING;
} else if (schemaTypeStr == "INT8") {
return INT8;
} else if (schemaTypeStr == "INT16") {
return INT16;
} else if (schemaTypeStr == "INT32") {
return INT32;
} else if (schemaTypeStr == "INT64") {
return INT64;
} else if (schemaTypeStr == "FLOAT") {
return FLOAT;
} else if (schemaTypeStr == "DOUBLE") {
return DOUBLE;
} else if (schemaTypeStr == "BYTES") {
return BYTES;
} else if (schemaTypeStr == "JSON") {
return JSON;
} else if (schemaTypeStr == "PROTOBUF") {
return PROTOBUF;
} else if (schemaTypeStr == "AVRO") {
return AVRO;
} else if (schemaTypeStr == "AUTO_CONSUME") {
return AUTO_CONSUME;
} else if (schemaTypeStr == "AUTO_PUBLISH") {
return AUTO_PUBLISH;
} else if (schemaTypeStr == "KEY_VALUE") {
return KEY_VALUE;
} else if (schemaTypeStr == "PROTOBUF_NATIVE") {
return PROTOBUF_NATIVE;
} else {
throw std::invalid_argument("No match schema type: " + schemaTypeStr);
}
}
class PULSAR_PUBLIC SchemaInfoImpl {
public:
const std::string name_;
const std::string schema_;
const SchemaType type_ = BYTES;
const std::map<std::string, std::string> properties_;
SchemaInfoImpl() : name_("BYTES") {}
SchemaInfoImpl(SchemaType schemaType, const std::string &name, const std::string &schema,
const StringMap &properties)
: name_(name), schema_(schema), type_(schemaType), properties_(properties) {}
};
SchemaInfo::SchemaInfo() : impl_(std::make_shared<SchemaInfoImpl>()) {}
SchemaInfo::SchemaInfo(SchemaType schemaType, const std::string &name, const std::string &schema,
const StringMap &properties)
: impl_(std::make_shared<SchemaInfoImpl>(schemaType, name, schema, properties)) {}
SchemaInfo::SchemaInfo(const SchemaInfo &keySchema, const SchemaInfo &valueSchema,
const KeyValueEncodingType &keyValueEncodingType) {
auto writeJson = [](const StringMap &properties) {
ptree pt;
for (auto &entry : properties) {
pt.put(entry.first, entry.second);
}
return toJson(pt);
};
StringMap properties;
properties.emplace(KEY_SCHEMA_NAME, keySchema.getName());
properties.emplace(KEY_SCHEMA_TYPE, strSchemaType(keySchema.getSchemaType()));
properties.emplace(KEY_SCHEMA_PROPS, writeJson(keySchema.getProperties()));
properties.emplace(VALUE_SCHEMA_NAME, valueSchema.getName());
properties.emplace(VALUE_SCHEMA_TYPE, strSchemaType(valueSchema.getSchemaType()));
properties.emplace(VALUE_SCHEMA_PROPS, writeJson(valueSchema.getProperties()));
properties.emplace(KV_ENCODING_TYPE, strEncodingType(keyValueEncodingType));
std::string keySchemaStr = keySchema.getSchema();
std::string valueSchemaStr = valueSchema.getSchema();
impl_ = std::make_shared<SchemaInfoImpl>(KEY_VALUE, "KeyValue",
mergeKeyValueSchema(keySchemaStr, valueSchemaStr), properties);
}
SchemaType SchemaInfo::getSchemaType() const { return impl_->type_; }
const std::string &SchemaInfo::getName() const { return impl_->name_; }
const std::string &SchemaInfo::getSchema() const { return impl_->schema_; }
const std::map<std::string, std::string> &SchemaInfo::getProperties() const { return impl_->properties_; }
} // namespace pulsar