Skip to content

Commit 9e0ff5e

Browse files
committed
Merge branch 'btree-handling' of github.com:CodeMaxx/postgres into btree-handling
2 parents d745ed9 + bd7287f commit 9e0ff5e

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

src/backend/access/smerge/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ subdir = src/backend/access/smerge
1212
top_builddir = ../../../..
1313
include $(top_builddir)/src/Makefile.global
1414

15-
OBJS = smerge.o smbtree.o smmeta.o
15+
OBJS = smerge.o smbtree.o smmeta.o smsort.o
1616

1717
include $(top_srcdir)/src/backend/common.mk

src/backend/access/smerge/smsort.c

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#include "postgres.h"
22
#include "access/smerge.h"
33
#include "access/nbtree.h"
4+
#include "access/genam.h"
5+
#include "access/relscan.h"
6+
#include "access/sdir.h"
7+
#include "access/skey.h"
48
#include "catalog/dependency.h"
59
#include "catalog/pg_class.h"
610
#include "nodes/parsenodes.h"
711
#include "commands/defrem.h"
812
#include "miscadmin.h"
13+
#include "pg_config_manual.h"
14+
#include "executor/tuptable.h"
915

1016
/*
1117
* Status record for spooling/sorting phase. (Note we may have two of
@@ -724,11 +730,75 @@ _sm_merge_delete_btree(Oid btreeOid) {
724730
performDeletion(&object, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
725731
}
726732

727-
void sm_flush(Relation rel, Relation heapRel, SmMetadata* metadata) {
733+
static void
734+
_sm_merge_rescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
735+
ScanKey orderbys, int norderbys) {
736+
if (scankey && scan->numberOfKeys > 0)
737+
memmove(scan->keyData,
738+
scankey,
739+
scan->numberOfKeys * sizeof(ScanKeyData));
740+
741+
/* Release any held pin on a heap page */
742+
if (BufferIsValid(scan->xs_cbuf))
743+
{
744+
ReleaseBuffer(scan->xs_cbuf);
745+
scan->xs_cbuf = InvalidBuffer;
746+
}
747+
748+
scan->xs_continue_hot = false;
749+
750+
scan->kill_prior_tuple = false; /* for safety */
751+
752+
btrescan(scan, scankey, nscankeys, orderbys, norderbys);
753+
}
754+
755+
void
756+
sm_flush(Relation heapRel, SmMetadata* metadata) {
728757
for(int i = 0; i < MAX_N - 1; i++) {
729758
if(metadata->levels[i] == MAX_K) {
730759

731760
BTSpool* btspools[MAX_K]; // TODO
761+
762+
for(int j = 0; j < MAX_K; j++) {
763+
Relation indexRel = index_open(metadata->tree[i][j], ExclusiveLock);
764+
_bt_spoolinit(heapRel, indexRel, metadata->unique, false); // Assuming heapRel is not being used
765+
766+
IndexScanDesc scan = btbeginscan(indexRel, metadata->attnum, 0);
767+
768+
ScanKeyData scankey;
769+
// sk_flags; /* flags, see below */
770+
// AttrNumber sk_attno; /* table or index column number */
771+
// StrategyNumber sk_strategy; operator strategy number -- no
772+
// Oid sk_subtype; /* strategy subtype */ -- no
773+
// Oid sk_collation; /* collation to use, if needed */ -- no
774+
// FmgrInfo sk_func; -- not req
775+
scankey.sk_flags = SK_SEARCHNOTNULL;
776+
scankey.sk_attno = metadata->attrs[0];
777+
scankey.sk_argument = (Datum) NULL;
778+
_sm_merge_rescan(scan, &scankey, metadata->attnum, NULL, 0);
779+
780+
TupleTableSlot* slot;
781+
slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRel));
782+
783+
while(btgettuple(scan, ForwardScanDirection)) {
784+
bool isnull[INDEX_MAX_KEYS];
785+
Datum values[INDEX_MAX_KEYS];
786+
787+
for(int k = 0; k < metadata->attnum; k++ ) {
788+
int keycol = metadata->attrs[k];
789+
Datum iDatum;
790+
bool isNull;
791+
iDatum = slot_getattr(slot, keycol, &isNull);
792+
values[k] = iDatum;
793+
isnull[k] = isNull;
794+
}
795+
_bt_spool(btspools[i], &(scan->xs_ctup.t_self), values, isnull);
796+
}
797+
798+
}
799+
800+
// IndexScanDesc scan = btbeginscan(, metadata->attnum, )
801+
732802
Oid mergeBtreeOid = _sm_merge_create_btree(heapRel, metadata);
733803
BTWriteState wstate;
734804

src/include/access/smerge.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ typedef struct SmMetadata {
9191

9292
Oid curr;
9393
Oid root;
94+
95+
bool unique;
9496
} SmMetadata;
9597

9698
typedef struct SmScanOpaqueData
@@ -124,5 +126,5 @@ extern void _sm_write_metadata(Relation index, SmMetadata* sm_metadata);
124126
extern Relation _get_curr_btree (SmMetadata* metadata);
125127

126128
// smsort functions
127-
extern void sm_flush(Relation rel, Relation heapRel, SmMetadata* metadata);
129+
extern void sm_flush(Relation heapRel, SmMetadata* metadata);
128130
#endif /* SMERGE_H */

0 commit comments

Comments
 (0)