Commit 71d5d953 authored by Tom Lane's avatar Tom Lane

Update hash and join routines to use fd.c's new temp-file

code, instead of not-very-bulletproof stuff they had before.
parent c1167a08
......@@ -5,7 +5,7 @@
*
*
* IDENTIFICATION
* $Id: nbtsort.c,v 1.37 1999/02/21 03:48:27 scrappy Exp $
* $Id: nbtsort.c,v 1.38 1999/05/09 00:53:19 tgl Exp $
*
* NOTES
*
......@@ -85,11 +85,9 @@ static void _bt_uppershutdown(Relation index, BTPageState *state);
#define MAXTAPES (7)
#define TAPEBLCKSZ (BLCKSZ << 2)
#define TAPETEMP "pg_btsortXXXXXXX"
extern int NDirectFileRead;
extern int NDirectFileWrite;
extern char *mktemp(char *template);
/*
* this is what we use to shovel BTItems in and out of memory. it's
......@@ -107,7 +105,7 @@ extern char *mktemp(char *template);
typedef struct
{
int bttb_magic; /* magic number */
int bttb_fd; /* file descriptor */
File bttb_fd; /* file descriptor */
int bttb_top; /* top of free space within bttb_data */
short bttb_ntup; /* number of tuples in this block */
short bttb_eor; /* End-Of-Run marker */
......@@ -380,7 +378,7 @@ _bt_tapereset(BTTapeBlock *tape)
static void
_bt_taperewind(BTTapeBlock *tape)
{
FileSeek(tape->bttb_fd, 0, SEEK_SET);
FileSeek(tape->bttb_fd, 0L, SEEK_SET);
}
/*
......@@ -411,7 +409,7 @@ _bt_tapeclear(BTTapeBlock *tape)
* as well as opening a physical tape file.
*/
static BTTapeBlock *
_bt_tapecreate(char *fname)
_bt_tapecreate(void)
{
BTTapeBlock *tape = (BTTapeBlock *) palloc(sizeof(BTTapeBlock));
......@@ -420,11 +418,7 @@ _bt_tapecreate(char *fname)
tape->bttb_magic = BTTAPEMAGIC;
#ifndef __CYGWIN32__
tape->bttb_fd = FileNameOpenFile(fname, O_RDWR | O_CREAT | O_TRUNC, 0600);
#else
tape->bttb_fd = FileNameOpenFile(fname, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0600);
#endif
tape->bttb_fd = OpenTemporaryFile();
Assert(tape->bttb_fd >= 0);
/* initialize the buffer */
......@@ -467,7 +461,7 @@ _bt_tapewrite(BTTapeBlock *tape, int eor)
static int
_bt_taperead(BTTapeBlock *tape)
{
int fd;
File fd;
int nread;
if (tape->bttb_eor)
......@@ -550,9 +544,8 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique)
{
BTSpool *btspool = (BTSpool *) palloc(sizeof(BTSpool));
int i;
char *fname = (char *) palloc(sizeof(TAPETEMP) + 1);
if (btspool == (BTSpool *) NULL || fname == (char *) NULL)
if (btspool == (BTSpool *) NULL)
elog(ERROR, "_bt_spoolinit: out of memory");
MemSet((char *) btspool, 0, sizeof(BTSpool));
btspool->bts_ntapes = ntapes;
......@@ -567,10 +560,9 @@ _bt_spoolinit(Relation index, int ntapes, bool isunique)
for (i = 0; i < ntapes; ++i)
{
btspool->bts_itape[i] = _bt_tapecreate(mktemp(strcpy(fname, TAPETEMP)));
btspool->bts_otape[i] = _bt_tapecreate(mktemp(strcpy(fname, TAPETEMP)));
btspool->bts_itape[i] = _bt_tapecreate();
btspool->bts_otape[i] = _bt_tapecreate();
}
pfree((void *) fname);
_bt_isortcmpinit(index, btspool);
......
......@@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California
*
*
* $Id: nodeHash.c,v 1.33 1999/05/06 00:30:46 tgl Exp $
* $Id: nodeHash.c,v 1.34 1999/05/09 00:53:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -39,9 +39,6 @@
extern int NBuffers;
#define HJ_TEMP_NAMELEN 16 /* max length for mk_hj_temp file names */
static void mk_hj_temp(char *tempname);
static int hashFunc(Datum key, int len, bool byVal);
static RelativeAddr hashTableAlloc(int size, HashJoinTable hashtable);
static void * absHashTableAlloc(int size, HashJoinTable hashtable);
......@@ -72,7 +69,6 @@ ExecHash(Hash *node)
RelativeAddr *batchPos;
int *batchSizes;
int i;
RelativeAddr *innerbatchNames;
/* ----------------
* get state info from node
......@@ -91,8 +87,6 @@ ExecHash(Hash *node)
if (nbatch > 0)
{ /* if needs hash partition */
innerbatchNames = (RelativeAddr *) ABSADDR(hashtable->innerbatchNames);
/* --------------
* allocate space for the file descriptors of batch files
* then open the batch files in the current processes.
......@@ -101,13 +95,7 @@ ExecHash(Hash *node)
batches = (File *) palloc(nbatch * sizeof(File));
for (i = 0; i < nbatch; i++)
{
#ifndef __CYGWIN32__
batches[i] = FileNameOpenFile(ABSADDR(innerbatchNames[i]),
O_CREAT | O_RDWR, 0600);
#else
batches[i] = FileNameOpenFile(ABSADDR(innerbatchNames[i]),
O_CREAT | O_RDWR | O_BINARY, 0600);
#endif
batches[i] = OpenTemporaryFile();
}
hashstate->hashBatches = batches;
batchPos = (RelativeAddr *) ABSADDR(hashtable->innerbatchPos);
......@@ -291,7 +279,7 @@ absHashTableAlloc(int size, HashJoinTable hashtable)
* ----------------------------------------------------------------
*/
#define NTUP_PER_BUCKET 10
#define FUDGE_FAC 1.5
#define FUDGE_FAC 2.0
HashJoinTable
ExecHashTableCreate(Hash *node)
......@@ -310,12 +298,9 @@ ExecHashTableCreate(Hash *node)
int totalbuckets;
int bucketsize;
int i;
RelativeAddr *outerbatchNames;
RelativeAddr *outerbatchPos;
RelativeAddr *innerbatchNames;
RelativeAddr *innerbatchPos;
int *innerbatchSizes;
RelativeAddr tempname;
/* ----------------
* Get information about the size of the relation to be hashed
......@@ -425,46 +410,32 @@ ExecHashTableCreate(Hash *node)
* allocate and initialize the outer batches
* ---------------
*/
outerbatchNames = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
outerbatchPos = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
for (i = 0; i < nbatch; i++)
{
tempname = hashTableAlloc(HJ_TEMP_NAMELEN, hashtable);
mk_hj_temp(ABSADDR(tempname));
outerbatchNames[i] = tempname;
outerbatchPos[i] = -1;
}
hashtable->outerbatchNames = RELADDR(outerbatchNames);
hashtable->outerbatchPos = RELADDR(outerbatchPos);
/* ---------------
* allocate and initialize the inner batches
* ---------------
*/
innerbatchNames = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
innerbatchPos = (RelativeAddr *)
absHashTableAlloc(nbatch * sizeof(RelativeAddr), hashtable);
innerbatchSizes = (int *)
absHashTableAlloc(nbatch * sizeof(int), hashtable);
for (i = 0; i < nbatch; i++)
{
tempname = hashTableAlloc(HJ_TEMP_NAMELEN, hashtable);
mk_hj_temp(ABSADDR(tempname));
innerbatchNames[i] = tempname;
innerbatchPos[i] = -1;
innerbatchSizes[i] = 0;
}
hashtable->innerbatchNames = RELADDR(innerbatchNames);
hashtable->innerbatchPos = RELADDR(innerbatchPos);
hashtable->innerbatchSizes = RELADDR(innerbatchSizes);
}
else
{
hashtable->outerbatchNames = (RelativeAddr) NULL;
hashtable->outerbatchPos = (RelativeAddr) NULL;
hashtable->innerbatchNames = (RelativeAddr) NULL;
hashtable->innerbatchPos = (RelativeAddr) NULL;
hashtable->innerbatchSizes = (RelativeAddr) NULL;
}
......@@ -886,15 +857,6 @@ ExecHashTableReset(HashJoinTable hashtable, int ntuples)
hashtable->pcount = hashtable->nprocess;
}
static void
mk_hj_temp(char *tempname)
{
static int hjtmpcnt = 0;
snprintf(tempname, HJ_TEMP_NAMELEN, "HJ%d.%d", (int) MyProcPid, hjtmpcnt);
hjtmpcnt = (hjtmpcnt + 1) % 1000;
}
void
ExecReScanHash(Hash *node, ExprContext *exprCtxt, Plan *parent)
{
......
......@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.18 1999/05/06 00:30:47 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/executor/nodeHashjoin.c,v 1.19 1999/05/09 00:53:21 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -74,7 +74,6 @@ ExecHashJoin(HashJoin *node)
int nbatch;
int curbatch;
File *outerbatches;
RelativeAddr *outerbatchNames;
RelativeAddr *outerbatchPos;
Var *innerhashkey;
int batch;
......@@ -166,21 +165,10 @@ ExecHashJoin(HashJoin *node)
*/
innerhashkey = hashNode->hashkey;
hjstate->hj_InnerHashKey = innerhashkey;
outerbatchNames = (RelativeAddr *)
ABSADDR(hashtable->outerbatchNames);
outerbatches = (File *)
palloc(nbatch * sizeof(File));
outerbatches = (File *) palloc(nbatch * sizeof(File));
for (i = 0; i < nbatch; i++)
{
#ifndef __CYGWIN32__
outerbatches[i] = FileNameOpenFile(
ABSADDR(outerbatchNames[i]),
O_CREAT | O_RDWR, 0600);
#else
outerbatches[i] = FileNameOpenFile(
ABSADDR(outerbatchNames[i]),
O_CREAT | O_RDWR | O_BINARY, 0600);
#endif
outerbatches[i] = OpenTemporaryFile();
}
hjstate->hj_OuterBatches = outerbatches;
......@@ -193,7 +181,6 @@ ExecHashJoin(HashJoin *node)
}
outerbatchPos = (RelativeAddr *) ABSADDR(hashtable->outerbatchPos);
curbatch = hashtable->curbatch;
outerbatchNames = (RelativeAddr *) ABSADDR(hashtable->outerbatchNames);
/* ----------------
* Now get an outer tuple and probe into the hash table for matches
......
This diff is collapsed.
......@@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: hashjoin.h,v 1.9 1999/05/06 00:30:45 tgl Exp $
* $Id: hashjoin.h,v 1.10 1999/05/09 00:53:18 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -54,9 +54,7 @@ typedef struct HashTableData
RelativeAddr batch; /* char* */
RelativeAddr readbuf; /* char* */
int nbatch;
RelativeAddr outerbatchNames; /* RelativeAddr* */
RelativeAddr outerbatchPos; /* RelativeAddr* */
RelativeAddr innerbatchNames; /* RelativeAddr* */
RelativeAddr innerbatchPos; /* RelativeAddr* */
RelativeAddr innerbatchSizes; /* int* */
int curbatch;
......
......@@ -6,27 +6,25 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: psort.h,v 1.17 1999/02/13 23:22:29 momjian Exp $
* $Id: psort.h,v 1.18 1999/05/09 00:53:18 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PSORT_H
#define PSORT_H
#include <stdio.h>
#include "storage/fd.h"
#include "access/relscan.h"
#include "utils/lselect.h"
#include "nodes/plannodes.h"
#define MAXTAPES 7 /* 7--See Fig. 70, p273 */
#define TAPEEXTLEN strlen("pg_psort.xxxxx.xxx") /* TEMPDIR/TAPEEXT */
#define FREE(x) pfree((char *) x)
#define MAXTAPES 7 /* See Knuth Fig. 70, p273 */
struct tape
{
int tp_dummy; /* (D) */
int tp_fib; /* (A) */
FILE *tp_file; /* (TAPE) */
BufFile *tp_file; /* (TAPE) */
struct tape *tp_prev;
};
......@@ -58,7 +56,7 @@ typedef struct Psortstate
struct leftist *Tuples;
FILE *psort_grab_file;
BufFile *psort_grab_file;
long psort_current; /* could be file offset, or array index */
long psort_saved; /* could be file offset, or array index */
bool using_tape_files;
......@@ -68,7 +66,6 @@ typedef struct Psortstate
} Psortstate;
#ifdef EBUG
#include <stdio.h>
#include "utils/elog.h"
#include "storage/buf.h"
#include "storage/bufmgr.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment