aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/sort/lselect.c
blob: 1f92e48d838d687813ccafd0eaa797afe2d27271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*-------------------------------------------------------------------------
 *
 * lselect.c--
 *    leftist tree selection algorithm (linked priority queue--Knuth, Vol.3,
 *    pp.150-52)
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/backend/utils/sort/Attic/lselect.c,v 1.1.1.1 1996/07/09 06:22:10 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include <stdio.h>

#include "c.h"

#include "storage/buf.h"
#include "access/skey.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "utils/rel.h"

#include "utils/psort.h"
#include "utils/lselect.h"

extern	Relation	SortRdesc;		/* later static */ 

/*
 *	PUTTUP		- writes the next tuple
 *	ENDRUN		- mark end of run
 *	GETLEN		- reads the length of the next tuple
 *	ALLOCTUP	- returns space for the new tuple
 *	SETTUPLEN	- stores the length into the tuple
 *	GETTUP		- reads the tuple
 *
 *	Note:
 *		LEN field must be a short; FP is a stream
 */

#define	PUTTUP(TUP, FP)	fwrite((char *)TUP, (TUP)->t_len, 1, FP)
#define	ENDRUN(FP)	fwrite((char *)&shortzero, sizeof (shortzero), 1, FP)
#define	GETLEN(LEN, FP)	fread(&(LEN), sizeof (shortzero), 1, FP)
#define	ALLOCTUP(LEN)	((HeapTuple)malloc((unsigned)LEN))
#define	GETTUP(TUP, LEN, FP)\
	fread((char *)(TUP) + sizeof (shortzero), 1, (LEN) - sizeof (shortzero), FP)
#define	SETTUPLEN(TUP, LEN)	(TUP)->t_len = LEN

/*
 *	USEMEM		- record use of memory
 *	FREEMEM		- record freeing of memory
 *	FULLMEM		- 1 iff a tuple will fit
 */

#define	USEMEM(AMT)	SortMemory -= (AMT)
#define	FREEMEM(AMT)	SortMemory += (AMT)
#define	LACKMEM()	(SortMemory <= BLCKSZ)		/* not accurate */

/*
 *	lmerge		- merges two leftist trees into one
 *
 *	Note:
 *		Enforcing the rule that pt->lt_dist >= qt->lt_dist may
 *		simplifify much of the code.  Removing recursion will not
 *		speed up code significantly.
 */
struct leftist *
lmerge(struct leftist *pt, struct leftist *qt)
{
    register struct	leftist	*root, *majorLeftist, *minorLeftist;
    int		dist;
    
    if (tuplecmp(pt->lt_tuple, qt->lt_tuple)) {
	root = pt;
	majorLeftist = qt;
    } else {
	root = qt;
	majorLeftist = pt;
    }
    if (root->lt_left == NULL)
	root->lt_left = majorLeftist;
    else {
	if ((minorLeftist = root->lt_right) != NULL)
	    majorLeftist = lmerge(majorLeftist, minorLeftist);
	if ((dist = root->lt_left->lt_dist) < majorLeftist->lt_dist) {
	    root->lt_dist = 1 + dist;
	    root->lt_right = root->lt_left;
	    root->lt_left = majorLeftist;
	} else {
	    root->lt_dist = 1 + majorLeftist->lt_dist;
	    root->lt_right = majorLeftist;
	}
    }
    return(root);
}

static struct leftist *
linsert(struct leftist *root, struct leftist *new1)
{
    register struct	leftist	*left, *right;
    
    if (! tuplecmp(root->lt_tuple, new1->lt_tuple)) {
	new1->lt_left = root;
	return(new1);
    }
    left = root->lt_left;
    right = root->lt_right;
    if (right == NULL) {
	if (left == NULL)
	    root->lt_left = new1;
	else {
	    root->lt_right = new1;
	    root->lt_dist = 2;
	}
	return(root);
    }
    right = linsert(right, new1);
    if (right->lt_dist < left->lt_dist) {
	root->lt_dist = 1 + left->lt_dist;
	root->lt_left = right;
	root->lt_right = left;
    } else {
	root->lt_dist = 1 + right->lt_dist;
	root->lt_right = right;
    }
    return(root);
}

/*
 *	gettuple	- returns tuple at top of tree (Tuples)
 *
 *	Returns:
 *		tuple at top of tree, NULL if failed ALLOC()
 *		*devnum is set to the devnum of tuple returned
 *		*treep is set to the new tree
 *
 *	Note:
 *		*treep must not be NULL
 *		NULL is currently never returned BUG
 */
HeapTuple
gettuple(struct leftist **treep,
	 short *devnum)		/* device from which tuple came */
{
    register struct	leftist	 *tp;
    HeapTuple	tup;
    
    tp = *treep;
    tup = tp->lt_tuple;
    *devnum = tp->lt_devnum;
    if (tp->lt_dist == 1)				/* lt_left == NULL */
	*treep = tp->lt_left;
    else
	*treep = lmerge(tp->lt_left, tp->lt_right);
    
    FREEMEM(sizeof (struct leftist));
    FREE(tp);
    return(tup);
}

/*
 *	puttuple	- inserts new tuple into tree
 *
 *	Returns:
 *		NULL iff failed ALLOC()
 *
 *	Note:
 *		Currently never returns NULL BUG
 */
int
puttuple(struct leftist **treep, HeapTuple newtuple, int devnum)
{
    register struct	leftist	*new1;
    register struct	leftist	*tp;
    
    new1 = (struct leftist *) malloc((unsigned) sizeof (struct leftist));
    USEMEM(sizeof (struct leftist));
    new1->lt_dist = 1;
    new1->lt_devnum = devnum;
    new1->lt_tuple = newtuple;
    new1->lt_left = NULL;
    new1->lt_right = NULL;
    if ((tp = *treep) == NULL)
	*treep = new1;
    else
	*treep = linsert(tp, new1);
    return(1);
}


/*
 *	dumptuples	- stores all the tuples in tree into file
 */
void
dumptuples(FILE *file)
{
    register struct	leftist	*tp;
    register struct	leftist	*newp;
    HeapTuple	tup;
    
    tp = Tuples;
    while (tp != NULL) {
	tup = tp->lt_tuple;
	if (tp->lt_dist == 1)			/* lt_right == NULL */
	    newp = tp->lt_left;
	else
	    newp = lmerge(tp->lt_left, tp->lt_right);
	FREEMEM(sizeof (struct leftist));
	FREE(tp);
	PUTTUP(tup, file);
	FREEMEM(tup->t_len);
	FREE(tup);
	tp = newp;
    }
    Tuples = NULL;
}

/*
 *	tuplecmp	- Compares two tuples with respect CmpList
 *
 *	Returns:
 *		1 if left < right ;0 otherwise
 *	Assumtions:
 */
int
tuplecmp(HeapTuple ltup, HeapTuple rtup)
{
    register char	*lattr, *rattr;
    int		nkey = 0;
    extern	int	Nkeys;
    extern	ScanKey	Key;
    int		result = 0;
    bool		isnull;
    
    if (ltup == (HeapTuple)NULL)
	return(0);
    if (rtup == (HeapTuple)NULL)
	return(1);
    while (nkey < Nkeys && !result) {
	lattr = heap_getattr(ltup, InvalidBuffer,
			     Key[nkey].sk_attno, 
			     RelationGetTupleDescriptor(SortRdesc),
			     &isnull);
	if (isnull)
	    return(0);
	rattr = heap_getattr(rtup, InvalidBuffer,
			     Key[nkey].sk_attno, 
			     RelationGetTupleDescriptor(SortRdesc),
			     &isnull);
	if (isnull)
	    return(1);
	if (Key[nkey].sk_flags & SK_COMMUTE) {
	    if (!(result = (long) (*Key[nkey].sk_func) (rattr, lattr)))
		result = -(long) (*Key[nkey].sk_func) (lattr, rattr);
	} else if (!(result = (long) (*Key[nkey].sk_func) (lattr, rattr)))
	    result = -(long) (*Key[nkey].sk_func) (rattr, lattr);
	nkey++;
    }
    return (result == 1);
}

#ifdef	EBUG
void
checktree(struct leftist *tree)
{
    int		lnodes;
    int		rnodes;
    
    if (tree == NULL) {
	puts("Null tree.");
	return;
    }
    lnodes = checktreer(tree->lt_left, 1);
    rnodes = checktreer(tree->lt_right, 1);
    if (lnodes < 0) {
	lnodes = -lnodes;
	puts("0:\tBad left side.");
    }
    if (rnodes < 0) {
	rnodes = -rnodes;
	puts("0:\tBad right side.");
    }
    if (lnodes == 0) {
	if (rnodes != 0)
	    puts("0:\tLeft and right reversed.");
	if (tree->lt_dist != 1)
	    puts("0:\tDistance incorrect.");
    } else if (rnodes == 0) {
	if (tree->lt_dist != 1)
	    puts("0:\tDistance incorrect.");
    } else if (tree->lt_left->lt_dist < tree->lt_right->lt_dist) {
	puts("0:\tLeft and right reversed.");
	if (tree->lt_dist != 1 + tree->lt_left->lt_dist)
	    puts("0:\tDistance incorrect.");
    } else if (tree->lt_dist != 1+ tree->lt_right->lt_dist)
	puts("0:\tDistance incorrect.");
    if (lnodes > 0)
	if (tuplecmp(tree->lt_left->lt_tuple, tree->lt_tuple))
	    printf("%d:\tLeft child < parent.\n");
    if (rnodes > 0)
	if (tuplecmp(tree->lt_right->lt_tuple, tree->lt_tuple))
	    printf("%d:\tRight child < parent.\n");
    printf("Tree has %d nodes\n", 1 + lnodes + rnodes);
}

int
checktreer(struct leftist *tree, int level)
{
    int	lnodes, rnodes;
    int	error = 0;
    
    if (tree == NULL)
	return(0);
    lnodes = checktreer(tree->lt_left, level + 1);
    rnodes = checktreer(tree->lt_right, level + 1);
    if (lnodes < 0) {
	error = 1;
	lnodes = -lnodes;
	printf("%d:\tBad left side.\n", level);
    }
    if (rnodes < 0) {
	error = 1;
	rnodes = -rnodes;
	printf("%d:\tBad right side.\n", level);
    }
    if (lnodes == 0) {
	if (rnodes != 0) {
	    error = 1;
	    printf("%d:\tLeft and right reversed.\n", level);
	}
	if (tree->lt_dist != 1) {
	    error = 1;
	    printf("%d:\tDistance incorrect.\n", level);
	}
    } else if (rnodes == 0) {
	if (tree->lt_dist != 1) {
	    error = 1;
	    printf("%d:\tDistance incorrect.\n", level);
	}			
    } else if (tree->lt_left->lt_dist < tree->lt_right->lt_dist) {
	error = 1;
	printf("%d:\tLeft and right reversed.\n", level);
	if (tree->lt_dist != 1 + tree->lt_left->lt_dist)
	    printf("%d:\tDistance incorrect.\n", level);
    } else if (tree->lt_dist != 1+ tree->lt_right->lt_dist) {
	error = 1;
	printf("%d:\tDistance incorrect.\n", level);
    }
    if (lnodes > 0)
	if (tuplecmp(tree->lt_left->lt_tuple, tree->lt_tuple)) {
	    error = 1;
	    printf("%d:\tLeft child < parent.\n");
	}
    if (rnodes > 0)
	if (tuplecmp(tree->lt_right->lt_tuple, tree->lt_tuple)) {
	    error = 1;
	    printf("%d:\tRight child < parent.\n");
	}
    if (error)
	return(-1 + -lnodes + -rnodes);
    return(1 + lnodes + rnodes);
}
#endif