StatsFn extends AppFunction { @Override public void run(Parameters p, PrintStream output) throws Exception { if (!p.containsKey("index")) { output.print(getHelpString()); return; } Retrieval r = RetrievalFactory.create(p); if (!p.containsKey("part") && !p.containsKey("field") && !p.containsKey("node")) { Set<String> available = r.getAvailableParts().getKeys(); if (available.contains("postings.krovetz")) { p.set("part", "postings.krovetz"); } else if (available.contains("postings.porter")) { p.set("part", "postings.porter"); } else if (available.contains("postings")) { p.set("part", "postings"); } else { output.print(getHelpString()); output.println("Could not determine default part."); return; } } int statItemCount = 0; output.println("{"); for (String part : (List<String>) p.getAsList("part")) { if (statItemCount > 0) { output.println(" ,"); } statItemCount += 1; output.println(" \"" + part + "\" : "); try { output.println(r.getIndexPartStatistics(part).toParameters().toPrettyString(" ")); } catch (IllegalArgumentException e) { System.err.println(e.toString()); } } for (String field : (List<String>) p.getAsList("field")) { if (statItemCount > 0) { output.println(" ,"); } statItemCount += 1; output.println(" \"" + field + "\" : "); Node n = StructuredQuery.parse(field); if(n.getOperator().equals("text")){ n.setOperator("lengths"); n.getNodeParameters().set("part", "lengths"); } try { output.println(r.getCollectionStatistics(n).toParameters().toPrettyString(" ")); } catch (IllegalArgumentException e) { System.err.println(e.toString()); } } for (String node : (List<String>) p.getAsList("node")) { if (statItemCount > 0) { output.println(" ,"); } statItemCount += 1; Node n = StructuredQuery.parse(node); n.getNodeParameters().set("queryType", "count"); n = r.transformQuery(n, Parameters.create()); output.println(" \"" + node + "\" : "); try { output.println(r.getNodeStatistics(n).toParameters().toPrettyString(" ")); } catch (IllegalArgumentException e) { System.err.println(e.toString()); } } output.println("}"); }  @Override String getName(); @Override String getHelpString(); @Override void run(Parameters p, PrintStream output);  }
@Test public void testSomeMethod() throws Exception { File input = FileUtility.createTemporary(); File index = FileUtility.createTemporaryDirectory(); try { makeIndex(input, index); ByteArrayOutputStream array = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(array); (new StatsFn()).run(new String[]{"stats", "--index=" + index.getAbsolutePath()}, printStream); Parameters results = Parameters.parseString(array.toString()); assert (results.containsKey("postings.krovetz")); array = new ByteArrayOutputStream(); printStream = new PrintStream(array); (new StatsFn()).run(new String[]{"stats", "--index=" + index.getAbsolutePath(), "--part+postings.porter", "--part+postings.krovetz", "--part+postings" }, printStream); results = Parameters.parseString(array.toString()); assertTrue(results.containsKey("postings.porter")); assertTrue (results.containsKey("postings.krovetz")); assertTrue(results.containsKey("postings")); array = new ByteArrayOutputStream(); printStream = new PrintStream(array); (new StatsFn()).run(new String[]{"stats", "--index=" + index.getAbsolutePath(), "--field+#lengths:document:part=lengths()", "--field+document" }, printStream); results = Parameters.parseString(array.toString()); assertTrue(results.containsKey("#lengths:document:part=lengths()")); assertTrue (results.containsKey("document")); array = new ByteArrayOutputStream(); printStream = new PrintStream(array); (new StatsFn()).run(new String[]{"stats", "--index=" + index.getAbsolutePath(), "--node+#counts:t1:part=postings()", "--node+#counts:t2:part=postings()", "--node+t3" }, printStream); results = Parameters.parseString(array.toString()); assertTrue (results.containsKey("#counts:t1:part=postings()")); assertTrue (results.containsKey("#counts:t2:part=postings()")); assertTrue (results.containsKey("t3")); array = new ByteArrayOutputStream(); printStream = new PrintStream(array); (new StatsFn()).run(new String[]{"stats", "--index=" + index.getAbsolutePath(), "--part+postings.porter", "--part+postings.krovetz", "--part+postings", "--node+#counts:t1:part=postings()", "--node+#counts:t2:part=postings()", "--field+#lengths:document:part=lengths()" }, printStream); results = Parameters.parseString(array.toString()); assertTrue (results.containsKey("postings.porter")); assertTrue (results.containsKey("postings.krovetz")); assertTrue (results.containsKey("postings")); assertTrue (results.containsKey("#lengths:document:part=lengths()")); assertTrue (results.containsKey("#counts:t1:part=postings()")); assertTrue (results.containsKey("#counts:t2:part=postings()")); } finally { input.delete(); FSUtil.deleteDirectory(index); } }