summaryrefslogtreecommitdiffstats
path: root/relblobif/blobtile.file.pgc
blob: 0547fdf019734504142ffdab5812c8fdc1cdb601 (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
400
401
402
/*
* 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>.
*/
#include "mymalloc/mymalloc.h"
// This is -*- C++ -*-
/*************************************************************
 *
 *
 * PURPOSE:
 * implements blobtile interface using the PostgreSQL DBMS.
 *
 *
 * COMMENTS:
 * - uses LO blob method, this has been tested to be faster than bytea
 * - RMDBG macros generate so weird output that I added DEBUG macros
 *   although this is very ugly (2 trace facilities in parallel)
 * - exceptions thrown are r_Error, either directly or through common
 *   function generateException() (reladminif) which throws r_Ebase_dbms
 *
 ************************************************************/

using namespace std;

static const char rcsid[] = "@(#)blobif,BLOBTile: $Id: blobtile.ec,v 1.8 2003/12/27 23:19:03 rasdev Exp $";

#include <stdio.h>
#include <stdlib.h>             /* atoi */

// PG stuff:
#include "libpq-fe.h"		/* C interface to PgSQL */
#include "libpq/libpq-fs.h"	/* large object (lo) api */

// simple trace facility
// trace macros are activated through externally defined compile variable DEBUG
#include "debug-srv.hh"

// general embedded SQL related definitions
EXEC SQL include "../reladminif/sqlglobals.h";

// libpg connection maintenance
// must have been initiated before use (see databasif.pgc)
extern PGconn *pgConn;

// all the DBMS independent code is factored out and
// will be included in the resulting .c file
#include "blobtile.hh"
#include "simplefilestorage.hh"
#include "raslib/error.hh"
#include "externs.h"
#include "raslib/rmdebug.hh"
#include "sqlerror.hh"
#include "tileid.hh"
#include "inlinetile.hh"
#include "objectbroker.hh"
#include "dbref.hh"


IFileStorage* initFileStorage() {
    SimpleFileStorage *fileStorage;
    char *path = getenv("RASDATA");
    if (path == NULL)
	path="/tmp";
    fileStorage = new SimpleFileStorage(path);
    return fileStorage;
}

// update blob in ras_tiles table, identified by variable myOId (from blobtile.cc), update map ref
void
BLOBTile::updateInDb() throw (r_Error)
{
	RMDBGENTER(3, RMDebug::module_blobif, "BLOBTile", "updateInDb() " << myOId);
	ENTER( "BLOBTile::updateInDb" );
	if (fileStorage == NULL) 
	    fileStorage = initFileStorage();

	Oid	tile;
	long	indbmyoId = 0;
	long	indbmyoid = 0;
	short	dataformat = 0;
	char pgQuery[SQL_QUERY_BUFFER_SIZE];
	PGresult *pgResult = NULL;

	// (1) --- get tuple 
	dataformat = dataFormat;
	indbmyoid = myOId.getCounter();

	fileStorage->update(cells, size, indbmyoid);

	// alternative solution for now:
	(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "UPDATE RAS_TILES SET DataFormat = %d WHERE BlobId = %d", dataformat, indbmyoid );
	TALK( pgQuery );
	pgResult = PQexec( pgConn, pgQuery );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		LEAVE( "BLOBTile::updateInDb() libpq 'update dataformat' error: " << PQerrorMessage(pgConn) );
		PQclear( pgResult );
		generateException();
	}

	// (4) --- update map ref
	DBObject::updateInDb();

	LEAVE( "BLOBTile::updateInDb, myOId=" << myOId );
	RMDBGEXIT(3, RMDebug::module_blobif, "BLOBTile", "updateInDb() " << myOId);
} // updateInDb()

// insert new blob into ras_tiles table, update map ref
// tuple is identified by blobtile.cc var myOId
// data is taken from buffer 'cells' containing 'size' bytes
void
BLOBTile::insertInDb() throw (r_Error)
{
	RMDBGENTER(3, RMDebug::module_blobif, "BLOBTile", "insertInDb() " << myOId);
	ENTER( "BLOBTile::insertInDb" );

	if (fileStorage == NULL) 
	    fileStorage = initFileStorage();

		Oid	blobOid;
		long	indbmyOId2;
		short	dataformat2;
		Oid 	tile2;
	dataformat2 = dataFormat;

	// prelim:
	char pgQuery[SQL_QUERY_BUFFER_SIZE];
	PGresult *pgResult = NULL;

	indbmyOId2 = myOId.getCounter();
	TALK( "myOId.getCounter = " << indbmyOId2 );

	fileStorage->insert(cells, size, indbmyOId2);

	// (2) --- insert tuple into db
	(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "INSERT INTO RAS_TILES ( BlobId, DataFormat, Tile) VALUES  ( %d, %d, %d )",  indbmyOId2, dataformat2, indbmyOId2 );
	TALK( pgQuery );
	pgResult = PQexec( pgConn, pgQuery );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		LEAVE( "BLOBTile::insertInDb() libpq 'insert' error: " << PQerrorMessage(pgConn) );
		PQclear( pgResult );
		generateException();
	}
	PQclear( pgResult );

	// FIXME: other sources have here an updateInDb();

	// (3) --- update map ref
	DBObject::insertInDb();

	LEAVE( "BLOBTile::insertInDb(), myOId=" << myOId );
	RMDBGEXIT(3, RMDebug::module_blobif, "BLOBTile", "insertInDb() " << myOId);
} // insertInDb()

// delete one tuple from ras_tiles table, update map ref
// tuple is identified by blobtile.cc var myOId
void
BLOBTile::deleteFromDb() throw (r_Error)
{
	RMDBGENTER(3, RMDebug::module_blobif, "BLOBTile", "deleteFromDb() " << myOId);
	ENTER( "BLOBTile::deleteFromDb" );
	if (fileStorage == NULL) 
	    fileStorage = initFileStorage();

		long blobId;	// blob tuple primary key
		Oid blobOid;	// blob oid "ptr"

	// get counter value (primary key) from oid
	blobId = myOId.getCounter();

	fileStorage->remove(blobId);
	PGresult *pgResult = NULL;
	char pgQuery[SQL_QUERY_BUFFER_SIZE];

	(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "DELETE FROM RAS_TILES WHERE BlobId = ", blobOid );
	TALK( pgQuery );
	pgResult = PQexec( pgConn, pgQuery );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		LEAVE( "BLOBTile::deleteFromDb() libpq 'delete tuple' error: " << PQerrorMessage(pgConn) );
		PQclear( pgResult );
		generateException();
	}
	PQclear( pgResult );

	// FIXME: other sources have here an updateInDb();

	// update map ref
	DBObject::deleteFromDb();

	LEAVE( "BLOBTile::deleteFromDb, myOId=" << myOId );
	RMDBGEXIT(3, RMDebug::module_blobif, "BLOBTile", "deleteFromDb() " << myOId);
}

// delete a range of tuple(s) from ras_tiles table, update map ref
// tuples are identified by target and a range
void
BLOBTile::kill(const OId& target, unsigned int range)
{
	RMDBGENTER(0, RMDebug::module_blobif, "BLOBTile", "kill(" << target << ", " << range <<")");
	ENTER( "BLOBTile::kill, target=" << target << ", range=" << range );

		long indbmyOId5;
		long indbmyOId6;
		long blobId;	// blob tuple primary key
		Oid blobOid;	// blob oid "ptr"
	char pgQuery[SQL_QUERY_BUFFER_SIZE];
	PGresult *pgResult = NULL;	// query result

	DBObject* targetobj = NULL;
	IFileStorage *fileStorage = initFileStorage();

	if (range == 0)		// single tuple
	{
		// (1) --- delete form cache
		targetobj = ObjectBroker::isInMemory(target);
		if (targetobj)
		{
			targetobj->setPersistent(false);
		}

		// (2) --- free blob
		indbmyOId5 = target.getCounter();
		fileStorage->remove(indbmyOId5);

		(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "DELETE FROM RAS_TILES WHERE BlobId = %d", indbmyOId5 );
		TALK( pgQuery );
		pgResult = PQexec( pgConn, pgQuery );
		if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
		{
			LEAVE( "BLOBTile::kill() libpq 'delete single' error: " << PQerrorMessage(pgConn) );
			PQclear( pgResult );
			generateException();
		}
		else if (PQntuples(pgResult) == 0)
		{
			if (target.getType() == OId::INLINETILEOID)
			{
				InlineTileId t(target);
				if (!t.is_null())
					t->setPersistent(false);
			}
			//else: this tile has been deleted before
		}
		PQclear( pgResult );
	}
	else
	{
		// (1) --- iterate over cache and remove
		DBObjectPMap& mapRef = ObjectBroker::getMap(target.getType());
		DBObjectPMap::iterator it = mapRef.begin();
		DBObjectPMap::iterator theEnd = mapRef.end();
		OId end(target.getCounter() + range, target.getType());
		while (it != theEnd)
		{
			if (target <= (const OId&)(*it).first && (*it).first <= (const OId&)end)
			{
				(*it).second->setPersistent(false);
			}
		}

		// (2) --- iterate over db and remove
		(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "DECLARE DeleteLoop CURSOR FOR SELECT Tile FROM RAS_TILES WHERE %d <= BlobId AND BlobId <= %d", indbmyOId5, indbmyOId6 );
		TALK( pgQuery );
		pgResult = PQexec( pgConn, pgQuery );
		if (PQresultStatus(pgResult) != PGRES_TUPLES_OK)
		{
			LEAVE( "BLOBTile::kill() libpq 'select for deleteLoop' error: " << PQerrorMessage(pgConn) );
			PQclear( pgResult );
			generateException();
		}

		// loop over elements & delete each one
		do
		{
			(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "FETCH NEXT FROM DeleteLoop" );
			TALK( pgQuery );
			pgResult = PQexec( pgConn, pgQuery );
			if (PQresultStatus(pgResult) != PGRES_TUPLES_OK)
			{
				LEAVE( "BLOBTile::kill() libpq 'fetch next' error: " << PQerrorMessage(pgConn) );
				PQclear( pgResult );
				generateException();
			}
			else if (PQntuples(pgResult) != 1)
			{
				LEAVE( "BLOBTile::kill() libpq 'fetch' did not yield 1 result but " << PQntuples(pgResult) );
				PQclear( pgResult );
				generateException();
			}
			blobOid = atoi( PQgetvalue( pgResult, 0, 0 ) );	// extract value from result

			// delete blob identified by blobOid
			fileStorage->remove(blobOid);
		// FIXME: what about errors? continue??
		} while (PQresultStatus(pgResult) != PGRES_TUPLES_OK);
		PQclear( pgResult );

		// (3) --- delete tuples in db
		indbmyOId5 = target.getCounter();
		indbmyOId6 = end.getCounter();
		(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "DELETE FROM RAS_TILES WHERE %d <= BlobId AND BlobId <= %d", indbmyOId5, indbmyOId6 );
		TALK( pgQuery );
		pgResult = PQexec( pgConn, pgQuery );
		if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
		{
			LEAVE( "BLOBTile::kill() libpq 'delete range' error: " << PQerrorMessage(pgConn) );
			PQclear( pgResult );
			generateException();
		}
		PQclear( pgResult );

	}

	LEAVE( "BLOBTile::kill" );
	RMDBGEXIT(0, RMDebug::module_blobif, "BLOBTile", "kill(" << target << " " << target.getType() << ")");
}

// read tuple from ras_tiles, identified by blobtile.cc var myOId
// allocates necessary mem into ptr 'cells' and fills it; must be freed elsewhere
// external var 'size' is set to the number of bytes read
void
BLOBTile::readFromDb() throw (r_Error)
{
	RMDBGENTER(3, RMDebug::module_blobif, "BLOBTile", "readFromDb() " << myOId);
	ENTER( "BLOBTile::readFromDb" );
	if (fileStorage == NULL) 
	    fileStorage = initFileStorage();

#ifdef RMANBENCHMARK
	DBObject::readTimer.resume();
#endif

		Oid	blobOid;
		long	indbmyOId3;
		short	dataformat3;
		short	indicatorr3;
	PGresult *pgResult = NULL;			// PostgreSQL call return values

	indbmyOId3 = myOId.getCounter();

	// (1) --- access tuple
	char pgQuery[SQL_QUERY_BUFFER_SIZE];
	
	(void) snprintf( pgQuery, (size_t) sizeof(pgQuery), "SELECT Tile, DataFormat FROM RAS_TILES WHERE BlobId = %d", indbmyOId3 );
	TALK( pgQuery );
	pgResult = PQexec( pgConn, pgQuery );
	if (PQresultStatus(pgResult) != PGRES_TUPLES_OK)
	{
		LEAVE( "BLOBTile::readFromDb() libpq 'select' error: " << PQerrorMessage(pgConn) );
		PQclear( pgResult );
		generateException();
	}
	else if (PQntuples(pgResult) != 1)
	{
		LEAVE( "BLOBTile::readFromDb() libpq 'select' did not yield 1 result but " << PQntuples(pgResult) );
		PQclear( pgResult );
		throw r_Error(r_Error::r_Error_ObjectUnknown);
	}
	blobOid     = atoi( PQgetvalue( pgResult, 0, 0 ) );	// extract 1st value from result
	dataformat3 = atoi( PQgetvalue( pgResult, 0, 1 ) );	// extract 2nd value from result
	PQclear( pgResult );

	// we have a tuple, extract data format
	dataFormat = (r_Data_Format)dataformat3;
	currentFormat = (r_Data_Format)dataformat3;
	TALK( "got dataFormat " << dataFormat );

	fileStorage->retrieve(indbmyOId3, &cells, &size);

	RMDBGIF(20, RMDebug::module_blobif, "BLOBTileOutput", for (int a = 0; a < size; a++)\
		 RMInit::dbgOut << " " << hex << (int)(cells[a]); RMInit::dbgOut << dec << endl;)

	DBObject::readFromDb();

#ifdef RMANBENCHMARK
	DBObject::readTimer.pause();
#endif

	LEAVE( "BLOBTile::readFromDb" );
	RMDBGEXIT(3, RMDebug::module_blobif, "BLOBTile", "readFromDb() " << myOId);
}