forked from confluentinc/libserdes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serdescpp.h
213 lines (164 loc) · 5.31 KB
/
serdescpp.h
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
/**
* Copyright 2015 Confluent Inc.
*
* Licensed 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.
*/
#pragma once
#include <string>
#include <avro/ValidSchema.hh>
/**
*
* serdes-common.h contains artifacts shared between the C and C++ interface,
* such as types and error codes.
*/
#include "serdes-common.h"
namespace Serdes {
SERDES_EXPORT
int version ();
SERDES_EXPORT
std::string version_str ();
typedef serdes_err_t ErrorCode;
/**
* Returns the human readable form of a serdes_err_t
* The returned pointer has infinite life time and must not be freed.
*/
SERDES_EXPORT
std::string err2str (Serdes::ErrorCode err);
/* Forward declarations */
class Handle;
/**
* Set optional log callback to use for serdes originated log messages.
*/
class SERDES_EXPORT LogCb {
public:
virtual ~LogCb () {};
virtual void log_cb (Handle *serdes, int level, const std::string &fac,
const std::string &buf) = 0;
};
/**
* Reusable configuration object passed to Serdes::Handle::create()
*
*/
class SERDES_EXPORT Conf {
public:
/**
* Create a configuration object with default parameters.
* The object must be deleted when the application is done with it.
*/
static Conf *create ();
virtual ~Conf () {};
/**
* Set configuration property `name` to value `value`.
* Returns SERDES_ERR_OK on success, else writes an error message to 'errstr'.
*/
virtual ErrorCode set (const std::string &name,
const std::string &value,
std::string &errstr) = 0;
/**
* Set an optional log callback class for logs originating from libserdes.
*/
virtual void set (LogCb *log_cb) = 0;
};
/**
* Main Serdes handle.
*/
class SERDES_EXPORT Handle {
public:
virtual ~Handle () {};
/**
* Create a new Serdes handle for serialization/deserialization.
* `conf` is an optional configuration object.
*
* Returns a new Handle object, or NULL on error (see errstr for reason).
*/
static Handle *create (const Conf *conf, std::string &errstr);
/**
* Purges any schemas from the local schema cache that have not been used
* in `max_age` seconds.
*
* Returns the number of schemas removed.
*/
virtual int schemas_purge (int max_age) = 0;
/**
* Returns the serializer/deserializer framing size,
* or 0 if no framing is configured.
*/
virtual ssize_t serializer_framing_size () const = 0;
virtual ssize_t deserializer_framing_size () const = 0;
};
/**
* A cached copy of a schema
*/
class SERDES_EXPORT Schema {
public:
virtual ~Schema () {};
/**
* Get and load schema from local cache or remote schema registry.
* The schema may be looked up by its `name` or by its `id`.
*
* The returned schema will be fully loaded and immediately usable.
*
* If the get or load fails NULL is returned and a human readable error
* description is written to `errstr`.
*/
static Schema *get (Handle *handle, int id, std::string &errstr);
static Schema *get (Handle *handle, const std::string &name,
std::string &errstr);
/**
* Add schema definition to the local cache and stores the schema to remote
* schema registry.
*
* If the name is not provided, then the definition will be ignored.
* If `id` is not set then the schema will be stored on the remote schema
* registry, else the id will be assigned as this schema's id.
*
* If an existing schema with an identical schema exists in the cache it
* will be returned instead, else the newly created schema will be returned.
*
* The returned schema will be fully loaded and immediately usable.
*
* In case schema parsing or storing fails NULL is returned and a human
* readable error description is written to `errstr`
*/
static Schema *add (Handle *handle, int id,
const std::string &definition, std::string &errstr);
static Schema *add (Handle *handle, const std::string &name,
const std::string &definition, std::string &errstr);
static Schema *add (Handle *handle, const std::string &name, int id,
const std::string &definition, std::string &errstr);
/**
* Returns the schema id.
*/
virtual int id () = 0;
/**
* Returns the schema name.
* The returned pointer is only valid until the schema is destroyed.
* NULL is returned if the name of the schema is not known.
*/
virtual const std::string name () = 0;
/**
* Returns the schema definition.
* The returned pointer is only valid until the schema is destroyed.
*/
virtual const std::string definition () = 0;
/**
* Returns the Avro schema object.
*/
virtual avro::ValidSchema *object () = 0;
/**
* Writes framing to vector.
* Returns the number of bytes written.
*/
virtual ssize_t framing_write (std::vector<char> &out) const = 0;
};
}