]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/adt/chunk.c
Move some system includes into c.h, and remove duplicates.
[postgresql] / src / backend / utils / adt / chunk.c
index fcfc8cdaeb5a6eb6d199457bc68e255c24d2d2e6..d64ed0cf02d5e8c832c35399da66234c408f2b3b 100644 (file)
@@ -1,12 +1,12 @@
 /*-------------------------------------------------------------------------
  *
- * 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]);
 
 /*------------------------------------------------------------------------
@@ -75,22 +60,22 @@ GetChunkSize(FILE * fd, int ndim, int dim[MAXDIM], int baseSize,
  *        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;
@@ -104,7 +89,7 @@ _ChunkArray(int fd,
        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);
@@ -124,35 +109,35 @@ _ChunkArray(int fd,
 }
 
 /*--------------------------------------------------------------------------
- * 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
@@ -169,7 +154,7 @@ GetChunkSize(FILE * fd,
 }
 
 /*-------------------------------------------------------------------------
- * _FindBestChunk --
+ * _FindBestChunk
  *               This routine does most of the number crunching to compute the
  *               optimal chunk shape.
  * Called by GetChunkSize
@@ -177,15 +162,15 @@ GetChunkSize(FILE * fd,
  */
 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;
@@ -193,12 +178,12 @@ _FindBestChunk(int size,
        {
 
                /*
-                * 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++)
                {
@@ -218,20 +203,20 @@ _FindBestChunk(int size,
                                ;
                }
        }
-       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])
        {
@@ -241,7 +226,7 @@ get_next(int d[], int k, int C, int dmax[])
                        d[j] = min(temp, dmax[j]);
                        temp = max(1, temp / d[j]);
                }
-               return (1);
+               return 1;
        }
 
        for (j = 0, temp = 1; j < k; j++)
@@ -254,7 +239,7 @@ get_next(int d[], int k, int C, int dmax[])
                        break;
        }
        if (i < 0)
-               return (0);
+               return 0;
 
        d[i]++;
        j = C / temp;
@@ -267,19 +252,19 @@ get_next(int d[], int k, int C, int dmax[])
                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];
@@ -300,18 +285,18 @@ initialize_info(CHUNK_INFO * A, int ndim, int dim[], int chunk[])
 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)
        {
@@ -319,8 +304,8 @@ _ConvertToChunkFile(int n,
                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);
@@ -328,7 +313,7 @@ _ConvertToChunkFile(int n,
                ;
        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);
 }
@@ -340,23 +325,23 @@ _ConvertToChunkFile(int n,
  *--------------------------------------------------------------------------
  */
 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++)
        {
@@ -392,12 +377,12 @@ read_chunk(int chunk_no[],
 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;
 }
 
 /*--------------------------------------------------------------------------
@@ -408,77 +393,77 @@ write_chunk(struct varlena * a_chunk, int ofile)
  *--------------------------------------------------------------------------
  */
 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);
@@ -581,10 +566,10 @@ _ReadChunkArray(int st[],
                        block_seek += (to_read / bsize);
 
                        /*
-                        * compute next tuple in range[]
+                        * compute next tuple in *range
                         */
                        {
-                               int                             x;
+                               int                     x;
 
                                if (!(i + 1))
                                        j = -1;
@@ -625,37 +610,37 @@ _ReadChunkArray(int st[],
                        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);