forked from hpcc-systems/HPCC-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroxieclient.ipp
325 lines (265 loc) · 8.06 KB
/
roxieclient.ipp
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*##############################################################################
HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
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.
############################################################################## */
#ifndef _ROXIECLIENT_IPP__
#define _ROXIECLIENT_IPP__
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include "jlib.hpp"
#include "jsmartsock.hpp"
#include "jmisc.hpp"
#include "jqueue.tpp"
#include "jstream.hpp"
#include "roxieclient.hpp"
#define MAXBUFSIZE 0x8000
interface IReceivedRoxieException : extends IException
{
};
class ReceivedRoxieException: public CInterface, public IReceivedRoxieException
{
public:
IMPLEMENT_IINTERFACE;
ReceivedRoxieException(int code, const char *_msg) : errcode(code), msg(_msg) { };
int errorCode() const { return (errcode); };
StringBuffer & errorMessage(StringBuffer &str) const
{
return str.append("ReceivedRoxieException: (").append(msg).append(")");
};
MessageAudience errorAudience() const { return (MSGAUD_user); };
private:
int errcode;
StringAttr msg;
};
interface IRoxieClientException : extends IException
{
};
class RoxieClientException: public CInterface, public IRoxieClientException
{
public:
IMPLEMENT_IINTERFACE;
RoxieClientException(int code, const char *_msg) : errcode(code), msg(_msg) { };
int errorCode() const { return (errcode); };
StringBuffer & errorMessage(StringBuffer &str) const
{
return str.append("RoxieClientException: (").append(msg).append(")");
};
MessageAudience errorAudience() const { return (MSGAUD_user); };
private:
int errcode;
StringAttr msg;
};
class CDataInput : public CInterface, implements IInterface
{
public:
IMPLEMENT_IINTERFACE;
CDataInput(const char* _name, int iw, IByteInputStream* _stream)
{
name.append(_name);
record_width = iw;
stream.set(_stream);
}
StringBuffer name;
int record_width;
Linked<IByteInputStream> stream;
Mutex readMutex;
};
class CDataOutput : public CInterface, implements IInterface
{
public:
IMPLEMENT_IINTERFACE;
CDataOutput(const char* _name, int ow, IByteOutputStream* _stream)
{
name.append(_name);
record_width = ow;
stream.set(_stream);
}
StringBuffer name;
int record_width;
Linked<IByteOutputStream> stream;
Mutex writeMutex;
};
class CDataInputCache : public CInterface, implements IInterface
{
public:
IMPLEMENT_IINTERFACE;
CDataInputCache(CDataInput* _input, unsigned _recordsPerQuery)
{
recordsPerQuery = _recordsPerQuery;
inputLen = 0;
curInput = 0;
if(_input != NULL)
{
name.append(_input->name);
input = _input;
if(input->record_width == 0)
{
bytesPerQuery = 0;
recordsPerBlock = 0;
}
else
{
bytesPerQuery = recordsPerQuery * _input->record_width;
recordsPerBlock = BYTESPERBLOCK / _input->record_width;
recordsPerBlock = (recordsPerBlock < 1)? 1: recordsPerBlock;
buffer.reserve(bytesPerQuery);
}
}
}
int readInput();
StringBuffer name;
int inputLen;
int curInput;
StringBuffer buffer;
unsigned recordsPerQuery;
unsigned bytesPerQuery;
unsigned recordsPerBlock;
CDataInput *input;
};
class CDataOutputCache : public CInterface, implements IInterface
{
public:
IMPLEMENT_IINTERFACE;
CDataOutputCache(CDataOutput* _output)
{
isNonBlocked = false;
if(_output != NULL)
{
name.append(_output->name);
output = _output;
}
}
virtual ~CDataOutputCache()
{
while (outputQ.ordinality() > 0)
delete outputQ.dequeue();
}
int writeOutput();
void clearOutput();
int immediateOutput(size32_t len, const char* result);
StringBuffer name;
QueueOf<MemoryBuffer, false> outputQ;
CDataOutput *output;
bool isNonBlocked;
};
class CRoxieClient;
class RoxieThread : public Thread
{
private:
Owned<ISmartSocket> roxieSock;
StringBuffer query;
CRoxieClient* roxieclient;
Owned<CDataInputCache> m_default_incache;
Owned<CDataOutputCache> m_default_outcache;
IArrayOf<CDataInputCache> m_incaches;
IArrayOf<CDataOutputCache> m_outcaches;
unsigned m_recordsPerQuery;
unsigned m_maxRetries;
int m_readTimeout;
public:
RoxieThread(CRoxieClient* _roxieclient, const char *_query, unsigned recordsPerQuery, unsigned maxRetries, int readTimeout);
void sendQuery();
bool readInput();
void writeOutput();
void clearOutput();
void immediateOutput(size32_t len, const char* result);
void sendData(const char *reqbuf);
void processQuery();
int run();
unsigned bytesRead;
unsigned bytesWritten;
};
class CRoxieClient : public CInterface, implements IRoxieClient
{
public:
IMPLEMENT_IINTERFACE;
Mutex readMutex;
Mutex writeMutex;
CRoxieClient(ISmartSocketFactory* socketfactory, int numThreads, int maxretries)
{
m_smartSocketFactory.set(socketfactory);
m_numThreads = (numThreads < 1)? 1: ((numThreads > MAX_ROXIECLIENT_THREADS)? MAX_ROXIECLIENT_THREADS: numThreads);
m_recordsPerQuery = DEFAULT_RECORDSPERQUERY;
m_maxRetries = maxretries;
m_readTimeout = 300;
}
virtual ~CRoxieClient()
{
}
virtual ISmartSocketFactory* querySocketFactory()
{
return m_smartSocketFactory.get();
}
virtual void setInput(const char* id, unsigned in_width, IByteInputStream* istream)
{
if(istream != NULL)
{
if(id == NULL || id[0] == '\0')
{
m_default_input.setown(new CDataInput("", in_width, istream));
}
else
{
m_inputs.append(*(new CDataInput(id, in_width, istream)));
}
}
else
{
ERRLOG("can't set input stream, it's NULL");
}
}
virtual void setOutput(const char* resultName, unsigned out_width, IByteOutputStream* ostream)
{
if(ostream != NULL)
{
if(resultName == NULL || resultName[0] == '\0')
{
m_default_output.setown(new CDataOutput("", out_width, ostream));
}
else
{
m_outputs.append(*(new CDataOutput(resultName, out_width, ostream)));
}
}
else
{
ERRLOG("can't set output stream, it's NULL");
}
}
virtual void setRecordsPerQuery(unsigned recordsPerQuery)
{
m_recordsPerQuery = recordsPerQuery;
}
virtual void setMaxRetries(unsigned retries)
{
m_maxRetries = retries;
}
virtual void setReadTimeout(int readTimeout)
{
m_readTimeout = readTimeout;
}
virtual void runQuery(const char* query, bool trim);
private:
Linked<ISmartSocketFactory> m_smartSocketFactory;
unsigned m_numThreads;
unsigned m_maxRetries;
int m_readTimeout;
protected:
IArrayOf<CDataInput> m_inputs;
IArrayOf<CDataOutput> m_outputs;
Owned<CDataInput> m_default_input;
Owned<CDataOutput> m_default_output;
unsigned m_recordsPerQuery;
friend class RoxieThread;
};
#endif