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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
/* Copyright (c) 2009 PrimeBase Technologies GmbH, Germany
*
* PrimeBase Media Stream for MySQL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Barry Leslie
*
* System variables table.
*
*/
#include "CSConfig.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#ifdef DRIZZLED
#include <drizzled/server_includes.h>
#endif
//#include "mysql_priv.h"
#include "CSGlobal.h"
#include "CSStrUtil.h"
#include "CSLog.h"
#include "CSPath.h"
#include "ha_pbms.h"
//#include <plugin.h>
#include "ms_mysql.h"
#include "Database_ms.h"
#include "OpenTable_ms.h"
#include "Util_ms.h"
#include "Discover_ms.h"
#include "SysTab_util.h"
#include "pbmslib.h"
#include "SysTab_httpheader.h"
#define METADATA_HEADER_FILE "http-meta-data-headers"
#define MIN_METADATA_HEADER_FILE_SIZE 3
SysTabRec *MSHTTPHeaderTable::gDefaultMetaDataHeaders;
DT_FIELD_INFO pbms_metadata_headers_info[]=
{
{"Name", 32, NULL, MYSQL_TYPE_VARCHAR, &UTF8_CHARSET, NOT_NULL_FLAG, "HTTP field name"},
{NULL, NULL, NULL, MYSQL_TYPE_STRING, NULL, 0, NULL}
};
DT_KEY_INFO pbms_metadata_headers_keys[]=
{
{NULL, 0, {NULL}}
};
/*
* -------------------------------------------------------------------------
* HTTP HEADER TABLE
*/
void MSHTTPHeaderTable::releaseDefaultMetaDataHeaders()
{
if (gDefaultMetaDataHeaders)
gDefaultMetaDataHeaders->release();
gDefaultMetaDataHeaders = NULL;
}
void MSHTTPHeaderTable::setDefaultMetaDataHeaders(const char *defaults)
{
const char *ptr;
enter_();
if (!gDefaultMetaDataHeaders)
new_(gDefaultMetaDataHeaders, SysTabRec("", METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
gDefaultMetaDataHeaders->clear();
if (defaults) {
while (*defaults) {
for (ptr = defaults; *ptr && (*ptr != ':'); ptr++){}
if (ptr != defaults) {
gDefaultMetaDataHeaders->beginRecord();
gDefaultMetaDataHeaders->setStringField(defaults, ptr - defaults);
gDefaultMetaDataHeaders->endRecord();
}
if (!ptr)
break;
defaults = ptr +1;
}
}
exit_();
}
//----------------------------
void MSHTTPHeaderTable::loadTable(MSDatabase *db)
{
CSPath *path;
SysTabRec *headerData;
enter_();
push_(db);
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
push_(path);
if (path->exists()) {
CSFile *file;
size_t size;
new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
push_(headerData);
file = path->openFile(CSFile::READONLY);
push_(file);
size = file->getEOF();
headerData->setLength(size);
file->read(headerData->getBuffer(0), 0, size, size);
release_(file);
} else if (gDefaultMetaDataHeaders) { // Load the defaults if they exist.
headerData = RETAIN(gDefaultMetaDataHeaders);
push_(headerData);
}
if (headerData) {
while (headerData->nextRecord()) {
const char *header = headerData->getStringField();
if (headerData->isValidRecord()) {
db->iHTTPMetaDataHeaders.add(CSString::newString(header));
}
}
release_(headerData);
}
release_(path);
release_(db);
exit_();
}
//----------------------------
void MSHTTPHeaderTable::saveTable(MSDatabase *db)
{
CSString *str;
SysTabRec *headerData;
enter_();
push_(db);
new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
push_(headerData);
// Build the table records
headerData->clear();
lock_(&db->iHTTPMetaDataHeaders);
// Note: the object returned by itemAt() is not returnd referenced.
for (u_int i =0; (str = (CSString*) db->iHTTPMetaDataHeaders.itemAt(i)); i++) {
headerData->beginRecord();
headerData->setStringField(str->getCString());
headerData->endRecord();
}
unlock_(&db->iHTTPMetaDataHeaders);
restoreTable(RETAIN(db), headerData->getBuffer(0), headerData->length(), false);
release_(headerData);
release_(db);
exit_();
}
//--------------------
CSStringBuffer *MSHTTPHeaderTable::dumpTable(MSDatabase *db)
{
CSPath *path;
CSStringBuffer *dump;
enter_();
push_(db);
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
release_(db);
push_(path);
new_(dump, CSStringBuffer(20));
push_(dump);
if (path->exists()) {
CSFile *file;
size_t size;
file = path->openFile(CSFile::READONLY);
push_(file);
size = file->getEOF();
dump->setLength(size);
file->read(dump->getBuffer(0), 0, size, size);
release_(file);
}
pop_(dump);
release_(path);
return_(dump);
}
//--------------------
void MSHTTPHeaderTable::restoreTable(MSDatabase *db, const char *data, size_t size, bool reload)
{
CSPath *path;
CSFile *file;
enter_();
push_(db);
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
push_(path);
file = path->openFile(CSFile::CREATE | CSFile::TRUNCATE);
push_(file);
file->write(data, 0, size);
file->close();
release_(file);
release_(path);
pop_(db);
if (reload)
loadTable(db);
else
db->release();
exit_();
}
//--------------------
void MSHTTPHeaderTable::transferTable(MSDatabase *dst_db, MSDatabase *src_db)
{
CSPath *path;
enter_();
push_(src_db);
push_(dst_db);
path = CSPath::newPath(RETAIN(src_db->myDatabasePath), METADATA_HEADER_FILE".dat");
push_(path);
if (path->exists()) {
CSPath *bu_path;
bu_path = CSPath::newPath(RETAIN(dst_db->myDatabasePath), METADATA_HEADER_FILE".dat");
path->copyTo(bu_path, true);
}
release_(path);
release_(dst_db);
release_(src_db);
exit_();
}
void MSHTTPHeaderTable::removeTable(CSString *db_path)
{
CSPath *path;
enter_();
path = getSysFile(db_path, METADATA_HEADER_FILE, 0);
push_(path);
path->removeFile();
release_(path);
exit_();
}
MSHTTPHeaderTable::MSHTTPHeaderTable(MSSystemTableShare *share, TABLE *table):
MSOpenSystemTable(share, table),
iDirty(false),
iHeaderIndex(0)
{
}
MSHTTPHeaderTable::~MSHTTPHeaderTable()
{
//unuse();
}
void MSHTTPHeaderTable::use()
{
myShare->mySysDatabase->iHTTPMetaDataHeaders.lock();
iDirty = false;
}
void MSHTTPHeaderTable::unuse()
{
if (iDirty) {
saveTable(RETAIN(myShare->mySysDatabase));
iDirty = false;
}
myShare->mySysDatabase->iHTTPMetaDataHeaders.unlock();
}
void MSHTTPHeaderTable::seqScanInit()
{
iHeaderIndex = 0;
}
bool MSHTTPHeaderTable::seqScanNext(char *buf)
{
TABLE *table = mySQLTable;
Field *curr_field;
byte *save;
MY_BITMAP *save_write_set;
CSString *header;
const char *name;
enter_();
header = (CSString*) (myShare->mySysDatabase->iHTTPMetaDataHeaders.itemAt(iHeaderIndex));
if (!header)
return_(false);
iHeaderIndex++;
name = header->getCString();
save_write_set = table->write_set;
table->write_set = NULL;
memset(buf, 0xFF, table->s->null_bytes);
for (Field **field=table->field ; *field ; field++) {
curr_field = *field;
save = curr_field->ptr;
#if MYSQL_VERSION_ID < 50114
curr_field->ptr = (byte *) buf + curr_field->offset();
#else
curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->table->record[0]);
#endif
switch (curr_field->field_name[0]) {
case 'N':
ASSERT(strcmp(curr_field->field_name, "Name") == 0);
curr_field->store(name, strlen(name), &UTF8_CHARSET);
ms_my_set_notnull_in_record(curr_field, buf);
break;
}
curr_field->ptr = save;
}
table->write_set = save_write_set;
return_(true);
}
void MSHTTPHeaderTable::seqScanPos(uint8_t *pos)
{
int32_t index = iHeaderIndex -1;
if (index < 0)
index = 0; // This is probably an error condition.
mi_int4store(pos, index);
}
void MSHTTPHeaderTable::seqScanRead(uint8_t *pos, char *buf)
{
iHeaderIndex = mi_uint4korr(pos);
seqScanNext(buf);
}
void MSHTTPHeaderTable::insertRow(char *data)
{
CSString *header;
TABLE *table = mySQLTable;
String str1, *name = &str1;
enter_();
GET_STR_FIELD(name, 0, table, data);
header = CSString::newString(name->c_ptr_safe());
myShare->mySysDatabase->iHTTPMetaDataHeaders.add(header);
iDirty = true;
exit_();
}
void MSHTTPHeaderTable::deleteRow(char *data)
{
CSString *header;
TABLE *table = mySQLTable;
String str1, *name = &str1;
enter_();
GET_STR_FIELD(name, 0, table, data);
header = CSString::newString(name->c_ptr_safe());
myShare->mySysDatabase->iHTTPMetaDataHeaders.remove(header);
iDirty = true;
exit_();
}
void MSHTTPHeaderTable::updateRow(char *old_data, char *new_data)
{
enter_();
insertRow(new_data);
try_(a) {
deleteRow(old_data);
}
catch_(a) {
deleteRow(new_data);
throw_();
}
cont_(a);
exit_();
}
|