-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsegment_index_source.cpp
186 lines (153 loc) · 7.45 KB
/
segment_index_source.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
#include "segment_index_source.h"
#include "google_codec.h"
#include "lucene_codec.h"
Trinity::SegmentIndexSource::SegmentIndexSource(const char *basePath)
{
int fd;
char path[PATH_MAX];
strwlen32_t bp(basePath);
try
{
bp.StripTrailingCharacter('/');
if (auto p = bp.SearchR('/'))
bp = bp.SuffixFrom(p + 1);
if (!bp.IsDigits())
throw Switch::data_error("Expected segment name to be a generation(digits)");
gen = bp.AsUint64();
snprintf(path, sizeof(path), "%s/updated_documents.ids", basePath);
fd = open(path, O_RDONLY | O_LARGEFILE);
if (fd == -1)
{
if (errno != ENOENT)
throw Switch::system_error("open() failed for updated_documents.ids");
}
else if (const auto fileSize = lseek64(fd, 0, SEEK_END); fileSize > 0)
{
auto fileData = mmap(nullptr, fileSize, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (unlikely(fileData == MAP_FAILED))
throw Switch::data_error("Failed to access ", path, ":", strerror(errno));
madvise(fileData, fileSize, MADV_DONTDUMP);
maskedDocuments.fileData.Set(reinterpret_cast<uint8_t *>(fileData), fileSize);
new (&maskedDocuments.set) updated_documents(unpack_updates(maskedDocuments.fileData));
}
else
close(fd);
terms.reset(new SegmentTerms(basePath));
snprintf(path, sizeof(path), "%s/index", basePath);
fd = open(path, O_RDONLY | O_LARGEFILE);
if (fd == -1)
{
if (errno != ENOENT)
throw Switch::data_error("Failed to access ", path);
else
{
// Missing index? someone created a directory here that's incomplete? We can't proceed anyway, so
// delegate responsibility to caller
throw Switch::data_error("Unexpected index structure ", path);
}
}
auto fileSize = lseek64(fd, 0, SEEK_END);
if (0 == fileSize)
{
// just updated documents
}
else
{
#ifdef TRINITY_MEMRESIDENT_INDEX
auto p = (uint8_t *)malloc(fileSize + 1);
if (pread64(fd, p, fileSize, 0) != fileSize)
{
free(p);
close(fd);
throw Switch::data_error("Failed to acess ", path);
}
close(fd);
index.Set(p, fileSize);
#else
auto fileData = mmap(nullptr, fileSize, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (unlikely(fileData == MAP_FAILED))
throw Switch::data_error("Failed to acess ", path);
madvise(fileData, fileSize, MADV_DONTDUMP);
index.Set(static_cast<const uint8_t *>(fileData), uint32_t(fileSize));
#endif
}
char codecStorage[128];
strwlen8_t codec;
snprintf(path, sizeof(path), "%s/id", basePath);
fd = open(path, O_RDONLY | O_LARGEFILE);
if (fd == -1)
{
if (errno != ENOENT)
throw Switch::data_error("Failed to access ", path);
snprintf(path, sizeof(path), "%s/codec", basePath);
fd = open(path, O_RDONLY | O_LARGEFILE);
if (unlikely(fd == -1))
throw Switch::data_error("Failed to acess ", path);
fileSize = lseek64(fd, 0, SEEK_END);
if (!IsBetweenRange<size_t>(fileSize, 3, 128))
{
close(fd);
throw Switch::data_error("Invalid segment codec file");
}
if (pread64(fd, codecStorage, fileSize, 0) != fileSize)
{
close(fd);
throw Switch::system_error("Failed to read codec");
}
else
{
close(fd);
codec.Set(codecStorage, fileSize);
}
}
else
{
const auto fileSize = lseek64(fd, 0, SEEK_END);
if (unlikely(fileSize > 1024 || (fileSize < sizeof(uint8_t) + 1 + sizeof(uint64_t) + sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))))
{
close(fd);
throw Switch::system_error("Unexpected ID contents");
}
uint8_t b[1024], *p{b};
if (pread64(fd, b, fileSize, 0) != fileSize)
{
close(fd);
throw Switch::system_error("Failed to read ID");
}
if (*p++ != 1)
{
close(fd);
throw Switch::system_error("Failed to read ID: unsupported release");
}
close(fd);
codec.len = *p++;
codec.p = codecStorage;
memcpy(codecStorage, p, codec.len);
p += codec.len;
defaultFieldStats.sumTermHits = *(uint64_t *)p;
p += sizeof(uint64_t);
defaultFieldStats.totalTerms = *(uint32_t *)p;
p += sizeof(uint32_t);
defaultFieldStats.sumTermsDocs = *(uint64_t *)p;
p += sizeof(uint64_t);
defaultFieldStats.docsCnt = *(uint32_t *)p;
p += sizeof(uint32_t);
// SLog("Restored codec '", codec, "' sumTermHits = ", dotnotation_repr(defaultFieldStats.sumTermHits), ", totalTerms = ", dotnotation_repr(defaultFieldStats.totalTerms), ", sumTermsDocs = ", dotnotation_repr(defaultFieldStats.sumTermsDocs), ", docsCnt = ", dotnotation_repr(defaultFieldStats.docsCnt), "\n");
}
if (codec.Eq(_S("LUCENE")))
accessProxy.reset(new Trinity::Codecs::Lucene::AccessProxy(basePath, index.start()));
#ifdef TRINITY_CODECS_GOOGLE_AVAILABLE
else if (codec.Eq(_S("GOOGLE")))
accessProxy.reset(new Trinity::Codecs::Google::AccessProxy(basePath, index.start()));
#endif
else
throw Switch::data_error("Unknown codec");
}
catch (...)
{
ResetRefs();
throw;
}
}