-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
366 lines (347 loc) · 10.9 KB
/
table.cpp
File metadata and controls
366 lines (347 loc) · 10.9 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
#include "table.h"
#include "index.h"
#include <QtDebug>
#include <QFile>
QString Table::getTable_name() const
{
return table_name;
}
void Table::setTable_name(const QString &value)
{
table_name = value;
}
void Table::print_table()
{
qDebug()<<"Table name : "<<table_name<<endl ;
foreach(QString attr_name, attribute_type_hash)
{
qDebug()<<attr_name<<" " ;
}
qDebug()<<endl ;
List_node* current_node=first_record ;
while(current_node)
{
current_node->record->print() ;
current_node=current_node->next ;
}
}
Table::List_node *Table::get_first_record_pointer()
{
return first_record ;
}
Table::List_node *Table::get_last_record_pointer()
{
return last_record ;
}
void Table::insertAtStart(List_node *val_record)
{
List_node* new_node=val_record ;
new_node->next=first_record ;
new_node->previous=NULL;
if(new_node->next)new_node->next->previous=new_node ;
else last_record=new_node ;
first_record=new_node ;
}
void Table::insertAtEnd(List_node *val_record)
{
// inserting at the end
List_node* new_node=val_record ;
new_node->next=NULL ;
new_node->previous=last_record ;
if(new_node->previous)new_node->previous->next=new_node ;
last_record=new_node ;
}
void Table::insertAfter(Table::List_node *val_record, Table::List_node *current)
{
// inserting after the current node
List_node* new_node=val_record ;
new_node->previous=current ;
new_node->next=current->next ;
if(new_node->next)new_node->next->previous=new_node ;
else
{
last_record=new_node ;
}
current->next=new_node ;
}
void Table::insertBefore(List_node *val_record, Table::List_node *current)
{
// inserting somewhere in between list
List_node* new_node=val_record ;
new_node->next=current ;
new_node->previous=current->previous ;
if(new_node->previous)new_node->previous->next=new_node ;
else
{
first_record=new_node ;
}
current->previous=new_node ;
}
void Table::deleteNode(Table::List_node *current)
{
if(current)
{
//record has been found
if(current->next==NULL) // current is the last node
{
last_record= (current->previous ? current->previous : NULL) ;
if(last_record)last_record->next=NULL ;
else first_record=NULL ;
delete current ;
}
else if(current->previous==NULL) // current is the first node
{
first_record=(current->next ? current->next : NULL);
if(first_record) first_record->previous=NULL ;
else last_record=NULL ;
delete current ;
}
else
{ //current is a node in-between
current->previous->next=current->next ;
current->next->previous=current->previous ;
delete current ;
}
}
}
void Table::replaceNode(Table::List_node *to_replace, Table::List_node *with)
{
if(to_replace->previous) to_replace->previous->next=with ;
else first_record=with ;
if(to_replace->next) to_replace->next->previous=with ;
else last_record=with ;
delete to_replace ;
}
void Table::serialized_to_file(QFile * filehandle)
{
QString to_return="***table\n" ;
to_return+=table_name+"\n" ;
to_return+=QString::number(times_used)+"\n" ;
to_return+="***Attributes\n" ;
foreach (QString attr_name, attribute_type_hash.keys())
{
to_return+=attr_name+"\n" ;
to_return+=attribute_type_hash[attr_name]+"\n" ;
}
to_return+="###Attributes\n" ;
to_return+="***data\n" ;
List_node* current_node=first_record ;
while(current_node)
{
foreach (QString attr_name, attribute_type_hash.keys()) {
to_return+=current_node->record->getAttribute(attr_name)+"\n" ;
}
current_node=current_node->next ;
}
to_return+="###data\n" ;
filehandle->write(to_return.toLocal8Bit()) ;
}
Table::Table(QString val_table_name , QHash<QString,QString> val_attribute_type_hash)
{
table_name=val_table_name ;
first_record=NULL ;
last_record=NULL ;
times_used=0;
attribute_type_hash=val_attribute_type_hash ;
}
void Table::addIndex(Index *val_index)
{
indices.append(val_index);
}
bool Table::createRecord(Record *val_record)
{
if(indices.size()==0)
{
// traverse the Linked list and insert record at proper place
if(!first_record)
{
first_record=new List_node ;
first_record->record=val_record ;
first_record->next=NULL ;
first_record->previous=NULL ;
last_record=first_record ;
return true ;
}
List_node* current=first_record ;
qDebug()<<"newer : "<<val_record->getAttribute(attribute_type_hash.keys().last());
qDebug()<<"previous : "<< current->record->getAttribute(attribute_type_hash.keys().last());
while(current && val_record->getAttribute(attribute_type_hash.keys().last())>current->record->getAttribute(attribute_type_hash.keys().last()))
{
current=current->next ;
}
if(current && current->next && current->previous)
{
// inserting somewhere in between list
List_node* new_node=new List_node ;
new_node->record=val_record ;
new_node->next=current ;
new_node->previous=current->next->previous ;
new_node->previous->next=new_node ;
new_node->next->previous=new_node ;
return true ;
}
else if(current && !current->previous)
{
// inserting at start
List_node* new_node=new List_node ;
new_node->record=val_record ;
new_node->next=current ;
new_node->previous=NULL;
new_node->next->previous=new_node ;
first_record=new_node ;
return true ;
}
else if(!current)
{
// inserting at the end
List_node* new_node=new List_node ;
new_node->record=val_record ;
new_node->next=NULL ;
new_node->previous=last_record ;
new_node->previous->next=new_node ;
last_record=new_node ;
return true ;
}
}
else
{
// insert the record using the available index. Index will handle the case of primary or secondary index by itself
// insertion through index should be much more faster than without index
foreach (Index* index, indices)
{
List_node* new_node=new List_node ;
new_node->record=val_record ;
index->add_record(new_node) ;
}
}
}
QHash<QString, QString> Table::readRecord(QList<QString> attributes, QString where_attribute, QString equals_value)
{
foreach (Index* index, indices)
{
if(index->getTarget_attribute()==where_attribute) // if there is an index available on this attribute use it
{
Table::List_node* found_record=index->find_record(equals_value) ;
if(found_record)
{
QHash<QString, QString> attribute_values ;
for(int i=0; i<attributes.size(); i++)
{
attribute_values[attributes[i]]=found_record->record->getAttribute(attributes[i]) ;
}
return attribute_values ;
}
else return QHash<QString, QString>() ;
}
}
// traverse the Linked list and read record from proper place
if(!first_record)
{
return QHash<QString, QString>() ; // no record was found. List is empty
}
List_node* current=first_record ;
while(current && current->record->getAttribute(where_attribute)!=equals_value)
{
current=current->next ;
}
if(current)
{
//record has been found
QHash<QString, QString> attribute_values ;
for(int i=0; i<attributes.size(); i++)
{
attribute_values[attributes[i]]=current->record->getAttribute(attributes[i]) ;
}
return attribute_values ;
}
else return QHash<QString, QString>() ; // no record was found
}
bool Table::updateRecord(QString attribute_to_change,QString new_value, QString where_attribute, QString has_value)
{
if(indices.size()==0)
{
// traverse the Linked list and read record from proper place
if(!first_record)
{
return false ; // no record was found. List is empty
}
List_node* current=first_record ;
while(current && current->record->getAttribute(where_attribute)!=has_value)
{
current=current->next ;
}
if(current)
{
//record has been found
current->record->setAttribute(attribute_to_change, new_value) ;
return true ;
}
return true ;
}
else
{
foreach (Index* index, indices)
{
if(index->getTarget_attribute()==where_attribute) // if there is an index available on this attribute use it
{
index->update_record(has_value, attribute_to_change, new_value) ;
}
}
return true ;
}
}
bool Table::deleteRecord(QString where_attribute, QString equals_value)
{
if(indices.size()==0)
{
// traverse the Linked list and read record from proper place
if(!first_record)
{
return false; // no record was found. List is empty
}
List_node* current=first_record ;
while(current && current->record->getAttribute(where_attribute)!=equals_value)
{
current=current->next ;
}
if(current)
{
//record has been found
if(current->next==NULL) // current is the last node
{
last_record= (current->previous ? current->previous : NULL) ;
if(last_record)last_record->next=NULL ;
else first_record=NULL ;
delete current ;
return true ;
}
else if(current->previous==NULL) // current is the first node
{
first_record=(current->next ? current->next : NULL);
if(first_record) first_record->previous=NULL ;
else last_record=NULL ;
delete current ;
return true ;
}
else
{ //current is a node in-between
current->previous->next=current->next ;
current->next->previous=current->previous ;
delete current ;
return true ;
}
}
return true ;
}
else
{
foreach (Index* index, indices)
{
if(index->getTarget_attribute()==where_attribute) // if there is an index available on this attribute use it
{
return index->delete_record(equals_value) ;
}
}
return true ;
}
}