forked from Yashasv-Prajapati/LSM-Trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.cpp
239 lines (183 loc) · 5.7 KB
/
read.cpp
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
#include<iostream>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;
int calc_size()
{
fstream file1;
file1.open("index.db",ios::in);
//vector<string> sstable_names;
//int sstablecount=0;
//int * sstable_sizes = (int *) malloc(sizeof(int));
//vector<int> sstable_sizes;
//define krdiye dono arrays
//Assuming that index.db has atleast 1 sstable
int counter = 0;
while(!file1.eof()){
string temp;
file1>>temp;
//sstable_names.push_back(temp);
int a;
file1>>a;
//sstable_sizes.push_back(a);
counter++;
}
file1.close();
return counter-1;
}
int ParseCharToIntRD(const char c){
return int(c)-97;
}
int SearchInDisk(const string key){
int IndexFileNumber = calc_size();
ifstream ReadIndex;
ReadIndex.open("index.db");
int in=0;
string *FileNames = new string[IndexFileNumber];
int *FileSizes = new int[IndexFileNumber];
for (int i = 0; i < IndexFileNumber; i++)
{
ReadIndex>>FileNames[i];
ReadIndex>>FileSizes[i];
}
for (int i = 0; i < IndexFileNumber; i++)
{
ifstream ReadFile;
const string filePath = "table/"+FileNames[i]+".db";
ReadFile.open(filePath);
int bloom[26];
for (int j = 0; j < 26; j++)
{
ReadFile>>bloom[j];
}
// index from where we will start looking
int IndexToStart = bloom[ParseCharToIntRD(key[0])];
if(IndexToStart==-1){
continue;
}
// index till where we will look
for (int j = 0; j < IndexToStart; j++)
{
// Redundant Reading to reach the index we want to start from
string s;
ReadFile>>s;
int k;
ReadFile>>k;
int tomb;
ReadFile>>tomb;
}
for (int j = IndexToStart; j < FileSizes[i]; j++)
{
string FileKey;
int FileValue;
int TombStone;
ReadFile>>FileKey;
ReadFile>>FileValue;
ReadFile>>TombStone;
// if the key you're looking for doesn't start from the letter of first letter of FileKey, then break
if(ParseCharToIntRD(FileKey[0])!=ParseCharToIntRD(key[0])){
break;
}
if(key==FileKey){ // key matches
if(TombStone==0){ // tombstone is absent
return FileValue;
}else{ // tombstone is present
return -1;
}
}
}
}
return -1;
}
int DeleteKey(const string key){
int IndexFileNumber = calc_size();
ifstream ReadIndex;
ReadIndex.open("index.db");
string *FileNames = new string[IndexFileNumber];
int *FileSizes = new int[IndexFileNumber];
for (int i = 0; i < IndexFileNumber; i++)
{
ReadIndex>>FileNames[i];
ReadIndex>>FileSizes[i];
}
for (int i = 0; i < IndexFileNumber; i++)
{
ifstream ReadFile;
const string filePath = "table/"+FileNames[i]+".db";
ReadFile.open(filePath);
int bloom[26];
for (int j = 0; j < 26; j++)
{
ReadFile>>bloom[j];
}
// index from where we will start looking
int IndexToStart = bloom[ParseCharToIntRD(key[0])];
if(IndexToStart==-1){
continue;
}
// index till where we will look
for (int j = 0; j < IndexToStart; j++)
{
// Redundant Reading to reach the index we want to start from
string s;
ReadFile>>s;
int k;
ReadFile>>k;
int tombstone;
ReadFile>>tombstone;
}
for (int j = IndexToStart; j < FileSizes[i]; j++)
{
string FileKey;
int FileValue;
int TombStone;
ReadFile>>FileKey;
ReadFile>>FileValue;
ReadFile>>TombStone;
// if the key you're looking for doesn't start from the letter of first letter of FileKey, then break
if(ParseCharToIntRD(FileKey[0])!=ParseCharToIntRD(key[0])){
break;
}
if(key==FileKey && TombStone==0){ // key found and tombstone is not present, so it's not deleted
// found the key-value pair, now deleting it and pushing changes
ifstream DeletionKeyFile;
DeletionKeyFile.open(filePath);
string *keys = new string[FileSizes[i]];
int *values = new int[FileSizes[i]];
int *TombStone = new int[FileSizes[i]];
int *BLOOM = new int[26];
for (int k = 0; k < 26; k++)
{
DeletionKeyFile>>BLOOM[k];
}
for (int k = 0; k < FileSizes[i]; k++)
{
DeletionKeyFile>>keys[k];
DeletionKeyFile>>values[k];
DeletionKeyFile>>TombStone[k];
}
TombStone[j]=1; // now file is deleted
ofstream Push;
Push.open(filePath);
for (int k = 0; k < 26; k++)
{
Push<<BLOOM[k]<<' ';
}
Push<<"\n";
for (int k = 0; k < FileSizes[i]; k++)
{
Push<<keys[k]<<" "<<values[k]<<" "<<TombStone[k]<<"\n";
}
return 1;
}
}
}
return -1;
}
/*
int main(){
int k = DeleteKey("anmdlxwcd");
cout<<k<<endl;
}
*/