1 /*-------------------------------------------------------------------------
5 * Copyright (c) 1994, Regents of the University of California
9 * $Header: /cvsroot/pgsql/src/backend/utils/adt/Attic/chunk.c,v 1.23 1999/07/17 20:17:54 momjian Exp $
11 *-------------------------------------------------------------------------
14 #include <sys/types.h>
19 #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"
27 #define INFTY 500000000
30 #define quot_ceil(x,y) (((x)+(y)-1)/(y))
31 #define min(x,y) (((x) < (y))? (x) : (y))
32 #define max(x,y) (((x) > (y))? (x) : (y))
34 static CHUNK_INFO cInfo;
36 /* non-export function prototypes */
37 static int _FindBestChunk(int size, int *dmax, int *dbest, int dim,
38 int A[MAXPAT][MAXDIM + 1], int N);
39 static int get_next(int *d, int k, int C, int *dmax);
40 static void initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk);
43 static void _ConvertToChunkFile(int n, int baseSize, int *dim, int *C,
44 int srcfd, int destfd);
45 static void read_chunk(int *chunk_no, int *C, char *a_chunk, int srcfd,
46 int n, int baseSize, int *PX, int *dist);
47 static int write_chunk(struct varlena * a_chunk, int ofile);
48 static int seek_and_read(int pos, int size, char *buff, int fp, int from);
51 static int GetChunkSize(FILE *fd, int ndim, int dim[MAXDIM], int baseSize,
54 /*------------------------------------------------------------------------
56 * converts an input array to chunked format using the information
57 * provided by the access pattern.
59 * creates a new file that stores the chunked array and returns
60 * information about the chunked file
61 *-----------------------------------------------------------------------
80 if (chunkfile == NULL)
87 /* create new LO for chunked file */
88 chunkfile = _array_newLO(&cfd, fileFlag);
90 cfd = LOopen(chunkfile, O_RDONLY);
92 elog(ERROR, "Unable to open chunk file");
95 strcpy(cInfo.lo_name, chunkfile);
98 csize = GetChunkSize(afd, ndim, dim, baseSize, chunk);
102 /* copy data from input file to chunked file */
103 _ConvertToChunkFile(ndim, baseSize, dim, chunk, fd, cfd);
106 initialize_info(&cInfo, ndim, dim, chunk);
107 *nbytes = sizeof(CHUNK_INFO);
108 return (char *) &cInfo;
111 /*--------------------------------------------------------------------------
113 * given an access pattern and array dimensionality etc, this program
114 * returns the dimensions of the chunk in "d"
115 *-----------------------------------------------------------------------
118 GetChunkSize(FILE *fd,
128 int A[MAXPAT][MAXDIM + 1],
132 * ----------- read input ------------
134 fscanf(fd, "%d", &N);
136 elog(ERROR, "array_in: too many access pattern elements");
137 for (i = 0; i < N; i++)
138 for (j = 0; j < ndim + 1; j++)
139 if (fscanf(fd, "%d ", &(A[i][j])) == EOF)
140 elog(ERROR, "array_in: bad access pattern input");
143 * estimate chunk size
145 for (i = 0; i < ndim; i++)
146 for (j = 0, dmax[i] = 1; j < N; j++)
147 if (dmax[i] < A[j][i])
149 csize = BLCKSZ / baseSize;
151 _FindBestChunk(csize, dmax, d, ndim, A, N);
156 /*-------------------------------------------------------------------------
158 * This routine does most of the number crunching to compute the
159 * optimal chunk shape.
160 * Called by GetChunkSize
161 *------------------------------------------------------------------------
164 _FindBestChunk(int size,
168 int A[MAXPAT][MAXDIM + 1],
177 while (get_next(d, dim, size, dmax))
181 * compute the number of page fetches for a given chunk size (*d)
182 * and access pattern (**A)
188 for (i = 0, tc = 0; i < N; i++)
190 for (j = 0, nc = 1; j < dim; j++)
191 nc *= quot_ceil(A[i][j], d[j]);
197 * tc holds the total number of page fetches
202 for (j = 0; j < dim; dbest[j] = d[j], j++)
209 /*----------------------------------------------------------------------
211 * Called by _GetBestChunk to get the next tuple in the lexicographic order
212 *---------------------------------------------------------------------
215 get_next(int *d, int k, int C, int *dmax)
224 for (j = k - 1; j >= 0; j--)
226 d[j] = min(temp, dmax[j]);
227 temp = max(1, temp / d[j]);
232 for (j = 0, temp = 1; j < k; j++)
235 for (i = k - 1; i >= 0; i--)
238 if (((temp * (d[i] + 1)) < C) && (d[i] + 1 <= dmax[i]))
246 d[i] = min(dmax[i], j / (j / d[i]));
250 for (j = k - 1; j > i; j--)
252 d[j] = min(temp, dmax[j]);
253 temp = max(1, temp / d[j]);
259 static char a_chunk[BLCKSZ + VARHDRSZ]; /* VARHDRSZ since a_chunk is in
265 initialize_info(CHUNK_INFO *A, int ndim, int *dim, int *chunk)
269 for (i = 0; i < ndim; i++)
273 /*--------------------------------------------------------------------------
274 * Procedure reorganize_data():
275 * This procedure reads the input multidimensional array that is organised
276 * in the order specified by array "X" and breaks it up into chunks of
277 * dimensions specified in "C".
279 * This is a very slow process, since reading and writing of LARGE files
282 *-------------------------------------------------------------------------
286 _ConvertToChunkFile(int n,
293 int max_chunks[MAXDIM],
301 for (i = 0; i < n; chunk_no[i++] = 0)
303 max_chunks[i] = dim[i] / C[i];
307 temp = csize + VARHDRSZ;
308 memmove(a_chunk, &temp, VARHDRSZ);
310 mda_get_prod(n, dim, PX);
311 mda_get_offset_values(n, dist, PX, C);
312 for (i = 0; i < n; dist[i] *= baseSize, i++)
316 read_chunk(chunk_no, C, &(a_chunk[VARHDRSZ]), srcfd, n, baseSize, PX, dist);
317 write_chunk((struct varlena *) a_chunk, destfd);
318 } while (next_tuple(n, chunk_no, max_chunks) != -1);
321 /*--------------------------------------------------------------------------
323 * reads a chunk from the input files into a_chunk, the position of the
324 * chunk is specified by chunk_no
325 *--------------------------------------------------------------------------
328 read_chunk(int *chunk_no,
346 for (i = start_pos = 0; i < n; i++)
348 pos[i] = chunk_no[i] * C[i];
349 start_pos += pos[i] * PX[i];
351 start_pos *= baseSize;
353 /* Read a block of dimesion C starting at co-ordinates pos */
354 unit_transfer = C[n - 1] * baseSize;
356 for (i = 0; i < n; indx[i++] = 0)
359 seek_and_read(fpOff, unit_transfer, a_chunk, srcfd, SEEK_SET);
360 fpOff += unit_transfer;
363 while ((j = next_tuple(n - 1, indx, C)) != -1)
366 seek_and_read(fpOff, unit_transfer, &(a_chunk[cp]), srcfd, SEEK_SET);
368 fpOff += unit_transfer;
372 /*--------------------------------------------------------------------------
374 * writes a chunk of size csize into the output file
375 *--------------------------------------------------------------------------
378 write_chunk(struct varlena * a_chunk, int ofile)
383 got_n = LOwrite(ofile, a_chunk);
388 /*--------------------------------------------------------------------------
390 * seeks to the asked location in the input file and reads the
391 * appropriate number of blocks
392 * Called By: read_chunk()
393 *--------------------------------------------------------------------------
396 seek_and_read(int pos, int size, char *buff, int fp, int from)
398 struct varlena *v = NULL;
400 /* Assuming only one file */
401 if (lo_lseek(fp, pos, from) < 0)
402 elog(ERROR, "File seek error");
404 v = (struct varlena *) LOread(fp, size);
406 if (VARSIZE(v) - VARHDRSZ < size)
407 elog(ERROR, "File read error");
408 memmove(buff, VARDATA(v), size);
416 /*----------------------------------------------------------------------------
418 * returns the subarray specified bu the range indices "st" and "endp"
419 * from the chunked array stored in file "fp"
420 *---------------------------------------------------------------------------
423 _ReadChunkArray(int *st,
438 int chunk_span[MAXDIM],
440 int chunk_st[MAXDIM],
449 int range_st[MAXDIM],
462 int srcOff; /* Needed since LO don't understand
464 char *baseDestFp = (char *) destfp;
466 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
469 dim = ARR_DIMS(array);
470 lb = ARR_LBOUND(array);
475 temp = dim[n - 1] / C[n - 1];
476 for (i = n - 2; i >= 0; i--)
478 PC[i] = PC[i + 1] * temp;
479 temp = dim[i] / C[i];
483 for (i = 0; i < n; st[i] -= lb[i], endp[i] -= lb[i], i++)
485 mda_get_prod(n, C, PCHUNK);
486 mda_get_range(n, array_span, st, endp);
487 mda_get_prod(n, array_span, PA);
489 array2chunk_coord(n, C, st, chunk_st);
490 array2chunk_coord(n, C, endp, chunk_end);
491 mda_get_range(n, chunk_span, chunk_st, chunk_end);
492 mda_get_offset_values(n, dist, PC, chunk_span);
494 for (i = 0; i < n; i++)
497 range_end[i] = min(chunk_st[i] * C[i] + C[i] - 1, endp[i]);
500 for (i = j = 0; i < n; i++)
501 j += chunk_st[i] * PC[i];
502 temp_seek = srcOff = j * csize * bsize;
503 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
507 for (i = 0; i < n; chunk_off[i++] = 0)
513 /* Write chunk (chunk_st) to output buffer */
514 mda_get_range(n, array_span, range_st, range_end);
515 mda_get_offset_values(n, adist, PA, array_span);
516 mda_get_offset_values(n, cdist, PCHUNK, array_span);
517 for (i = 0; i < n; range[i] = range_st[i] - st[i], i++);
518 bptr = tuple2linear(n, range, PA);
519 for (i = 0; i < n; range[i++] = 0);
524 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
528 destfp = baseDestFp + bptr;
529 for (i = 0, block_seek = 0; i < n; i++)
530 block_seek += (range_st[i] - (chunk_st[i] + chunk_off[i])
532 if (dist[jj] + block_seek + temp_seek)
534 temp = (dist[jj] * csize + block_seek + temp_seek) * bsize;
536 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
539 for (i = n - 1, to_read = bsize; i >= 0;
540 to_read *= min(C[i], array_span[i]), i--)
541 if (cdist[i] || adist[i])
547 srcOff += (cdist[j] * bsize);
548 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
551 block_seek += cdist[j];
552 bptr += adist[j] * bsize;
555 if (lo_lseek((int) destfp, bptr, SEEK_SET) < 0)
559 destfp = baseDestFp + bptr;
560 temp = _LOtransfer((char **) &destfp, to_read, 1, (char **) &fp, 1, isDestLO);
564 words_read += to_read;
566 block_seek += (to_read / bsize);
569 * compute next tuple in *range
578 range[i] = (range[i] + 1) % array_span[i];
579 for (x = i; x * (!range[x]); x--)
580 range[x - 1] = (range[x - 1] + 1) % array_span[x - 1];
594 * end of compute next tuple -- j is set to -1 if tuple
599 block_seek = csize - block_seek;
600 temp_seek = block_seek;
601 jj = next_tuple(n, chunk_off, chunk_span);
604 range_st[jj] = (chunk_st[jj] + chunk_off[jj]) * C[jj];
605 range_end[jj] = min(range_st[jj] + C[jj] - 1, endp[jj]);
607 for (i = jj + 1; i < n; i++)
610 range_end[i] = min((chunk_st[i] + chunk_off[i]) * C[i] + C[i] - 1, endp[i]);
616 /*------------------------------------------------------------------------
618 * returns one element of the chunked array as specified by the index "st"
619 * the chunked file descriptor is "fp"
620 *-------------------------------------------------------------------------
623 _ReadChunkArray1El(int *st,
634 int chunk_st[MAXDIM];
643 CHUNK_INFO *A = (CHUNK_INFO *) ARR_DATA_PTR(array);
646 lb = ARR_LBOUND(array);
648 dim = ARR_DIMS(array);
652 temp = dim[n - 1] / C[n - 1];
653 for (i = n - 2; i >= 0; i--)
655 PC[i] = PC[i + 1] * temp;
656 temp = dim[i] / C[i];
660 for (i = 0; i < n; st[i] -= lb[i], i++);
661 mda_get_prod(n, C, PCHUNK);
663 array2chunk_coord(n, C, st, chunk_st);
665 for (i = j = 0; i < n; i++)
666 j += chunk_st[i] * PC[i];
669 for (i = 0; i < n; i++)
670 srcOff += (st[i] - chunk_st[i] * C[i]) * PCHUNK[i];
673 if (lo_lseek(fp, srcOff, SEEK_SET) < 0)
676 return (struct varlena *) LOread(fp, bsize);
678 return (struct varlena *) 0;