Exemplo n.º 1
0
  @Override
  public Iterator<Edge> edges(final Object... edgeIds) {
    try {
      if (0 == edgeIds.length) {
        return new HadoopEdgeIterator(this);
      } else {
        // base the conversion function on the first item in the id list as the expectation is that
        // these
        // id values will be a uniform list
        if (edgeIds[0] instanceof Edge) {
          // based on the first item assume all Edges in the argument list
          if (!Stream.of(edgeIds).allMatch(id -> id instanceof Edge))
            throw Graph.Exceptions.idArgsMustBeEitherIdOrElement();

          // no need to get the vertices again, so just flip it back - some implementation may want
          // to treat this
          // as a refresh operation. that's not necessary for hadoopgraph.
          return Stream.of(edgeIds).map(id -> (Edge) id).iterator();
        } else {
          final Class<?> firstClass = edgeIds[0].getClass();
          if (!Stream.of(edgeIds).map(Object::getClass).allMatch(firstClass::equals))
            throw Graph.Exceptions
                .idArgsMustBeEitherIdOrElement(); // todo: change exception to be ids of the same
                                                  // type
          return IteratorUtils.filter(
              new HadoopEdgeIterator(this), vertex -> ElementHelper.idExists(vertex.id(), edgeIds));
        }
      }
    } catch (final IOException e) {
      throw new IllegalStateException(e.getMessage(), e);
    }
  }
Exemplo n.º 2
0
 /**
  * gets the requested values from the Has step. If it's a single value, wrap it in an array,
  * otherwise return the array
  */
 private Optional<Pair<String, Iterator<Object>>> getValuePair(HasContainer c) {
   Iterator<Object> values;
   if (c.getPredicate().getBiPredicate() == Contains.within)
     values = ((Iterable<Object>) c.getValue()).iterator();
   else values = IteratorUtils.of(c.getValue());
   return Optional.of(new Pair<>(c.getKey(), values));
 }
 @Test
 @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
 public void shouldGracefullyHandleBadGremlinHadoopLibs() throws Exception {
   System.setProperty(Constants.HADOOP_GREMLIN_LIBS, "test/" + UUID.randomUUID());
   this.graph
       .configuration()
       .setProperty(Constants.GREMLIN_HADOOP_JARS_IN_DISTRIBUTED_CACHE, true);
   this.console.addBinding("graph", this.graph);
   this.console.addBinding("g", this.g);
   this.remote.connect(Arrays.asList("graph", "g"));
   Traversal<?, ?> traversal = (Traversal<?, ?>) this.remote.submit(Arrays.asList("g.V()"));
   assertEquals(6, IteratorUtils.count(traversal));
   assertNotNull(this.console.getBindings().get(RemoteAcceptor.RESULT));
 }
 /**
  * Determine if the traversal has any of the supplied steps of an assignable class in the current
  * {@link Traversal} and its child traversals.
  *
  * @param stepClasses the step classes to look for
  * @param traversal the traversal in which to look for the given step classes
  * @return <code>true</code> if any step in the given traversal (and its child traversals) is an
  *     instance of a class provided in <code>stepClasses</code>, otherwise <code>false</code>.
  */
 public static boolean hasStepOfAssignableClassRecursively(
     final Collection<Class> stepClasses, final Traversal.Admin<?, ?> traversal) {
   if (stepClasses.size() == 1)
     return hasStepOfAssignableClassRecursively(stepClasses.iterator().next(), traversal);
   for (final Step<?, ?> step : traversal.getSteps()) {
     if (IteratorUtils.anyMatch(
         stepClasses.iterator(), stepClass -> stepClass.isAssignableFrom(step.getClass()))) {
       return true;
     }
     if (step instanceof TraversalParent) {
       for (final Traversal.Admin<?, ?> globalChild :
           ((TraversalParent) step).getGlobalChildren()) {
         if (hasStepOfAssignableClassRecursively(stepClasses, globalChild)) return true;
       }
     }
   }
   return false;
 }
  public static void validateVertexEquality(
      final Vertex originalVertex, final Vertex otherVertex, boolean testEdges) {
    assertEquals(originalVertex, otherVertex);
    assertEquals(otherVertex, originalVertex);
    assertEquals(originalVertex.id(), otherVertex.id());
    assertEquals(originalVertex.label(), otherVertex.label());
    assertEquals(originalVertex.keys().size(), otherVertex.keys().size());
    for (final String key : originalVertex.keys()) {
      final List<VertexProperty<Object>> originalVertexProperties =
          IteratorUtils.list(originalVertex.properties(key));
      final List<VertexProperty<Object>> otherVertexProperties =
          IteratorUtils.list(otherVertex.properties(key));
      assertEquals(originalVertexProperties.size(), otherVertexProperties.size());
      for (VertexProperty<Object> originalVertexProperty : originalVertexProperties) {
        final VertexProperty<Object> otherVertexProperty =
            otherVertexProperties
                .parallelStream()
                .filter(vp -> vp.equals(originalVertexProperty))
                .findAny()
                .get();
        validateVertexPropertyEquality(originalVertexProperty, otherVertexProperty);
      }
    }
    if (testEdges) {
      Iterator<Edge> originalEdges =
          IteratorUtils.list(originalVertex.edges(Direction.OUT), Comparators.ELEMENT_COMPARATOR)
              .iterator();
      Iterator<Edge> otherEdges =
          IteratorUtils.list(otherVertex.edges(Direction.OUT), Comparators.ELEMENT_COMPARATOR)
              .iterator();
      while (originalEdges.hasNext()) {
        validateEdgeEquality(originalEdges.next(), otherEdges.next());
      }
      assertFalse(otherEdges.hasNext());

      originalEdges =
          IteratorUtils.list(originalVertex.edges(Direction.IN), Comparators.ELEMENT_COMPARATOR)
              .iterator();
      otherEdges =
          IteratorUtils.list(otherVertex.edges(Direction.IN), Comparators.ELEMENT_COMPARATOR)
              .iterator();
      while (originalEdges.hasNext()) {
        validateEdgeEquality(originalEdges.next(), otherEdges.next());
      }
      assertFalse(otherEdges.hasNext());
    }
  }
 @Test
 @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
 public void shouldSupportHDFSMethods() throws Exception {
   ////////////////
   List<String> ls = (List<String>) this.console.eval("hdfs.ls()");
   for (final String line : ls) {
     assertTrue(
         line.startsWith("-")
             || line.startsWith("r")
             || line.startsWith("w")
             || line.startsWith("x"));
     assertEquals(" ", line.substring(9, 10));
   }
   ls = (List<String>) this.console.eval("local.ls()");
   for (final String line : ls) {
     assertTrue(
         line.startsWith("-")
             || line.startsWith("r")
             || line.startsWith("w")
             || line.startsWith("x"));
     assertEquals(" ", line.substring(9, 10));
   }
   ////////////////
   this.console.eval(
       "hdfs.copyFromLocal('"
           + HadoopGraphProvider.PATHS.get("tinkerpop-classic.txt")
           + "', 'target/tinkerpop-classic.txt')");
   assertTrue((Boolean) this.console.eval("hdfs.exists('target/tinkerpop-classic.txt')"));
   ////////////////
   List<String> head =
       IteratorUtils.asList(this.console.eval("hdfs.head('target/tinkerpop-classic.txt')"));
   assertEquals(6, head.size());
   for (final String line : head) {
     assertEquals(":", line.substring(1, 2));
     assertTrue(Integer.valueOf(line.substring(0, 1)) <= 6);
   }
   head = IteratorUtils.asList(this.console.eval("hdfs.head('target/tinkerpop-classic.txt',3)"));
   assertEquals(3, head.size());
   for (final String line : head) {
     assertEquals(":", line.substring(1, 2));
     assertTrue(Integer.valueOf(line.substring(0, 1)) <= 3);
   }
   ////////////////
   this.console.eval("hdfs.rm('target/tinkerpop-classic.txt')");
   assertFalse((Boolean) this.console.eval("hdfs.exists('target/tinkerpop-classic.txt')"));
   ////////////////
   this.console.addBinding("graph", this.graph);
   this.console.addBinding("g", this.g);
   this.remote.connect(Arrays.asList("graph", "g"));
   Traversal<Vertex, String> traversal =
       (Traversal<Vertex, String>)
           this.remote.submit(
               Arrays.asList(
                   "g.V().hasLabel('person').group('m').by('age').by('name').out('knows').out('created').values('name')"));
   AbstractGremlinProcessTest.checkResults(Arrays.asList("ripple", "lop"), traversal);
   assertTrue((Boolean) this.console.eval("hdfs.exists('target/test-output/m')"));
   assertTrue(
       (Boolean)
           this.console.eval(
               "hdfs.exists('target/test-output/" + TraverserMapReduce.TRAVERSERS + "')"));
   final List<KeyValue<Integer, Collection<String>>> mList =
       IteratorUtils.asList(this.console.eval("hdfs.head('target/test-output/m',ObjectWritable)"));
   assertEquals(4, mList.size());
   mList.forEach(
       keyValue -> {
         if (keyValue.getKey().equals(29)) assertTrue(keyValue.getValue().contains("marko"));
         else if (keyValue.getKey().equals(35)) assertTrue(keyValue.getValue().contains("peter"));
         else if (keyValue.getKey().equals(32)) assertTrue(keyValue.getValue().contains("josh"));
         else if (keyValue.getKey().equals(27)) assertTrue(keyValue.getValue().contains("vadas"));
         else throw new IllegalStateException("The provided key/value is unknown: " + keyValue);
       });
   final List<KeyValue<MapReduce.NullObject, Traverser<String>>> traversersList =
       IteratorUtils.asList(
           this.console.eval(
               "hdfs.head('target/test-output/"
                   + TraverserMapReduce.TRAVERSERS
                   + "',ObjectWritable)"));
   assertEquals(2, traversersList.size());
   traversersList.forEach(
       keyValue -> {
         assertEquals(MapReduce.NullObject.instance(), keyValue.getKey());
         final String name = keyValue.getValue().get();
         assertTrue(name.equals("ripple") || name.equals("lop"));
       });
   ////////////////
 }
Exemplo n.º 7
0
 @Override
 public Iterator<M> receiveMessages() {
   return IteratorUtils.removeOnNext(this.incomingMessages.iterator());
 }