~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/systab_httpheader_ms.cc

  • Committer: Mark Atwood
  • Date: 2011-12-20 02:32:53 UTC
  • mfrom: (2469.1.1 drizzle-build)
  • Revision ID: me@mark.atwood.name-20111220023253-bvu0kr14kwsdvz7g
mergeĀ lp:~brianaker/drizzle/deprecate-pbms

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
2
 
 *
3
 
 * PrimeBase Media Stream for MySQL
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
 
 *
19
 
 * Barry Leslie
20
 
 *
21
 
 * System variables table.
22
 
 *
23
 
 */
24
 
#ifdef DRIZZLED
25
 
#include <config.h>
26
 
#include <drizzled/common.h>
27
 
#include <drizzled/session.h>
28
 
#include <drizzled/sql_lex.h>
29
 
#include <drizzled/field/blob.h>
30
 
#include <drizzled/field/varstring.h>
31
 
#endif
32
 
 
33
 
#include "cslib/CSConfig.h"
34
 
 
35
 
#include <sys/types.h>
36
 
#include <sys/stat.h>
37
 
#include <stdlib.h>
38
 
#include <time.h>
39
 
 
40
 
//#include "mysql_priv.h"
41
 
#include "cslib/CSGlobal.h"
42
 
#include "cslib/CSStrUtil.h"
43
 
#include "cslib/CSLog.h"
44
 
#include "cslib/CSPath.h"
45
 
 
46
 
#include "ha_pbms.h"
47
 
//#include <plugin.h>
48
 
 
49
 
#include "mysql_ms.h"
50
 
#include "database_ms.h"
51
 
#include "open_table_ms.h"
52
 
#include "discover_ms.h"
53
 
#include "systab_util_ms.h"
54
 
#include "pbmslib.h"
55
 
 
56
 
#include "systab_httpheader_ms.h"
57
 
 
58
 
#define METADATA_HEADER_FILE    "http-meta-data-headers"
59
 
#define MIN_METADATA_HEADER_FILE_SIZE   3
60
 
 
61
 
SysTabRec       *MSHTTPHeaderTable::gDefaultMetaDataHeaders;
62
 
 
63
 
DT_FIELD_INFO pbms_metadata_headers_info[]=
64
 
{
65
 
        {"Name", 32,    NULL, MYSQL_TYPE_VARCHAR,       &UTF8_CHARSET,  NOT_NULL_FLAG,  "HTTP field name"},
66
 
        {NULL,  NOVAL,                          NULL, MYSQL_TYPE_STRING,        NULL, 0, NULL}
67
 
};
68
 
 
69
 
DT_KEY_INFO pbms_metadata_headers_keys[]=
70
 
{
71
 
        {NULL, 0, {NULL}}
72
 
};
73
 
 
74
 
/*
75
 
 * -------------------------------------------------------------------------
76
 
 * HTTP HEADER TABLE
77
 
 */
78
 
void MSHTTPHeaderTable::releaseDefaultMetaDataHeaders()
79
 
{
80
 
        if (gDefaultMetaDataHeaders) 
81
 
                gDefaultMetaDataHeaders->release();
82
 
        gDefaultMetaDataHeaders = NULL;
83
 
}
84
 
 
85
 
void MSHTTPHeaderTable::setDefaultMetaDataHeaders(const char *defaults)
86
 
{
87
 
        const char *ptr;
88
 
        enter_();
89
 
        
90
 
        if (!gDefaultMetaDataHeaders) 
91
 
                new_(gDefaultMetaDataHeaders, SysTabRec("", METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
92
 
 
93
 
        gDefaultMetaDataHeaders->clear();
94
 
        if (defaults) {
95
 
                while (*defaults) {
96
 
                        for (ptr = defaults; *ptr && (*ptr != ':'); ptr++){}
97
 
                        if (ptr != defaults) {
98
 
                                gDefaultMetaDataHeaders->beginRecord();
99
 
                                gDefaultMetaDataHeaders->setStringField(defaults, ptr - defaults);
100
 
                                gDefaultMetaDataHeaders->endRecord();
101
 
                        }
102
 
                        if (!*ptr)
103
 
                                break;
104
 
                                
105
 
                        defaults = ptr +1;
106
 
                }
107
 
        }
108
 
        exit_();
109
 
}
110
 
 
111
 
//----------------------------
112
 
void MSHTTPHeaderTable::loadTable(MSDatabase *db)
113
 
{
114
 
        CSPath          *path;
115
 
        SysTabRec       *headerData = NULL;
116
 
 
117
 
        enter_();
118
 
        
119
 
        push_(db);
120
 
        path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
121
 
        push_(path);
122
 
 
123
 
        if (path->exists()) {
124
 
                CSFile          *file;
125
 
                size_t          size;
126
 
                
127
 
                new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
128
 
                push_(headerData);
129
 
 
130
 
                file = path->openFile(CSFile::READONLY);
131
 
                push_(file);
132
 
                size = file->getEOF();
133
 
                headerData->setLength(size);
134
 
                file->read(headerData->getBuffer(0), 0, size, size);
135
 
                release_(file);
136
 
                
137
 
                
138
 
        } else if (gDefaultMetaDataHeaders) { // Load the defaults if they exist.
139
 
                headerData = gDefaultMetaDataHeaders;
140
 
        }
141
 
 
142
 
        if (headerData) {
143
 
                while (headerData->nextRecord()) {
144
 
                        const char      *header = headerData->getStringField();
145
 
                        
146
 
                        if (headerData->isValidRecord()) {
147
 
                                db->iHTTPMetaDataHeaders.add(CSString::newString(header));
148
 
                        }
149
 
                }
150
 
                
151
 
                if (headerData == gDefaultMetaDataHeaders)
152
 
                        gDefaultMetaDataHeaders->resetRecord();
153
 
                else {
154
 
                        release_(headerData); 
155
 
                }
156
 
        }
157
 
        
158
 
        release_(path); 
159
 
        release_(db);
160
 
 
161
 
        exit_();
162
 
}
163
 
 
164
 
//----------------------------
165
 
void MSHTTPHeaderTable::saveTable(MSDatabase *db)
166
 
{
167
 
        CSString *str;
168
 
        SysTabRec       *headerData;
169
 
        enter_();
170
 
        
171
 
        push_(db);
172
 
        
173
 
        new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
174
 
        push_(headerData);
175
 
        
176
 
        // Build the table records
177
 
        headerData->clear();
178
 
        lock_(&db->iHTTPMetaDataHeaders);
179
 
        
180
 
        // Note: the object returned by itemAt() is not returnd referenced.
181
 
        for (uint32_t i =0; (str = (CSString*) db->iHTTPMetaDataHeaders.itemAt(i)); i++) {
182
 
                headerData->beginRecord();
183
 
                headerData->setStringField(str->getCString());
184
 
                headerData->endRecord();
185
 
        }
186
 
        unlock_(&db->iHTTPMetaDataHeaders);
187
 
        
188
 
        restoreTable(RETAIN(db), headerData->getBuffer(0), headerData->length(), false);
189
 
        
190
 
        release_(headerData);
191
 
        release_(db);
192
 
        exit_();
193
 
}
194
 
 
195
 
//--------------------
196
 
CSStringBuffer *MSHTTPHeaderTable::dumpTable(MSDatabase *db)
197
 
{
198
 
 
199
 
        CSPath                  *path;
200
 
        CSStringBuffer  *dump;
201
 
 
202
 
        enter_();
203
 
        
204
 
        push_(db);
205
 
        path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
206
 
        release_(db);
207
 
        
208
 
        push_(path);
209
 
        new_(dump, CSStringBuffer(20));
210
 
        push_(dump);
211
 
 
212
 
        if (path->exists()) {
213
 
                CSFile  *file;
214
 
                size_t  size;
215
 
                
216
 
                file = path->openFile(CSFile::READONLY);
217
 
                push_(file);
218
 
                
219
 
                size = file->getEOF();
220
 
                dump->setLength(size);
221
 
                file->read(dump->getBuffer(0), 0, size, size);
222
 
                release_(file);
223
 
        }
224
 
        
225
 
        pop_(dump);
226
 
        release_(path);
227
 
        return_(dump);
228
 
}
229
 
 
230
 
//--------------------
231
 
void MSHTTPHeaderTable::restoreTable(MSDatabase *db, const char *data, size_t size, bool reload)
232
 
{
233
 
        CSPath  *path;
234
 
        CSFile  *file;
235
 
 
236
 
        enter_();
237
 
        
238
 
        push_(db);
239
 
        path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
240
 
        push_(path);
241
 
        
242
 
        file = path->openFile(CSFile::CREATE | CSFile::TRUNCATE);
243
 
        push_(file);
244
 
        
245
 
        file->write(data, 0, size);
246
 
        file->close();
247
 
        release_(file);
248
 
        
249
 
        release_(path);
250
 
        
251
 
        pop_(db);
252
 
        if (reload)
253
 
                loadTable(db);
254
 
        else
255
 
                db->release();
256
 
        exit_();
257
 
}
258
 
 
259
 
//--------------------
260
 
void MSHTTPHeaderTable::transferTable(MSDatabase *dst_db, MSDatabase *src_db)
261
 
{
262
 
        CSPath  *path;
263
 
        enter_();
264
 
        
265
 
        push_(src_db);
266
 
        push_(dst_db);
267
 
        
268
 
        path = CSPath::newPath(RETAIN(src_db->myDatabasePath), METADATA_HEADER_FILE".dat");
269
 
        push_(path);
270
 
        if (path->exists()) {
271
 
                CSPath  *bu_path;
272
 
                bu_path = CSPath::newPath(RETAIN(dst_db->myDatabasePath), METADATA_HEADER_FILE".dat");
273
 
                path->copyTo(bu_path, true);
274
 
        }
275
 
        
276
 
        release_(path);
277
 
        release_(dst_db);
278
 
        release_(src_db);
279
 
        
280
 
        exit_();
281
 
}
282
 
 
283
 
void MSHTTPHeaderTable::removeTable(CSString *db_path)
284
 
{
285
 
        CSPath  *path;
286
 
        enter_();
287
 
        
288
 
        path = getSysFile(db_path, METADATA_HEADER_FILE, 0);
289
 
        push_(path);
290
 
        
291
 
        path->removeFile();
292
 
        release_(path);
293
 
        exit_();
294
 
}
295
 
 
296
 
MSHTTPHeaderTable::MSHTTPHeaderTable(MSSystemTableShare *share, TABLE *table):
297
 
MSOpenSystemTable(share, table),
298
 
iHeaderIndex(0),
299
 
iDirty(false)
300
 
{
301
 
}
302
 
 
303
 
MSHTTPHeaderTable::~MSHTTPHeaderTable()
304
 
{
305
 
        //unuse();
306
 
}
307
 
 
308
 
void MSHTTPHeaderTable::use()
309
 
{
310
 
        myShare->mySysDatabase->iHTTPMetaDataHeaders.lock();
311
 
        iDirty = false;
312
 
}
313
 
 
314
 
void MSHTTPHeaderTable::unuse()
315
 
{
316
 
        if (iDirty) {
317
 
                saveTable(RETAIN(myShare->mySysDatabase));
318
 
                iDirty = false; 
319
 
        }
320
 
        myShare->mySysDatabase->iHTTPMetaDataHeaders.unlock();
321
 
        
322
 
}
323
 
 
324
 
 
325
 
void MSHTTPHeaderTable::seqScanInit()
326
 
{
327
 
        iHeaderIndex = 0;
328
 
}
329
 
 
330
 
bool MSHTTPHeaderTable::seqScanNext(char *buf)
331
 
{
332
 
        TABLE           *table = mySQLTable;
333
 
        Field           *curr_field;
334
 
        byte            *save;
335
 
        MY_BITMAP       *save_write_set;
336
 
        CSString        *header;
337
 
        const char      *name;
338
 
        
339
 
        enter_();
340
 
        
341
 
        header = (CSString*) (myShare->mySysDatabase->iHTTPMetaDataHeaders.itemAt(iHeaderIndex));
342
 
        if (!header)
343
 
                return_(false);
344
 
                
345
 
        iHeaderIndex++;
346
 
        name = header->getCString();
347
 
        
348
 
        save_write_set = table->write_set;
349
 
        table->write_set = NULL;
350
 
 
351
 
#ifdef DRIZZLED
352
 
        memset(buf, 0xFF, table->getNullBytes());
353
 
#else
354
 
        memset(buf, 0xFF, table->s->null_bytes);
355
 
#endif
356
 
        for (Field **field=GET_TABLE_FIELDS(table); *field ; field++) {
357
 
                curr_field = *field;
358
 
                save = curr_field->ptr;
359
 
#if MYSQL_VERSION_ID < 50114
360
 
                curr_field->ptr = (byte *) buf + curr_field->offset();
361
 
#else
362
 
#ifdef DRIZZLED
363
 
                curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->getTable()->getInsertRecord());
364
 
#else
365
 
                curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->table->record[0]);
366
 
#endif
367
 
#endif
368
 
                switch (curr_field->field_name[0]) {
369
 
                        case 'N':
370
 
                                ASSERT(strcmp(curr_field->field_name, "Name") == 0);
371
 
                                        curr_field->store(name, strlen(name), &UTF8_CHARSET);
372
 
                                        setNotNullInRecord(curr_field, buf);
373
 
                                break;
374
 
 
375
 
                }
376
 
                curr_field->ptr = save;
377
 
        }
378
 
 
379
 
        table->write_set = save_write_set;
380
 
        return_(true);
381
 
}
382
 
 
383
 
void MSHTTPHeaderTable::seqScanPos(unsigned char *pos)
384
 
{
385
 
        int32_t index = iHeaderIndex -1;
386
 
        if (index < 0)
387
 
                index = 0; // This is probably an error condition.
388
 
                
389
 
        mi_int4store(pos, index);
390
 
}
391
 
 
392
 
void MSHTTPHeaderTable::seqScanRead(unsigned char *pos, char *buf)
393
 
{
394
 
        iHeaderIndex = mi_uint4korr(pos);
395
 
        seqScanNext(buf);
396
 
}
397
 
 
398
 
void MSHTTPHeaderTable::insertRow(char *data) 
399
 
{
400
 
        CSString *header;
401
 
        String name;    
402
 
        enter_();
403
 
        
404
 
        getFieldValue(data, 0, &name);
405
 
 
406
 
        header = CSString::newString(name.c_str());
407
 
        myShare->mySysDatabase->iHTTPMetaDataHeaders.add(header);
408
 
        iDirty = true;
409
 
        
410
 
        exit_();
411
 
}
412
 
 
413
 
void MSHTTPHeaderTable::deleteRow(char *data) 
414
 
{
415
 
        CSString *header;
416
 
        String name;    
417
 
        enter_();
418
 
        
419
 
        getFieldValue(data, 0, &name);
420
 
        
421
 
        header = CSString::newString(name.c_str());
422
 
        push_(header);
423
 
        myShare->mySysDatabase->iHTTPMetaDataHeaders.remove(header);
424
 
        release_(header);
425
 
        iDirty = true;
426
 
                
427
 
        exit_();
428
 
}
429
 
 
430
 
void MSHTTPHeaderTable::updateRow(char *old_data, char *new_data) 
431
 
{
432
 
        enter_();
433
 
        insertRow(new_data);
434
 
        try_(a) {
435
 
                deleteRow(old_data);
436
 
        }
437
 
        catch_(a) {
438
 
                deleteRow(new_data);
439
 
                throw_();
440
 
        }
441
 
        cont_(a);
442
 
        exit_();
443
 
}
444