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 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 if ( ac != null && ac.getGav() != null )
56 {
57 Document d = ac.createDocument( context );
58
59 if ( d != null )
60 {
61 context.getIndexWriter().addDocument( d );
62
63 context.updateTimestamp();
64 }
65 }
66 }
67
68 public void update( IndexingContext context, ArtifactContext ac )
69 throws IOException
70 {
71 if ( ac != null && ac.getGav() != null )
72 {
73 Document d = ac.createDocument( context );
74
75 if ( d != null )
76 {
77 Document old = getOldDocument( context, ac );
78
79 if ( !equals( d, old ) )
80 {
81 IndexWriter w = context.getIndexWriter();
82
83 w.updateDocument( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ), d );
84
85 updateGroups( context, ac );
86
87 context.updateTimestamp();
88 }
89 }
90 }
91 }
92
93 public void remove( IndexingContext context, ArtifactContext ac )
94 throws IOException
95 {
96 if ( ac != null )
97 {
98 final String uinfo = ac.getArtifactInfo().getUinfo();
99
100
101 final Document doc = new Document();
102
103 doc.add( new Field( ArtifactInfo.DELETED, uinfo, Field.Store.YES, Field.Index.NO ) );
104 doc.add( new Field( ArtifactInfo.LAST_MODIFIED,
105 Long.toString( System.currentTimeMillis() ), Field.Store.YES, Field.Index.NO ) );
106
107 IndexWriter w = context.getIndexWriter();
108 w.addDocument( doc );
109 w.deleteDocuments( new Term( ArtifactInfo.UINFO, uinfo ) );
110 context.updateTimestamp();
111 }
112 }
113
114
115
116 private boolean equals( final Document d1, final Document d2 )
117 {
118 if ( d1 == null && d2 == null )
119 {
120 return true;
121 }
122 if ( d1 == null || d2 == null )
123 {
124 return false;
125 }
126
127 final Map<String, String> m1 = toMap( d1 );
128 final Map<String, String> m2 = toMap( d2 );
129
130 m1.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() );
131 m2.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() );
132
133 return m1.equals( m2 );
134 }
135
136 private Map<String, String> toMap( Document d )
137 {
138 final HashMap<String, String> result = new HashMap<String, String>();
139
140 for ( Object o : d.getFields() )
141 {
142 Fieldable f = (Fieldable) o;
143 if ( f.isStored() )
144 {
145 result.put( f.name(), f.stringValue() );
146 }
147 }
148
149 return result;
150 }
151
152 private Document getOldDocument( IndexingContext context, ArtifactContext ac )
153 {
154 try
155 {
156 IndexSearcher indexSearcher = context.getIndexSearcher();
157
158 TopDocs result =
159 indexSearcher.search( new TermQuery( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ) ),
160 2 );
161
162 if ( result.totalHits == 1 )
163 {
164 return indexSearcher.doc( result.scoreDocs[0].doc );
165 }
166 }
167 catch ( IOException e )
168 {
169 }
170 return null;
171 }
172
173 private void updateGroups( IndexingContext context, ArtifactContext ac )
174 throws IOException
175 {
176 String rootGroup = ac.getArtifactInfo().getRootGroup();
177 Set<String> rootGroups = context.getRootGroups();
178 if ( !rootGroups.contains( rootGroup ) )
179 {
180 rootGroups.add( rootGroup );
181 context.setRootGroups( rootGroups );
182 }
183
184 Set<String> allGroups = context.getAllGroups();
185 if ( !allGroups.contains( ac.getArtifactInfo().groupId ) )
186 {
187 allGroups.add( ac.getArtifactInfo().groupId );
188 context.setAllGroups( allGroups );
189 }
190 }
191 }