-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cpp
More file actions
185 lines (159 loc) · 5.07 KB
/
Copy pathindex.cpp
File metadata and controls
185 lines (159 loc) · 5.07 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
//index.cpp
#include "index.h"
#include <cf/IndexBlob.h>
#include <ctype.h>
ReaderWriterLock chunk_lock;
const SerialTuple *IndexReadHandler::Find(const char * key_in) {
if (fcntl(fd, F_GETFD) == -1)
return nullptr;
const SerialTuple *tup = blob->Find(key_in, filesize);
return tup;
}
const SerialString *IndexReadHandler::getDocument( const size_t &index_in ) {
const SerialString *str = blob->getDocument(index_in);
return str;
}
// Read entire index from memory mapped file
int IndexReadHandler::ReadIndex(const char * fname) {
// Open the file for reading, map it, check the header,
// and note the blob address.
fd = open(fname, O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
filesize = lseek(fd, 0, SEEK_END);
if (filesize == -1) { //get file size
perror("fstat");
return 1;
}
void* mapped_memory = mmap(nullptr, filesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (mapped_memory == MAP_FAILED) {
perror("Error mapping memory");
close(fd);
return 1;
}
blob = static_cast<IndexBlob*>(mapped_memory); //map bytes to 'blob'
return 0;
}
void IndexWriteHandler::WriteIndex() {
std::cerr << "Writing out blob!" << std::endl;
//should be optimizing hash to prioritize tokens that appear less
index->optimizeDict();
const IndexBlob *h = IndexBlob::Create(index);
size_t n = h->BlobSize;
write(fd, h, n); // write hash(index)blob to fd
IndexBlob::Discard(h);
close(fd);
}
string nextChunk( const char * foldername, int &chunkID ) {
char * out;
chunkID= -1;
for (const auto& entry : std::filesystem::directory_iterator(foldername)) {
int currID = atoi(entry.path().filename().c_str());
if (currID > chunkID)
chunkID = currID;
}
if (chunkID == -1) {
chunkID = 0;
return string(foldername) + string("/") + string("0");
}
chunkID += 1;
char newFile[5];
snprintf(newFile, sizeof(newFile), "%d", chunkID);
return string(foldername) + string("/") + string(newFile);
}
void IndexHandler::UpdateIH() {
WithWriteLock wl(chunk_lock);
if (index != nullptr)
delete index;
index = new Index();
string fname = nextChunk(folder, chunkID);
fileString = fname;
fd = open(fname.c_str(), O_RDWR | O_CREAT | O_APPEND, (mode_t)0600);
if (fd == -1) {
std::cerr << "Error opening index file";
exit(1);
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("Error getting file size");
close(fd);
exit(1);
}
fsize = sb.st_size;
}
IndexHandler::IndexHandler( const char * foldername ) {
int result;
folder = foldername;
UpdateIH();
}
void lowerize(string &i) {
char * p = i.c_str();
for ( ; *p; ++p) *p = tolower(*p);
}
void Index::addDocument(HtmlParser &parser) {
Tuple<string, PostingList> *seek;
string concat;
/*stem(parser.bodyWords);
stem(parser.headWords);
stem(parser.titleWords);*/
int n = 0;
for (auto &i : parser.bodyWords) {
lowerize(i);
seek = dict.Find(i, PostingList(Token::Body));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
}
for (auto &i : parser.headWords) {
lowerize(i);
concat = headMarker + i;
seek = dict.Find(concat, PostingList(Token::Body));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
}
for (auto &i : parser.titleWords) {
lowerize(i);
concat = titleMarker + i;
seek = dict.Find(concat, PostingList(Token::Title));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
}
for (auto &i : parser.links) {
for (auto &j : i.anchorText) {
lowerize(j);
concat = anchorMarker + j;
seek = dict.Find(concat, PostingList(Token::Anchor));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
}
if (parser.pURL.Host == i.URL.substr(parser.pURL.Service.length() + 3, parser.pURL.Host.length()))
concat = selfRefUrlMarker + i.URL;
else
concat = otherRefUrlMarker + i.URL;
seek = dict.Find(concat, PostingList(Token::URL));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
}
seek = dict.Find(eodMarker, PostingList(Token::EoD));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
concat = selfUrlMarker + parser.base;
seek = dict.Find(concat, PostingList(Token::URL));
seek->value.appendDelta(WordsInIndex, DocumentsInIndex);
DocumentsInIndex += 1;
documents.push_back(parser.base);
}
//for utillity
uint8_t bitsNeeded(const size_t n) {
if (n == 0) {
return 1;
}
return std::max(1, static_cast<int>(std::ceil(std::log2(n + 1))));
}
uint8_t *formatUtf8(const size_t &delta) {
size_t size = SizeOfCustomUtf8(delta);
uint8_t *deltaUtf8 = new uint8_t[size];
WriteCustomUtf8(deltaUtf8, delta, size);
assert(deltaUtf8[0] != 0xfe);
return deltaUtf8;
}
void PostingList::appendDelta(size_t &WordsInIndex, size_t &doc) {
size_t delta = Delta(WordsInIndex, doc);
list.emplace_back(formatUtf8(delta)); // TODO: memory leak?
UpdateSeek(list.size()-1, WordsInIndex);
}