@Test public void degreeCounting() throws Exception { int numV = 200; int numE = generateRandomGraph(numV); clopen(); final TitanGraphComputer computer = graph.compute(); computer.resultMode(TitanGraphComputer.ResultMode.NONE); computer.workers(4); computer.program(new DegreeCounter()); computer.mapReduce(new DegreeMapper()); ComputerResult result = computer.submit().get(); System.out.println("Execution time (ms) ["+numV+"|"+numE+"]: " + result.memory().getRuntime()); assertTrue(result.memory().exists(DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numV,degrees.size()); int totalCount = 0; for (Map.Entry<Long,Integer> entry : degrees.entrySet()) { int degree = entry.getValue(); TitanVertex v = getV(tx, entry.getKey().longValue()); int count = v.value("uid"); assertEquals(count,degree); totalCount+= degree; } assertEquals(numV*(numV+1)/2,totalCount); assertEquals(1,result.memory().getIteration()); }
Map<Long,Integer> degrees = result.memory().get(OLAPTest.DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numVertices,degrees.size());
@Override public boolean terminate(final Memory memory) { final boolean voteToHalt = memory.<Boolean>get(VOTE_TO_HALT) || memory.getIteration() >= (this.distributeVote ? this.maxIterations + 1 : this.maxIterations); if (voteToHalt) { return true; } else { memory.set(VOTE_TO_HALT, true); return false; } }
@Override public void execute(Vertex vertex, Messenger messenger, Memory memory) { assertEquals(memory.getIteration() * 6, memory.<Integer>get("test").intValue()); memory.add("test", 1); }
@Test public void degreeCounting() throws Exception { int numV = 200; int numE = generateRandomGraph(numV); clopen(); final JanusGraphComputer computer = graph.compute(); computer.resultMode(JanusGraphComputer.ResultMode.NONE); computer.workers(4); computer.program(new DegreeCounter()); computer.mapReduce(new DegreeMapper()); ComputerResult result = computer.submit().get(); System.out.println("Execution time (ms) ["+numV+"|"+numE+"]: " + result.memory().getRuntime()); assertTrue(result.memory().exists(DegreeMapper.DEGREE_RESULT)); Map<Long,Integer> degrees = result.memory().get(DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numV,degrees.size()); int totalCount = 0; for (Map.Entry<Long,Integer> entry : degrees.entrySet()) { int degree = entry.getValue(); final JanusGraphVertex v = getV(tx, entry.getKey()); int count = v.value("uid"); assertEquals(count,degree); totalCount+= degree; } assertEquals(numV*(numV+1)/2,totalCount); assertEquals(1,result.memory().getIteration()); }
Map<Long,Integer> degrees = result.memory().get(OLAPTest.DegreeMapper.DEGREE_RESULT); assertNotNull(degrees); assertEquals(numVertices,degrees.size());
@Override public void workerIterationStart(final Memory memory) { assertEquals(memory.getIteration() * 6, memory.<Integer>get("test").intValue()); try { memory.add("test", memory.getIteration()); fail("Should throw an immutable memory exception"); } catch (IllegalStateException e) { assertEquals(Memory.Exceptions.memoryIsCurrentlyImmutable().getMessage(), e.getMessage()); } }
ComputerResult result = computer.submit().get(); Iterator<KeyValue<Long, Double>> ranks = result.memory().get(PageRankMapReduce.DEFAULT_MEMORY_KEY); assertNotNull(ranks); int vertexCounter = 0;
@Override public void workerIterationEnd(final Memory memory) { assertEquals(memory.getIteration() * 6, memory.<Integer>get("test").intValue()); try { memory.set("test", memory.getIteration()); fail("Should throw an immutable memory exception"); } catch (IllegalStateException e) { assertEquals(Memory.Exceptions.memoryIsCurrentlyImmutable().getMessage(), e.getMessage()); } }
ComputerResult result = computer.submit().get(); Iterator<KeyValue<Long, Double>> ranks = result.memory().get(PageRankMapReduce.DEFAULT_MEMORY_KEY); assertNotNull(ranks); int vertexCounter = 0;
result.memory().get(ShortestDistanceMapReduce.DEFAULT_MEMORY_KEY);
result.memory().get(ShortestDistanceMapReduce.DEFAULT_MEMORY_KEY);
/** * A helper method that generates a {@link Map} of the memory key/values. * * @return the map representation of the memory key/values */ public default Map<String, Object> asMap() { final Map<String, Object> map = keys().stream() .map(key -> Pair.with(key, get(key))) .collect(Collectors.toMap(Pair::getValue0, Pair::getValue1)); return Collections.unmodifiableMap(map); }
@Test @LoadGraphWith(MODERN) public void shouldSortMapOutput() throws Exception { final ComputerResult results = graphProvider.getGraphComputer(graph).mapReduce(new MapReduceBB()).submit().get(); final List<Integer> nameLengths = results.memory().get("nameLengths"); assertEquals(6, nameLengths.size()); for (int i = 1; i < nameLengths.size(); i++) { assertTrue(nameLengths.get(i) <= nameLengths.get(i - 1)); } }
@Test @LoadGraphWith(MODERN) public void shouldSortReduceOutput() throws Exception { final ComputerResult results = graphProvider.getGraphComputer(graph).mapReduce(new MapReduceB()).submit().get(); final List<Integer> nameLengths = results.memory().get("nameLengths"); assertEquals(6, nameLengths.size()); for (int i = 1; i < nameLengths.size(); i++) { assertTrue(nameLengths.get(i) <= nameLengths.get(i - 1)); } }
private void processMemorySideEffects(final Memory memory) { // update the traversal side-effects with the state of the memory after the OLAP job execution final TraversalSideEffects sideEffects = this.getTraversal().getSideEffects(); for (final String key : memory.keys()) { if (sideEffects.exists(key)) { // halted traversers should never be propagated through sideEffects assert !key.equals(TraversalVertexProgram.HALTED_TRAVERSERS); sideEffects.set(key, memory.get(key)); } } }
@Test @LoadGraphWith(MODERN) public void shouldFindShortestPathsWithStartVertexFilter() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().source(__.has("name", "marko")).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS) .filter(p -> p[0].equals("marko")).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldFindAllShortestPathsWithDefaultParameters() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS).map(helper::makePath).collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }
@Test @LoadGraphWith(MODERN) public void shouldSupportMultipleMapReduceJobs() throws Exception { final ComputerResult results = graphProvider.getGraphComputer(graph) .program(new VertexProgramH()) .mapReduce(new MapReduceH1()) .mapReduce(new MapReduceH2()).submit().get(); assertEquals(60, results.memory().<Integer>get("a").intValue()); assertEquals(1, results.memory().<Integer>get("b").intValue()); }
@Test @LoadGraphWith(MODERN) public void shouldFindAllShortestPathsWithEdgesIncluded() throws Exception { final ComputerResult result = graph.compute(graphProvider.getGraphComputer(graph).getClass()). program(ShortestPathVertexProgram.build().includeEdges(true).create(graph)).submit().get(); assertTrue(result.memory().exists(ShortestPathVertexProgram.SHORTEST_PATHS)); final List<Path> shortestPaths = result.memory().get(ShortestPathVertexProgram.SHORTEST_PATHS); final List<Path> expected = Arrays.stream(ALL_SHORTEST_PATHS).map(p -> helper.makePath(true, p)) .collect(Collectors.toList()); helper.checkResults(expected, shortestPaths); }