summaryrefslogtreecommitdiff
path: root/src/backend/access/gin/ginutil.c
blob: 544956c84ef42f70724a024fdbb184996d17ea58 (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
/*-------------------------------------------------------------------------
 *
 * ginutil.c
 *    utilities routines for the postgres inverted index access method.
 *
 *
 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *          $PostgreSQL: pgsql/src/backend/access/gin/ginutil.c,v 1.3 2006/07/03 22:45:36 tgl Exp $
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "access/genam.h"
#include "access/gin.h"
#include "access/heapam.h"
#include "access/reloptions.h"
#include "miscadmin.h"
#include "storage/freespace.h"

void 
initGinState( GinState *state, Relation index ) {
	if ( index->rd_att->natts != 1 )
			elog(ERROR, "numberOfAttributes %d != 1",
					index->rd_att->natts);
	
	state->tupdesc = index->rd_att;

	fmgr_info_copy(&(state->compareFn),
			index_getprocinfo(index, 1, GIN_COMPARE_PROC),
			CurrentMemoryContext);
	fmgr_info_copy(&(state->extractValueFn),
			index_getprocinfo(index, 1, GIN_EXTRACTVALUE_PROC),
			CurrentMemoryContext);
	fmgr_info_copy(&(state->extractQueryFn),
			index_getprocinfo(index, 1, GIN_EXTRACTQUERY_PROC),
			CurrentMemoryContext);
	fmgr_info_copy(&(state->consistentFn),
			index_getprocinfo(index, 1, GIN_CONSISTENT_PROC),
			CurrentMemoryContext);
}

/*
 * Allocate a new page (either by recycling, or by extending the index file)
 * The returned buffer is already pinned and exclusive-locked
 * Caller is responsible for initializing the page by calling GinInitBuffer
 */

Buffer
GinNewBuffer(Relation index) {
	Buffer	buffer;
	bool	needLock;

	/* First, try to get a page from FSM */
	for(;;) {
		BlockNumber blkno = GetFreeIndexPage(&index->rd_node);
		if (blkno == InvalidBlockNumber)
			break;

		buffer = ReadBuffer(index, blkno);

		/*
		 * We have to guard against the possibility that someone else already
		 * recycled this page; the buffer may be locked if so.
		 */
		 if (ConditionalLockBuffer(buffer)) {
			Page        page = BufferGetPage(buffer);

			if (PageIsNew(page))
				return buffer;  /* OK to use, if never initialized */

			if (GinPageIsDeleted(page))
				return buffer;  /* OK to use */

			LockBuffer(buffer, GIN_UNLOCK);
		}

		/* Can't use it, so release buffer and try again */
		ReleaseBuffer(buffer);
	}

	/* Must extend the file */
	needLock = !RELATION_IS_LOCAL(index);
	if (needLock)
		LockRelationForExtension(index, ExclusiveLock);

	buffer = ReadBuffer(index, P_NEW);
	LockBuffer(buffer, GIN_EXCLUSIVE);

	if (needLock)
		UnlockRelationForExtension(index, ExclusiveLock);

	return buffer;
}

void
GinInitPage(Page page, uint32 f, Size pageSize) {
	GinPageOpaque opaque;

	PageInit(page, pageSize, sizeof(GinPageOpaqueData));

	opaque = GinPageGetOpaque(page);
	memset( opaque, 0, sizeof(GinPageOpaqueData) );
	opaque->flags = f;   
	opaque->rightlink = InvalidBlockNumber;
}

void
GinInitBuffer(Buffer b, uint32 f) {
	GinInitPage( BufferGetPage(b), f, BufferGetPageSize(b) );
}

int
compareEntries(GinState *ginstate, Datum a, Datum b) {
	return DatumGetInt32(
		FunctionCall2(
			&ginstate->compareFn,
			a, b
		)
	);
}

static FmgrInfo* cmpDatumPtr=NULL;
static bool needUnique = FALSE;

static int
cmpEntries(const void * a, const void * b) {
	int res = DatumGetInt32(
		FunctionCall2(
			cmpDatumPtr,
			*(Datum*)a,
			*(Datum*)b
		)
	);

	if ( res == 0 ) 
		needUnique = TRUE;

	return res;
}

Datum*
extractEntriesS(GinState *ginstate, Datum value, uint32 *nentries) {
	Datum *entries;

   	entries = (Datum*)DatumGetPointer(
							FunctionCall2(
								&ginstate->extractValueFn,
								value,
								PointerGetDatum( nentries )
							)
			);

	if ( entries == NULL )
		*nentries = 0;

	if ( *nentries > 1 ) {
		cmpDatumPtr = &ginstate->compareFn;
		needUnique = FALSE;
		qsort(entries, *nentries, sizeof(Datum), cmpEntries); 
	}

	return entries;
}


Datum*
extractEntriesSU(GinState *ginstate, Datum value, uint32 *nentries) {
	Datum *entries = extractEntriesS(ginstate, value, nentries);

	if ( *nentries>1 && needUnique ) {
		Datum *ptr, *res;

		ptr = res = entries;

		while( ptr - entries < *nentries ) {
			if ( compareEntries(ginstate, *ptr, *res ) != 0 ) 
				*(++res) = *ptr++;
			else
				ptr++;
		}

		*nentries = res + 1 - entries;
	}

	return entries;
}

/*
 * It's analog of PageGetTempPage(), but copies whole page
 */
Page
GinPageGetCopyPage( Page page ) {
	Size    pageSize = PageGetPageSize( page );
	Page tmppage;

	tmppage=(Page)palloc( pageSize );
	memcpy( tmppage, page, pageSize );
	
	return tmppage;
}

Datum
ginoptions(PG_FUNCTION_ARGS)
{
	Datum		reloptions = PG_GETARG_DATUM(0);
	bool		validate = PG_GETARG_BOOL(1);
	bytea	   *result;

	/*
	 * It's not clear that fillfactor is useful for GIN, but for the moment
	 * we'll accept it anyway.  (It won't do anything...)
	 */
#define GIN_MIN_FILLFACTOR			50
#define GIN_DEFAULT_FILLFACTOR		100

	result = default_reloptions(reloptions, validate,
								GIN_MIN_FILLFACTOR,
								GIN_DEFAULT_FILLFACTOR);
	if (result)
		PG_RETURN_BYTEA_P(result);
	PG_RETURN_NULL();
}