1 /*-------------------------------------------------------------------------
5 * Copyright (c) 1994, Regents of the University of California
9 * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.16 1998/02/26 04:36:56 momjian Exp $
11 *-------------------------------------------------------------------------
14 #include <sys/types.h>
20 #include "catalog/pg_type.h"
21 #include "libpq/be-fsstubs.h"
22 #include "libpq/libpq-fs.h"
23 #include "optimizer/internal.h"
24 #include "utils/array.h"
25 #include "utils/memutils.h"
28 #include <regex/utils.h>
33 #define INFTY 500000000
36 #define quot_ceil(x,y) (((x)+(y)-1)/(y))
37 #define min(x,y) (((x) < (y))? (x) : (y))
38 #define max(x,y) (((x) > (y))? (x) : (y))
40 static CHUNK_INFO cInfo;
42 /* non-export function prototypes */
44 _FindBestChunk(int size, int dmax[], int dbest[], int dim,
45 int A[MAXPAT][MAXDIM + 1], int N);
46 static int get_next(int d[], int k, int C, int dmax[]);
47 static void initialize_info(CHUNK_INFO *A, int ndim, int dim[], int chunk[]);
51 _ConvertToChunkFile(int n, int baseSize, int dim[], int C[],
52 int srcfd, int destfd);
54 read_chunk(int chunk_no[], int C[], char a_chunk[], int srcfd,
55 int n, int baseSize, int PX[], int dist[]);
56 static int write_chunk(struct varlena * a_chunk, int ofile);
57 static int seek_and_read(int pos, int size, char buff[], int fp, int from);
61 GetChunkSize(FILE *fd, int ndim, int dim[MAXDIM], int baseSize,
64 /*------------------------------------------------------------------------
66 * converts an input array to chunked format using the information
67 * provided by the access pattern.
69 * creates a new file that stores the chunked array and returns
70 * information about the chunked file
71 *-----------------------------------------------------------------------
90 if (chunkfile == NULL)
97 /* create new LO for chunked file */
98 chunkfile = _array_newLO(&cfd, fileFlag);
100 cfd = LOopen(chunkfile, O_RDONLY);
102 elog(ERROR, "Unable to open chunk file");
105 strcpy(cInfo.lo_name, chunkfile);
107 /* find chunk size */
108 csize = GetChunkSize(afd, ndim, dim, baseSize, chunk);
112 /* copy data from input file to chunked file */
113 _ConvertToChunkFile(ndim, baseSize, dim, chunk, fd, cfd);
116 initialize_info(&cInfo, ndim, dim, chunk);
117 *nbytes = sizeof(CHUNK_INFO);
118 return (char *) &cInfo;
121 /*--------------------------------------------------------------------------
123 * given an access pattern and array dimensionality etc, this program
124 * returns the dimensions of the chunk in "d"
125 *-----------------------------------------------------------------------
128 GetChunkSize(FILE *fd,
138 int A[MAXPAT][MAXDIM + 1],
142 * ----------- read input ------------
144 fscanf(fd, "%d", &N);
146 elog(ERROR, "array_in: too many access pattern elements");
147 for (i = 0; i < N; i++)
148 for (j = 0; j < ndim + 1; j++)
149 if (fscanf(fd, "%d ", &(A[i][j])) == EOF)
150 elog(ERROR, "array_in: bad access pattern input");
153 * estimate chunk size
155 for (i = 0; i < ndim; i++)
156 for (j = 0, dmax[i] = 1; j < N; j++)
157 if (dmax[i] < A[j][i])
159 csize = BLCKSZ / baseSize;
161 _FindBestChunk(csize, dmax, d, ndim, A, N);
166 /*-------------------------------------------------------------------------
168 * This routine does most of the number crunching to compute the
169 * optimal chunk shape.
170 * Called by GetChunkSize
171 *------------------------------------------------------------------------
174 _FindBestChunk(int size,
178 int A[MAXPAT][MAXDIM + 1],
187 while (get_next(d, dim, size, dmax))
191 * compute the number of page fetches for a given chunk size (d[])
192 * and access pattern (A[][])
198 for (i = 0, tc = 0; i < N; i++)
200 for (j = 0, nc = 1; j < dim; j++)
201 nc *= quot_ceil(A[i][j], d[j]);
207 * tc holds the total number of page fetches
212 for (j = 0; j < dim; dbest[j] = d[j], j++)
219 /*----------------------------------------------------------------------
221 * Called by _GetBestChunk to get the next tuple in the lexicographic order
222 *---------------------------------------------------------------------
225 get_next(int d[], int k, int C, int dmax[])
234 for (j = k - 1; j >= 0; j--)
236 d[j] = min(temp, dmax[j]);
237 temp = max(1, temp / d[j]);
242 for (j = 0, temp = 1; j < k; j++)
245 for (i = k - 1; i >= 0; i--)
248 if (((temp * (d[i] + 1)) < C) && (d[i] + 1 <= dmax[i]))
256 d[i] = min(dmax[i], j / (j / d[i]));
260 for (j = k - 1; j > i; j--)
262 d[j] = min(temp, dmax[j]);
263 temp = max(1, temp / d[j]);
269 static char a_chunk[BLCKSZ + VARHDRSZ]; /* VARHDRSZ since a_chunk is in
275 initialize_info(CHUNK_INFO *A, int ndim, int dim[], int chunk[])
279 for (i = 0; i < ndim; i++)
283 /*--------------------------------------------------------------------------
284 * Procedure reorganize_data():
285 * This procedure reads the input multidimensional array that is organised
286 * in the order specified by array "X" and breaks it up into chunks of
287 * dimensions specified in "C".
289 * This is a very slow process, since reading and writing of LARGE files
292 *-------------------------------------------------------------------------
296 _ConvertToChunkFile(int n,
303 int max_chunks[MAXDIM],
311 for (i = 0; i < n; chunk_no[i++] = 0)
313 max_chunks[i] = dim[i] / C[i];
317 temp = csize + VARHDRSZ;
318 memmove(a_chunk, &temp, VARHDRSZ);
320 mda_get_prod(n, dim, PX);
321 mda_get_offset_values(n, dist, PX, C);
322 for (i = 0; i < n; dist[i] *= baseSize, i++)
326 read_chunk(chunk_no, C, &(a_chunk[VARHDRSZ]), srcfd, n, baseSize, PX, dist);
327 write_chunk((struct varlena *) a_chunk, destfd);
328 } while (next_tuple(n, chunk_no, max_chunks) != -1);
331 /*--------------------------------------------------------------------------
333 * reads a chunk from the input files into a_chunk, the position of the
334 * chunk is specified by chunk_no
335 *--------------------------------------------------------------------------
338 read_chunk(int chunk_no[],
356 for (i = start_pos = 0; i < n; i++)
358 pos[i] = chunk_no[i] * C[i];
359 start_pos += pos[i] * PX[i];
361 start_pos *= baseSize;
363 /* Read a block of dimesion C starting at co-ordinates pos */
364 unit_transfer = C[n - 1] * baseSize;
366 for (i = 0; i < n; indx[i++] = 0)
369 seek_and_read(fpOff, unit_transfer, a_chunk, srcfd, SEEK_SET);
370 fpOff += unit_transfer;
373 while ((j = next_tuple(n - 1, indx, C)) != -1)
376 seek_and_read(fpOff, unit_transfer, &(a_chunk[cp]), srcfd, SEEK_SET);
378 fpOff += unit_transfer;
382 /*--------------------------------------------------------------------------
384 * writes a chunk of size csize into the output file
385 *--------------------------------------------------------------------------
388 write_chunk(struct varlena * a_chunk, int ofile)
393 got_n = LOwrite(ofile, a_chunk);
398 /*--------------------------------------------------------------------------
400 * seeks to the asked location in the input file and reads the
401 * appropriate number of blocks
402 * Called By: read_chunk()
403 *--------------------------------------------------------------------------
406 seek_and_read(int pos, int size, char buff[], int fp, int from)
408 struct varlena *v = NULL;
410 /* Assuming only one file */
411 if (lo_lseek(fp, pos, from) < 0)
412 elog(ERROR, "File seek error");
414 v = (struct varlena *) LOread(fp, size);
416 if (VARSIZE(v) - VARHDRSZ < size)
417 elog(ERROR, "File read error");
418 memmove(buff, VARDATA(v), size);
426 /*----------------------------------------------------------------------------
428 * returns the subarray specified bu the range indices "st" and "endp"
429 * from the chunked array stored in file "fp"
430 *---------------------------------------------------------------------------
433 _ReadChunkArray(int st[],
448 int chunk_span[MAXDIM],
450 int chunk_st[MAXDIM],
459 int range_st[MAXDIM],
472 int srcOff; /* Needed since LO don't understand
474 char *baseDestFp = (char *) destfp;
476 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
479 dim = ARR_DIMS(array);
480 lb = ARR_LBOUND(array);
485 temp = dim[n - 1] / C[n - 1];
486 for (i = n - 2; i >= 0; i--)
488 PC[i] = PC[i + 1] * temp;
489 temp = dim[i] / C[i];
493 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++)
495 mda_get_prod(n, C, PCHUNK);
496 mda_get_range(n, array_span, st, endp);
497 mda_get_prod(n, array_span, PA);
499 array2chunk_coord(n, C, st, chunk_st);
500 array2chunk_coord(n, C, endp, chunk_end);
501 mda_get_range(n, chunk_span, chunk_st, chunk_end);
502 mda_get_offset_values(n, dist, PC, chunk_span);
504 for (i = 0; i < n; i++)
507 range_end[i] = min(chunk_st[i] * C[i] + C[i] - 1, endp[i]);
510 for (i = j = 0; i < n; i++)
511 j += chunk_st[i] * PC[i];
512 temp_seek = srcOff = j * csize * bsize;
513 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
517 for (i = 0; i < n; chunk_off[i++] = 0)
523 /* Write chunk (chunk_st) to output buffer */
524 mda_get_range(n, array_span, range_st, range_end);
525 mda_get_offset_values(n, adist, PA, array_span);
526 mda_get_offset_values(n, cdist, PCHUNK, array_span);
527 for (i = 0; i < n; range[i] = range_st[i] - st[i], i++);
528 bptr = tuple2linear(n, range, PA);
529 for (i = 0; i < n; range[i++] = 0);
534 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
538 destfp = baseDestFp + bptr;
539 for (i = 0, block_seek = 0; i < n; i++)
540 block_seek += (range_st[i] - (chunk_st[i] + chunk_off[i])
542 if (dist[jj] + block_seek + temp_seek)
544 temp = (dist[jj] * csize + block_seek + temp_seek) * bsize;
546 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
549 for (i = n - 1, to_read = bsize; i >= 0;
550 to_read *= min(C[i], array_span[i]), i--)
551 if (cdist[i] || adist[i])
557 srcOff += (cdist[j] * bsize);
558 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
561 block_seek += cdist[j];
562 bptr += adist[j] * bsize;
565 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
569 destfp = baseDestFp + bptr;
570 temp = _LOtransfer((char **) &destfp, to_read, 1, (char **) &fp, 1, isDestLO);
574 words_read += to_read;
576 block_seek += (to_read / bsize);
579 * compute next tuple in range[]
588 range[i] = (range[i] + 1) % array_span[i];
589 for (x = i; x * (!range[x]); x--)
590 range[x - 1] = (range[x - 1] + 1) % array_span[x - 1];
604 * end of compute next tuple -- j is set to -1 if tuple
609 block_seek = csize - block_seek;
610 temp_seek = block_seek;
611 jj = next_tuple(n, chunk_off, chunk_span);
614 range_st[jj] = (chunk_st[jj] + chunk_off[jj]) * C[jj];
615 range_end[jj] = min(range_st[jj] + C[jj] - 1, endp[jj]);
617 for (i = jj + 1; i < n; i++)
620 range_end[i] = min((chunk_st[i] + chunk_off[i]) * C[i] + C[i] - 1, endp[i]);
626 /*------------------------------------------------------------------------
627 * _ReadChunkArray1El --
628 * returns one element of the chunked array as specified by the index "st"
629 * the chunked file descriptor is "fp"
630 *-------------------------------------------------------------------------
633 _ReadChunkArray1El(int st[],
644 int chunk_st[MAXDIM];
653 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
656 lb = ARR_LBOUND(array);
658 dim = ARR_DIMS(array);
662 temp = dim[n - 1] / C[n - 1];
663 for (i = n - 2; i >= 0; i--)
665 PC[i] = PC[i + 1] * temp;
666 temp = dim[i] / C[i];
670 for (i = 0; i < n; st[i] -= lb[i], i++);
671 mda_get_prod(n, C, PCHUNK);
673 array2chunk_coord(n, C, st, chunk_st);
675 for (i = j = 0; i < n; i++)
676 j += chunk_st[i] * PC[i];
679 for (i = 0; i < n; i++)
680 srcOff += (st[i] - chunk_st[i] * C[i]) * PCHUNK[i];
683 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
686 return (struct varlena *) LOread(fp, bsize);
688 return (struct varlena *) 0;