aboutsummaryrefslogtreecommitdiff
path: root/contrib/btree_gist/btree_common.c
blob: 9306076e0aed94434980f1bfdb8377b7c68455c0 (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
#include "btree_gist.h"

PG_FUNCTION_INFO_V1(btree_decompress);
Datum		btree_decompress(PG_FUNCTION_ARGS);

/*
** GiST DeCompress methods
** do not do anything.
*/
Datum
btree_decompress(PG_FUNCTION_ARGS)
{
	PG_RETURN_POINTER(PG_GETARG_POINTER(0));
}


/**************************************************
 * Common btree-function (for all ops)
 **************************************************/

/*
** The GiST PickSplit method
*/
extern GIST_SPLITVEC *
btree_picksplit(bytea *entryvec, GIST_SPLITVEC *v, BINARY_UNION bu, CMPFUNC cmp)
{
	OffsetNumber i;
	RIX		   *array;
	OffsetNumber maxoff;
	int			nbytes;

	maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 1;
	nbytes = (maxoff + 2) * sizeof(OffsetNumber);
	v->spl_left = (OffsetNumber *) palloc(nbytes);
	v->spl_right = (OffsetNumber *) palloc(nbytes);
	v->spl_nleft = 0;
	v->spl_nright = 0;
	v->spl_ldatum = PointerGetDatum(0);
	v->spl_rdatum = PointerGetDatum(0);
	array = (RIX *) palloc(sizeof(RIX) * (maxoff + 1));

	/* copy the data into RIXes, and sort the RIXes */
	for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
	{
		array[i].index = i;
		array[i].r = (char *) DatumGetPointer((((GISTENTRY *) (VARDATA(entryvec)))[i].key));
	}
	qsort((void *) &array[FirstOffsetNumber], maxoff - FirstOffsetNumber + 1,
		  sizeof(RIX), cmp);

	for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
	{
		if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
		{
			v->spl_left[v->spl_nleft] = array[i].index;
			v->spl_nleft++;
			(*bu) (&v->spl_ldatum, array[i].r);
		}
		else
		{
			v->spl_right[v->spl_nright] = array[i].index;
			v->spl_nright++;
			(*bu) (&v->spl_rdatum, array[i].r);
		}
	}
	pfree(array);

	return (v);
}