~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/pbms_enabled.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) 2010 PrimeBase Technologies GmbH
2
 
 * All rights reserved.
3
 
 * 
4
 
 * Redistribution and use in source and binary forms, with or without 
5
 
 * modification, are permitted provided that the following conditions are met:
6
 
 * 
7
 
 *     * Redistributions of source code must retain the above copyright notice, 
8
 
 *              this list of conditions and the following disclaimer.
9
 
 *     * Redistributions in binary form must reproduce the above copyright notice, 
10
 
 *              this list of conditions and the following disclaimer in the documentation 
11
 
 *              and/or other materials provided with the distribution.
12
 
 *     * Neither the name of the "PrimeBase Technologies GmbH" nor the names of its 
13
 
 *              contributors may be used to endorse or promote products derived from this 
14
 
 *              software without specific prior written permission.
15
 
 * 
16
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
17
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
18
 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
19
 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
20
 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
21
 
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
22
 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
23
 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
24
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
25
 
 * POSSIBILITY OF SUCH DAMAGE. 
26
 
 *  
27
 
 *
28
 
 * PrimeBase Media Stream for MySQL and Drizzle
29
 
 *
30
 
 *
31
 
 * Barry Leslie
32
 
 *
33
 
 * 2009-07-16
34
 
 *
35
 
 * H&G2JCtL
36
 
 *
37
 
 * PBMS interface used to enable engines for use with the PBMS daemon.
38
 
 *
39
 
 * For an example on how to build this into an engine have a look at the PBXT engine
40
 
 * in file ha_pbxt.cc. Search for 'PBMS_ENABLED'.
41
 
 *
42
 
 */
43
 
 
44
 
#ifndef DRIZZLED
45
 
#if defined(MSDOS) || defined(__WIN__)
46
 
#include "pbms_enabled.h"
47
 
 
48
 
// Windows is not supported yet so just stub out the functions..
49
 
bool pbms_initialize(const char *engine_name __attribute__((unused)), 
50
 
                                        bool isServer __attribute__((unused)), 
51
 
                                        bool isTransactional __attribute__((unused)), 
52
 
                                        PBMSResultPtr result __attribute__((unused)), 
53
 
                                        IsPBMSFilterFunc is_pbms_blob __attribute__((unused))
54
 
                                        ) { return true;}
55
 
void pbms_finalize() {}
56
 
int pbms_write_row_blobs(const TABLE *table __attribute__((unused)), 
57
 
                                                unsigned char *buf __attribute__((unused)), 
58
 
                                                PBMSResultPtr result __attribute__((unused))
59
 
                                                ){ return 0;}
60
 
int pbms_update_row_blobs(const TABLE *table __attribute__((unused)), 
61
 
                                                const unsigned char *old_row __attribute__((unused)), 
62
 
                                                unsigned char *new_row __attribute__((unused)), 
63
 
                                                PBMSResultPtr result __attribute__((unused))
64
 
                                                ){ return 0;}
65
 
int pbms_delete_row_blobs(const TABLE *table __attribute__((unused)), 
66
 
                                                const unsigned char *buf __attribute__((unused)), 
67
 
                                                PBMSResultPtr result __attribute__((unused))
68
 
                                                ){ return 0;}
69
 
int pbms_rename_table_with_blobs(const char *old_table_path __attribute__((unused)), 
70
 
                                                                const char *new_table_path __attribute__((unused)), 
71
 
                                                                PBMSResultPtr result __attribute__((unused))
72
 
                                                                ){ return 0;}
73
 
int pbms_delete_table_with_blobs(const char *table_path __attribute__((unused)), 
74
 
                                                                PBMSResultPtr result __attribute__((unused))
75
 
                                                                ){ return 0;}
76
 
void pbms_completed(TABLE *table __attribute__((unused)), 
77
 
                                        bool ok __attribute__((unused))
78
 
                                        ){}
79
 
#else
80
 
#define PBMS_API        pbms_enabled_api
81
 
 
82
 
#include "pbms_enabled.h"
83
 
#include "mysql_priv.h"
84
 
#include <mysql/plugin.h>
85
 
#define session_alloc(sess, size) thd_alloc(sess, size);
86
 
#define current_session current_thd
87
 
 
88
 
#define GET_BLOB_FIELD(t, i) (Field_blob *)(t->field[t->s->blob_field[i]])
89
 
#define DB_NAME(f) (f->table->s->db.str)
90
 
#define TAB_NAME(f) (*(f->table_name))
91
 
 
92
 
static PBMS_API pbms_api;
93
 
 
94
 
/* 
95
 
 * A callback function to check if the column is a PBMS BLOB. 
96
 
 * Can be NULL if no check is to be done. 
97
 
 */ 
98
 
static IsPBMSFilterFunc is_pbms_blob = NULL; 
99
 
 
100
 
//====================
101
 
bool pbms_initialize(const char *engine_name, bool isServer, bool isTransactional, PBMSResultPtr result, IsPBMSFilterFunc is_pbms_blob_arg)
102
 
{
103
 
        int     err;
104
 
        PBMSEngineRec enabled_engine = {
105
 
                MS_ENGINE_VERSION,
106
 
                0,
107
 
                0,
108
 
                0,
109
 
                {0},    
110
 
                0
111
 
        };
112
 
 
113
 
        strncpy(enabled_engine.ms_engine_name, engine_name, 32);
114
 
        enabled_engine.ms_internal = isServer;
115
 
        enabled_engine.ms_has_transactions = isTransactional;
116
 
        enabled_engine.ms_engine_name[31] = 0;
117
 
 
118
 
        err = pbms_api.registerEngine(&enabled_engine, result);
119
 
        is_pbms_blob = is_pbms_blob_arg;
120
 
 
121
 
        return (err == 0);
122
 
}
123
 
 
124
 
 
125
 
//====================
126
 
void pbms_finalize(const char *engine_name)
127
 
{
128
 
        pbms_api.deregisterEngine(engine_name);
129
 
}
130
 
 
131
 
//==================================
132
 
static int insertRecord(Field_blob *field, char *blob,  size_t org_length, unsigned char *blob_rec, size_t packlength, PBMSResultPtr result)
133
 
{
134
 
        int err;
135
 
        size_t length;
136
 
        PBMSBlobURLRec blob_url;
137
 
        
138
 
        err = pbms_api.retainBlob(DB_NAME(field), TAB_NAME(field), &blob_url, blob, org_length, field->position(), result);
139
 
        if (err)
140
 
                return err;
141
 
                
142
 
        // If the BLOB length changed reset it. 
143
 
        // This will happen if the BLOB data was replaced with a BLOB reference. 
144
 
        length = strlen(blob_url.bu_data)  +1;
145
 
        if ((length != org_length) || memcmp(blob_url.bu_data, blob, length)) {
146
 
                if (length != org_length) {
147
 
                        field->store_length(blob_rec, packlength, length);
148
 
                }
149
 
                
150
 
                if (length > org_length) {
151
 
                        // This can only happen if the BLOB URL is actually larger than the BLOB itself.
152
 
                        blob = (char *) session_alloc(current_session, length);
153
 
                        memcpy(blob_rec+packlength, &blob, sizeof(char*));
154
 
                }                       
155
 
                memcpy(blob, blob_url.bu_data, length);
156
 
        } 
157
 
                
158
 
        return 0;
159
 
}
160
 
 
161
 
//====================
162
 
int pbms_update_row_blobs(const TABLE *table, const unsigned char *old_row, unsigned char *new_row, PBMSResultPtr result)
163
 
{
164
 
        Field_blob *field;
165
 
        uint32_t field_offset;
166
 
        const unsigned char *old_blob_rec;
167
 
        unsigned char *new_blob_rec;
168
 
        char *old_blob_url, *new_blob_url;
169
 
        size_t packlength, i, old_length, new_length;
170
 
        int err;
171
 
        bool old_null_blob, new_null_blob;
172
 
 
173
 
        result->mr_had_blobs = false;
174
 
        
175
 
        if (!pbms_api.isPBMSLoaded())
176
 
                return 0;
177
 
                
178
 
        if (table->s->blob_fields == 0)
179
 
                return 0;
180
 
                
181
 
        for (i= 0; i < table->s->blob_fields; i++) {
182
 
                field = GET_BLOB_FIELD(table, i);
183
 
 
184
 
                old_null_blob = field->is_null_in_record(old_row);
185
 
                new_null_blob = field->is_null_in_record(new_row);
186
 
                if (old_null_blob && new_null_blob)
187
 
                        continue;
188
 
 
189
 
                {
190
 
                        String type_name;
191
 
                        // Note: field->type() always returns MYSQL_TYPE_BLOB regardless of the type of BLOB
192
 
                        field->sql_type(type_name);
193
 
                        if (strcasecmp(type_name.c_ptr(), "LongBlob"))
194
 
                                continue;
195
 
                }
196
 
                        
197
 
                if( is_pbms_blob && !is_pbms_blob(field) )
198
 
                        continue;
199
 
                        
200
 
                        
201
 
                // Get the blob record:
202
 
                field_offset = field->offset(field->table->record[0]);
203
 
                packlength = field->pack_length() - field->table->s->blob_ptr_size;
204
 
 
205
 
                if (new_null_blob) {
206
 
                        new_blob_url = NULL;
207
 
                } else {
208
 
                        new_blob_rec = new_row + field_offset;
209
 
                        new_length = field->get_length(new_blob_rec);
210
 
                        memcpy(&new_blob_url, new_blob_rec +packlength, sizeof(char*));
211
 
                }
212
 
                
213
 
                if (old_null_blob) {
214
 
                        old_blob_url = NULL;
215
 
                } else {
216
 
                        old_blob_rec = old_row + field_offset;
217
 
                        old_length = field->get_length(old_blob_rec);
218
 
                        memcpy(&old_blob_url, old_blob_rec +packlength, sizeof(char*));
219
 
                }
220
 
                
221
 
                // Check to see if the BLOBs are the same.
222
 
                // I am assuming that if the BLOB pointer is different then teh BLOB has changed.
223
 
                // Zero length BLOBs are a special case because they may have a NULL data pointer,
224
 
                // to catch this and distiguish it from a NULL BLOB I do a check to see if one field was NULL:
225
 
                // (old_null_blob != new_null_blob)
226
 
                if ((old_blob_url != new_blob_url) || (old_null_blob != new_null_blob)) {
227
 
                        
228
 
                        result->mr_had_blobs = true;
229
 
 
230
 
                        // The BLOB was updated so delete the old one and insert the new one.
231
 
                        if ((old_null_blob == false) && (err = pbms_api.releaseBlob(DB_NAME(field), TAB_NAME(field), old_blob_url, old_length, result)))
232
 
                                return err;
233
 
                                
234
 
                        if ((new_null_blob == false) && (err = insertRecord(field, new_blob_url, new_length, new_blob_rec, packlength, result)))
235
 
                                return err;
236
 
                } 
237
 
        }
238
 
        
239
 
        return 0;
240
 
}
241
 
 
242
 
//====================
243
 
int pbms_write_row_blobs(const TABLE *table, unsigned char *row_buffer, PBMSResultPtr result)
244
 
{
245
 
 
246
 
        Field_blob *field;
247
 
        unsigned char *blob_rec;
248
 
        char *blob_url;
249
 
        size_t packlength, i, length;
250
 
        int err;
251
 
 
252
 
        result->mr_had_blobs = false;
253
 
 
254
 
        if (!pbms_api.isPBMSLoaded())
255
 
                return 0;
256
 
                
257
 
        if (table->s->blob_fields == 0)
258
 
                return 0;
259
 
                
260
 
        for (i= 0; i <  table->s->blob_fields; i++) {
261
 
                field =  GET_BLOB_FIELD(table, i);
262
 
                
263
 
                if (field->is_null_in_record(row_buffer))
264
 
                        continue;
265
 
                        
266
 
                {
267
 
                        String type_name;
268
 
                        // Note: field->type() always returns MYSQL_TYPE_BLOB regardless of the type of BLOB
269
 
                        field->sql_type(type_name);
270
 
                        if (strcasecmp(type_name.c_ptr(), "LongBlob"))
271
 
                                continue;
272
 
                }
273
 
                        
274
 
                if( is_pbms_blob && !is_pbms_blob(field) )
275
 
                        continue;
276
 
 
277
 
                result->mr_had_blobs = true;
278
 
 
279
 
                // Get the blob record:
280
 
                packlength = field->pack_length() - field->table->s->blob_ptr_size;
281
 
                blob_rec = row_buffer + field->offset(field->table->record[0]);
282
 
                
283
 
                length = field->get_length(blob_rec);
284
 
                memcpy(&blob_url, blob_rec +packlength, sizeof(char*));
285
 
 
286
 
                if ((err = insertRecord(field, blob_url, length, blob_rec, packlength, result)))
287
 
                        return err;
288
 
        }
289
 
 
290
 
        return 0;
291
 
}
292
 
 
293
 
//====================
294
 
int pbms_delete_row_blobs(const TABLE *table, const unsigned char *row_buffer, PBMSResultPtr result)
295
 
{
296
 
        Field_blob *field;
297
 
        const unsigned char *blob_rec;
298
 
        char *blob;
299
 
        size_t packlength, i, length;
300
 
        bool call_failed = false;
301
 
        int err;
302
 
        
303
 
        result->mr_had_blobs = false;
304
 
 
305
 
        if (!pbms_api.isPBMSLoaded())
306
 
                return 0;
307
 
                
308
 
        if (table->s->blob_fields == 0)
309
 
                return 0;
310
 
                
311
 
        for (i= 0; i < table->s->blob_fields; i++) {
312
 
                field = GET_BLOB_FIELD(table, i);
313
 
                
314
 
                if (field->is_null_in_record(row_buffer))
315
 
                        continue;
316
 
                        
317
 
                {
318
 
                        String type_name;
319
 
                        // Note: field->type() always returns MYSQL_TYPE_BLOB regardless of the type of BLOB
320
 
                        field->sql_type(type_name);
321
 
                        if (strcasecmp(type_name.c_ptr(), "LongBlob"))
322
 
                                continue;
323
 
                }
324
 
                        
325
 
                if(is_pbms_blob && !is_pbms_blob(field) )
326
 
                        continue;
327
 
 
328
 
                result->mr_had_blobs = true;    
329
 
                
330
 
                // Get the blob record:
331
 
                packlength = field->pack_length() - field->table->s->blob_ptr_size;
332
 
 
333
 
                blob_rec = row_buffer + field->offset(field->table->record[0]);
334
 
                length = field->get_length(blob_rec);
335
 
                memcpy(&blob, blob_rec +packlength, sizeof(char*));
336
 
 
337
 
                // Signal PBMS to delete the reference to the BLOB.
338
 
                err = pbms_api.releaseBlob(DB_NAME(field), TAB_NAME(field), blob, length, result);
339
 
                if (err)
340
 
                        return err;
341
 
        }
342
 
        
343
 
        return 0;
344
 
}
345
 
 
346
 
#define MAX_NAME_SIZE 64
347
 
static void parse_table_path(const char *path, char *db_name, char *tab_name)
348
 
{
349
 
        const char *ptr = path + strlen(path) -1, *eptr;
350
 
        int len;
351
 
        
352
 
        *db_name = *tab_name = 0;
353
 
        
354
 
        while ((ptr > path) && (*ptr != '/'))ptr --;
355
 
        if (*ptr != '/') 
356
 
                return;
357
 
                
358
 
        strncpy(tab_name, ptr+1, MAX_NAME_SIZE);
359
 
        tab_name[MAX_NAME_SIZE-1] = 0;
360
 
        eptr = ptr;
361
 
        ptr--;
362
 
        
363
 
        while ((ptr > path) && (*ptr != '/'))ptr --;
364
 
        if (*ptr != '/') 
365
 
                return;
366
 
        ptr++;
367
 
        
368
 
        len = eptr - ptr;
369
 
        if (len >= MAX_NAME_SIZE)
370
 
                len = MAX_NAME_SIZE-1;
371
 
                
372
 
        memcpy(db_name, ptr, len);
373
 
        db_name[len] = 0;
374
 
        
375
 
}
376
 
 
377
 
//====================
378
 
int pbms_rename_table_with_blobs(const char *old_table_path, const char *new_table_path, PBMSResultPtr result)
379
 
{
380
 
        char o_db_name[MAX_NAME_SIZE], n_db_name[MAX_NAME_SIZE], o_tab_name[MAX_NAME_SIZE], n_tab_name[MAX_NAME_SIZE];
381
 
 
382
 
        result->mr_had_blobs = false; 
383
 
        if (!pbms_api.isPBMSLoaded())
384
 
                return 0;
385
 
                
386
 
        result->mr_had_blobs = true; // Assume it has blobs.
387
 
        
388
 
        parse_table_path(old_table_path, o_db_name, o_tab_name);
389
 
        parse_table_path(new_table_path, n_db_name, n_tab_name);
390
 
        
391
 
        return pbms_api.renameTable(o_db_name, o_tab_name, n_db_name, n_tab_name, result);
392
 
}
393
 
 
394
 
//====================
395
 
int pbms_delete_table_with_blobs(const char *table_path, PBMSResultPtr result)
396
 
{
397
 
        char db_name[MAX_NAME_SIZE], tab_name[MAX_NAME_SIZE];
398
 
                
399
 
        result->mr_had_blobs = false; 
400
 
        if (!pbms_api.isPBMSLoaded())
401
 
                return 0;
402
 
                
403
 
        result->mr_had_blobs = true; // Assume it has blobs.
404
 
        parse_table_path(table_path, db_name, tab_name);
405
 
 
406
 
        return pbms_api.dropTable(db_name, tab_name, result);
407
 
}
408
 
 
409
 
//====================
410
 
void pbms_completed(const TABLE *table, bool ok)
411
 
{
412
 
        if (!pbms_api.isPBMSLoaded())
413
 
                return;
414
 
                
415
 
        if ((!table) || (table->s->blob_fields != 0))
416
 
                pbms_api.completed(ok) ;
417
 
                
418
 
         return ;
419
 
}
420
 
#endif
421
 
#endif // DRIZZLED