/*-------------------------------------------------------------------------
*
- * chunk.c--
+ * chunk.c
*
* Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.7 1997/09/07 04:50:04 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.23 1999/07/17 20:17:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
-#include <libpq/be-fsstubs.h>
-#include "utils/memutils.h"
-#include "libpq/libpq-fs.h"
-
-#include "storage/fd.h" /* for SEEK_ */
-
#include "catalog/pg_type.h"
-
#include "fmgr.h"
-#include "utils/array.h"
-
+#include "libpq/be-fsstubs.h"
+#include "libpq/libpq-fs.h"
#include "optimizer/internal.h"
-#ifndef HAVE_MEMMOVE
-#include <regex/utils.h>
-#else
-#include <string.h>
-#endif
-
+#include "utils/array.h"
+#include "utils/memutils.h"
#define INFTY 500000000
#define MANY 10000
static CHUNK_INFO cInfo;
/* non-export function prototypes */
-static int
-_FindBestChunk(int size, int dmax[], int dbest[], int dim,
+static int _FindBestChunk(int size, int *dmax, int *dbest, int dim,
int A[MAXPAT][MAXDIM + 1], int N);
-static int get_next(int d[], int k, int C, int dmax[]);
-static void initialize_info(CHUNK_INFO * A, int ndim, int dim[], int chunk[]);
+static int get_next(int *d, int k, int C, int *dmax);
+static void initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk);
#ifdef LOARRAY
-static void
-_ConvertToChunkFile(int n, int baseSize, int dim[], int C[],
+static void _ConvertToChunkFile(int n, int baseSize, int *dim, int *C,
int srcfd, int destfd);
-static void
-read_chunk(int chunk_no[], int C[], char a_chunk[], int srcfd,
- int n, int baseSize, int PX[], int dist[]);
-static int write_chunk(struct varlena * a_chunk, int ofile);
-static int seek_and_read(int pos, int size, char buff[], int fp, int from);
+static void read_chunk(int *chunk_no, int *C, char *a_chunk, int srcfd,
+ int n, int baseSize, int *PX, int *dist);
+static int write_chunk(struct varlena * a_chunk, int ofile);
+static int seek_and_read(int pos, int size, char *buff, int fp, int from);
#endif
-static int
-GetChunkSize(FILE * fd, int ndim, int dim[MAXDIM], int baseSize,
+static int GetChunkSize(FILE *fd, int ndim, int dim[MAXDIM], int baseSize,
int d[MAXDIM]);
/*------------------------------------------------------------------------
* information about the chunked file
*-----------------------------------------------------------------------
*/
-char *
+char *
_ChunkArray(int fd,
- FILE * afd,
+ FILE *afd,
int ndim,
- int dim[],
+ int *dim,
int baseSize,
int *nbytes,
char *chunkfile)
{
#ifdef LOARRAY
- int cfd = 0;
+ int cfd = 0;
#endif
- int chunk[MAXDIM],
- csize;
- bool reorgFlag;
+ int chunk[MAXDIM],
+ csize;
+ bool reorgFlag;
if (chunkfile == NULL)
reorgFlag = true;
else
cfd = LOopen(chunkfile, O_RDONLY);
if (cfd < 0)
- elog(WARN, "Unable to open chunk file");
+ elog(ERROR, "Unable to open chunk file");
#endif
strcpy(cInfo.lo_name, chunkfile);
}
/*--------------------------------------------------------------------------
- * GetChunkSize --
+ * GetChunkSize
* given an access pattern and array dimensionality etc, this program
* returns the dimensions of the chunk in "d"
*-----------------------------------------------------------------------
*/
static int
-GetChunkSize(FILE * fd,
+GetChunkSize(FILE *fd,
int ndim,
int dim[MAXDIM],
int baseSize,
int d[MAXDIM])
{
- int N,
- i,
- j,
- csize;
- int A[MAXPAT][MAXDIM + 1],
- dmax[MAXDIM];
+ int N,
+ i,
+ j,
+ csize;
+ int A[MAXPAT][MAXDIM + 1],
+ dmax[MAXDIM];
/*
* ----------- read input ------------
*/
fscanf(fd, "%d", &N);
if (N > MAXPAT)
- elog(WARN, "array_in: too many access pattern elements");
+ elog(ERROR, "array_in: too many access pattern elements");
for (i = 0; i < N; i++)
for (j = 0; j < ndim + 1; j++)
if (fscanf(fd, "%d ", &(A[i][j])) == EOF)
- elog(WARN, "array_in: bad access pattern input");
+ elog(ERROR, "array_in: bad access pattern input");
/*
* estimate chunk size
}
/*-------------------------------------------------------------------------
- * _FindBestChunk --
+ * _FindBestChunk
* This routine does most of the number crunching to compute the
* optimal chunk shape.
* Called by GetChunkSize
*/
static int
_FindBestChunk(int size,
- int dmax[],
- int dbest[],
+ int *dmax,
+ int *dbest,
int dim,
int A[MAXPAT][MAXDIM + 1],
int N)
{
- int d[MAXDIM];
- int tc,
- mintc = INFTY;
+ int d[MAXDIM];
+ int tc,
+ mintc = INFTY;
d[0] = 0;
mintc = INFTY;
{
/*
- * compute the number of page fetches for a given chunk size (d[])
- * and access pattern (A[][])
+ * compute the number of page fetches for a given chunk size (*d)
+ * and access pattern (**A)
*/
- register int i,
- j,
- nc;
+ int i,
+ j,
+ nc;
for (i = 0, tc = 0; i < N; i++)
{
;
}
}
- return (mintc);
+ return mintc;
}
/*----------------------------------------------------------------------
- * get_next --
+ * get_next
* Called by _GetBestChunk to get the next tuple in the lexicographic order
*---------------------------------------------------------------------
*/
static int
-get_next(int d[], int k, int C, int dmax[])
+get_next(int *d, int k, int C, int *dmax)
{
- register int i,
- j,
- temp;
+ int i,
+ j,
+ temp;
if (!d[0])
{
d[j] = min(temp, dmax[j]);
temp = max(1, temp / d[j]);
}
- return (1);
+ return 1;
}
for (j = 0, temp = 1; j < k; j++)
break;
}
if (i < 0)
- return (0);
+ return 0;
d[i]++;
j = C / temp;
d[j] = min(temp, dmax[j]);
temp = max(1, temp / d[j]);
}
- return (1);
+ return 1;
}
#ifdef LOARRAY
-static char a_chunk[BLCKSZ + 4]; /* 4 since a_chunk is in varlena
- * format */
+static char a_chunk[BLCKSZ + VARHDRSZ]; /* VARHDRSZ since a_chunk is in
+ * varlena format */
#endif
static void
-initialize_info(CHUNK_INFO * A, int ndim, int dim[], int chunk[])
+initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk)
{
- int i;
+ int i;
for (i = 0; i < ndim; i++)
A->C[i] = chunk[i];
static void
_ConvertToChunkFile(int n,
int baseSize,
- int dim[],
- int C[],
+ int *dim,
+ int *C,
int srcfd,
int destfd)
{
- int max_chunks[MAXDIM],
- chunk_no[MAXDIM];
- int PX[MAXDIM],
- dist[MAXDIM];
- int csize = 1,
- i,
- temp;
+ int max_chunks[MAXDIM],
+ chunk_no[MAXDIM];
+ int PX[MAXDIM],
+ dist[MAXDIM];
+ int csize = 1,
+ i,
+ temp;
for (i = 0; i < n; chunk_no[i++] = 0)
{
csize *= C[i];
}
csize *= baseSize;
- temp = csize + 4;
- memmove(a_chunk, &temp, 4);
+ temp = csize + VARHDRSZ;
+ memmove(a_chunk, &temp, VARHDRSZ);
mda_get_prod(n, dim, PX);
mda_get_offset_values(n, dist, PX, C);
;
do
{
- read_chunk(chunk_no, C, &(a_chunk[4]), srcfd, n, baseSize, PX, dist);
+ read_chunk(chunk_no, C, &(a_chunk[VARHDRSZ]), srcfd, n, baseSize, PX, dist);
write_chunk((struct varlena *) a_chunk, destfd);
} while (next_tuple(n, chunk_no, max_chunks) != -1);
}
*--------------------------------------------------------------------------
*/
static void
-read_chunk(int chunk_no[],
- int C[],
- char a_chunk[],
+read_chunk(int *chunk_no,
+ int *C,
+ char *a_chunk,
int srcfd,
int n,
int baseSize,
- int PX[],
- int dist[])
+ int *PX,
+ int *dist)
{
- int i,
- j,
- cp,
- unit_transfer;
- int start_pos,
- pos[MAXDIM];
- int indx[MAXDIM];
- int fpOff;
+ int i,
+ j,
+ cp,
+ unit_transfer;
+ int start_pos,
+ pos[MAXDIM];
+ int indx[MAXDIM];
+ int fpOff;
for (i = start_pos = 0; i < n; i++)
{
static int
write_chunk(struct varlena * a_chunk, int ofile)
{
- int got_n = 0;
+ int got_n = 0;
#ifdef LOARRAY
got_n = LOwrite(ofile, a_chunk);
#endif
- return (got_n);
+ return got_n;
}
/*--------------------------------------------------------------------------
*--------------------------------------------------------------------------
*/
static int
-seek_and_read(int pos, int size, char buff[], int fp, int from)
+seek_and_read(int pos, int size, char *buff, int fp, int from)
{
struct varlena *v = NULL;
/* Assuming only one file */
if (lo_lseek(fp, pos, from) < 0)
- elog(WARN, "File seek error");
+ elog(ERROR, "File seek error");
#ifdef LOARRAY
v = (struct varlena *) LOread(fp, size);
#endif
- if (VARSIZE(v) - 4 < size)
- elog(WARN, "File read error");
+ if (VARSIZE(v) - VARHDRSZ < size)
+ elog(ERROR, "File read error");
memmove(buff, VARDATA(v), size);
pfree(v);
- return (1);
+ return 1;
}
-#endif /* LOARRAY */
+#endif /* LOARRAY */
/*----------------------------------------------------------------------------
- * _ReadChunkArray --
+ * _ReadChunkArray
* returns the subarray specified bu the range indices "st" and "endp"
* from the chunked array stored in file "fp"
*---------------------------------------------------------------------------
*/
int
-_ReadChunkArray(int st[],
- int endp[],
+_ReadChunkArray(int *st,
+ int *endp,
int bsize,
int fp,
char *destfp,
- ArrayType * array,
+ ArrayType *array,
int isDestLO,
- bool * isNull)
+ bool *isNull)
{
- int i,
- j,
- jj;
- int n,
- temp,
- words_read;
- int chunk_span[MAXDIM],
- chunk_off[MAXDIM];
- int chunk_st[MAXDIM],
- chunk_end[MAXDIM];
- int block_seek;
-
- int bptr,
- *C,
- csize,
- *dim,
- *lb;
- int range_st[MAXDIM],
- range_end[MAXDIM],
- range[MAXDIM],
- array_span[MAXDIM];
- int PA[MAXDIM],
- PCHUNK[MAXDIM],
- PC[MAXDIM];
- int to_read;
- int cdist[MAXDIM],
- adist[MAXDIM];
- int dist[MAXDIM],
- temp_seek;
-
- int srcOff; /* Needed since LO don't understand
+ int i,
+ j,
+ jj;
+ int n,
+ temp,
+ words_read;
+ int chunk_span[MAXDIM],
+ chunk_off[MAXDIM];
+ int chunk_st[MAXDIM],
+ chunk_end[MAXDIM];
+ int block_seek;
+
+ int bptr,
+ *C,
+ csize,
+ *dim,
+ *lb;
+ int range_st[MAXDIM],
+ range_end[MAXDIM],
+ range[MAXDIM],
+ array_span[MAXDIM];
+ int PA[MAXDIM],
+ PCHUNK[MAXDIM],
+ PC[MAXDIM];
+ int to_read;
+ int cdist[MAXDIM],
+ adist[MAXDIM];
+ int dist[MAXDIM],
+ temp_seek;
+
+ int srcOff; /* Needed since LO don't understand
* SEEK_CUR */
- char *baseDestFp = (char *) destfp;
+ char *baseDestFp = (char *) destfp;
- CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
+ CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
n = ARR_NDIM(array);
dim = ARR_DIMS(array);
block_seek += (to_read / bsize);
/*
- * compute next tuple in range[]
+ * compute next tuple in *range
*/
{
- int x;
+ int x;
if (!(i + 1))
j = -1;
range_end[i] = min((chunk_st[i] + chunk_off[i]) * C[i] + C[i] - 1, endp[i]);
}
} while (jj != -1);
- return (words_read);
+ return words_read;
}
/*------------------------------------------------------------------------
- * _ReadChunkArray1El --
+ * _ReadChunkArray1El
* returns one element of the chunked array as specified by the index "st"
* the chunked file descriptor is "fp"
*-------------------------------------------------------------------------
*/
struct varlena *
-_ReadChunkArray1El(int st[],
+_ReadChunkArray1El(int *st,
int bsize,
int fp,
- ArrayType * array,
- bool * isNull)
+ ArrayType *array,
+ bool *isNull)
{
- int i,
- j,
- n,
- temp,
- srcOff;
- int chunk_st[MAXDIM];
-
- int *C,
- csize,
- *dim,
- *lb;
- int PCHUNK[MAXDIM],
- PC[MAXDIM];
-
- CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
+ int i,
+ j,
+ n,
+ temp,
+ srcOff;
+ int chunk_st[MAXDIM];
+
+ int *C,
+ csize,
+ *dim,
+ *lb;
+ int PCHUNK[MAXDIM],
+ PC[MAXDIM];
+
+ CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
n = ARR_NDIM(array);
lb = ARR_LBOUND(array);