View Javadoc

1   package org.apache.maven.index.context;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0    
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.apache.lucene.index.IndexReader;
30  import org.apache.lucene.index.MultiReader;
31  import org.apache.lucene.search.IndexSearcher;
32  
33  public class NexusIndexMultiReader
34  {
35      private final List<IndexingContext> contexts;
36  
37      private List<IndexSearcher> searchers;
38  
39      public NexusIndexMultiReader( final Collection<IndexingContext> contexts )
40      {
41          this.contexts = Collections.unmodifiableList( new ArrayList<IndexingContext>( contexts ) );
42      }
43  
44      public synchronized IndexReader acquire()
45          throws IOException
46      {
47          if ( searchers != null )
48          {
49              release();
50              throw new IllegalStateException( "acquire() called 2nd time without release() in between!" );
51          }
52          this.searchers = new ArrayList<IndexSearcher>();
53          final ArrayList<IndexReader> contextReaders = new ArrayList<IndexReader>( contexts.size() );
54          for ( IndexingContext ctx : contexts )
55          {
56              final IndexSearcher indexSearcher = ctx.acquireIndexSearcher();
57              searchers.add( indexSearcher );
58              contextReaders.add( indexSearcher.getIndexReader() );
59          }
60          return new MultiReader( contextReaders.toArray( new IndexReader[contextReaders.size()] ) );
61      }
62  
63      public synchronized void release()
64          throws IOException
65      {
66          if ( searchers != null )
67          {
68              final Iterator<IndexingContext> ic = contexts.iterator();
69              final Iterator<IndexSearcher> is = searchers.iterator();
70  
71              while ( ic.hasNext() && is.hasNext() )
72              {
73                  ic.next().releaseIndexSearcher( is.next() );
74              }
75  
76              if ( ic.hasNext() || is.hasNext() )
77              {
78                  throw new IllegalStateException( "Context and IndexSearcher mismatch: " + contexts + " vs " + searchers );
79              }
80          }
81  
82          searchers = null;
83      }
84  
85      /**
86       * Watch out with this method, as it's use depends on (if you control it at all) was {@link #acquire()} method
87       * invoked at all or not. Returns {@code null} if not, otherwise the list of acquired searchers. Not thread safe.
88       * 
89       * @return
90       */
91      public synchronized List<IndexSearcher> getAcquiredSearchers()
92      {
93          return searchers;
94      }
95  }