forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindexerprocess.cpp
More file actions
190 lines (156 loc) · 4.48 KB
/
Copy pathindexerprocess.cpp
File metadata and controls
190 lines (156 loc) · 4.48 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
#include <sstream>
#include <memory>
#include <boost/log/expressions.hpp>
#include <thrift/transport/TFDTransport.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <util/logutil.h>
#include <indexer/indexerprocess.h>
#ifdef JAVAMEMORYAMOUNT
#undef JAVAMEMORYAMOUNT
#endif
#ifdef __i386
// JVM won't start with -Xmx2g on 32-bit platforms
#define JAVAMEMORYAMOUNT "-Xmx1532m"
#else
#define JAVAMEMORYAMOUNT "-Xmx2g"
#endif
namespace cc
{
namespace parser
{
IndexerProcess::IndexerProcess(
const std::string& indexDatabase_,
const std::string& compassRoot_,
IndexerProcess::OpenMode openMode_,
IndexerProcess::LockMode lockMode_,
const std::string& logTarget_)
{
openPipe(_pipeFd2[0], _pipeFd2[1]);
if (startProcess() == 0)
{
// This is the child process.
std::string inFd(std::to_string(_pipeFd[0]));
std::string outFd(std::to_string(_pipeFd2[1]));
std::string logLevelOpt("-Dcc.search.logLevel=");
auto fmtSeverity = util::getSeverityLevel();
if (fmtSeverity == boost::log::trivial::info)
logLevelOpt += "INFO";
else if (fmtSeverity == boost::log::trivial::error ||
fmtSeverity == boost::log::trivial::warning)
logLevelOpt += "WARNING";
else if (fmtSeverity == boost::log::trivial::fatal)
logLevelOpt += "SEVERE";
else
logLevelOpt += "FINEST";
std::string classpath = compassRoot_ + "/lib/java/*";
std::vector<const char*> execArguments {
"java", JAVAMEMORYAMOUNT,
"-classpath", classpath.c_str(),
"-Djava.util.logging.config.class=cc.search.common.config.LogConfigurator",
"-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tT [%4$s] %5$s%6$s%n",
logLevelOpt.c_str(),
"cc.search.indexer.app.Indexer",
"-indexDB", indexDatabase_.c_str(),
"-ipcInFd", inFd.c_str(),
"-ipcOutFd", outFd.c_str(),
"-logTarget", logTarget_.c_str()
};
switch (openMode_)
{
case OpenMode::Create:
execArguments.push_back("-create");
break;
case OpenMode::ReplaceExisting:
execArguments.push_back("-replaceExisting");
break;
case OpenMode::Merge:
execArguments.push_back("-merge");
break;
}
switch (lockMode_)
{
case LockMode::Native:
// This is the default
break;
case LockMode::Simple:
execArguments.push_back("-useSimpleFileLock");
execArguments.push_back("-cleanupLocks");
break;
}
execArguments.push_back(nullptr);
::execvp("java", const_cast<char* const*>(execArguments.data()));
LOG(error) << "execlp failed!";
// this shouldn't be executed by child process
::abort();
}
else
{
// Get the client interface
using Transport = apache::thrift::transport::TFDTransport;
using ProtocolFactory =
apache::thrift::protocol::TBinaryProtocolFactoryT<Transport>;
std::shared_ptr<apache::thrift::transport::TTransport> transIn(
new Transport(_pipeFd2[0], Transport::NO_CLOSE_ON_DESTROY));
std::shared_ptr<apache::thrift::transport::TTransport> transOut(
new Transport(_pipeFd[1], Transport::NO_CLOSE_ON_DESTROY));
ProtocolFactory protFactory;
_indexer.reset(new search::IndexerServiceClient(
protFactory.getProtocol(transIn),
protFactory.getProtocol(transOut)));
}
}
IndexerProcess::~IndexerProcess()
{
if (_indexer && isAlive())
_indexer->stop();
closePipe(_pipeFd[0], _pipeFd[1]);
closePipe(_pipeFd2[0], _pipeFd2[1]);
_indexer.reset(nullptr);
}
void IndexerProcess::stop()
{
assert(false && "IndexerProcess::stop called");
}
void IndexerProcess::indexFile(
const std::string& fileId_,
const std::string& filePath_,
const std::string& mimeType_)
{
if (!isAlive())
{
LOG(error) << "Index process is not alive!";
::abort();
}
_indexer->indexFile(fileId_, filePath_, mimeType_);
}
void IndexerProcess::addFieldValues(
const std::string& fileId_,
const search::Fields& fields_)
{
if (!isAlive())
{
LOG(error) << "Index process is not alive!";
::abort();
}
_indexer->addFieldValues(fileId_, fields_);
}
void IndexerProcess::buildSuggestions()
{
if (!isAlive())
{
LOG(error) << "Index process is not alive!";
::abort();
}
_indexer->buildSuggestions();
}
void IndexerProcess::getStatistics(std::map<std::string, std::string>& stat_)
{
if (!isAlive())
{
LOG(error) << "Index process is not alive!";
::abort();
}
_indexer->getStatistics(stat_);
}
} // namespace parser
} // namespace cc