1 /*-------------------------------------------------------------------------
5 * Copyright (c) 1994, Regents of the University of California
9 * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.8 1997/09/08 02:30:31 momjian Exp $
11 *-------------------------------------------------------------------------
14 #include <sys/types.h>
19 #include <libpq/be-fsstubs.h>
20 #include "utils/memutils.h"
21 #include "libpq/libpq-fs.h"
23 #include "storage/fd.h" /* for SEEK_ */
25 #include "catalog/pg_type.h"
28 #include "utils/array.h"
30 #include "optimizer/internal.h"
32 #include <regex/utils.h>
38 #define INFTY 500000000
41 #define quot_ceil(x,y) (((x)+(y)-1)/(y))
42 #define min(x,y) (((x) < (y))? (x) : (y))
43 #define max(x,y) (((x) > (y))? (x) : (y))
45 static CHUNK_INFO cInfo;
47 /* non-export function prototypes */
49 _FindBestChunk(int size, int dmax[], int dbest[], int dim,
50 int A[MAXPAT][MAXDIM + 1], int N);
51 static int get_next(int d[], int k, int C, int dmax[]);
52 static void initialize_info(CHUNK_INFO * A, int ndim, int dim[], int chunk[]);
56 _ConvertToChunkFile(int n, int baseSize, int dim[], int C[],
57 int srcfd, int destfd);
59 read_chunk(int chunk_no[], int C[], char a_chunk[], int srcfd,
60 int n, int baseSize, int PX[], int dist[]);
61 static int write_chunk(struct varlena * a_chunk, int ofile);
62 static int seek_and_read(int pos, int size, char buff[], int fp, int from);
66 GetChunkSize(FILE * fd, int ndim, int dim[MAXDIM], int baseSize,
69 /*------------------------------------------------------------------------
71 * converts an input array to chunked format using the information
72 * provided by the access pattern.
74 * creates a new file that stores the chunked array and returns
75 * information about the chunked file
76 *-----------------------------------------------------------------------
95 if (chunkfile == NULL)
102 /* create new LO for chunked file */
103 chunkfile = _array_newLO(&cfd, fileFlag);
105 cfd = LOopen(chunkfile, O_RDONLY);
107 elog(WARN, "Unable to open chunk file");
110 strcpy(cInfo.lo_name, chunkfile);
112 /* find chunk size */
113 csize = GetChunkSize(afd, ndim, dim, baseSize, chunk);
117 /* copy data from input file to chunked file */
118 _ConvertToChunkFile(ndim, baseSize, dim, chunk, fd, cfd);
121 initialize_info(&cInfo, ndim, dim, chunk);
122 *nbytes = sizeof(CHUNK_INFO);
123 return (char *) &cInfo;
126 /*--------------------------------------------------------------------------
128 * given an access pattern and array dimensionality etc, this program
129 * returns the dimensions of the chunk in "d"
130 *-----------------------------------------------------------------------
133 GetChunkSize(FILE * fd,
143 int A[MAXPAT][MAXDIM + 1],
147 * ----------- read input ------------
149 fscanf(fd, "%d", &N);
151 elog(WARN, "array_in: too many access pattern elements");
152 for (i = 0; i < N; i++)
153 for (j = 0; j < ndim + 1; j++)
154 if (fscanf(fd, "%d ", &(A[i][j])) == EOF)
155 elog(WARN, "array_in: bad access pattern input");
158 * estimate chunk size
160 for (i = 0; i < ndim; i++)
161 for (j = 0, dmax[i] = 1; j < N; j++)
162 if (dmax[i] < A[j][i])
164 csize = BLCKSZ / baseSize;
166 _FindBestChunk(csize, dmax, d, ndim, A, N);
171 /*-------------------------------------------------------------------------
173 * This routine does most of the number crunching to compute the
174 * optimal chunk shape.
175 * Called by GetChunkSize
176 *------------------------------------------------------------------------
179 _FindBestChunk(int size,
183 int A[MAXPAT][MAXDIM + 1],
192 while (get_next(d, dim, size, dmax))
196 * compute the number of page fetches for a given chunk size (d[])
197 * and access pattern (A[][])
203 for (i = 0, tc = 0; i < N; i++)
205 for (j = 0, nc = 1; j < dim; j++)
206 nc *= quot_ceil(A[i][j], d[j]);
212 * tc holds the total number of page fetches
217 for (j = 0; j < dim; dbest[j] = d[j], j++)
224 /*----------------------------------------------------------------------
226 * Called by _GetBestChunk to get the next tuple in the lexicographic order
227 *---------------------------------------------------------------------
230 get_next(int d[], int k, int C, int dmax[])
239 for (j = k - 1; j >= 0; j--)
241 d[j] = min(temp, dmax[j]);
242 temp = max(1, temp / d[j]);
247 for (j = 0, temp = 1; j < k; j++)
250 for (i = k - 1; i >= 0; i--)
253 if (((temp * (d[i] + 1)) < C) && (d[i] + 1 <= dmax[i]))
261 d[i] = min(dmax[i], j / (j / d[i]));
265 for (j = k - 1; j > i; j--)
267 d[j] = min(temp, dmax[j]);
268 temp = max(1, temp / d[j]);
274 static char a_chunk[BLCKSZ + 4];/* 4 since a_chunk is in varlena format */
279 initialize_info(CHUNK_INFO * A, int ndim, int dim[], int chunk[])
283 for (i = 0; i < ndim; i++)
287 /*--------------------------------------------------------------------------
288 * Procedure reorganize_data():
289 * This procedure reads the input multidimensional array that is organised
290 * in the order specified by array "X" and breaks it up into chunks of
291 * dimensions specified in "C".
293 * This is a very slow process, since reading and writing of LARGE files
296 *-------------------------------------------------------------------------
300 _ConvertToChunkFile(int n,
307 int max_chunks[MAXDIM],
315 for (i = 0; i < n; chunk_no[i++] = 0)
317 max_chunks[i] = dim[i] / C[i];
322 memmove(a_chunk, &temp, 4);
324 mda_get_prod(n, dim, PX);
325 mda_get_offset_values(n, dist, PX, C);
326 for (i = 0; i < n; dist[i] *= baseSize, i++)
330 read_chunk(chunk_no, C, &(a_chunk[4]), srcfd, n, baseSize, PX, dist);
331 write_chunk((struct varlena *) a_chunk, destfd);
332 } while (next_tuple(n, chunk_no, max_chunks) != -1);
335 /*--------------------------------------------------------------------------
337 * reads a chunk from the input files into a_chunk, the position of the
338 * chunk is specified by chunk_no
339 *--------------------------------------------------------------------------
342 read_chunk(int chunk_no[],
360 for (i = start_pos = 0; i < n; i++)
362 pos[i] = chunk_no[i] * C[i];
363 start_pos += pos[i] * PX[i];
365 start_pos *= baseSize;
367 /* Read a block of dimesion C starting at co-ordinates pos */
368 unit_transfer = C[n - 1] * baseSize;
370 for (i = 0; i < n; indx[i++] = 0)
373 seek_and_read(fpOff, unit_transfer, a_chunk, srcfd, SEEK_SET);
374 fpOff += unit_transfer;
377 while ((j = next_tuple(n - 1, indx, C)) != -1)
380 seek_and_read(fpOff, unit_transfer, &(a_chunk[cp]), srcfd, SEEK_SET);
382 fpOff += unit_transfer;
386 /*--------------------------------------------------------------------------
388 * writes a chunk of size csize into the output file
389 *--------------------------------------------------------------------------
392 write_chunk(struct varlena * a_chunk, int ofile)
397 got_n = LOwrite(ofile, a_chunk);
402 /*--------------------------------------------------------------------------
404 * seeks to the asked location in the input file and reads the
405 * appropriate number of blocks
406 * Called By: read_chunk()
407 *--------------------------------------------------------------------------
410 seek_and_read(int pos, int size, char buff[], int fp, int from)
412 struct varlena *v = NULL;
414 /* Assuming only one file */
415 if (lo_lseek(fp, pos, from) < 0)
416 elog(WARN, "File seek error");
418 v = (struct varlena *) LOread(fp, size);
420 if (VARSIZE(v) - 4 < size)
421 elog(WARN, "File read error");
422 memmove(buff, VARDATA(v), size);
430 /*----------------------------------------------------------------------------
432 * returns the subarray specified bu the range indices "st" and "endp"
433 * from the chunked array stored in file "fp"
434 *---------------------------------------------------------------------------
437 _ReadChunkArray(int st[],
452 int chunk_span[MAXDIM],
454 int chunk_st[MAXDIM],
463 int range_st[MAXDIM],
476 int srcOff; /* Needed since LO don't understand
478 char *baseDestFp = (char *) destfp;
480 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
483 dim = ARR_DIMS(array);
484 lb = ARR_LBOUND(array);
489 temp = dim[n - 1] / C[n - 1];
490 for (i = n - 2; i >= 0; i--)
492 PC[i] = PC[i + 1] * temp;
493 temp = dim[i] / C[i];
497 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++)
499 mda_get_prod(n, C, PCHUNK);
500 mda_get_range(n, array_span, st, endp);
501 mda_get_prod(n, array_span, PA);
503 array2chunk_coord(n, C, st, chunk_st);
504 array2chunk_coord(n, C, endp, chunk_end);
505 mda_get_range(n, chunk_span, chunk_st, chunk_end);
506 mda_get_offset_values(n, dist, PC, chunk_span);
508 for (i = 0; i < n; i++)
511 range_end[i] = min(chunk_st[i] * C[i] + C[i] - 1, endp[i]);
514 for (i = j = 0; i < n; i++)
515 j += chunk_st[i] * PC[i];
516 temp_seek = srcOff = j * csize * bsize;
517 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
521 for (i = 0; i < n; chunk_off[i++] = 0)
527 /* Write chunk (chunk_st) to output buffer */
528 mda_get_range(n, array_span, range_st, range_end);
529 mda_get_offset_values(n, adist, PA, array_span);
530 mda_get_offset_values(n, cdist, PCHUNK, array_span);
531 for (i = 0; i < n; range[i] = range_st[i] - st[i], i++);
532 bptr = tuple2linear(n, range, PA);
533 for (i = 0; i < n; range[i++] = 0);
538 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
542 destfp = baseDestFp + bptr;
543 for (i = 0, block_seek = 0; i < n; i++)
544 block_seek += (range_st[i] - (chunk_st[i] + chunk_off[i])
546 if (dist[jj] + block_seek + temp_seek)
548 temp = (dist[jj] * csize + block_seek + temp_seek) * bsize;
550 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
553 for (i = n - 1, to_read = bsize; i >= 0;
554 to_read *= min(C[i], array_span[i]), i--)
555 if (cdist[i] || adist[i])
561 srcOff += (cdist[j] * bsize);
562 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
565 block_seek += cdist[j];
566 bptr += adist[j] * bsize;
569 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
573 destfp = baseDestFp + bptr;
574 temp = _LOtransfer((char **) &destfp, to_read, 1, (char **) &fp, 1, isDestLO);
578 words_read += to_read;
580 block_seek += (to_read / bsize);
583 * compute next tuple in range[]
592 range[i] = (range[i] + 1) % array_span[i];
593 for (x = i; x * (!range[x]); x--)
594 range[x - 1] = (range[x - 1] + 1) % array_span[x - 1];
608 * end of compute next tuple -- j is set to -1 if tuple
613 block_seek = csize - block_seek;
614 temp_seek = block_seek;
615 jj = next_tuple(n, chunk_off, chunk_span);
618 range_st[jj] = (chunk_st[jj] + chunk_off[jj]) * C[jj];
619 range_end[jj] = min(range_st[jj] + C[jj] - 1, endp[jj]);
621 for (i = jj + 1; i < n; i++)
624 range_end[i] = min((chunk_st[i] + chunk_off[i]) * C[i] + C[i] - 1, endp[i]);
630 /*------------------------------------------------------------------------
631 * _ReadChunkArray1El --
632 * returns one element of the chunked array as specified by the index "st"
633 * the chunked file descriptor is "fp"
634 *-------------------------------------------------------------------------
637 _ReadChunkArray1El(int st[],
648 int chunk_st[MAXDIM];
657 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
660 lb = ARR_LBOUND(array);
662 dim = ARR_DIMS(array);
666 temp = dim[n - 1] / C[n - 1];
667 for (i = n - 2; i >= 0; i--)
669 PC[i] = PC[i + 1] * temp;
670 temp = dim[i] / C[i];
674 for (i = 0; i < n; st[i] -= lb[i], i++);
675 mda_get_prod(n, C, PCHUNK);
677 array2chunk_coord(n, C, st, chunk_st);
679 for (i = j = 0; i < n; i++)
680 j += chunk_st[i] * PC[i];
683 for (i = 0; i < n; i++)
684 srcOff += (st[i] - chunk_st[i] * C[i]) * PCHUNK[i];
687 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
690 return (struct varlena *) LOread(fp, bsize);
692 return (struct varlena *) 0;