-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.h
158 lines (110 loc) · 4.37 KB
/
rm.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef _rm_h_
#define _rm_h_
#include <string>
#include <vector>
#include <map>
#include "../rbf/rbfm.h"
#include "../ix/ix.h"
using namespace std;
# define RM_EOF (-1) // end of a scan operator
// RM_ScanIterator is an iteratr to go through tuples
// The way to use it is like the following:
// RM_ScanIterator rmScanIterator;
// rm.open(..., rmScanIterator);
// while (rmScanIterator(rid, data) != RM_EOF) {
// process the data;
// }
// rmScanIterator.close();
class RM_ScanIterator {
public:
RM_ScanIterator() {};
~RM_ScanIterator() {};
// "data" follows the same format as RelationManager::insertTuple()
RC getNextTuple(RID &rid, void *data) {
if(rbfm_ScanIterator.getNextRecord(rid,data)==RM_EOF)
{
return -1;
}
return 0;
};
RC close() { return rbfm_ScanIterator.close(); };
RC setScanIterator(FileHandle &fileHandle,
const vector<Attribute> &recordDescriptor,
const string &conditionAttribute,
const CompOp compOp, // comparision type such as "<" and "="
const void *value, // used in the comparison
const vector<string> &attributeNames){
RecordBasedFileManager* rbfm = RecordBasedFileManager::instance();
rbfm->scan(fileHandle,recordDescriptor,conditionAttribute,compOp,value,attributeNames,rbfm_ScanIterator);
return 0;
}
private:
RBFM_ScanIterator rbfm_ScanIterator ;
};
class RM_IndexScanIterator {
public:
RM_IndexScanIterator() {}; // Constructor
~RM_IndexScanIterator() {}; // Destructor
// "key" follows the same format as in IndexManager::insertEntry()
RC getNextEntry(RID &rid, void *key) {return RM_EOF;}; // Get next matching entry
RC close() {return -1;}; // Terminate index scan
};
// Relation Manager
class RelationManager
{
public:
static RelationManager* instance();
// RC initialize();
RC initialFiles();
RC initialVectors();
int memoryNeeded(const vector<Attribute> &attributes);
int getTableId();
RC createTable(const string &tableName, const vector<Attribute> &attrs);
//RC createTable(const string &tableName, const vector<Attribute> &attrs, const string &type);
RC deleteTable(const string &tableName);
RC getAttributes(const string &tableName, vector<Attribute> &attrs);
RC insertTuple(const string &tableName, const void *data, RID &rid);
RC deleteTuples(const string &tableName);
RC deleteTuple(const string &tableName, const RID &rid);
// Assume the rid does not change after update
RC updateTuple(const string &tableName, const void *data, const RID &rid);
RC readTuple(const string &tableName, const RID &rid, void *data);
RC readAttribute(const string &tableName, const RID &rid, const string &attributeName, void *data);
RC reorganizePage(const string &tableName, const unsigned pageNumber);
// scan returns an iterator to allow the caller to go through the results one by one.
RC scan(const string &tableName,
const string &conditionAttribute,
const CompOp compOp, // comparision type such as "<" and "="
const void *value, // used in the comparison
const vector<string> &attributeNames, // a list of projected attributes
RM_ScanIterator &rm_ScanIterator);
RC createIndex(const string &tableName, const string &attributeName);
RC destroyIndex(const string &tableName, const string &attributeName);
// indexScan returns an iterator to allow the caller to go through qualified entries in index
RC indexScan(const string &tableName,
const string &attributeName,
const void *lowKey,
const void *highKey,
bool lowKeyInclusive,
bool highKeyInclusive,
RM_IndexScanIterator &rm_IndexScanIterator);
// Extra credit
public:
RC dropAttribute(const string &tableName, const string &attributeName);
RC addAttribute(const string &tableName, const Attribute &attr);
RC reorganizeTable(const string &tableName);
protected:
RelationManager();
~RelationManager();
private:
static RelationManager *_rm;
IndexManager *ixManager;
vector<Attribute> Tables;
vector<Attribute> column;
RecordBasedFileManager* rbfm;
int tableId;
public:
string indexSuffix="Index";
string metaSuffix="_Meta";
};
#endif