-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTreeIndex.h
120 lines (101 loc) · 3.96 KB
/
BTreeIndex.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (C) 2008 by The Regents of the University of California
* Redistribution of this file is permitted under the terms of the GNU
* Public License (GPL).
*
* @author Junghoo "John" Cho <cho AT cs.ucla.edu>
* @date 3/24/2008
*/
#ifndef BTREEINDEX_H
#define BTREEINDEX_H
#include "Bruinbase.h"
#include "PageFile.h"
#include "RecordFile.h"
/**
* The data structure to point to a particular entry at a b+tree leaf node.
* An IndexCursor consists of pid (PageId of the leaf node) and
* eid (the location of the index entry inside the node).
* IndexCursor is used for index lookup and traversal.
*/
typedef struct {
// PageId of the index entry
PageId pid;
// The entry number inside the node
int eid;
} IndexCursor;
/**
* Implements a B-Tree index for bruinbase.
*
*/
class BTreeIndex {
public:
BTreeIndex();
// BTreeIndex(const std::string& indexname, char mode);
/**
* Open the index file in read or write mode.
* Under 'w' mode, the index file should be created if it does not exist.
* @param indexname[IN] the name of the index file
* @param mode[IN] 'r' for read, 'w' for write
* @return error code. 0 if no error
*/
RC open(const std::string& indexname, char mode);
/**
* Close the index file.
* @return error code. 0 if no error
*/
RC close();
/**
* Insert (key, RecordId) pair to the index.
* @param key[IN] the key for the value inserted into the index
* @param rid[IN] the RecordId for the record being inserted into the index
* @return error code. 0 if no error
*/
RC insert(int key, const RecordId& rid);
/**
* Run the standard B+Tree key search algorithm and identify the
* leaf node where searchKey may exist. If an index entry with
* searchKey exists in the leaf node, set IndexCursor to its location
* (i.e., IndexCursor.pid = PageId of the leaf node, and
* IndexCursor.eid = the searchKey index entry number.) and return 0.
* If not, set IndexCursor.pid = PageId of the leaf node and
* IndexCursor.eid = the index entry immediately after the largest
* index key that is smaller than searchKey, and return the error
* code RC_NO_SUCH_RECORD.
* Using the returned "IndexCursor", you will have to call readForward()
* to retrieve the actual (key, rid) pair from the index.
* @param key[IN] the key to find
* @param cursor[OUT] the cursor pointing to the index entry with
* searchKey or immediately behind the largest key
* smaller than searchKey.
* @return 0 if searchKey is found. Othewise, an error code
*/
RC locate(int searchKey, IndexCursor& cursor);
IndexCursor recursor(PageId pid,int searchkey,PageId traverse[],int num);
RC Treerecursor(PageId traverse[],int level, int siblingkey, PageId siblingpid);
/**
* Read the (key, rid) pair at the location specified by the index cursor,
* and move foward the cursor to the next entry.
* @param cursor[IN/OUT] the cursor pointing to an leaf-node index entry in the b+tree
* @param key[OUT] the key stored at the index cursor location
* @param rid[OUT] the RecordId stored at the index cursor location
* @return error code. 0 if no error
*/
RC readForward(IndexCursor& cursor, int& key, RecordId& rid);
PageId rootPid;
int treeHeight;
RC BTLprint(PageId pid);
RC BTNLprint(PageId pid);
PageId GetNextpid(PageId pid);
int GetKeycount(PageId pid);
PageId GetLfEpid();
PageFile pf;
private:
//PageFile pf; /// the PageFile used to store the actual b+tree in disk
// PageId rootPid; /// the PageId of the root node
// int treeHeight; /// the height of the tree
/// Note that the content of the above two variables will be gone when
/// this class is destructed. Make sure to store the values of the two
/// variables in disk, so that they can be reconstructed when the index
/// is opened again later.
};
#endif /* BTREEINDEX_H */