This repository has been archived by the owner on May 4, 2023. It is now read-only.
/
DatabaseIndexer.java
773 lines (663 loc) · 21.7 KB
/
DatabaseIndexer.java
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
package com.github.rnewson.couchdb.lucene;
import static java.lang.Math.max;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.SocketException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexReader.FieldOption;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.mozilla.javascript.ClassShutter;
import org.mozilla.javascript.Context;
import com.github.rnewson.couchdb.lucene.couchdb.CouchDocument;
import com.github.rnewson.couchdb.lucene.couchdb.Database;
import com.github.rnewson.couchdb.lucene.couchdb.DesignDocument;
import com.github.rnewson.couchdb.lucene.couchdb.View;
import com.github.rnewson.couchdb.lucene.util.Constants;
import com.github.rnewson.couchdb.lucene.util.ServletUtils;
import com.github.rnewson.couchdb.lucene.util.StopWatch;
import com.github.rnewson.couchdb.lucene.util.Utils;
public final class DatabaseIndexer implements Runnable, ResponseHandler<Void> {
private class IndexState {
private final DocumentConverter converter;
private boolean dirty;
private String etag;
private final QueryParser parser;
private long pending_seq;
private IndexReader reader;
private final IndexWriter writer;
private final Database database;
public IndexState(final DocumentConverter converter,
final IndexWriter writer, final QueryParser parser,
final Database database) {
this.converter = converter;
this.writer = writer;
this.parser = parser;
this.database = database;
}
public synchronized IndexReader borrowReader(final boolean staleOk)
throws IOException {
blockForLatest(staleOk);
if (reader == null) {
etag = newEtag();
}
if (reader != null) {
reader.decRef();
}
reader = writer.getReader();
if (dirty) {
etag = newEtag();
dirty = false;
}
reader.incRef();
return reader;
}
public IndexSearcher borrowSearcher(final boolean staleOk)
throws IOException {
return new IndexSearcher(borrowReader(staleOk));
}
public void returnReader(final IndexReader reader) throws IOException {
reader.decRef();
}
public void returnSearcher(final IndexSearcher searcher)
throws IOException {
returnReader(searcher.getIndexReader());
}
private synchronized void close() throws IOException {
if (reader != null)
reader.close();
if (writer != null)
writer.rollback();
}
private synchronized String getEtag() {
return etag;
}
private String newEtag() {
return Long.toHexString(now());
}
private synchronized boolean notModified(final HttpServletRequest req) {
return etag != null && etag.equals(req.getHeader("If-None-Match"));
}
private void blockForLatest(final boolean staleOk) throws IOException {
if (staleOk) {
return;
}
final long latest = database.getInfo().getUpdateSequence();
synchronized (this) {
while (pending_seq < latest) {
try {
wait(getSearchTimeout());
} catch (final InterruptedException e) {
throw new IOException("Search timed out.");
}
}
}
}
private synchronized void setPendingSequence(final long newSequence) {
pending_seq = newSequence;
notifyAll();
}
@Override
public String toString() {
return writer.getDirectory().toString();
}
}
private final class RestrictiveClassShutter implements ClassShutter {
public boolean visibleToScripts(final String fullClassName) {
return false;
}
}
private static final JSONObject JSON_SUCCESS = JSONObject
.fromObject("{\"ok\":true}");
public static File uuidDir(final File root, final UUID uuid) {
return new File(root, uuid.toString());
}
public static File viewDir(final File root, final UUID uuid,
final String digest, final boolean mkdirs) throws IOException {
final File uuidDir = uuidDir(root, uuid);
final File viewDir = new File(uuidDir, digest);
if (mkdirs) {
viewDir.mkdirs();
}
return viewDir;
}
private static long now() {
return System.nanoTime();
}
private final HttpClient client;
private boolean closed;
private Context context;
private final Database database;
private long ddoc_seq;
private long lastCommit;
private final CountDownLatch latch = new CountDownLatch(1);
private final Logger logger;
private final Map<String, View> paths = new HashMap<String, View>();
private HttpUriRequest req;
private final File root;
private long since;
private final Map<View, IndexState> states = Collections
.synchronizedMap(new HashMap<View, IndexState>());
private UUID uuid;
private final HierarchicalINIConfiguration ini;
public DatabaseIndexer(final HttpClient client, final File root,
final Database database, final HierarchicalINIConfiguration ini)
throws IOException {
this.client = client;
this.root = root;
this.database = database;
this.ini = ini;
this.logger = Logger.getLogger(DatabaseIndexer.class.getName() + "."
+ database.getInfo().getName());
}
public void admin(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
final IndexState state = getState(req, resp);
if (state == null)
return;
final String command = new PathParts(req).getCommand();
if ("_expunge".equals(command)) {
logger.info("Expunging deletes from " + state);
state.writer.expungeDeletes(false);
ServletUtils.setResponseContentTypeAndEncoding(req, resp);
resp.setStatus(202);
ServletUtils.writeJSON(resp, JSON_SUCCESS);
return;
}
if ("_optimize".equals(command)) {
logger.info("Optimizing " + state);
state.writer.optimize(false);
ServletUtils.setResponseContentTypeAndEncoding(req, resp);
resp.setStatus(202);
ServletUtils.writeJSON(resp, JSON_SUCCESS);
return;
}
}
public void awaitInitialization() {
try {
latch.await();
} catch (final InterruptedException e) {
// Ignore.
}
}
public Void handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
final HttpEntity entity = response.getEntity();
final BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent(), "UTF-8"));
String line;
loop: while ((line = reader.readLine()) != null) {
maybeCommit();
// Heartbeat.
if (line.length() == 0) {
logger.trace("heartbeat");
continue loop;
}
final JSONObject json = JSONObject.fromObject(line);
if (json.has("error")) {
logger.warn("Indexing stopping due to error: " + json);
break loop;
}
if (json.has("last_seq")) {
logger.warn("End of changes detected.");
break loop;
}
final long seq = json.getLong("seq");
final String id = json.getString("id");
CouchDocument doc;
if (json.has("doc")) {
doc = new CouchDocument(json.getJSONObject("doc"));
} else {
// include_docs=true doesn't work prior to 0.11.
try {
doc = database.getDocument(id);
} catch (final HttpResponseException e) {
switch (e.getStatusCode()) {
case HttpStatus.SC_NOT_FOUND:
doc = CouchDocument.deletedDocument(id);
break;
default:
logger.warn("Failed to fetch " + id);
break loop;
}
}
}
if (id.startsWith("_design") && seq > ddoc_seq) {
logger.info("Exiting due to design document change.");
break loop;
}
if (doc.isDeleted()) {
for (final IndexState state : states.values()) {
state.writer.deleteDocuments(new Term("_id", id));
state.setPendingSequence(seq);
}
} else {
for (final Entry<View, IndexState> entry : states.entrySet()) {
final View view = entry.getKey();
final IndexState state = entry.getValue();
final Document[] docs;
try {
docs = state.converter.convert(doc, view
.getDefaultSettings(), database);
} catch (final Exception e) {
logger.warn(id + " caused " + e.getMessage());
continue loop;
}
state.writer.deleteDocuments(new Term("_id", id));
for (final Document d : docs) {
state.writer.addDocument(d, view.getAnalyzer());
}
state.setPendingSequence(seq);
state.dirty = true;
}
}
}
req.abort();
return null;
}
public void info(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
final IndexState state = getState(req, resp);
if (state == null)
return;
final IndexReader reader = state.borrowReader(isStaleOk(req));
try {
final JSONObject result = new JSONObject();
result.put("current", reader.isCurrent());
result.put("disk_size", Utils.directorySize(reader.directory()));
result.put("doc_count", reader.numDocs() - 1);
result.put("doc_del_count", reader.numDeletedDocs());
final JSONArray fields = new JSONArray();
for (final Object field : reader.getFieldNames(FieldOption.INDEXED)) {
if (((String) field).startsWith("_")) {
continue;
}
fields.add(field);
}
result.put("fields", fields);
result.put("last_modified", Long.toString(IndexReader
.lastModified(reader.directory())));
result.put("optimized", reader.isOptimized());
result.put("ref_count", reader.getRefCount());
final JSONObject info = new JSONObject();
info.put("code", 200);
info.put("json", result);
ServletUtils.setResponseContentTypeAndEncoding(req, resp);
final Writer writer = resp.getWriter();
try {
writer.write(result.toString());
} finally {
writer.close();
}
} finally {
state.returnReader(reader);
}
}
public void run() {
if (closed) {
throw new IllegalStateException("closed!");
}
try {
init();
} catch (final IOException e) {
logger.warn("Exiting after init() raised I/O exception.", e);
return;
}
try {
try {
req = database.getChangesRequest(since);
logger.info("Indexing from update_seq " + since);
client.execute(req, this);
} finally {
close();
}
} catch (final SocketException e) {
// Ignored because req.abort() does this.
} catch (final IOException e) {
logger.warn("Exiting due to I/O exception.", e);
}
}
public void search(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
final IndexState state = getState(req, resp);
if (state == null)
return;
if (state.notModified(req)) {
resp.setStatus(304);
return;
}
final IndexSearcher searcher = state.borrowSearcher(isStaleOk(req));
final String etag = state.getEtag();
final JSONArray result = new JSONArray();
try {
for (final String queryString : req.getParameterValues("q")) {
final Query q = state.parser.parse(queryString);
final JSONObject queryRow = new JSONObject();
queryRow.put("q", q.toString());
if (getBooleanParameter(req, "debug")) {
queryRow.put("plan", QueryPlan.toPlan(q));
}
queryRow.put("etag", etag);
if (getBooleanParameter(req, "rewrite")) {
final Query rewritten_q = q.rewrite(searcher
.getIndexReader());
queryRow.put("rewritten_q", rewritten_q.toString());
final JSONObject freqs = new JSONObject();
final Set<Term> terms = new HashSet<Term>();
rewritten_q.extractTerms(terms);
for (final Object term : terms) {
final int freq = searcher.docFreq((Term) term);
freqs.put(term, freq);
}
queryRow.put("freqs", freqs);
} else {
// Perform the search.
final TopDocs td;
final StopWatch stopWatch = new StopWatch();
final boolean include_docs = getBooleanParameter(req,
"include_docs");
final int limit = getIntParameter(req, "limit", 25);
final Sort sort = CustomQueryParser.toSort(req
.getParameter("sort"));
final int skip = getIntParameter(req, "skip", 0);
if (sort == null) {
td = searcher.search(q, null, skip + limit);
} else {
td = searcher.search(q, null, skip + limit, sort);
}
stopWatch.lap("search");
// Fetch matches (if any).
final int max = Math.max(0, Math.min(td.totalHits - skip,
limit));
final JSONArray rows = new JSONArray();
final String[] fetch_ids = new String[max];
for (int i = skip; i < skip + max; i++) {
final Document doc = searcher.doc(td.scoreDocs[i].doc);
final JSONObject row = new JSONObject();
final JSONObject fields = new JSONObject();
// Include stored fields.
for (final Object f : doc.getFields()) {
final Field fld = (Field) f;
if (!fld.isStored()) {
continue;
}
final String name = fld.name();
final String value = fld.stringValue();
if (value != null) {
if ("_id".equals(name)) {
row.put("id", value);
} else {
if (!fields.has(name)) {
fields.put(name, value);
} else {
final Object obj = fields.get(name);
if (obj instanceof String) {
final JSONArray arr = new JSONArray();
arr.add(obj);
arr.add(value);
fields.put(name, arr);
} else {
assert obj instanceof JSONArray;
((JSONArray) obj).add(value);
}
}
}
}
}
if (!Float.isNaN(td.scoreDocs[i].score)) {
row.put("score", td.scoreDocs[i].score);
}// Include sort order (if any).
if (td instanceof TopFieldDocs) {
final FieldDoc fd = (FieldDoc) ((TopFieldDocs) td).scoreDocs[i];
row.put("sort_order", fd.fields);
}
// Fetch document (if requested).
if (include_docs) {
fetch_ids[i - skip] = doc.get("_id");
}
if (fields.size() > 0) {
row.put("fields", fields);
}
rows.add(row);
}
// Fetch documents (if requested).
if (include_docs && fetch_ids.length > 0) {
database.getDocuments(fetch_ids);
final List<CouchDocument> fetched_docs = database
.getDocuments(fetch_ids);
for (int j = 0; j < max; j++) {
rows.getJSONObject(j).put("doc",
fetched_docs.get(j).asJson());
}
}
stopWatch.lap("fetch");
queryRow.put("skip", skip);
queryRow.put("limit", limit);
queryRow.put("total_rows", td.totalHits);
queryRow.put("search_duration", stopWatch
.getElapsed("search"));
queryRow.put("fetch_duration", stopWatch
.getElapsed("fetch"));
// Include sort info (if requested).
if (td instanceof TopFieldDocs) {
queryRow.put("sort_order", CustomQueryParser
.toString(((TopFieldDocs) td).fields));
}
queryRow.put("rows", rows);
}
result.add(queryRow);
}
} catch (final ParseException e) {
ServletUtils.sendJSONError(req, resp, 400, "Bad query syntax: "
+ e.getMessage());
return;
} finally {
state.returnSearcher(searcher);
}
resp.setHeader("ETag", etag);
resp.setHeader("Cache-Control", "must-revalidate");
ServletUtils.setResponseContentTypeAndEncoding(req, resp);
final JSON json = result.size() > 1 ? result : result.getJSONObject(0);
final String callback = req.getParameter("callback");
final String body;
if (callback != null) {
body = String.format("%s(%s)", callback, json);
} else {
body = json.toString(getBooleanParameter(req, "debug") ? 2 : 0);
}
final Writer writer = resp.getWriter();
try {
writer.write(body);
} finally {
writer.close();
}
}
private void close() throws IOException {
this.closed = true;
for (final IndexState state : states.values()) {
state.close();
}
states.clear();
Context.exit();
}
private void commitAll() throws IOException {
for (final Entry<View, IndexState> entry : states.entrySet()) {
final View view = entry.getKey();
final IndexState state = entry.getValue();
if (state.pending_seq > getUpdateSequence(state.writer)) {
final Map<String, String> userData = new HashMap<String, String>();
userData.put("last_seq", Long.toString(state.pending_seq));
state.writer.updateDocument(forceTerm(), forceDocument());
state.writer.commit(userData);
logger.info(view + " now at update_seq " + state.pending_seq);
}
}
lastCommit = now();
}
private Document forceDocument() {
final Document result = new Document();
result.add(new Field("_cl", uuid.toString(), Store.NO,
Index.NOT_ANALYZED_NO_NORMS));
return result;
}
private Term forceTerm() {
return new Term("_cl", uuid.toString());
}
private boolean getBooleanParameter(final HttpServletRequest req,
final String parameterName) {
return Boolean.parseBoolean(req.getParameter(parameterName));
}
private int getIntParameter(final HttpServletRequest req,
final String parameterName, final int defaultValue) {
final String result = req.getParameter(parameterName);
return result != null ? Integer.parseInt(result) : defaultValue;
}
private IndexState getState(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
final View view = paths.get(toPath(req));
if (view == null) {
ServletUtils.sendJSONError(req, resp, 400, "no_such_view");
return null;
}
final IndexState result = states.get(view);
if (result == null) {
ServletUtils.sendJSONError(req, resp, 400, "no_such_state");
}
return result;
}
private long getUpdateSequence(final Directory dir) throws IOException {
if (!IndexReader.indexExists(dir)) {
return 0L;
}
return getUpdateSequence(IndexReader.getCommitUserData(dir));
}
private long getUpdateSequence(final IndexWriter writer) throws IOException {
return getUpdateSequence(writer.getDirectory());
}
private long getUpdateSequence(final Map<String, String> userData) {
if (userData != null && userData.containsKey("last_seq")) {
return Long.parseLong(userData.get("last_seq"));
}
return 0L;
}
private void init() throws IOException {
this.uuid = database.getOrCreateUuid();
this.context = Context.enter();
context.setClassShutter(new RestrictiveClassShutter());
context.setOptimizationLevel(9);
this.ddoc_seq = database.getInfo().getUpdateSequence();
this.since = 0;
for (final DesignDocument ddoc : database.getAllDesignDocuments()) {
for (final Entry<String, View> entry : ddoc.getAllViews()
.entrySet()) {
final String name = entry.getKey();
final View view = entry.getValue();
paths.put(toPath(ddoc.getId(), name), view);
if (!states.containsKey(view)) {
final Directory dir = FSDirectory.open(viewDir(view, true));
final long seq = getUpdateSequence(dir);
if (since == 0) {
since = seq;
}
if (seq != -1L) {
since = Math.min(since, seq);
}
logger.debug(dir + " bumped since to " + since);
final DocumentConverter converter = new DocumentConverter(
context, view);
final IndexWriter writer = newWriter(dir);
final QueryParser parser = new CustomQueryParser(
Constants.VERSION, Constants.DEFAULT_FIELD, view
.getAnalyzer());
final IndexState state = new IndexState(converter, writer,
parser, database);
state.setPendingSequence(seq);
states.put(view, state);
}
}
}
logger.debug("paths: " + paths);
this.lastCommit = now();
latch.countDown();
}
private boolean isStaleOk(final HttpServletRequest req) {
return "ok".equals(req.getParameter("stale"));
}
private void maybeCommit() throws IOException {
if (now() - lastCommit >= getCommitInterval()) {
commitAll();
}
}
private IndexWriter newWriter(final Directory dir) throws IOException {
final IndexWriter result = new IndexWriter(dir, Constants.ANALYZER,
MaxFieldLength.UNLIMITED);
result.setMergeFactor(ini.getInt("lucene.mergeFactor", 10));
result.setUseCompoundFile(ini.getBoolean("lucene.useCompoundFile",
false));
result.setRAMBufferSizeMB(ini.getDouble("lucene.ramBufferSizeMB",
IndexWriter.DEFAULT_RAM_BUFFER_SIZE_MB));
return result;
}
private File viewDir(final View view, final boolean mkdirs)
throws IOException {
return viewDir(root, uuid, view.getDigest(), mkdirs);
}
private long getSearchTimeout() {
return ini.getLong("lucene.timeout", 5000);
}
private long getCommitInterval() {
final long commitSeconds = max(1L, ini
.getLong("lucene.commitEvery", 15));
return SECONDS.toNanos(commitSeconds);
}
private String toPath(final HttpServletRequest req) {
final PathParts parts = new PathParts(req);
return toPath(parts.getDesignDocumentName(), parts.getViewName());
}
private String toPath(final String ddoc, final String view) {
return ddoc + "/" + view;
}
}