public Set<MavenArtifactInfo> search(int indexId, Query query, int maxResult) throws MavenServerIndexerException { try { IndexingContext index = getIndex(indexId); TopDocs docs = null; try { BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE); docs = index.getIndexSearcher().search(query, null, maxResult); } catch (BooleanQuery.TooManyClauses ignore) { // this exception occurs when too wide wildcard is used on too big data. } if (docs == null || docs.scoreDocs.length == 0) return Collections.emptySet(); Set<MavenArtifactInfo> result = new THashSet<MavenArtifactInfo>(); for (int i = 0; i < docs.scoreDocs.length; i++) { int docIndex = docs.scoreDocs[i].doc; Document doc = index.getIndexReader().document(docIndex); ArtifactInfo a = IndexUtils.constructArtifactInfo(doc, index); if (a == null) continue; a.repository = getRepositoryPathOrUrl(index); result.add(Maven2ModelConverter.convertArtifactInfo(a)); } return result; } catch (Exception e) { throw new MavenServerIndexerException(wrapException(e)); } }
public static String validateArtifactInfo(List<ArtifactInfo> artifactInfos) { for (ArtifactInfo artifactInfo : artifactInfos) { Artifact artifact = artifactInfo.getArtifact(); if (validateArtifact(artifact).equalsIgnoreCase(RepositoryServiceClientConstants.FAILURE)) { return RepositoryServiceClientConstants.FAILURE; } if (artifactInfo.getArtifactDetail() == null) { return RepositoryServiceClientConstants.FAILURE; } } return RepositoryServiceClientConstants.SUCCESS; }
@Test public void testWriteStoreRequest() throws IOException { final String base64EncodedData = "AAAAAgAgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAAIDkwMDA" + "wMDAwMDAwMDAwMDAwMDAwMDA4MDAwMDAwMDA1AAAAXgAAAAIAIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA" + "wMDAwACA5MDAwMDAwMDAwMDAwMDAwMDAwMDAwODAwMDAwMDAwNQAAAAEAA2tleQAAAAV2YWx1ZRf0zcZkYXRhZGF" + "0YQ=="; final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000"); final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005"); HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest( ArtifactInfo.builder() .addRuleKeys(ruleKey, ruleKey2) .setMetadata(ImmutableMap.of("key", "value")) .build(), new ByteSource() { @Override public InputStream openStream() throws IOException { return new ByteArrayInputStream("datadata".getBytes(Charsets.UTF_8)); } }); assertThat(storeRequest.getContentLength(), Matchers.is(178L)); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); StoreWriteResult writeResult = storeRequest.write(byteArrayOutputStream); assertThat( writeResult.getArtifactContentHashCode(), Matchers.equalTo(HashCode.fromString("2c0b14a4"))); assertThat(writeResult.getArtifactSizeBytes(), Matchers.is(8L)); byte[] expectedBytes = BaseEncoding.base64().decode(base64EncodedData); assertThat(byteArrayOutputStream.toByteArray(), Matchers.equalTo(expectedBytes)); }
@Test public void testStoreRequest() throws IOException { final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000"); final RuleKey ruleKey2 = new RuleKey("90000000000000000000008000000005"); final String data = "data"; ImmutableMap<String, String> metadata = ImmutableMap.of("metaKey", "metaValue"); HttpArtifactCacheBinaryProtocol.StoreRequest storeRequest = new HttpArtifactCacheBinaryProtocol.StoreRequest( ArtifactInfo.builder().addRuleKeys(ruleKey, ruleKey2).setMetadata(metadata).build(), new ByteSource() { @Override public InputStream openStream() throws IOException { return new ByteArrayInputStream(data.getBytes(Charsets.UTF_8)); } }); ByteArrayOutputStream storeRequestOutputStream = new ByteArrayOutputStream(); storeRequest.write(storeRequestOutputStream); ByteArrayOutputStream storeRequestPayloadStream = new ByteArrayOutputStream(); StoreResponseReadResult readStoreRequest = HttpArtifactCacheBinaryProtocol.readStoreRequest( new DataInputStream(new ByteArrayInputStream(storeRequestOutputStream.toByteArray())), storeRequestPayloadStream); assertThat(readStoreRequest.getRuleKeys(), Matchers.containsInAnyOrder(ruleKey, ruleKey2)); assertThat(readStoreRequest.getMetadata(), Matchers.equalTo(metadata)); assertThat( storeRequestPayloadStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8))); }
protected int searchFlat( AbstractSearchRequest req, Collection<ArtifactInfo> result, IndexingContext context, Query query, int from, int aiCount) throws IOException { Hits hits = context .getIndexSearcher() .search(query, new Sort(new SortField(ArtifactInfo.UINFO, SortField.STRING))); if (hits == null || hits.length() == 0) { return 0; } if (req.isHitLimited() && hits.length() > req.getResultHitLimit()) { return AbstractSearchResponse.LIMIT_EXCEEDED; } int hitCount = hits.length(); int start = 0; // from == FlatSearchRequest.UNDEFINED ? 0 : from; // we have to pack the results as long: a) we have found aiCount ones b) we depleted hits for (int i = start; i < hits.length(); i++) { Document doc = hits.doc(i); ArtifactInfo artifactInfo = IndexUtils.constructArtifactInfo(doc, context); if (artifactInfo != null) { artifactInfo.repository = context.getRepositoryId(); artifactInfo.context = context.getId(); result.add(artifactInfo); if (req.isHitLimited() && result.size() > req.getResultHitLimit()) { // we hit limit, back out now !! return AbstractSearchResponse.LIMIT_EXCEEDED; } } } return hitCount; }
public void testBrokenJar() throws Exception { Query q = nexusIndexer.constructQuery(MAVEN.ARTIFACT_ID, "brokenjar", SearchType.SCORED); FlatSearchRequest searchRequest = new FlatSearchRequest(q); FlatSearchResponse response = nexusIndexer.searchFlat(searchRequest); Set<ArtifactInfo> r = response.getResults(); assertEquals(r.toString(), 1, r.size()); ArtifactInfo ai = r.iterator().next(); assertEquals("brokenjar", ai.getGroupId()); assertEquals("brokenjar", ai.getArtifactId()); assertEquals("1.0", ai.getVersion()); assertEquals(null, ai.getClassNames()); }
public void testPlugin() throws Exception { // String term = "plugin"; // String term = "maven-core-it-plugin"; String term = "org.apache.maven.plugins"; // Query bq = new TermQuery(new Term(ArtifactInfo.GROUP_ID, "org.apache.maven.plugins")); // Query bq = new TermQuery(new Term(ArtifactInfo.ARTIFACT_ID, term)); Query bq = new PrefixQuery(new Term(ArtifactInfo.GROUP_ID, term)); // BooleanQuery bq = new BooleanQuery(); // bq.add(new PrefixQuery(new Term(ArtifactInfo.GROUP_ID, term + "*")), Occur.SHOULD); // bq.add(new PrefixQuery(new Term(ArtifactInfo.ARTIFACT_ID, term + "*")), Occur.SHOULD); TermQuery tq = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-plugin")); Query query = new FilteredQuery(tq, new QueryWrapperFilter(bq)); FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(query)); Collection<ArtifactInfo> r = response.getResults(); assertEquals(r.toString(), 1, r.size()); ArtifactInfo ai = r.iterator().next(); assertEquals("org.apache.maven.plugins", ai.getGroupId()); assertEquals("maven-core-it-plugin", ai.getArtifactId()); assertEquals("core-it", ai.getPrefix()); List<String> goals = ai.getGoals(); assertEquals(14, goals.size()); assertEquals("catch", goals.get(0)); assertEquals("fork", goals.get(1)); assertEquals("fork-goal", goals.get(2)); assertEquals("touch", goals.get(3)); assertEquals("setter-touch", goals.get(4)); assertEquals("generate-envar-properties", goals.get(5)); assertEquals("generate-properties", goals.get(6)); assertEquals("loadable", goals.get(7)); assertEquals("light-touch", goals.get(8)); assertEquals("package", goals.get(9)); assertEquals("reachable", goals.get(10)); assertEquals("runnable", goals.get(11)); assertEquals("throw", goals.get(12)); assertEquals("tricky-params", goals.get(13)); }
public void testMissingPom() throws Exception { Query q = nexusIndexer.constructQuery(MAVEN.ARTIFACT_ID, "missingpom", SearchType.SCORED); FlatSearchRequest searchRequest = new FlatSearchRequest(q); FlatSearchResponse response = nexusIndexer.searchFlat(searchRequest); Set<ArtifactInfo> r = response.getResults(); assertEquals(r.toString(), 1, r.size()); ArtifactInfo ai = r.iterator().next(); assertEquals("missingpom", ai.getGroupId()); assertEquals("missingpom", ai.getArtifactId()); assertEquals("1.0", ai.getVersion()); // See Nexus 2318. It should be null for a jar without classes assertNull(ai.getClassNames()); }
protected int searchGrouped( AbstractSearchRequest req, Map<String, ArtifactInfoGroup> result, Grouping grouping, IndexingContext context, Query query) throws IOException { Hits hits = context .getIndexSearcher() .search(query, new Sort(new SortField(ArtifactInfo.UINFO, SortField.STRING))); if (hits != null && hits.length() != 0) { int hitCount = hits.length(); for (int i = 0; i < hits.length(); i++) { ArtifactInfo artifactInfo = IndexUtils.constructArtifactInfo(hits.doc(i), context); if (artifactInfo != null) { artifactInfo.repository = context.getRepositoryId(); artifactInfo.context = context.getId(); if (!grouping.addArtifactInfo(result, artifactInfo)) { // fix the hitCount accordingly hitCount--; } } } if (req.isHitLimited() && hits.length() > req.getResultHitLimit()) { return AbstractSearchResponse.LIMIT_EXCEEDED; } return hitCount; } else { return 0; } }
public void testSearchFlat() throws Exception { Query q = nexusIndexer.constructQuery(MAVEN.GROUP_ID, "xstream", SearchType.SCORED); FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(q)); Collection<ArtifactInfo> r = response.getResults(); assertEquals(1, r.size()); List<ArtifactInfo> list = new ArrayList<ArtifactInfo>(r); assertEquals(1, list.size()); ArtifactInfo ai = list.get(0); assertEquals("xstream", ai.getGroupId()); assertEquals("xstream", ai.getArtifactId()); assertEquals("1.2.2", ai.getVersion()); assertEquals("jar", ai.getPackaging()); }
public void testIndexTimestamp() throws Exception { final File targetDir = File.createTempFile("testIndexTimestamp", "ut-tmp"); targetDir.delete(); targetDir.mkdirs(); final IndexPacker indexPacker = lookup(IndexPacker.class); final IndexSearcher indexSearcher = context.acquireIndexSearcher(); try { final IndexPackingRequest request = new IndexPackingRequest(context, indexSearcher.getIndexReader(), targetDir); indexPacker.packIndex(request); } finally { context.releaseIndexSearcher(indexSearcher); } Thread.sleep(1000L); File newIndex = new File(getBasedir(), "target/test-new"); Directory newIndexDir = FSDirectory.open(newIndex); IndexingContext newContext = nexusIndexer.addIndexingContext( "test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS); final IndexUpdater indexUpdater = lookup(IndexUpdater.class); indexUpdater.fetchAndUpdateIndex( new IndexUpdateRequest(newContext, new DefaultIndexUpdater.FileFetcher(targetDir))); assertEquals(context.getTimestamp().getTime(), newContext.getTimestamp().getTime()); assertEquals(context.getTimestamp(), newContext.getTimestamp()); // make sure context has the same artifacts Query query = nexusIndexer.constructQuery(MAVEN.GROUP_ID, "qdox", SearchType.SCORED); FlatSearchRequest request = new FlatSearchRequest(query, newContext); FlatSearchResponse response = nexusIndexer.searchFlat(request); Collection<ArtifactInfo> r = response.getResults(); System.out.println(r); assertEquals(2, r.size()); List<ArtifactInfo> list = new ArrayList<ArtifactInfo>(r); assertEquals(2, list.size()); ArtifactInfo ai = list.get(0); assertEquals("1.6.1", ai.getVersion()); ai = list.get(1); assertEquals("1.5", ai.getVersion()); assertEquals("test", ai.getRepository()); Date timestamp = newContext.getTimestamp(); newContext.close(false); newIndexDir = FSDirectory.open(newIndex); newContext = nexusIndexer.addIndexingContext( "test-new", "test", null, newIndexDir, null, null, DEFAULT_CREATORS); indexUpdater.fetchAndUpdateIndex( new IndexUpdateRequest(newContext, new DefaultIndexUpdater.FileFetcher(targetDir))); assertEquals(timestamp, newContext.getTimestamp()); newContext.close(true); assertFalse(new File(newIndex, "timestamp").exists()); }
public void testSearchArchetypes() throws Exception { // TermQuery tq = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype")); // BooleanQuery bq = new BooleanQuery(); // bq.add(new WildcardQuery(new Term(ArtifactInfo.GROUP_ID, term + "*")), Occur.SHOULD); // bq.add(new WildcardQuery(new Term(ArtifactInfo.ARTIFACT_ID, term + "*")), Occur.SHOULD); // FilteredQuery query = new FilteredQuery(tq, new QueryWrapperFilter(bq)); Query q = new TermQuery(new Term(ArtifactInfo.PACKAGING, "maven-archetype")); FlatSearchResponse response = nexusIndexer.searchFlat(new FlatSearchRequest(q)); Collection<ArtifactInfo> r = response.getResults(); assertEquals(4, r.size()); Iterator<ArtifactInfo> it = r.iterator(); { ArtifactInfo ai = it.next(); assertEquals("org.apache.directory.server", ai.getGroupId()); assertEquals("apacheds-schema-archetype", ai.getArtifactId()); assertEquals("1.0.2", ai.getVersion()); } { ArtifactInfo ai = it.next(); assertEquals("org.apache.servicemix.tooling", ai.getGroupId()); assertEquals("servicemix-service-engine", ai.getArtifactId()); assertEquals("3.1", ai.getVersion()); } { ArtifactInfo ai = it.next(); assertEquals("org.terracotta.maven.archetypes", ai.getGroupId()); assertEquals("pojo-archetype", ai.getArtifactId()); assertEquals("1.0.3", ai.getVersion()); } { ArtifactInfo ai = it.next(); assertEquals("proptest", ai.getGroupId()); assertEquals("proptest-archetype", ai.getArtifactId()); assertEquals("1.0", ai.getVersion()); } }