1 | |
package org.apache.maven.index; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
import java.util.Set; |
26 | |
|
27 | |
import org.apache.lucene.document.Document; |
28 | |
import org.apache.lucene.document.Field; |
29 | |
import org.apache.lucene.document.Fieldable; |
30 | |
import org.apache.lucene.index.IndexWriter; |
31 | |
import org.apache.lucene.index.Term; |
32 | |
import org.apache.lucene.search.IndexSearcher; |
33 | |
import org.apache.lucene.search.TermQuery; |
34 | |
import org.apache.lucene.search.TopDocs; |
35 | |
import org.apache.maven.index.context.IndexingContext; |
36 | |
import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator; |
37 | |
import org.codehaus.plexus.component.annotations.Component; |
38 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
@Component( role = IndexerEngine.class ) |
46 | 208 | public class DefaultIndexerEngine |
47 | |
extends AbstractLogEnabled |
48 | |
implements IndexerEngine |
49 | |
{ |
50 | |
|
51 | |
public void index( IndexingContext context, ArtifactContext ac ) |
52 | |
throws IOException |
53 | |
{ |
54 | |
|
55 | 5989 | if ( ac != null && ac.getGav() != null ) |
56 | |
{ |
57 | 5989 | Document d = ac.createDocument( context ); |
58 | |
|
59 | 5989 | if ( d != null ) |
60 | |
{ |
61 | 5989 | context.getIndexWriter().addDocument( d ); |
62 | |
|
63 | 5989 | context.updateTimestamp(); |
64 | |
} |
65 | |
} |
66 | 5989 | } |
67 | |
|
68 | |
public void update( IndexingContext context, ArtifactContext ac ) |
69 | |
throws IOException |
70 | |
{ |
71 | 5586 | if ( ac != null && ac.getGav() != null ) |
72 | |
{ |
73 | 5586 | Document d = ac.createDocument( context ); |
74 | |
|
75 | 5586 | if ( d != null ) |
76 | |
{ |
77 | 5586 | Document old = getOldDocument( context, ac ); |
78 | |
|
79 | 5586 | if ( !equals( d, old ) ) |
80 | |
{ |
81 | 5584 | IndexWriter w = context.getIndexWriter(); |
82 | |
|
83 | 5584 | w.updateDocument( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ), d ); |
84 | |
|
85 | 5584 | updateGroups( context, ac ); |
86 | |
|
87 | 5584 | context.updateTimestamp(); |
88 | |
} |
89 | |
} |
90 | |
} |
91 | 5586 | } |
92 | |
|
93 | |
public void remove( IndexingContext context, ArtifactContext ac ) |
94 | |
throws IOException |
95 | |
{ |
96 | 2 | if ( ac != null ) |
97 | |
{ |
98 | 2 | final String uinfo = ac.getArtifactInfo().getUinfo(); |
99 | |
|
100 | |
|
101 | 2 | final Document doc = new Document(); |
102 | |
|
103 | 2 | doc.add( new Field( ArtifactInfo.DELETED, uinfo, Field.Store.YES, Field.Index.NO ) ); |
104 | 2 | doc.add( new Field( ArtifactInfo.LAST_MODIFIED, |
105 | |
Long.toString( System.currentTimeMillis() ), Field.Store.YES, Field.Index.NO ) ); |
106 | |
|
107 | 2 | IndexWriter w = context.getIndexWriter(); |
108 | 2 | w.addDocument( doc ); |
109 | 2 | w.deleteDocuments( new Term( ArtifactInfo.UINFO, uinfo ) ); |
110 | 2 | context.updateTimestamp(); |
111 | |
} |
112 | 2 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
private boolean equals( final Document d1, final Document d2 ) |
117 | |
{ |
118 | 5586 | if ( d1 == null && d2 == null ) |
119 | |
{ |
120 | 0 | return true; |
121 | |
} |
122 | 5586 | if ( d1 == null || d2 == null ) |
123 | |
{ |
124 | 5583 | return false; |
125 | |
} |
126 | |
|
127 | 3 | final Map<String, String> m1 = toMap( d1 ); |
128 | 3 | final Map<String, String> m2 = toMap( d2 ); |
129 | |
|
130 | 3 | m1.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() ); |
131 | 3 | m2.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() ); |
132 | |
|
133 | 3 | return m1.equals( m2 ); |
134 | |
} |
135 | |
|
136 | |
private Map<String, String> toMap( Document d ) |
137 | |
{ |
138 | 6 | final HashMap<String, String> result = new HashMap<String, String>(); |
139 | |
|
140 | 6 | for ( Object o : d.getFields() ) |
141 | |
{ |
142 | 55 | Fieldable f = (Fieldable) o; |
143 | 55 | if ( f.isStored() ) |
144 | |
{ |
145 | 34 | result.put( f.name(), f.stringValue() ); |
146 | |
} |
147 | 55 | } |
148 | |
|
149 | 6 | return result; |
150 | |
} |
151 | |
|
152 | |
private Document getOldDocument( IndexingContext context, ArtifactContext ac ) |
153 | |
{ |
154 | |
try |
155 | |
{ |
156 | 5586 | IndexSearcher indexSearcher = context.getIndexSearcher(); |
157 | |
|
158 | 5586 | TopDocs result = |
159 | |
indexSearcher.search( new TermQuery( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ) ), |
160 | |
2 ); |
161 | |
|
162 | 5586 | if ( result.totalHits == 1 ) |
163 | |
{ |
164 | 3 | return indexSearcher.doc( result.scoreDocs[0].doc ); |
165 | |
} |
166 | |
} |
167 | 0 | catch ( IOException e ) |
168 | |
{ |
169 | 5583 | } |
170 | 5583 | return null; |
171 | |
} |
172 | |
|
173 | |
private void updateGroups( IndexingContext context, ArtifactContext ac ) |
174 | |
throws IOException |
175 | |
{ |
176 | 5584 | String rootGroup = ac.getArtifactInfo().getRootGroup(); |
177 | 5584 | Set<String> rootGroups = context.getRootGroups(); |
178 | 5584 | if ( !rootGroups.contains( rootGroup ) ) |
179 | |
{ |
180 | 24 | rootGroups.add( rootGroup ); |
181 | 24 | context.setRootGroups( rootGroups ); |
182 | |
} |
183 | |
|
184 | 5584 | Set<String> allGroups = context.getAllGroups(); |
185 | 5584 | if ( !allGroups.contains( ac.getArtifactInfo().groupId ) ) |
186 | |
{ |
187 | 28 | allGroups.add( ac.getArtifactInfo().groupId ); |
188 | 28 | context.setAllGroups( allGroups ); |
189 | |
} |
190 | 5584 | } |
191 | |
} |