|
1 | 1 | #include "postgres.h"
|
2 | 2 | #include "access/smerge.h"
|
3 | 3 | #include "access/nbtree.h"
|
| 4 | +#include "access/genam.h" |
| 5 | +#include "access/relscan.h" |
| 6 | +#include "access/sdir.h" |
| 7 | +#include "access/skey.h" |
4 | 8 | #include "catalog/dependency.h"
|
5 | 9 | #include "catalog/pg_class.h"
|
6 | 10 | #include "nodes/parsenodes.h"
|
7 | 11 | #include "commands/defrem.h"
|
8 | 12 | #include "miscadmin.h"
|
| 13 | +#include "pg_config_manual.h" |
| 14 | +#include "executor/tuptable.h" |
9 | 15 |
|
10 | 16 | /*
|
11 | 17 | * Status record for spooling/sorting phase. (Note we may have two of
|
@@ -724,11 +730,75 @@ _sm_merge_delete_btree(Oid btreeOid) {
|
724 | 730 | performDeletion(&object, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);
|
725 | 731 | }
|
726 | 732 |
|
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) { |
728 | 757 | for(int i = 0; i < MAX_N - 1; i++) {
|
729 | 758 | if(metadata->levels[i] == MAX_K) {
|
730 | 759 |
|
731 | 760 | 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 | + |
732 | 802 | Oid mergeBtreeOid = _sm_merge_create_btree(heapRel, metadata);
|
733 | 803 | BTWriteState wstate;
|
734 | 804 |
|
|
0 commit comments