View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.lucene.document.Document;
30  import org.apache.lucene.document.StoredField;
31  import org.apache.lucene.index.IndexWriter;
32  import org.apache.lucene.index.IndexableField;
33  import org.apache.lucene.index.Term;
34  import org.apache.lucene.search.IndexSearcher;
35  import org.apache.lucene.search.TermQuery;
36  import org.apache.lucene.search.TopDocs;
37  import org.apache.maven.index.context.IndexingContext;
38  import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator;
39  
40  /**
41   * A default {@link IndexerEngine} implementation.
42   *
43   * @author Tamas Cservenak
44   */
45  @Singleton
46  @Named
47  public class DefaultIndexerEngine implements IndexerEngine {
48  
49      public void index(IndexingContext context, ArtifactContext ac) throws IOException {
50          // skip artifacts not obeying repository layout (whether m1 or m2)
51          if (ac != null && ac.getGav() != null) {
52              Document d = ac.createDocument(context);
53  
54              if (d != null) {
55                  context.getIndexWriter().addDocument(d);
56  
57                  context.updateTimestamp();
58              }
59          }
60      }
61  
62      public void update(IndexingContext context, ArtifactContext ac) throws IOException {
63          if (ac != null && ac.getGav() != null) {
64              Document d = ac.createDocument(context);
65  
66              if (d != null) {
67                  Document old = getOldDocument(context, ac);
68  
69                  if (!equals(d, old)) {
70                      IndexWriter w = context.getIndexWriter();
71  
72                      w.updateDocument(
73                              new Term(ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo()), d);
74  
75                      updateGroups(context, ac);
76  
77                      context.updateTimestamp();
78                  }
79              }
80          }
81      }
82  
83      public void remove(IndexingContext context, ArtifactContext ac) throws IOException {
84          if (ac != null) {
85              final String uinfo = ac.getArtifactInfo().getUinfo();
86  
87              // add artifact deletion marker
88              final Document doc = new Document();
89  
90              doc.add(new StoredField(ArtifactInfo.DELETED, uinfo));
91              doc.add(new StoredField(
92                      ArtifactInfo.LAST_MODIFIED, //
93                      Long.toString(System.currentTimeMillis())));
94  
95              IndexWriter w = context.getIndexWriter();
96              w.addDocument(doc);
97              w.deleteDocuments(new Term(ArtifactInfo.UINFO, uinfo));
98              context.updateTimestamp();
99          }
100     }
101 
102     // ==
103 
104     private boolean equals(final Document d1, final Document d2) {
105         if (d1 == null && d2 == null) {
106             return true;
107         }
108         if (d1 == null || d2 == null) {
109             return false;
110         }
111 
112         final Map<String, String> m1 = toMap(d1);
113         final Map<String, String> m2 = toMap(d2);
114 
115         m1.remove(MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey());
116         m2.remove(MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey());
117 
118         return m1.equals(m2);
119     }
120 
121     private Map<String, String> toMap(Document d) {
122         final HashMap<String, String> result = new HashMap<>();
123 
124         for (Object o : d.getFields()) {
125             IndexableField f = (IndexableField) o;
126             if (f.fieldType().stored()) {
127                 result.put(f.name(), f.stringValue());
128             }
129         }
130 
131         return result;
132     }
133 
134     private Document getOldDocument(IndexingContext context, ArtifactContext ac) {
135         try {
136             final IndexSearcher indexSearcher = context.acquireIndexSearcher();
137             try {
138                 TopDocs result = indexSearcher.search(
139                         new TermQuery(new Term(
140                                 ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo())),
141                         2);
142 
143                 if (result.totalHits.value == 1) {
144                     return indexSearcher.doc(result.scoreDocs[0].doc);
145                 }
146             } finally {
147                 context.releaseIndexSearcher(indexSearcher);
148             }
149         } catch (IOException e) {
150             // huh?
151             throw new IllegalStateException(e);
152         }
153         return null;
154     }
155 
156     private void updateGroups(IndexingContext context, ArtifactContext ac) throws IOException {
157         String rootGroup = ac.getArtifactInfo().getRootGroup();
158         Set<String> rootGroups = context.getRootGroups();
159         if (!rootGroups.contains(rootGroup)) {
160             rootGroups.add(rootGroup);
161             context.setRootGroups(rootGroups);
162         }
163 
164         Set<String> allGroups = context.getAllGroups();
165         if (!allGroups.contains(ac.getArtifactInfo().getGroupId())) {
166             allGroups.add(ac.getArtifactInfo().getGroupId());
167             context.setAllGroups(allGroups);
168         }
169     }
170 }