Skip to content

Commit c93e9cc

Browse files
committed
Merge branch 'btree-handling' of github.com:CodeMaxx/postgres into btree-handling
2 parents 1739eb0 + f367ec1 commit c93e9cc

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

reinitdb.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
cd install
4+
5+
rm -r data_old
6+
mv data data_old
7+
8+
bin/initdb -D data
9+
10+
sed -i s/\#port/port/ data/postgresql.conf;
11+
12+
bin/pg_ctl -D data -l logfile start;
13+
14+
sleep 2;
15+
16+
bin/createdb -p 5432 test;
17+
18+
bin/psql -h localhost -p 5432 -d test -f start.sql;
19+
20+
bin/pg_ctl -D data stop;

src/backend/access/smerge/smbtree.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ create_btree_index_stmt(Relation heap, int attsnum, AttrNumber *attrs, char *ind
2828
IndexElem* indexElem;
2929

3030
ListCell* head;
31-
ListCell* tail;
31+
ListCell* prevCell;
3232
ListCell* currCell;
3333

3434
relation = (RangeVar*) palloc(sizeof(RangeVar));
@@ -52,30 +52,33 @@ create_btree_index_stmt(Relation heap, int attsnum, AttrNumber *attrs, char *ind
5252
indexParams->type = T_List;
5353
indexParams->length = attsnum;
5454

55-
// currCell = NULL;
56-
// for (int i = 0; i != attsnum; i++) {
55+
prevCell = NULL;
56+
currCell = NULL;
57+
for (int i = 0; i < attsnum; i++) {
5758
// {type = T_IndexElem, name = 0x555555e80688 "uid", expr = 0x0, indexcolname = 0x0, collation = 0x0, opclass = 0x0, ordering = SORTBY_DEFAULT, nulls_ordering = SORTBY_NULLS_DEFAULT}
5859
indexElem = (IndexElem*) palloc(sizeof(IndexElem));
5960
indexElem->type = T_IndexElem;
60-
indexElem->name = heap->rd_att->attrs[attrs[0] - 1]->attname.data;
61+
indexElem->name = heap->rd_att->attrs[attrs[i] - 1]->attname.data;
6162
indexElem->expr = NULL;
6263
indexElem->indexcolname = NULL;
6364
indexElem->collation = NULL;
6465
indexElem->opclass = NULL;
6566
indexElem->ordering = SORTBY_DEFAULT;
6667
indexElem->nulls_ordering = SORTBY_NULLS_DEFAULT;
6768

68-
// if (currCell != NULL)
69-
69+
prevCell = currCell;
7070
currCell = (ListCell*) palloc(sizeof(ListCell));
7171
currCell->data.ptr_value = (void *) indexElem;
72-
currCell->next = NULL;
7372

74-
// if (i == 0)
75-
indexParams->head = currCell;
76-
indexParams->tail = currCell;
77-
// if (i == attsnum - 1)
78-
// }
73+
if (prevCell != NULL)
74+
prevCell->next = currCell;
75+
76+
if (i == 0)
77+
indexParams->head = currCell;
78+
}
79+
80+
indexParams->tail = currCell;
81+
currCell->next = NULL;
7982

8083
btreeIndStmt->indexParams = indexParams;
8184
btreeIndStmt->options = NULL;

src/backend/access/smerge/smerge.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ smergebuildempty(Relation index)
162162

163163
Relation
164164
_get_curr_btree (SmMetadata* metadata) {
165-
return index_open(metadata->list[metadata->numList - 1], RowExclusiveLock);
165+
return index_open(metadata->curr, RowExclusiveLock);
166166
}
167167

168168
/*
@@ -176,18 +176,17 @@ smergeinsert(Relation rel, Datum *values, bool *isnull,
176176
ItemPointer ht_ctid, Relation heapRel,
177177
IndexUniqueCheck checkUnique)
178178
{
179+
Relation btreeRel;
179180
SmMetadata* sm_metadata = _sm_getmetadata(rel);
180181
printf("K: %d, N: %d\n", sm_metadata->K, sm_metadata->N);
181-
for (int i = 0; i != sm_metadata->numList; i++) {
182-
printf("Btree OIDs %d: %d \n", i, sm_metadata->list[i]);
183-
}
182+
printf("Curr BTree OID: %d\n", sm_metadata->curr);
184183

185184
RelationCloseSmgr(rel);
186185

187186
// insert into sub btrees only if there any btrees
188-
if (sm_metadata->numList > 0) {
187+
btreeRel = _get_curr_btree(sm_metadata);
189188

190-
Relation btreeRel = _get_curr_btree(sm_metadata);
189+
if (true /* Check whether curr btree is full */) {
191190
bool b = btinsert(btreeRel, values, isnull,
192191
ht_ctid, heapRel, checkUnique);
193192

@@ -219,6 +218,9 @@ smergegettuple(IndexScanDesc scan, ScanDirection dir)
219218
printf("btgettuple returns %d\n", res);
220219

221220
scan->xs_ctup = bt_scan->xs_ctup;
221+
222+
scan->xs_itup = bt_scan->xs_itup;
223+
scan->xs_itupdesc = bt_scan->xs_itupdesc;
222224

223225
/* If we're out of index entries, we're done */
224226
// if (!res)
@@ -258,8 +260,9 @@ smergebeginscan(Relation rel, int nkeys, int norderbys)
258260
so->bt_rel = _get_curr_btree(so->metadata);
259261
so->bt_isd = btbeginscan(so->bt_rel, nkeys, norderbys);
260262

261-
scan->opaque = so;
263+
scan->xs_itupdesc = RelationGetDescr(rel);
262264

265+
scan->opaque = so;
263266

264267
return scan;
265268
}

src/backend/access/smerge/smmeta.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ _sm_init_metadata(Page metapage, Oid bt_index, IndexInfo *indexInfo) {
99
PageInit(metapage, BLCKSZ, 0);
1010

1111
sm_metadata = (SmMetadata*) PageGetContents(metapage);
12-
sm_metadata->K = 132;
13-
sm_metadata->N = 35;
14-
12+
sm_metadata->K = 3;
13+
sm_metadata->N = 3;
14+
1515
sm_metadata->attnum = indexInfo->ii_NumIndexAttrs;
1616
for (int i = 0; i < sm_metadata->attnum; i++)
1717
sm_metadata->attrs[i] = indexInfo->ii_KeyAttrNumbers[i];
1818

19-
sm_metadata->numList = 1;
20-
sm_metadata->list[0] = bt_index;
19+
for (int i = 0; i < MAX_N; i++)
20+
sm_metadata->levels[i] = 0;
21+
22+
for (int i = 0; i < MAX_N; i++)
23+
for (int j = 0; j < MAX_K; j++)
24+
sm_metadata->tree[i][j] = InvalidOid;
25+
26+
sm_metadata->curr = bt_index;
27+
sm_metadata->root = InvalidOid;
2128
memcpy(sm_metadata, PageGetContents(metapage), sizeof(SmMetadata));
2229

2330
((PageHeader) metapage)->pd_lower =

src/include/access/smerge.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ typedef struct SmMetadata {
8080
int attnum;
8181
AttrNumber attrs[INDEX_MAX_KEYS];
8282

83-
int numList;
84-
Oid list[64];
83+
int levels[MAX_N];
84+
Oid tree[MAX_N][MAX_K];
85+
86+
Oid curr;
87+
Oid root;
8588
} SmMetadata;
8689

8790
typedef struct SmScanOpaqueData
@@ -106,4 +109,6 @@ extern void _sm_init_metadata(Page metapage, Oid bt_index, IndexInfo *indexInfo)
106109
extern void _sm_writepage(Relation index, Page page, BlockNumber blkno);
107110
extern SmMetadata* _sm_getmetadata(Relation rel);
108111

112+
// smerge functions
113+
extern Relation _get_curr_btree (SmMetadata* metadata);
109114
#endif /* SMERGE_H */

0 commit comments

Comments
 (0)