-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.cpp
More file actions
367 lines (326 loc) · 14.8 KB
/
Index.cpp
File metadata and controls
367 lines (326 loc) · 14.8 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "Index.h"
#include "Record.h"
#include "Doctor.h"
#include "Appointment.h"
void useSecondaryIndex:: addSecondaryIndex(const string &secondaryKey,const string &primaryKey) { //take seckey and pk to put in inverted list
int newNodeIndex = invertedList.size(); // Create a new node for the inverted list
invertedList.emplace_back(primaryKey, -1); // New node points to -1 initially (end of the list)
// If the secondary key doesn't exist, add it with the new node as the head
if (secondaryIndex.find(secondaryKey) == secondaryIndex.end()) {
secondaryIndex[secondaryKey] = newNodeIndex;
} else {
// Otherwise, find the last node in the linked list and append the new node
int currentIndex = secondaryIndex[secondaryKey];
while (invertedList[currentIndex].nextPointer != -1) {
currentIndex = invertedList[currentIndex].nextPointer;
}
invertedList[currentIndex].nextPointer = newNodeIndex; // Update the last node's next pointer
}
cout << "Added record for " << secondaryKey << " with pointer to primary key in inverted list " << primaryKey << endl;
}
// Function to search for primary keys by secondary key
vector<string> useSecondaryIndex :: searchByName(const string &secondaryKey) const {
vector<string> results;
if (secondaryIndex.find(secondaryKey) == secondaryIndex.end()) {
return results; // Return empty if the secondary key is not found
}
int currentIndex = secondaryIndex.at(secondaryKey);
while (currentIndex != -1) {
results.push_back(invertedList[currentIndex].primaryKey);
currentIndex = invertedList[currentIndex].nextPointer; // Move to the next node
}
return results;
}
// Function to delete a primary key from the secondary key's linked list
bool useSecondaryIndex:: removeSecondaryIndex(const string &secondaryKey, const string &primaryKey) {
if (secondaryIndex.find(secondaryKey) == secondaryIndex.end()) {
return false; // Secondary key not found
}
int currentIndex = secondaryIndex[secondaryKey];
int prevIndex = -1;
while (currentIndex != -1) {
if (invertedList[currentIndex].primaryKey == primaryKey) {
// If it's the head node, update the secondary index
if (prevIndex == -1) {
secondaryIndex[secondaryKey] = invertedList[currentIndex].nextPointer;
} else { // Otherwise, update previous node to skip the current node
invertedList[prevIndex].nextPointer = invertedList[currentIndex].nextPointer;
}
// Mark the current node as deleted (reset primary key to "#")
invertedList[currentIndex] = InvertedListNode("#", -1);
// Check if the list for this secondary key is now empty (i.e., no valid nodes)
if (secondaryIndex[secondaryKey] == -1) {
// Instead of removing the secondary key, mark it as invalid
secondaryIndex[secondaryKey] = -1;
}
return true; // Successfully deleted
}
prevIndex = currentIndex;
currentIndex = invertedList[currentIndex].nextPointer; // Move to the next node
}
return false; // If the primary key is not found in the list
}
// Function to save the index and list into a file
void useSecondaryIndex:: saveSecondaryIndexToFile(const string &filename) const {
ofstream outFile(filename, ios::out);
if (!outFile.is_open()) {
cerr << "Error: Unable to open file for writing: " << filename << endl;
return;
}
// Write the secondary index
outFile << "Secondary Index:\n";
for (const auto &entry: secondaryIndex) {
if (entry.second >= 0 && entry.second < static_cast<int>(invertedList.size())) {
outFile << entry.first << " -> " << entry.second << "\n";
} else if (entry.second == -1) {
// Handle case where secondary key has been deleted and marked as -1
outFile << entry.first << " -> -1\n";
}
}
// Write the inverted list
outFile << "\nInverted List:\n";
for (size_t i = 0; i < invertedList.size(); ++i) {
const auto &node = invertedList[i];
if (node.primaryKey == "#") {
outFile << i << ": (#, #)\n"; // Mark deleted nodes as (#, #)
} else {
outFile << i << ": (" << node.primaryKey << ", " << node.nextPointer << ")\n";
}
}
outFile.close();
cout << "Secondary index saved successfully to " << filename << endl;
}
void useSecondaryIndex:: loadSecondaryIndexFromFile(const string &filename) {
ifstream inFile(filename);
if (!inFile.is_open()) {
cerr << "Error: Unable to open file for reading: " << filename << endl;
return;
}
secondaryIndex.clear();
invertedList.clear();
string line, section;
while (getline(inFile, line)) {
if (line == "Secondary Index:") {
section = "SecondaryIndex";
} else if (line == "Inverted List:") {
section = "InvertedList";
} else if (!line.empty()) {
if (section == "SecondaryIndex") {
// Parse secondary index entries
size_t arrowPos = line.find(" -> ");
if (arrowPos != string::npos) {
string key = line.substr(0, arrowPos);
try {
int headIndex = stoi(line.substr(arrowPos + 4));
secondaryIndex[key] = headIndex;
} catch (const std::invalid_argument &e) {
cerr << "Error: Invalid format for headIndex: " << line << endl;
} catch (const std::out_of_range &e) {
cerr << "Error: headIndex out of range: " << line << endl;
}
}
} else if (section == "InvertedList") {
// Parse inverted list entries
size_t colonPos = line.find(": ");
if (colonPos != string::npos) {
string rest = line.substr(colonPos + 2);
size_t commaPos = rest.find(", ");
if (rest.front() == '(' && rest.back() == ')' && commaPos != string::npos) {
string primaryKey = rest.substr(1, commaPos - 1); // Remove parentheses
string nextPointerStr = rest.substr(commaPos + 2, rest.length() - commaPos - 3);
try {
// Replace any invalid nextPointer with -1
int nextPointer = (nextPointerStr == "#") ? -1 : stoi(nextPointerStr);
invertedList.emplace_back(primaryKey, nextPointer);
} catch (const std::invalid_argument &e) {
cerr << "Error: Invalid format for nextPointer: " << nextPointerStr << endl;
} catch (const std::out_of_range &e) {
cerr << "Error: nextPointer out of range: " << nextPointerStr << endl;
}
} else {
cerr << "Error: Malformed inverted list entry: " << rest << endl;
}
}
}
}
}
inFile.close();
cout << "Secondary index loaded from" << filename << endl;
}
// Function to add a record to the secondary index
void UseSecondaryIndexonAppoint:: addSecondaryIndex(const string &doctorID, const string &appointmentID) {
int newNodeIndex = invertedList.size();
invertedList.emplace_back(appointmentID, -1);
if (secondaryIndex.find(doctorID) == secondaryIndex.end()) {
secondaryIndex[doctorID] = newNodeIndex;
} else {
int currentIndex = secondaryIndex[doctorID];
while (invertedList[currentIndex].nextPointer != -1) {
currentIndex = invertedList[currentIndex].nextPointer;
}
invertedList[currentIndex].nextPointer = newNodeIndex;
}
cout << "Added record for " << doctorID << " with pointer to primary key in inverted list " << appointmentID << endl;
}
// Function to search for appointment IDs by Doctor ID
vector<string> UseSecondaryIndexonAppoint:: searchByDoctorID(const string &doctorID) const {
vector<string> results;
trim(doctorID); // Remove leading and trailing spaces
if (secondaryIndex.find(doctorID) == secondaryIndex.end()) {
return results;
}
int currentIndex = secondaryIndex.at(doctorID);
while (currentIndex != -1) {
results.push_back(invertedList[currentIndex].appointmentID);
currentIndex = invertedList[currentIndex].nextPointer;
}
return results;
}
// Function to delete an appointment ID from the Doctor ID's linked list
bool UseSecondaryIndexonAppoint:: removeSecondaryIndex(const string &doctorID, const string &appointmentID) {
if (secondaryIndex.find(doctorID) == secondaryIndex.end()) {
return false;
}
int currentIndex = secondaryIndex[doctorID];
int prevIndex = -1;
while (currentIndex != -1) {
if (invertedList[currentIndex].appointmentID == appointmentID) {
if (prevIndex == -1) {
secondaryIndex[doctorID] = invertedList[currentIndex].nextPointer;
} else {
invertedList[prevIndex].nextPointer = invertedList[currentIndex].nextPointer;
}
invertedList[currentIndex]=(InvertedListNodeA("#", -1));
if (secondaryIndex[doctorID] == -1) {
secondaryIndex[doctorID] = -1;
}
return true;
}
prevIndex = currentIndex;
currentIndex = invertedList[currentIndex].nextPointer;
}
return false;
}
// Function to save the index and list into a file
void UseSecondaryIndexonAppoint:: saveSecondaryIndexToFile(const string &filename) const {
ofstream outFile(filename);
if (!outFile.is_open()) {
cerr << "Error: Unable to open file for writing: " << filename << endl;
return;
}
outFile << "Secondary Index:\n";
for (const auto &entry: secondaryIndex) {
if (entry.second >= 0 && entry.second < static_cast<int>(invertedList.size())) {
outFile << entry.first << " -> " << entry.second << "\n";
} else if (entry.second == -1) {
outFile << entry.first << " -> -1\n";
}
}
outFile << "\nInverted List:\n";
for (size_t i = 0; i < invertedList.size(); ++i) {
const auto &node = invertedList[i];
if (node.appointmentID == "#") {
outFile << i << ": (#, #)\n";
} else {
outFile << i << ": (" << node.appointmentID << ", " << node.nextPointer << ")\n";
}
}
outFile.close();
cout << "Secondary index saved successfully to " << filename << endl;
}
void UseSecondaryIndexonAppoint:: loadSecondaryIndexFromFile(const string &filename) {
ifstream inFile(filename);
if (!inFile.is_open()) {
cerr << "Error: Unable to open file for reading: " << filename << endl;
return;
}
secondaryIndex.clear();
invertedList.clear();
string line, section;
while (getline(inFile, line)) {
if (line == "Secondary Index:") {
section = "SecondaryIndex";
} else if (line == "Inverted List:") {
section = "InvertedList";
} else if (!line.empty()) {
if (section == "SecondaryIndex") {
size_t arrowPos = line.find(" -> ");
if (arrowPos != string::npos) {
string key = line.substr(0, arrowPos);
try {
int headIndex = stoi(line.substr(arrowPos + 4));
secondaryIndex[key] = headIndex;
} catch (const std::exception &e) {
cerr << "Error: Invalid format in secondary index: " << line << endl;
}
}
} else if (section == "InvertedList") {
size_t colonPos = line.find(": ");
if (colonPos != string::npos) {
string rest = line.substr(colonPos + 2);
size_t commaPos = rest.find(", ");
if (rest.front() == '(' && rest.back() == ')' && commaPos != string::npos) {
string appointmentID = rest.substr(1, commaPos - 1);
string nextPointerStr = rest.substr(commaPos + 2, rest.length() - commaPos - 3);
try {
int nextPointer = (nextPointerStr == "#") ? -1 : stoi(nextPointerStr);
invertedList.emplace_back(appointmentID, nextPointer);
} catch (const std::exception &e) {
cerr << "Error: Invalid format in inverted list: " << rest << endl;
}
}
}
}
}
}
inFile.close();
cout << "Secondary index loaded from" << filename << endl;
}
void useprimaryIndex:: addPrimaryIndex(map<string, int> &primaryIndexm, const string &key, int offset) {
primaryIndexm[key] = offset;
}
void useprimaryIndex:: removePrimaryIndex(map<string, int> &primaryIndexm, const string &key) {
primaryIndexm.erase(key);
}
int useprimaryIndex:: binarySearchPrimaryIndex(const map<string, int> &primaryIndexm, const string &key) {
auto it = primaryIndexm.find(key); // Binary search happens internally in the map
if (it != primaryIndexm.end()) {
return it->second; // Return offset if key is found
}
return -1; // Return -1 if not found
}
void useprimaryIndex:: savePrimaryIndexToFile(const map<string, int> &primaryIndexm, const string &filename) {
ofstream outfile(filename, ios::out);
if (!outfile) {
cerr << "Error opening file for saving primary index!" << endl;
return;
}
for (const auto &entry: primaryIndexm) {
outfile << entry.first << " " << entry.second << "\n";
}
outfile.close();
cout << "Primary index saved to " << filename << endl;
}
void useprimaryIndex:: loadPrimaryIndexFromFile(map<string, int> &primaryIndex, const string &filename) {
ifstream infile(filename, ios::in);
if (!infile) {
cerr << "Error opening file for loading primary index: " << filename << endl;
return;
}
string key;
int offset;
while (infile >> key >> offset) {
primaryIndex[key] = offset;
}
infile.close();
cout << "Primary index loaded from " << filename << endl;
}
void useprimaryIndex:: updatePrimaryIndex(map<string, int> &primaryIndexm, const string &key, int newOffset) {
auto it = primaryIndexm.find(key);
if (it != primaryIndexm.end()) {
it->second = newOffset; // Update the offset for the key
cout << "Primary index updated for key: " << key << " with new offset: " << newOffset << endl;
} else {
cerr << "Key not found in primary index: " << key << ". Update failed.\n";
}
}