-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.cpp
More file actions
59 lines (50 loc) · 1.8 KB
/
update.cpp
File metadata and controls
59 lines (50 loc) · 1.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
//
// Created by Yuchen Zhang on 2/22/23.
//
#include "library.h"
bool update(RecordID* recordId, Heapfile *heapfile, int attribute_id, char* newValue){
Record record;
Page* page = new Page;
init_fixed_len_page(page, heapfile->page_size, SLOT_SIZE);
if(recordId->page_id > maxPid(heapfile)){
return false;
}
read_page(heapfile, recordId->page_id, page);
if(recordId->slot > fixed_len_page_capacity(page)){
return false;
}
read_fixed_len_page(page, recordId->slot, &record);
if(attribute_id >= record.size()){
return false;
}
record[attribute_id] = newValue;
write_fixed_len_page(page, recordId->slot, &record);
write_page(page, heapfile, recordId->page_id);
return true;
}
//<heapfile> <record_id> <attribute_id> <new_value> <page_size>
int main(int argc, char* argv[]){
auto startTime = system_clock::now();
FILE* heapFile = fopen(argv[1], "r+");
string record_id = argv[2];
auto* recordId = new RecordID;
for(int i = 0; i < record_id.size(); ++i){
if(record_id[i] == '-'){
recordId->page_id = stoi(record_id.substr(0, i+1));
recordId->slot = stoi(record_id.substr(i+1, record_id.size()-i-1));
}
}
int attribute_id = atoi(argv[3]);
char* newValue = new char[ATTRIBUTE_SIZE];
newValue = argv[4];
int page_size = atoi(argv[5]);
// Initialize heapfile
auto* heapfile = new Heapfile;
init_heapfile(heapfile, page_size, heapFile);
if(!update(recordId, heapfile, attribute_id, newValue)){
cerr << "ERROR";
}
auto endTime = system_clock::now();
auto duration = duration_cast<microseconds>(endTime - startTime);
std::cout << "TIME: " << double(duration.count()) * microseconds::period::num / microseconds::period::den << "s" << endl;
}