-
Notifications
You must be signed in to change notification settings - Fork 1
/
bTreeUtils.c
65 lines (50 loc) · 1.88 KB
/
bTreeUtils.c
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
#include "bTreeUtils.h"
#include "streamHandler.h"
// *Função utilizada para criar uma nova página da bTree
bTreePage *createPage() {
bTreePage *bPage = (bTreePage*)malloc(PAGESIZE);
bPage->numRecords = 0;
bPage->isLeaf = 1;
for(int i = 0; i < MAXCHILDS; i++) {
bPage->childs[i] = -1;
if(i != (MAXCHILDS-1)) {
bPage->records[i].key = 0;
bPage->records[i].RRN = -1;
}
}
return bPage;
}
// *Função utilizada para criar a raíz do arquivo da bTree
bPageInfo *createRoot(FILE *bFile) {
bPageInfo *bInfo = (bPageInfo*)malloc(sizeof(bPageInfo));
bInfo->bPage = createPage();
bInfo->RRN = 1;
int created = -1;
fseek(bFile, 0, SEEK_SET);
fwrite(&created, sizeof(int), 1, bFile);
fwrite(&bInfo->RRN, PAGESIZE - sizeof(int), 1, bFile);
fflush(bFile);
insertNodeInBTreeFile(bInfo, bFile, bInfo->RRN);
return bInfo;
}
// *Função utilizada para coletar a raíz
bPageInfo *getOrCreateRoot(FILE *bFile) {
int created, RRN;
fseek(bFile, 0, SEEK_SET);
fread(&created, sizeof(int), 1, bFile);
if(created != -1) {
bPageInfo *bInfo = createRoot(bFile);
return bInfo;
} else {
fread(&RRN, sizeof(int), 1, bFile);
bPageInfo *bInfo = getPageFromBTreeFile(RRN);
return bInfo;
}
}
// *Função para busca binária de chave em nó da árvore
long pageBinarySearch(int searchKey, record *records, long firstSearch, long lastSearch) {
long middle = (firstSearch + lastSearch) / 2;
if(records[middle].key == searchKey || (middle == firstSearch && middle == lastSearch)) { return middle; }
if(records[middle].key < searchKey) { return pageBinarySearch(searchKey, records, middle+1, lastSearch); }
return pageBinarySearch(searchKey, records, firstSearch, middle);
}