/
RepositoryQueryManager.java
580 lines (536 loc) · 25.8 KB
/
RepositoryQueryManager.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
/*
* ModeShape (http://www.modeshape.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.modeshape.jcr;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.jcr.RepositoryException;
import org.modeshape.common.annotation.GuardedBy;
import org.modeshape.common.logging.Logger;
import org.modeshape.common.util.CheckArg;
import org.modeshape.jcr.JcrRepository.RunningState;
import org.modeshape.jcr.RepositoryIndexManager.ScanOperation;
import org.modeshape.jcr.RepositoryIndexManager.ScanningRequest;
import org.modeshape.jcr.RepositoryIndexManager.ScanningTasks;
import org.modeshape.jcr.api.index.IndexDefinition;
import org.modeshape.jcr.api.index.IndexManager;
import org.modeshape.jcr.api.query.QueryCancelledException;
import org.modeshape.jcr.api.query.qom.QueryCommand;
import org.modeshape.jcr.cache.CachedNode;
import org.modeshape.jcr.cache.ChildReference;
import org.modeshape.jcr.cache.ChildReferences;
import org.modeshape.jcr.cache.NodeCache;
import org.modeshape.jcr.cache.NodeKey;
import org.modeshape.jcr.cache.PathCache;
import org.modeshape.jcr.cache.RepositoryCache;
import org.modeshape.jcr.cache.change.ChangeSet;
import org.modeshape.jcr.cache.change.ChangeSetListener;
import org.modeshape.jcr.query.BufferManager;
import org.modeshape.jcr.query.CancellableQuery;
import org.modeshape.jcr.query.QueryContext;
import org.modeshape.jcr.query.QueryEngine;
import org.modeshape.jcr.query.QueryEngineBuilder;
import org.modeshape.jcr.query.QueryResults;
import org.modeshape.jcr.query.engine.IndexQueryEngine;
import org.modeshape.jcr.query.engine.ScanningQueryEngine;
import org.modeshape.jcr.query.plan.PlanHints;
import org.modeshape.jcr.query.validate.Schemata;
import org.modeshape.jcr.spi.index.IndexWriter;
import org.modeshape.jcr.spi.index.provider.IndexProvider;
import org.modeshape.jcr.spi.index.provider.ManagedIndex;
import org.modeshape.jcr.value.Path;
import org.modeshape.jcr.value.Path.Segment;
/**
* The query manager a the repository. Each instance lazily starts up the {@link QueryEngine}, which can be expensive.
*/
class RepositoryQueryManager implements ChangeSetListener {
private final Logger logger = Logger.getLogger(getClass());
private final Logger indexLogger = Logger.getLogger(getClass().getPackage().getName() + ".index");
private final RunningState runningState;
private final ExecutorService indexingExecutorService;
private final RepositoryConfiguration repoConfig;
private final RepositoryIndexManager indexManager;
private final Lock engineInitLock = new ReentrantLock();
@GuardedBy( "engineInitLock" )
private volatile QueryEngine queryEngine;
private volatile Future<Void> asyncReindexingResult;
private volatile ScanningTasks toBeScanned = new ScanningTasks();
private final AtomicBoolean started = new AtomicBoolean(false);
RepositoryQueryManager( RunningState runningState,
ExecutorService indexingExecutorService,
RepositoryConfiguration config ) {
this.runningState = runningState;
this.indexingExecutorService = indexingExecutorService;
this.repoConfig = config;
this.indexManager = new RepositoryIndexManager(runningState, config);
}
synchronized void initialize() {
this.toBeScanned.add(indexManager.initialize());
started.set(true);
}
@Override
public synchronized void notify( ChangeSet changeSet ) {
if (started.get()) {
boolean scanRequired = this.toBeScanned.add(this.indexManager.notify(changeSet));
if (scanRequired) {
// refresh the index writer
this.indexManager.refreshIndexWriter();
// It's initialized, so we have to call it ...
reindexIfNeeded();
}
}
// If not yet initialized, the "reindexIfNeeded" method will be called by the JcrRepository.
}
ChangeSetListener getListener() {
return this;
}
void shutdown() {
started.compareAndSet(true, false);
indexingExecutorService.shutdown();
if (queryEngine != null) {
try {
engineInitLock.lock();
if (queryEngine != null) {
try {
queryEngine.shutdown();
} finally {
queryEngine = null;
}
}
} finally {
engineInitLock.unlock();
}
}
indexManager.shutdown();
}
void stopReindexing() {
try {
engineInitLock.lock();
if (asyncReindexingResult != null) {
try {
asyncReindexingResult.get(1, TimeUnit.MINUTES);
} catch (java.util.concurrent.TimeoutException e) {
logger.debug("Re-indexing has not finished in time, attempting to cancel operation");
asyncReindexingResult.cancel(true);
} catch (Exception e) {
logger.debug(e, "Unexpected exception while waiting for re-indexing to terminate");
}
}
} finally {
asyncReindexingResult = null;
engineInitLock.unlock();
}
}
public CancellableQuery query( ExecutionContext context,
RepositoryCache repositoryCache,
Set<String> workspaceNames,
Map<String, NodeCache> overriddenNodeCachesByWorkspaceName,
final QueryCommand query,
Schemata schemata,
RepositoryIndexes indexDefns,
NodeTypes nodeTypes,
PlanHints hints,
Map<String, Object> variables ) {
final QueryEngine queryEngine = queryEngine();
final QueryContext queryContext = queryEngine.createQueryContext(context, repositoryCache, workspaceNames,
overriddenNodeCachesByWorkspaceName, schemata,
indexDefns, nodeTypes, new BufferManager(context),
hints, variables);
final org.modeshape.jcr.query.model.QueryCommand command = (org.modeshape.jcr.query.model.QueryCommand)query;
return new CancellableQuery() {
private final Lock lock = new ReentrantLock();
private QueryResults results;
@Override
public QueryResults execute() throws QueryCancelledException, RepositoryException {
try {
lock.lock();
if (results == null) {
// this will block and will hold the lock until it is done ...
results = queryEngine.execute(queryContext, command);
}
return results;
} finally {
lock.unlock();
}
}
@Override
public boolean cancel() {
return queryContext.cancel();
}
};
}
/**
* Get the writer to the indexes. The resulting instance will only write to the index providers that were registered at the
* time this method is called. Therefore, the writer should be used and discarded relatively quickly, since query index
* providers may be {@link RepositoryIndexManager#register(org.modeshape.jcr.spi.index.provider.IndexProvider) added} or
* {@link RepositoryIndexManager#unregister(String) removed} at any time.
*
* @return the index writer; never null
*/
public IndexWriter getIndexWriter() {
return indexManager.getIndexWriter();
}
protected RepositoryIndexManager getIndexManager() {
return indexManager;
}
/**
* Get an immutable snapshot of the index definitions. This can be used by the query engine to determine which indexes might
* be usable when quering a specific selector (node type).
*
* @return a snapshot of the index definitions at this moment; never null
*/
RepositoryIndexes getIndexes() {
return indexManager.getIndexes();
}
/**
* Obtain the query engine, which is created lazily and in a thread-safe manner.
*
* @return the query engine; never null
*/
protected final QueryEngine queryEngine() {
if (queryEngine == null) {
try {
engineInitLock.lock();
if (queryEngine == null) {
QueryEngineBuilder builder = null;
if (!repoConfig.getIndexProviders().isEmpty()) {
// There is at least one index provider ...
builder = IndexQueryEngine.builder();
logger.debug("Queries with indexes are enabled for the '{0}' repository. Executing queries may require scanning the repository contents when the query cannot use the defined indexes.",
repoConfig.getName());
} else {
// There are no indexes ...
builder = ScanningQueryEngine.builder();
logger.debug("Queries with no indexes are enabled for the '{0}' repository. Executing queries will always scan the repository contents.",
repoConfig.getName());
}
queryEngine = builder.using(repoConfig, indexManager, runningState.context()).build();
}
} finally {
engineInitLock.unlock();
}
}
return queryEngine;
}
/**
* Reindex the repository only if there is at least one provider that required scanning and reindexing.
*/
protected void reindexIfNeeded() {
final ScanningRequest request = toBeScanned.drain();
if (!request.isEmpty()) {
final IndexWriter writer = indexManager.getIndexWriterForProviders(request.providerNames());
final RepositoryCache repoCache = runningState.repositoryCache();
scan(true, writer, new Callable<Void>() {
@Override
public Void call() throws Exception {
// Scan each of the workspace-path pairs ...
ScanOperation op = new ScanOperation() {
@Override
public void scan( String workspaceName,
Path path ) {
NodeCache workspaceCache = repoCache.getWorkspaceCache(workspaceName);
if (workspaceCache != null) {
// The workspace is still valid ...
CachedNode node = workspaceCache.getNode(workspaceCache.getRootKey());
if (!path.isRoot()) {
for (Path.Segment segment : path) {
ChildReference child = node.getChildReferences(workspaceCache).getChild(segment);
if (child == null) {
// The child no longer exists, so ignore this pair ...
node = null;
break;
}
node = workspaceCache.getNode(child);
if (node == null) break;
}
}
if (node != null) {
// If we find a node to start at, then scan the content ...
boolean scanSystemContent = repoCache.getSystemWorkspaceName().equals(workspaceName);
reindexContent(workspaceName, workspaceCache, node, Integer.MAX_VALUE, scanSystemContent,
writer);
}
}
}
};
request.onEachPathInWorkspace(op);
return null;
}
});
}
}
/**
* Clean all indexes and reindex all content.
*
* @param async true if the reindexing should be done in the background, or false if it should be done using this thread
*/
protected void cleanAndReindex( boolean async ) {
final IndexWriter writer = getIndexWriter();
scan(async, getIndexWriter(), new Callable<Void>() {
@SuppressWarnings( "synthetic-access" )
@Override
public Void call() throws Exception {
writer.clearAllIndexes();
reindexContent(true, writer);
return null;
}
});
}
private void scan( boolean async,
final IndexWriter indexes,
Callable<Void> callable ) {
if (!indexes.canBeSkipped()) {
if (async) {
asyncReindexingResult = indexingExecutorService.submit(callable);
} else {
try {
callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
/**
* Crawl and index all of the repository content.
*
* @param includeSystemContent true if the system content should also be indexed
* @param indexes the index writer that should be use; may not be null
*/
private void reindexContent( boolean includeSystemContent,
IndexWriter indexes ) {
if (indexes.canBeSkipped()) return;
// The node type schemata changes every time a node type is (un)registered, so get the snapshot that we'll use throughout
RepositoryCache repoCache = runningState.repositoryCache();
logger.debug(JcrI18n.reindexAll.text(runningState.name()));
if (includeSystemContent) {
NodeCache systemWorkspaceCache = repoCache.getWorkspaceCache(repoCache.getSystemWorkspaceName());
CachedNode rootNode = systemWorkspaceCache.getNode(repoCache.getSystemKey());
// Index the system content ...
logger.debug("Starting reindex of system content in '{0}' repository.", runningState.name());
reindexSystemContent(rootNode, Integer.MAX_VALUE, indexes);
logger.debug("Completed reindex of system content in '{0}' repository.", runningState.name());
}
// Index the non-system workspaces ...
for (String workspaceName : repoCache.getWorkspaceNames()) {
NodeCache workspaceCache = repoCache.getWorkspaceCache(workspaceName);
CachedNode rootNode = workspaceCache.getNode(workspaceCache.getRootKey());
logger.debug("Starting reindex of workspace '{0}' content in '{1}' repository.", runningState.name(), workspaceName);
reindexContent(workspaceName, workspaceCache, rootNode, Integer.MAX_VALUE, false, indexes);
logger.debug("Completed reindex of workspace '{0}' content in '{1}' repository.", runningState.name(), workspaceName);
}
}
/**
* Crawl and index the content in the named workspace.
*
* @param workspace the workspace
* @throws IllegalArgumentException if the workspace is null
*/
public void reindexContent( JcrWorkspace workspace ) {
reindexContent(workspace, Path.ROOT_PATH, Integer.MAX_VALUE);
}
/**
* Crawl and index the content starting at the supplied path in the named workspace, to the designated depth.
*
* @param workspace the workspace
* @param path the path of the content to be indexed
* @param depth the depth of the content to be indexed
* @throws IllegalArgumentException if the workspace or path are null, or if the depth is less than 1
*/
public void reindexContent( JcrWorkspace workspace,
Path path,
int depth ) {
if (getIndexWriter().canBeSkipped()) {
// There's no indexes that require updating ...
return;
}
CheckArg.isPositive(depth, "depth");
JcrSession session = workspace.getSession();
NodeCache cache = session.cache().getWorkspace();
String workspaceName = workspace.getName();
// Look for the node ...
CachedNode node = cache.getNode(cache.getRootKey());
for (Segment segment : path) {
// Look for the child by name ...
ChildReference ref = node.getChildReferences(cache).getChild(segment);
if (ref == null) return;
node = cache.getNode(ref);
}
// If the node is in the system workspace ...
String systemWorkspaceKey = runningState.repositoryCache().getSystemWorkspaceKey();
if (node.getKey().getWorkspaceKey().equals(systemWorkspaceKey)) {
reindexSystemContent(node, depth, getIndexWriter());
} else {
// It's just a regular node in the workspace ...
reindexContent(workspaceName, cache, node, depth, path.isRoot(), getIndexWriter());
}
}
protected void reindexContent( final String workspaceName,
NodeCache cache,
CachedNode node,
int depth,
boolean reindexSystemContent,
final IndexWriter indexes ) {
assert indexes != null;
if (indexes.canBeSkipped()) return;
if (node.isExcludedFromSearch(cache)) {
return;
}
try {
// change the status of the indexes to reindexing
updateIndexesStatus(workspaceName, IndexManager.IndexStatus.ENABLED, IndexManager.IndexStatus.REINDEXING);
// Get the path for the first node (we already have it, but we need to populate the cache) ...
final PathCache paths = new PathCache(cache);
Path nodePath = paths.getPath(node);
// Index the first node ...
if (indexLogger.isTraceEnabled()) {
String path = runningState.context().getValueFactories().getStringFactory().create(nodePath);
indexLogger.debug("Reindexing node '{0}' in workspace '{1}' of repository '{2}': {3}", path, workspaceName,
runningState.name(), node);
}
indexes.add(workspaceName, node.getKey(), nodePath, node.getPrimaryType(cache), node.getMixinTypes(cache),
node.getPropertiesByName(cache));
if (depth == 1) return;
// Create a queue for processing the subgraph
final Queue<NodeKey> queue = new LinkedList<NodeKey>();
if (reindexSystemContent) {
// We need to look for the system node, and index it differently ...
ChildReferences childRefs = node.getChildReferences(cache);
ChildReference systemRef = childRefs.getChild(JcrLexicon.SYSTEM);
NodeKey systemKey = systemRef != null ? systemRef.getKey() : null;
for (ChildReference childRef : node.getChildReferences(cache)) {
NodeKey childKey = childRef.getKey();
if (childKey.equals(systemKey)) {
// This is the "/jcr:system" node ...
node = cache.getNode(childKey);
reindexSystemContent(node, depth - 1, indexes);
} else {
queue.add(childKey);
}
}
} else {
// Add all children to the queue ...
for (ChildReference childRef : node.getChildReferences(cache)) {
NodeKey childKey = childRef.getKey();
// we should not reindex anything which is in the system area
if (!childKey.getWorkspaceKey().equals(runningState.systemWorkspaceKey())) {
queue.add(childKey);
}
}
}
// Now, process the queue until empty ...
while (true) {
NodeKey key = queue.poll();
if (key == null) break;
// Look up the node and find the path ...
node = cache.getNode(key);
if (node == null || node.isExcludedFromSearch(cache)) {
continue;
}
nodePath = paths.getPath(node);
// Index the node ...
if (indexLogger.isTraceEnabled()) {
String path = runningState.context().getValueFactories().getStringFactory().create(nodePath);
indexLogger.debug("Reindexing node '{0}' in workspace '{1}' of repository '{2}': {3}", path, workspaceName,
runningState.name(), node);
}
indexes.add(workspaceName, node.getKey(), nodePath, node.getPrimaryType(cache), node.getMixinTypes(cache),
node.getPropertiesByName(cache));
// Check the depth ...
if (nodePath.size() <= depth) {
// Add the children to the queue ...
for (ChildReference childRef : node.getChildReferences(cache)) {
queue.add(childRef.getKey());
}
}
}
} finally {
// set the index status back to enabled
updateIndexesStatus(workspaceName, IndexManager.IndexStatus.REINDEXING, IndexManager.IndexStatus.ENABLED);
}
}
protected void updateIndexesStatus( String workspaceName, final IndexManager.IndexStatus currentStatus, final IndexManager.IndexStatus newStatus ) {
for (IndexProvider indexProvider : indexManager.getProviders()) {
indexProvider.onEachIndexInWorkspace(workspaceName, new IndexProvider.ManagedIndexOperation() {
@Override
public void apply( String workspaceName, ManagedIndex index, IndexDefinition defn ) {
index.updateStatus(currentStatus, newStatus);
}
});
}
}
protected void reindexSystemContent( CachedNode nodeInSystemBranch,
int depth,
IndexWriter indexes ) {
RepositoryCache repoCache = runningState.repositoryCache();
String workspaceName = repoCache.getSystemWorkspaceName();
NodeCache systemWorkspaceCache = repoCache.getWorkspaceCache(workspaceName);
reindexContent(workspaceName, systemWorkspaceCache, nodeInSystemBranch, depth, true, indexes);
}
protected void reindexSystemContent() {
RepositoryCache repoCache = runningState.repositoryCache();
String workspaceName = repoCache.getSystemWorkspaceName();
NodeCache systemWorkspaceCache = repoCache.getWorkspaceCache(workspaceName);
CachedNode systemNode = systemWorkspaceCache.getNode(repoCache.getSystemKey());
reindexContent(workspaceName, systemWorkspaceCache, systemNode, Integer.MAX_VALUE, true, getIndexWriter());
}
/**
* Asynchronously crawl and index the content in the named workspace.
*
* @param workspace the workspace
* @return the future for the asynchronous operation; never null
* @throws IllegalArgumentException if the workspace is null
*/
public Future<Boolean> reindexContentAsync( final JcrWorkspace workspace ) {
return indexingExecutorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
reindexContent(workspace);
return Boolean.TRUE;
}
});
}
/**
* Asynchronously crawl and index the content starting at the supplied path in the named workspace, to the designated depth.
*
* @param workspace the workspace
* @param path the path of the content to be indexed
* @param depth the depth of the content to be indexed
* @return the future for the asynchronous operation; never null
* @throws IllegalArgumentException if the workspace or path are null, or if the depth is less than 1
*/
public Future<Boolean> reindexContentAsync( final JcrWorkspace workspace,
final Path path,
final int depth ) {
return indexingExecutorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
reindexContent(workspace, path, depth);
return Boolean.TRUE;
}
});
}
}