portalbuf.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * portalbuf.c--
 *    portal buffer support routines for src/libpq/portal.c
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *    $Header: /cvsroot/pgsql/src/backend/libpq/Attic/portalbuf.c,v 1.3 1996/11/06 08:48:29 scrappy Exp $
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
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
 *	pbuf_alloc 	  - allocate memory for libpq routines
 *	pbuf_free 	  - free memory for libpq routines
 *	pbuf_addPortal 	  - Allocate a new portal buffer
 *	pbuf_addGroup 	  - Add a new tuple group to the portal
 *	pbuf_addTypes 	  - Allocate n type blocks
 *	pbuf_addTuples 	  - Allocate a tuple block
 *	pbuf_addTuple 	  - Allocate a tuple of n fields (attributes)
 *	pbuf_addValues 	  - Allocate n bytes for a value
 *	pbuf_addEntry 	  - Allocate a portal entry
 *	pbuf_freeEntry    - Free a portal entry in the portal table
 *	pbuf_freeTypes 	  - Free up the space used by a portal 
 *	pbuf_freeTuples   - free space used by tuple block
 *	pbuf_freeGroup 	  - free space used by group, types and tuples
 *	pbuf_freePortal   - free space used by portal and portal's group
 *	pbuf_getIndex 	  - Return the index of the portal entry
 *	pbuf_setup 	  - Set up a portal for dumping data
 *	pbuf_close 	  - Close a portal, remove it from the portal table
 *	pbuf_findGroup 	  - Return group given the group_index
 *	pbuf_findFnumber  - Return field index of a given field within a group
 *	pbuf_findFname 	  - Find the field name given the field index
 *	pbuf_checkFnumber - signal an error if field number is out of bounds
 *
 * NOTES
 *	These functions may be used by both frontend routines which
 *	communicate with a backend or by user-defined functions which
 *	are compiled or dynamically loaded into a backend.
 *
 *	the portals[] array should be organized as a hash table for
 *	quick portal-by-name lookup.
 *
 *	Do not confuse "PortalEntry" (or "PortalBuffer") with "Portal"
 *	see utils/mmgr/portalmem.c for why. -cim 2/22/91
 *
 */
50
#include <string.h>
51 52
#include <sys/types.h>

53 54 55 56
#include <postgres.h>

#include <libpq/libpq.h>		/* where the declarations go */
#include <utils/exc.h>
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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

PortalEntry** portals = (PortalEntry**) NULL;
size_t portals_array_size = 0;

/* portals array memory is malloc'd instead of using MemoryContexts */
/* since it will be used by both front and backend programs*/
/*  GlobalMemory portals_mmcxt = (GlobalMemory) NULL;  */

/* ------------------------------- 
 * portals_realloc --
 *    grow the size of the portals array by size
 *
 *    also ensures that elements are initially NULL 
 */

static void
portals_realloc(size_t size)
{
    size_t oldsize;
    int i;
    PortalEntry** newp;
    
    oldsize = portals_array_size;
    
    portals_array_size += size;
    if (portals)
	newp= (PortalEntry**)realloc(portals,
				     portals_array_size*sizeof(PortalEntry*));
    else
	newp= (PortalEntry**)malloc(portals_array_size*sizeof(PortalEntry*));
    
    if (newp)
	portals = newp;
    else
	libpq_raise(&PortalError,
		    form("Cannot alloc more memory in portals_realloc"));
    
    for (i=oldsize;i<portals_array_size;i++)
	portals[i]=(PortalEntry*)NULL;
    
}

/* --------------------------------
 *	pbuf_alloc - allocate memory for portal buffers
 *
 *	remember: palloc() in the backend uses the postgres MemoryContext
 *	library and palloc() in the frontend (fe-pqstubs.c) calls malloc().
 * --------------------------------
 */
caddr_t
pbuf_alloc(size_t size)
{
    caddr_t 	addr;
    
    if (size <= 0)
	libpq_raise(&MemoryError, form("Invalid argument to pbuf_alloc()."));
    
    addr = (caddr_t) palloc(size);
    if (addr == (caddr_t) NULL)
	libpq_raise(&MemoryError, form("Cannot Allocate space."));
    
    return (addr);
}

/* --------------------------------
 *	pbuf_free - free memory for portal buffers
 *
 *	remember: pfree() in the backend uses the postgres MemoryContext
 *	library and pfree() in the frontend (fe-pqstubs.c) calls free().
 * --------------------------------
 */
void
pbuf_free(caddr_t pointer)
{
    if (pointer)
	pfree(pointer);
    else
	libpq_raise(&MemoryError, form("Tried to free NULL memory pointer"));
    
}

/* --------------------------------
 *	pbuf_addPortal - Allocate a new portal buffer
 * --------------------------------
 */
PortalBuffer *
pbuf_addPortal()
{
    PortalBuffer *portal;
    
    portal = (PortalBuffer *)
	pbuf_alloc(sizeof (PortalBuffer));
    
    portal->rule_p = 0;
    portal->no_tuples = 0;
    portal->no_groups = 0;
    portal->groups = NULL;
    
    return (portal);
}

/* --------------------------------
 *	pbuf_addGroup - Add a new tuple group to the portal
 * --------------------------------
 */
GroupBuffer *
pbuf_addGroup(PortalBuffer *portal)
{
    GroupBuffer *group, *group1;
    
    group = (GroupBuffer *)
	pbuf_alloc(sizeof (GroupBuffer));
    
    /* Initialize the new group buffer. */
    group->no_tuples  = 0;
    group->no_fields = 0;
    group->types = NULL;
    group->tuples = NULL;
    group->next = NULL;
    
    if ((group1 = portal->groups) == NULL)
	portal->groups = group;
    else {
	while (group1->next != NULL) 
	    group1 = group1->next;
	group1->next = group;
    }
    
    return (group);
}

/* --------------------------------
 *	pbuf_addTypes - Allocate n type blocks
 * --------------------------------
 */
TypeBlock *
pbuf_addTypes(int n)
{
    TypeBlock *types;
    
    types = (TypeBlock *)
	pbuf_alloc(n * sizeof (TypeBlock));
    
    return (types);
}

/* --------------------------------
 *	pbuf_addTuples - Allocate a tuple block
 * --------------------------------
 */
TupleBlock *
pbuf_addTuples()
{
    TupleBlock *tuples;
    
    tuples = (TupleBlock *)
	pbuf_alloc(sizeof (TupleBlock));
    
    tuples->next = NULL;
    tuples->tuple_index = 0;
    
    return (tuples);
}

/* --------------------------------
 *	pbuf_addTuple - Allocate a tuple of n fields (attributes)
 * --------------------------------
 */
char **
pbuf_addTuple(int n)
{
    return (char **)
	pbuf_alloc(n * sizeof (char *));
}

/* --------------------------------
 *	pbuf_addTupleValueLengths - Allocate a tuple of n lengths (attributes)
 * --------------------------------
 */
int *
pbuf_addTupleValueLengths(int n)
{
    return (int *)
	pbuf_alloc(n * sizeof(int));
}

/* --------------------------------
 *	pbuf_addValues - Allocate n bytes for a value
 * --------------------------------
 */
char *
pbuf_addValues(int n)
{
    return
	pbuf_alloc(n);
}

/* --------------------------------
 *	pbuf_addEntry - Allocate a portal entry
 * --------------------------------
 */
PortalEntry *pbuf_addEntry()
{
    return (PortalEntry *)
	pbuf_alloc (sizeof (PortalEntry));
}

/* --------------------------------
 *	pbuf_freeEntry - Free a portal entry in the portal table
 *	the portal is freed separately.
 * --------------------------------
 */
void
pbuf_freeEntry(int i)
{
    if (portals)
	{
	    pbuf_free ((caddr_t)portals[i]);
	    portals[i] = NULL;
	}
}


/* --------------------------------
 *	pbuf_freeTypes - Free up the space used by a portal 
 * --------------------------------
 */
void
pbuf_freeTypes(TypeBlock *types)
{
    pbuf_free((caddr_t)types);
}

/* --------------------------------
 *	pbuf_freeTuples - free space used by tuple block
 * --------------------------------
 */
void
pbuf_freeTuples(TupleBlock *tuples,
		int no_tuples,
		int no_fields)
{
    int i, j;
    
    if (no_tuples > TupleBlockSize) {
	pbuf_freeTuples (tuples->next, no_tuples - TupleBlockSize, no_fields);
	no_tuples = TupleBlockSize;
    }
    
    /* For each tuple, free all its attribute values. */
    for (i = 0; i < no_tuples; i++) {
	for (j = 0; j < no_fields; j++)
	    if (tuples->values[i][j] != NULL)
		pbuf_free((caddr_t)tuples->values[i][j]);
	if (tuples->lengths[i])
	    pbuf_free((caddr_t)tuples->lengths[i]);
	if (tuples->values[i])
	    pbuf_free((caddr_t)tuples->values[i]);
    }
    
    pbuf_free((caddr_t)tuples);
}

/* --------------------------------
 *	pbuf_freeGroup - free space used by group, types and tuples
 * --------------------------------
 */
void
pbuf_freeGroup(GroupBuffer *group)
{
    if (group->next != NULL)
	pbuf_freeGroup(group->next);
    
    if (group->types != NULL)
	pbuf_freeTypes(group->types);
    
    if (group->tuples != NULL)
	pbuf_freeTuples(group->tuples, group->no_tuples,group->no_fields);
    
    pbuf_free((caddr_t)group);
}

/* --------------------------------
 *	pbuf_freePortal - free space used by portal and portal's group
 * --------------------------------
 */
void
pbuf_freePortal(PortalBuffer *portal)
{
    if (portal->groups != NULL)
	pbuf_freeGroup(portal->groups);
    
    pbuf_free((caddr_t)portal);
}

/* --------------------------------
 *	pbuf_getIndex - Return the index of the portal entry
 * 	note: portals[] maps portal names to portal buffers.
 * --------------------------------
 */
int
pbuf_getIndex(char *pname)
{
    int i;
    
    if (portals) {
	for (i = 0; i < portals_array_size; i++) 
	    if (portals[i] != NULL &&
		strncmp(portals[i]->name, pname, PortalNameLength) == 0)
		return i;
    }
    
    return (-1);
}

/* --------------------------------
 *	pbuf_setportalname - assign a user given name to a portal
 * --------------------------------
 */
void
pbuf_setportalinfo(PortalEntry *entry, char *pname)
{
    if (entry)
	strncpy(entry->name, pname, PortalNameLength-1);
    entry->name[PortalNameLength-1] = '\0';
}

/* --------------------------------
 *	pbuf_setup - Set up a portal for dumping data
 * --------------------------------
 */
PortalEntry *
pbuf_setup(char *pname)
{
    int i;
    
    if (!portals) /* the portals array has not been allocated yet */
	{
	    /* allocate portals[] array here */
	    portals_realloc(PORTALS_INITIAL_SIZE);
	}
    
    /* If a portal with the same name already exists, close it. */
    /* else look for an empty entry in the portal table. */
    if ((i = pbuf_getIndex(pname)) != -1) 
	pbuf_freePortal(portals[i]->portal);
    else {
	for (i = 0; i < portals_array_size; i++)
	    if (portals[i] == NULL)
		break;
	
	/* If the portal table is full, enlarge it */
	if (i >= portals_array_size) 
	    portals_realloc(PORTALS_GROW_BY);
	
	portals[i] = pbuf_addEntry();
	strncpy(portals[i]->name, pname, PortalNameLength);
    }
    portals[i]->portal = pbuf_addPortal();
    portals[i]->portalcxt = NULL;
    portals[i]->result = NULL;
    
    return portals[i];
}

/* --------------------------------
 *	pbuf_close - Close a portal, remove it from the portal table
 *			and free up the space
 * --------------------------------
 */
void
pbuf_close(char *pname)
{
    int i;
    
    if ((i = pbuf_getIndex(pname)) == -1) 
	libpq_raise(&PortalError, form("Portal %s does not exist.", pname));
    
    pbuf_freePortal(portals[i]->portal);
    pbuf_freeEntry(i);
}

/* --------------------------------
 *	pbuf_findGroup - Return the group given the group_index
 * --------------------------------
 */
GroupBuffer *
pbuf_findGroup(PortalBuffer *portal,
	       int group_index)
{
    GroupBuffer *group;
    
    group = portal->groups;
    while (group_index > 0 && group != NULL) {
	group = group->next;
	group_index--;
    }
    
    if (group == NULL)
	libpq_raise(&PortalError, 
		    form("Group index %d out of bound.", group_index));
    
    return (group);
}

/* --------------------------------
 * pbuf_findFnumber - Return the field index of a given field within a group
 * --------------------------------
 */
int
pbuf_findFnumber(GroupBuffer *group,
		 char *field_name)
{	
    TypeBlock *types;
    int i;
    
    types = group->types;
    
    for (i = 0; i < group->no_fields; i++) 
	if (strncmp(types[i].name, field_name, NAMEDATALEN) == 0)
	    return (i);
    
    libpq_raise(&PortalError, 
		form("Field-name %s does not exist.", field_name));
 
     /* not reached, here to make compiler happy */
     return 0;

}

/* --------------------------------
 *	pbuf_checkFnumber - signal an error if field number is out of bounds
 * --------------------------------
 */
void
pbuf_checkFnumber(GroupBuffer *group,
		  int field_number)
{
    if (field_number < 0 || field_number >= group->no_fields)
	libpq_raise(&PortalError, 
		    form("Field number %d out of bound.", field_number));
}

/* --------------------------------
 *	pbuf_findFname - Find the field name given the field index
 * --------------------------------
 */
char *
pbuf_findFname(GroupBuffer *group,
	       int field_number)
{
    pbuf_checkFnumber(group, field_number);
    return
	(group->types[field_number]).name;
}