summaryrefslogtreecommitdiffstats
path: root/rasodmg/test/test_db2blob.sqC
blob: a77a168808c47663aa624c82a2f00e771d9093a7 (plain)
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
// -*-C++-*- (for Emacs)
/*
* This file is part of rasdaman community.
*
* Rasdaman community 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 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * SOURCE: test_db2blob.cc
 *
 * MODULE: rasodmg
 *
 * PURPOSE:
 *
 * COMMENTS:
 *
 ************************************************************/

#ifdef LINUX
#define __EXECUTABLE__
#include "raslib/template_inst.hh"
#endif

#ifdef __VISUALC__
#include <strstrea.h>
#else
#include <strstream.h>
#endif

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h> // for drand48()

#include "rasodmg/marray.hh"
#include "raslib/shhopt.h"

#include <sys/time.h>

#include "sql.h"

// number of repetitions for each query
unsigned long repeat = 20;

static int printFlag = 0;

EXEC SQL INCLUDE SQLCA;

#define  CHECKERR(CE_STR)   if (check_error (CE_STR, &sqlca) != 0)

// copied from DB2 example program
int check_error (char eString[], struct sqlca *caPointer) {
   char eBuffer[1024];
   char sBuffer[1024];
   short rc, Erc;

   if (caPointer->sqlcode != 0) {
      printf ("--- error report ---\n");
      printf ("ERROR occured : %s.\nSQLCODE : %ld\n", eString,
         caPointer->sqlcode);

      /**********************\
      * GET SQLSTATE MESSAGE *
      \**********************/
      rc = sqlogstt (sBuffer, 1024, 80, caPointer->sqlstate);

      /******************************\
      * GET ERROR MESSAGE API called *
      \******************************/
      Erc = sqlaintp (eBuffer, 1024, 80, caPointer);

      /* return code is the length of the eBuffer string */
      if (Erc > 0) printf ("%s", eBuffer);

      if (caPointer->sqlcode < 0) {
	     if (rc == 0) {
            printf ("\n%s", sBuffer);
		 }
         printf ("--- end error report ---\n");
         return 1;
      } else {
      /* errorCode is just a Warning message */
	     if (rc == 0) {
            printf ("\n%s", sBuffer);
		 }
         printf ("--- end error report ---\n");
         printf ("WARNING - CONTINUING PROGRAM WITH WARNINGS!\n");
         return 0;
      } /* endif */
   } /* endif */
   return 0;
}

class BMTimer
{
public:
  /// constructor, initializes members
  inline BMTimer();

  inline void start();
  inline void stop();

private:  
  /// reference parameter for gettimeofday().
  timeval acttime;
  // reference parameter for gettimeofday, not used.
  static struct timezone dummy;
  /// used to calculate time spent in function.
  static long oldsec;
  /// used to calculate time spent in function.
  static long oldusec;
};

struct timezone BMTimer::dummy;
long BMTimer::oldsec;
long BMTimer::oldusec;

inline
BMTimer::BMTimer()
{
  oldsec = 0;
  oldusec = 0;
}

inline void
BMTimer::start()
{
  gettimeofday(&acttime, &dummy);
}

inline void
BMTimer::stop()
{
  oldsec = acttime.tv_sec;
  oldusec = acttime.tv_usec;
  gettimeofday(&acttime, &dummy);
  cout << (acttime.tv_sec-oldsec)*1000000 + acttime.tv_usec
                    - oldusec << "us";
}

BMTimer myTimer;

r_Marray<char>*
blobRead(r_Minterval& sd)
{
  EXEC SQL BEGIN DECLARE SECTION;
    long beginLoc;
    long endLoc;
    SQL TYPE IS BLOB_LOCATOR tomoLoc;
    short tomo_ind;
    // buf will be a structure with a char[] element called data and
    // an unsigned long called length
    SQL TYPE IS BLOB(256) blobBuf;
  EXEC SQL END DECLARE SECTION;

  int i,j;
  // storing result
  r_Marray<char>* result = new r_Marray<char>(sd);
  char* resBuf = result->get_array();

  EXEC SQL CONNECT TO sample;

  myTimer.start();

  EXEC SQL DECLARE curs1 CURSOR FOR
    SELECT blob_col
    FROM   tomo_blob;

  EXEC SQL OPEN curs1;

  EXEC SQL FETCH curs1 INTO :tomoLoc :tomo_ind;
  if (SQLCODE != 0) {
    cout << "FETCH curs1: " << SQLCODE << endl;
  }

  if(tomo_ind < 0)
    cout << "No BLOB there!" << endl;

  // iterating through the Minterval
  for(i=sd[0].low(); i<=sd[0].high(); i++) {
    for(j=sd[1].low(); j<=sd[1].high(); j++) {
      beginLoc = sd[2].low() + j*256 + i*256*256;
      endLoc = sd[2].high() + j*256 + i*256*256;
      unsigned long rowSize = endLoc - beginLoc + 1;
      EXEC SQL VALUES (SUBSTR( :tomoLoc, :beginLoc, :endLoc - :beginLoc + 1))
	INTO :blobBuf;
      memcpy(resBuf, blobBuf.data, rowSize);
      resBuf += rowSize;
    }
  }

  EXEC SQL CLOSE curs1;

  myTimer.stop();

  EXEC SQL CONNECT RESET;

  return result;
}

r_Marray<char>*
blob1DRead(float sel)
{
  EXEC SQL BEGIN DECLARE SECTION;
    long beginLoc2;
    long endLoc2;
    SQL TYPE IS BLOB_LOCATOR tomoLoc2;
    short tomo_ind2;
    // buf will be a structure with a char[] element called data and
    // an unsigned long called length
    SQL TYPE IS BLOB(4000000) blobBuf2;
  EXEC SQL END DECLARE SECTION;

  int i,j;
  // storing result
  r_Minterval* sd_res;
  sd_res = new r_Minterval("[0:255,0:255,0:153]");
  r_Marray<char>* result = new r_Marray<char>(*sd_res);
  char* resBuf = result->get_array();

  EXEC SQL CONNECT TO sample;

  EXEC SQL DECLARE curs2 CURSOR FOR
    SELECT blob_col
    FROM   tomo_blob;

  EXEC SQL OPEN curs2;

  EXEC SQL FETCH curs2 INTO :tomoLoc2 :tomo_ind2;
  if (SQLCODE != 0) {
    cout << "FETCH curs2: " << SQLCODE << endl;
  }
  if(tomo_ind2 < 0)
    cout << "No BLOB there!" << endl;

  unsigned long rowSize = (int)(sel/100 * 10092544);
  beginLoc2 = (int)(drand48() * (10092544 - rowSize));
  endLoc2 = beginLoc2 + rowSize;

  EXEC SQL VALUES (SUBSTR( :tomoLoc2, :beginLoc2, :endLoc2 - :beginLoc2 + 1))
    INTO :blobBuf2;

  memcpy(resBuf, blobBuf2.data, rowSize);
  resBuf += rowSize;

  EXEC SQL CLOSE curs2;

  EXEC SQL CONNECT RESET;

  delete sd_res;
  return result;
}

void
blobInsert( char *fileName )
{
  EXEC SQL BEGIN DECLARE SECTION;
    SQL TYPE IS BLOB_FILE tomoFile;
  EXEC SQL END DECLARE SECTION;

  EXEC SQL CONNECT TO sample;
  CHECKERR ("CONNECT TO") exit(1);

  strcpy(tomoFile.name, fileName);
  tomoFile.name_length = strlen(fileName);
  tomoFile.file_options = SQL_FILE_READ;

  EXEC SQL INSERT INTO tomo_blob
    VALUES(:tomoFile);
  CHECKERR ("INSERT INTO") exit(1);

  EXEC SQL CONNECT RESET;
}

void
blob1DExec(int queryNum, float sel)
{
  int i;
  // storing the result
  r_Marray<char>* res;

  for(i=1; i<=repeat; i++) {
    myTimer.start();
    res = blob1DRead(sel);
    cout << queryNum << "." << i << ": ";
    myTimer.stop();
    cout << endl;
    delete res;
  }

  // optionally printing the result
  if( printFlag )
    ((r_GMarray*)(res))->r_GMarray::print_status();
}

void
execQuery(char* myRectStr, int queryNum, int i)
{
  // storing the result
  r_Marray<char>* res;
  // the query rectangle
  r_Minterval* sd_res;
  
  sd_res = new r_Minterval(myRectStr);

  cout << queryNum << "." << i << ": ";
  res = blobRead(*sd_res);
  cout << endl;
  delete res;

  delete sd_res;

  // optionally printing the result
  if( printFlag )
    ((r_GMarray*)(res))->r_GMarray::print_status();
}

static void 
printUsage(void)
{
  cout << "Usage: test_db2blob [options] aFileName" 
       << endl;
  cout << "  aFileName                   Name of file with query rectangles "
       << endl
       << "                              or BLOB data when inserting." << endl;
  cout << "  -h, --help                  Print this message and exit." << endl;
  cout << "  -i, --insert                Insert BLOB instead of querying" 
       << endl;
  cout << "  -p, --print                 Print data queried."
       << endl;
  exit(0);
}

int 
main( int argc, char** argv )
{
  int iFlag = 0;
  int queryNum = 0;
  char fName[1024] = "";
  float selArray[] = { 0.5, 1, 2, 5, 10, 20, 50, 100 };

  optStruct testDB2BLOBOpt[] = {
    /* short long           type        var/func        special    */
    { 'h',   "help",        OPT_FLAG,   printUsage,     OPT_CALLFUNC },
    { 'i',   "insert",      OPT_FLAG,   &iFlag,         0 },
    { 'p',   "print",       OPT_FLAG,   &printFlag,     0 },
    { 0, 0, OPT_END, 0, 0 }  /* no more options */
  };

  /* parse all options */
  optParseOptions(&argc, argv, testDB2BLOBOpt, 0);

  strcpy(fName, argv[argc-1]);

  if( iFlag ) {
      blobInsert( fName );
      exit(1);
  }

  ifstream fileStream( fName );
  char buf[256];
  char dummy;

  int j = 0;
  while( fileStream.get( buf, 255, '\n' ) ) {
    fileStream.get(dummy);
    if((buf[0] == '/' && buf[1] == '/') || buf[0] == 0) {
      queryNum++;
      cout << "Query " << queryNum << ": " << buf << endl;
      j=0;
    } else if(buf[0] != 0) { 
      execQuery(buf, queryNum, ++j);
    }
  }

//   // Selectivity 50% and 100% does not work
//   for(int i=0; i<6; i++) {
//     cout << "BLOB selectivity " << selArray[i] << endl;
//     blob1DExec(++queryNum, selArray[i]);
//   }
}