aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/gin/gininsert.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2006-10-04 00:30:14 +0000
committerBruce Momjian <bruce@momjian.us>2006-10-04 00:30:14 +0000
commitf99a569a2ee3763b4ae174e81250c95ca0fdcbb6 (patch)
tree76e6371fe8b347c73d7020c0bc54b9fba519dc10 /src/backend/access/gin/gininsert.c
parent451e419e9852cdf9d7e7cefc09d5355abb3405e9 (diff)
downloadpostgresql-f99a569a2ee3763b4ae174e81250c95ca0fdcbb6.tar.gz
postgresql-f99a569a2ee3763b4ae174e81250c95ca0fdcbb6.zip
pgindent run for 8.2.
Diffstat (limited to 'src/backend/access/gin/gininsert.c')
-rw-r--r--src/backend/access/gin/gininsert.c249
1 files changed, 136 insertions, 113 deletions
diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c
index 60107a57709..e4f87e720da 100644
--- a/src/backend/access/gin/gininsert.c
+++ b/src/backend/access/gin/gininsert.c
@@ -1,14 +1,14 @@
/*-------------------------------------------------------------------------
*
* gininsert.c
- * insert routines for the postgres inverted index access method.
+ * insert 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/gininsert.c,v 1.4 2006/07/14 14:52:16 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/access/gin/gininsert.c,v 1.5 2006/10/04 00:29:47 momjian Exp $
*-------------------------------------------------------------------------
*/
@@ -19,12 +19,13 @@
#include "miscadmin.h"
#include "utils/memutils.h"
-typedef struct {
- GinState ginstate;
- double indtuples;
- MemoryContext tmpCtx;
- MemoryContext funcCtx;
- BuildAccumulator accum;
+typedef struct
+{
+ GinState ginstate;
+ double indtuples;
+ MemoryContext tmpCtx;
+ MemoryContext funcCtx;
+ BuildAccumulator accum;
} GinBuildState;
/*
@@ -32,24 +33,26 @@ typedef struct {
* suppose that items[] fits to page
*/
static BlockNumber
-createPostingTree( Relation index, ItemPointerData *items, uint32 nitems ) {
+createPostingTree(Relation index, ItemPointerData *items, uint32 nitems)
+{
BlockNumber blkno;
- Buffer buffer = GinNewBuffer(index);
- Page page;
+ Buffer buffer = GinNewBuffer(index);
+ Page page;
START_CRIT_SECTION();
- GinInitBuffer( buffer, GIN_DATA|GIN_LEAF );
+ GinInitBuffer(buffer, GIN_DATA | GIN_LEAF);
page = BufferGetPage(buffer);
blkno = BufferGetBlockNumber(buffer);
- memcpy( GinDataPageGetData(page), items, sizeof(ItemPointerData) * nitems );
+ memcpy(GinDataPageGetData(page), items, sizeof(ItemPointerData) * nitems);
GinPageGetOpaque(page)->maxoff = nitems;
- if (!index->rd_istemp) {
- XLogRecPtr recptr;
+ if (!index->rd_istemp)
+ {
+ XLogRecPtr recptr;
XLogRecData rdata[2];
- ginxlogCreatePostingTree data;
+ ginxlogCreatePostingTree data;
data.node = index->rd_node;
data.blkno = blkno;
@@ -71,7 +74,7 @@ createPostingTree( Relation index, ItemPointerData *items, uint32 nitems ) {
PageSetLSN(page, recptr);
PageSetTLI(page, ThisTimeLineID);
- }
+ }
MarkBufferDirty(buffer);
UnlockReleaseBuffer(buffer);
@@ -89,21 +92,25 @@ createPostingTree( Relation index, ItemPointerData *items, uint32 nitems ) {
* GinFormTuple().
*/
static IndexTuple
-addItemPointersToTuple(Relation index, GinState *ginstate, GinBtreeStack *stack,
- IndexTuple old, ItemPointerData *items, uint32 nitem, bool isBuild) {
- bool isnull;
- Datum key = index_getattr(old, FirstOffsetNumber, ginstate->tupdesc, &isnull);
- IndexTuple res = GinFormTuple(ginstate, key, NULL, nitem + GinGetNPosting(old));
-
- if ( res ) {
+addItemPointersToTuple(Relation index, GinState *ginstate, GinBtreeStack *stack,
+ IndexTuple old, ItemPointerData *items, uint32 nitem, bool isBuild)
+{
+ bool isnull;
+ Datum key = index_getattr(old, FirstOffsetNumber, ginstate->tupdesc, &isnull);
+ IndexTuple res = GinFormTuple(ginstate, key, NULL, nitem + GinGetNPosting(old));
+
+ if (res)
+ {
/* good, small enough */
- MergeItemPointers( GinGetPosting(res),
- GinGetPosting(old), GinGetNPosting(old),
- items, nitem
- );
-
+ MergeItemPointers(GinGetPosting(res),
+ GinGetPosting(old), GinGetNPosting(old),
+ items, nitem
+ );
+
GinSetNPosting(res, nitem + GinGetNPosting(old));
- } else {
+ }
+ else
+ {
BlockNumber postingRoot;
GinPostingTreeScan *gdi;
@@ -112,7 +119,7 @@ addItemPointersToTuple(Relation index, GinState *ginstate, GinBtreeStack *stack,
postingRoot = createPostingTree(index, GinGetPosting(old), GinGetNPosting(old));
GinSetPostingTree(res, postingRoot);
- gdi = prepareScanPostingTree(index, postingRoot, FALSE);
+ gdi = prepareScanPostingTree(index, postingRoot, FALSE);
gdi->btree.isBuild = isBuild;
insertItemPointer(gdi, items, nitem);
@@ -124,36 +131,39 @@ addItemPointersToTuple(Relation index, GinState *ginstate, GinBtreeStack *stack,
}
/*
- * Inserts only one entry to the index, but it can adds more that 1
- * ItemPointer.
+ * Inserts only one entry to the index, but it can adds more that 1
+ * ItemPointer.
*/
static void
-ginEntryInsert( Relation index, GinState *ginstate, Datum value, ItemPointerData *items, uint32 nitem, bool isBuild) {
- GinBtreeData btree;
+ginEntryInsert(Relation index, GinState *ginstate, Datum value, ItemPointerData *items, uint32 nitem, bool isBuild)
+{
+ GinBtreeData btree;
GinBtreeStack *stack;
- IndexTuple itup;
- Page page;
+ IndexTuple itup;
+ Page page;
- prepareEntryScan( &btree, index, value, ginstate );
+ prepareEntryScan(&btree, index, value, ginstate);
stack = ginFindLeafPage(&btree, NULL);
- page = BufferGetPage( stack->buffer );
+ page = BufferGetPage(stack->buffer);
- if ( btree.findItem( &btree, stack ) ) {
+ if (btree.findItem(&btree, stack))
+ {
/* found entry */
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, stack->off));
- if ( GinIsPostingTree(itup) ) {
+ if (GinIsPostingTree(itup))
+ {
/* lock root of posting tree */
GinPostingTreeScan *gdi;
- BlockNumber rootPostingTree = GinGetPostingTree(itup);
+ BlockNumber rootPostingTree = GinGetPostingTree(itup);
/* release all stack */
LockBuffer(stack->buffer, GIN_UNLOCK);
- freeGinBtreeStack( stack );
+ freeGinBtreeStack(stack);
/* insert into posting tree */
- gdi = prepareScanPostingTree( index, rootPostingTree, FALSE );
+ gdi = prepareScanPostingTree(index, rootPostingTree, FALSE);
gdi->btree.isBuild = isBuild;
insertItemPointer(gdi, items, nitem);
@@ -163,23 +173,26 @@ ginEntryInsert( Relation index, GinState *ginstate, Datum value, ItemPointerData
itup = addItemPointersToTuple(index, ginstate, stack, itup, items, nitem, isBuild);
btree.isDelete = TRUE;
- } else {
+ }
+ else
+ {
/* We suppose, that tuple can store at list one itempointer */
- itup = GinFormTuple( ginstate, value, items, 1);
- if ( itup==NULL || IndexTupleSize(itup) >= GinMaxItemSize )
+ itup = GinFormTuple(ginstate, value, items, 1);
+ if (itup == NULL || IndexTupleSize(itup) >= GinMaxItemSize)
elog(ERROR, "huge tuple");
- if ( nitem>1 ) {
+ if (nitem > 1)
+ {
IndexTuple previtup = itup;
- itup = addItemPointersToTuple(index, ginstate, stack, previtup, items+1, nitem-1, isBuild);
+ itup = addItemPointersToTuple(index, ginstate, stack, previtup, items + 1, nitem - 1, isBuild);
pfree(previtup);
}
}
btree.entry = itup;
ginInsertValue(&btree, stack);
- pfree( itup );
+ pfree(itup);
}
/*
@@ -187,48 +200,53 @@ ginEntryInsert( Relation index, GinState *ginstate, Datum value, ItemPointerData
* Function isnt use during normal insert
*/
static uint32
-ginHeapTupleBulkInsert(GinBuildState *buildstate, Datum value, ItemPointer heapptr) {
- Datum *entries;
- uint32 nentries;
+ginHeapTupleBulkInsert(GinBuildState *buildstate, Datum value, ItemPointer heapptr)
+{
+ Datum *entries;
+ uint32 nentries;
MemoryContext oldCtx;
oldCtx = MemoryContextSwitchTo(buildstate->funcCtx);
- entries = extractEntriesSU( buildstate->accum.ginstate, value, &nentries);
+ entries = extractEntriesSU(buildstate->accum.ginstate, value, &nentries);
MemoryContextSwitchTo(oldCtx);
- if ( nentries==0 )
+ if (nentries == 0)
/* nothing to insert */
return 0;
- ginInsertRecordBA( &buildstate->accum, heapptr, entries, nentries);
+ ginInsertRecordBA(&buildstate->accum, heapptr, entries, nentries);
MemoryContextReset(buildstate->funcCtx);
return nentries;
}
-static void
+static void
ginBuildCallback(Relation index, HeapTuple htup, Datum *values,
- bool *isnull, bool tupleIsAlive, void *state) {
+ bool *isnull, bool tupleIsAlive, void *state)
+{
- GinBuildState *buildstate = (GinBuildState*)state;
+ GinBuildState *buildstate = (GinBuildState *) state;
MemoryContext oldCtx;
- if ( *isnull )
+ if (*isnull)
return;
oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
buildstate->indtuples += ginHeapTupleBulkInsert(buildstate, *values, &htup->t_self);
- /* we use only half maintenance_work_mem, because there is some leaks
- during insertion and extract values */
- if ( buildstate->accum.allocatedMemory >= maintenance_work_mem*1024L/2L ) {
- ItemPointerData *list;
- Datum entry;
- uint32 nlist;
+ /*
+ * we use only half maintenance_work_mem, because there is some leaks
+ * during insertion and extract values
+ */
+ if (buildstate->accum.allocatedMemory >= maintenance_work_mem * 1024L / 2L)
+ {
+ ItemPointerData *list;
+ Datum entry;
+ uint32 nlist;
- while( (list=ginGetEntry(&buildstate->accum, &entry, &nlist)) != NULL )
+ while ((list = ginGetEntry(&buildstate->accum, &entry, &nlist)) != NULL)
ginEntryInsert(index, &buildstate->ginstate, entry, list, nlist, TRUE);
MemoryContextReset(buildstate->tmpCtx);
@@ -239,22 +257,23 @@ ginBuildCallback(Relation index, HeapTuple htup, Datum *values,
}
Datum
-ginbuild(PG_FUNCTION_ARGS) {
- Relation heap = (Relation) PG_GETARG_POINTER(0);
- Relation index = (Relation) PG_GETARG_POINTER(1);
+ginbuild(PG_FUNCTION_ARGS)
+{
+ Relation heap = (Relation) PG_GETARG_POINTER(0);
+ Relation index = (Relation) PG_GETARG_POINTER(1);
IndexInfo *indexInfo = (IndexInfo *) PG_GETARG_POINTER(2);
IndexBuildResult *result;
- double reltuples;
- GinBuildState buildstate;
+ double reltuples;
+ GinBuildState buildstate;
Buffer buffer;
- ItemPointerData *list;
- Datum entry;
- uint32 nlist;
+ ItemPointerData *list;
+ Datum entry;
+ uint32 nlist;
MemoryContext oldCtx;
if (RelationGetNumberOfBlocks(index) != 0)
elog(ERROR, "index \"%s\" already contains data",
- RelationGetRelationName(index));
+ RelationGetRelationName(index));
initGinState(&buildstate.ginstate, index);
@@ -262,10 +281,11 @@ ginbuild(PG_FUNCTION_ARGS) {
buffer = GinNewBuffer(index);
START_CRIT_SECTION();
GinInitBuffer(buffer, GIN_LEAF);
- if (!index->rd_istemp) {
- XLogRecPtr recptr;
+ if (!index->rd_istemp)
+ {
+ XLogRecPtr recptr;
XLogRecData rdata;
- Page page;
+ Page page;
rdata.buffer = InvalidBuffer;
rdata.data = (char *) &(index->rd_node);
@@ -279,7 +299,7 @@ ginbuild(PG_FUNCTION_ARGS) {
PageSetLSN(page, recptr);
PageSetTLI(page, ThisTimeLineID);
- }
+ }
MarkBufferDirty(buffer);
UnlockReleaseBuffer(buffer);
@@ -293,26 +313,26 @@ ginbuild(PG_FUNCTION_ARGS) {
* inserted into the index
*/
buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
- "Gin build temporary context",
- ALLOCSET_DEFAULT_MINSIZE,
- ALLOCSET_DEFAULT_INITSIZE,
- ALLOCSET_DEFAULT_MAXSIZE);
+ "Gin build temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
buildstate.funcCtx = AllocSetContextCreate(buildstate.tmpCtx,
- "Gin build temporary context for user-defined function",
- ALLOCSET_DEFAULT_MINSIZE,
- ALLOCSET_DEFAULT_INITSIZE,
- ALLOCSET_DEFAULT_MAXSIZE);
+ "Gin build temporary context for user-defined function",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
buildstate.accum.ginstate = &buildstate.ginstate;
- ginInitBA( &buildstate.accum );
+ ginInitBA(&buildstate.accum);
/* do the heap scan */
reltuples = IndexBuildHeapScan(heap, index, indexInfo,
- ginBuildCallback, (void *) &buildstate);
+ ginBuildCallback, (void *) &buildstate);
oldCtx = MemoryContextSwitchTo(buildstate.tmpCtx);
- while( (list=ginGetEntry(&buildstate.accum, &entry, &nlist)) != NULL )
+ while ((list = ginGetEntry(&buildstate.accum, &entry, &nlist)) != NULL)
ginEntryInsert(index, &buildstate.ginstate, entry, list, nlist, TRUE);
MemoryContextSwitchTo(oldCtx);
@@ -333,55 +353,58 @@ ginbuild(PG_FUNCTION_ARGS) {
* Inserts value during normal insertion
*/
static uint32
-ginHeapTupleInsert( Relation index, GinState *ginstate, Datum value, ItemPointer item) {
- Datum *entries;
- uint32 i,nentries;
+ginHeapTupleInsert(Relation index, GinState *ginstate, Datum value, ItemPointer item)
+{
+ Datum *entries;
+ uint32 i,
+ nentries;
- entries = extractEntriesSU( ginstate, value, &nentries);
+ entries = extractEntriesSU(ginstate, value, &nentries);
- if ( nentries==0 )
+ if (nentries == 0)
/* nothing to insert */
return 0;
- for(i=0;i<nentries;i++)
+ for (i = 0; i < nentries; i++)
ginEntryInsert(index, ginstate, entries[i], item, 1, FALSE);
return nentries;
}
Datum
-gininsert(PG_FUNCTION_ARGS) {
- Relation index = (Relation) PG_GETARG_POINTER(0);
- Datum *values = (Datum *) PG_GETARG_POINTER(1);
- bool *isnull = (bool *) PG_GETARG_POINTER(2);
+gininsert(PG_FUNCTION_ARGS)
+{
+ Relation index = (Relation) PG_GETARG_POINTER(0);
+ Datum *values = (Datum *) PG_GETARG_POINTER(1);
+ bool *isnull = (bool *) PG_GETARG_POINTER(2);
ItemPointer ht_ctid = (ItemPointer) PG_GETARG_POINTER(3);
+
#ifdef NOT_USED
- Relation heapRel = (Relation) PG_GETARG_POINTER(4);
- bool checkUnique = PG_GETARG_BOOL(5);
+ Relation heapRel = (Relation) PG_GETARG_POINTER(4);
+ bool checkUnique = PG_GETARG_BOOL(5);
#endif
- GinState ginstate;
+ GinState ginstate;
MemoryContext oldCtx;
MemoryContext insertCtx;
- uint32 res;
+ uint32 res;
- if ( *isnull )
+ if (*isnull)
PG_RETURN_BOOL(false);
insertCtx = AllocSetContextCreate(CurrentMemoryContext,
- "Gin insert temporary context",
- ALLOCSET_DEFAULT_MINSIZE,
- ALLOCSET_DEFAULT_INITSIZE,
- ALLOCSET_DEFAULT_MAXSIZE);
+ "Gin insert temporary context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
oldCtx = MemoryContextSwitchTo(insertCtx);
initGinState(&ginstate, index);
- res = ginHeapTupleInsert(index, &ginstate, *values, ht_ctid);
+ res = ginHeapTupleInsert(index, &ginstate, *values, ht_ctid);
MemoryContextSwitchTo(oldCtx);
MemoryContextDelete(insertCtx);
- PG_RETURN_BOOL(res>0);
+ PG_RETURN_BOOL(res > 0);
}
-