-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicStore.cpp
More file actions
372 lines (348 loc) · 9.66 KB
/
Copy pathMagicStore.cpp
File metadata and controls
372 lines (348 loc) · 9.66 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "Triple.hpp"
#include "TreePattern.hpp"
#include "BinaryTriples.hpp"
#include "Timer.hpp"
#include <iostream>
#include <map>
#include <queue>
#include <iterator>
#include <cassert>
#include <algorithm>
#include <sstream>
const std::string ub="http://swat.cse.lehigh.edu/onto/univ-bench.owl#";
const std::string rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
const std::string a=rdf+"type";
Triples operator+(Triples t, const Triple& x)
{
t.push_back(x);
return t;
}
Triples operator+(const Triple& x, const Triple& y)
{
Triples t;
t.push_back(x);
t.push_back(y);
return t;
}
std::string resolve(const std::string& input)
{
std::map<std::string,std::string> prefixes;
if(input[0]=='?')
return input;
if(input=="a")
return rdf+"type";
prefixes["ub:"]=ub;
for(auto p:prefixes)
if(input.find(p.first)==0)
return p.second+input.substr(p.first.length());
return input;
}
typedef std::unique_ptr<BinaryTriples> PBinaryTriples;
#if 0
PBinaryTriples LoadDir(const std::string& dirname, PCodes soCodes, PCodes pCodes, const std::string& format)
{
DIR *d=opendir(dirname.c_str());
dirent *de;
PBinaryTriples result;
while((de=readdir(d))!=NULL)
{
std::string file(de->d_name);
file=dirname+"/"+file;
if(file.find(".owl")==file.length()-4)
{
Triples triples;
std::cout<<"Loading "<<file<<std::endl;
LoadTriples(file, [&triples](const Triple& t)->void {triples.push_back(t);}, format);
if(result!=NULL)
{
BinaryTriples bt;
bt.fill(soCodes,pCodes,triples);
PBinaryTriples x(new BinaryTriples());
x->merge(*result, bt);
result=std::move(x);
}
else
{
result=PBinaryTriples(new BinaryTriples());
result->fill(soCodes,pCodes,triples);
}
}
}
closedir(d);
return result;
}
#endif
class Adder
{
public:
void operator()(int x)
{
// std::cout<<x<<"\n";
if(i==end)
throw std::runtime_error("Did not expect anything, got "+std::to_string(x));
if(x!=*i)
throw std::runtime_error("Expected "+std::to_string(*i)+", got "+std::to_string(x));
i++;
}
explicit Adder(const std::deque<int>& c)
:i(c.cbegin()),end(c.cend())
{
}
~Adder()
{
if(i!=end)
throw std::runtime_error("Finished before "+std::to_string(*i));
}
private:
std::deque<int>::const_iterator i,end;
};
#include "Merger.hpp"
void testMerge()
{
std::deque<int>
a({1,2,3}),
b({4,5,6}),
c({1,2,3,4,5,6}),
d({1,3,5}),
e({2,4,6}),
f({2,4,5,6}),
g({4,6}),
h({1,2,3,4,5});
auto comparator=[](int x,int y)->int{return x-y;};
merge<int>(JavaIterator<int>(a),
JavaIterator<int>(b),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(b),
JavaIterator<int>(a),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(a),
JavaIterator<int>(c),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(c),
JavaIterator<int>(a),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(d),
JavaIterator<int>(e),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(e),
JavaIterator<int>(d),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(a),
JavaIterator<int>(a),
Adder(a),
comparator);
merge<int>(JavaIterator<int>(a),
JavaIterator<int>(f),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(f),
JavaIterator<int>(a),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(c),
JavaIterator<int>(g),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(g),
JavaIterator<int>(c),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(h),
JavaIterator<int>(g),
Adder(c),
comparator);
merge<int>(JavaIterator<int>(g),
JavaIterator<int>(h),
Adder(c),
comparator);
}
class Loader
{
public:
Loader(PCodes soCodes,PCodes pCodes)
:soCodes(soCodes),pCodes(pCodes)
{
}
void operator()(const Triple& t)
{
triples.push_back(BinaryTriple((*soCodes)[t.s()], (*pCodes)[t.p()], (*soCodes)[t.o()]));
if(triples.size()>1e6)
merge();
}
PBinaryTriples result()
{
merge();
return std::move(res);
}
private:
void merge()
{
if(triples.empty())
return;
std::cout<<"Merge of "<<triples.size()<<" triples ";
std::cout.flush();
if(res!=NULL)
{
BinaryTriples bt;
bt.fill(soCodes,pCodes,triples);
PBinaryTriples x(new BinaryTriples());
x->merge(*res, bt);
res=std::move(x);
}
else
{
res=PBinaryTriples(new BinaryTriples());
res->fill(soCodes,pCodes,triples);
}
triples.clear();
std::cout<<"done"<<std::endl;
}
PBinaryTriples res;
RawBinaryTriples triples;
PCodes soCodes,pCodes;
};
class Coder
{
public:
void operator()(const Triple& t)
{
soCodes->inc(t.s());
pCodes->inc(t.p());
soCodes->inc(t.o());
}
Coder()
:soCodes(new Codes()),pCodes(new Codes())
{
}
PCodes soCodes;
PCodes pCodes;
};
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <microhttpd.h>
int answer_to_connection (void *cls, struct MHD_Connection *connection,
const char */*url*/,
const char */*method*/, const char */*version*/,
const char */*upload_data*/,
size_t */*upload_data_size*/, void **/*con_cls*/)
{
std::string page;
try
{
BinaryTriples& bt(*reinterpret_cast<BinaryTriples*>(cls));
auto query=TreePattern::Query::fromSPARQL(MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND,"query"));
std::ostringstream s;
std::string var=query.variable();
s<<"<?xml version=\"1.0\"?>\n"
<<"<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\">\n"
<<"<head>\n"
<<"<variable name=\""<<var<<"\"/>\n"
<<"</head>\n"
<<"<results>\n";
if(query.isCount())
{
s<<"<result><binding name=\""<<var<<"\">"
<<"<literal datatype=\"http://www.w3.org/2001/XMLSchema#integer\">"
<<bt.count(query.where())
<<"</literal></binding></result>\n";
}
else
{
std::deque<std::string> answer = bt.answer(query.where());
for(auto& a:answer)
s<<"<result><binding name=\""<<var<<"\"><uri>"<<a<<"</uri></binding></result>\n";
}
s<<"</results>\n</sparql>\n";
page=s.str();
}
catch(const std::exception& e)
{
//TODO: MalformedQuery and QueryRequestRefused, are bound to HTTP status codes 400 Bad Request and 500 Internal Server Error, respectively [HTTP].
page=e.what();
}
struct MHD_Response *response;
int ret;
response = MHD_create_response_from_buffer (page.length(),
(void*) page.c_str(), MHD_RESPMEM_MUST_COPY);
MHD_add_response_header(response, "Content-Type", "application/sparql-results+xml");
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response);
return ret;
}
void listen(BinaryTriples& bt)
{
struct MHD_Daemon *daemon;
daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, 12345, NULL, NULL,
&answer_to_connection, &bt, MHD_OPTION_END);
if (NULL == daemon)
return;
for(;;)
sleep(1000);
}
int main(int argc, char **argv)
{
if(argc==1)
{
return 1;
}
if(argv[1][0]=='c')
{
std::string format(argv[3]);
std::cout<<"Generating codes to file "<<argv[2]<<", the input format is "<<format<<"\n";
auto coder=Coder();
for(int i=4;i<argc;++i)
{
std::cout<<"Generating codes from "<<argv[i]<<"\n";
LoadTriples(argv[i], coder, format);
}
PCodes soCodes=coder.soCodes;
PCodes pCodes=coder.pCodes;
soCodes->compress();
pCodes->compress();
std::ofstream f(argv[2]);
soCodes->save(f);
pCodes->save(f);
f.close();
}
if(argv[1][0]=='l')
{
std::cout<<"Codes in "<<argv[2]<<"\n";
PCodes soCodes(new Codes());
PCodes pCodes(new Codes());
{
std::ifstream f(argv[2]);
soCodes->load(f);
pCodes->load(f);
f.close();
}
std::string format(argv[4]);
std::cout<<"Loading to file "<<argv[3]<<", the input format is "<<format<<"\n";
Loader loader(soCodes,pCodes);
for(int i=5;i<argc;++i)
{
std::cout<<"Doing magic about "<<argv[i]<<"\n";
LoadTriples(argv[i], loader, format);
}
PBinaryTriples bt=loader.result();
std::ofstream f(argv[3]);
bt->save(f);
f.close();
}
if(argv[1][0]=='q')
{
std::cout<<"Querying dataset "<<argv[2]<<"\n";
std::ifstream f(argv[2]);
BinaryTriples bt;
bt.load(f);
f.close();
listen(bt);
}
return 0;
}