org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AggregateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_hasLabelXpersonX_aggregateXxX_byXageX_capXxX_asXyX_selectXyX() {
    final Traversal<Vertex, Collection<Integer>> traversal = get_g_V_hasLabelXpersonX_aggregateXxX_byXageX_capXxX_asXyX_selectXyX();
    final Collection<Integer> ages = traversal.next();

    // in 3.3.x a BulkSet will coerce to List under GraphSON
    assumeThat(ages instanceof BulkSet, is(true));
    assertEquals(4, ages.size());
    assertTrue(ages.contains(29));
    assertTrue(ages.contains(27));
    assertTrue(ages.contains(32));
    assertTrue(ages.contains(35));
    final BulkSet<Integer> bulkSet = new BulkSet<>();
    bulkSet.add(29);
    bulkSet.add(27);
    bulkSet.add(32);
    bulkSet.add(35);
    assertEquals(bulkSet, ages); // ensure bulk set equality
    assertFalse(traversal.hasNext());
}
 
Example #2
Source File: BulkSetTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHaveCorrectBulkCounts() {
    final BulkSet<String> set = new BulkSet<>();
    set.add("marko");
    set.add("matthias");
    set.add("marko", 7);
    set.add("stephen");
    set.add("stephen");
    assertEquals(8, set.get("marko"));
    assertEquals(1, set.get("matthias"));
    assertEquals(2, set.get("stephen"));
    final Iterator<String> iterator = set.iterator();
    for (int i = 0; i < 11; i++) {
        if (i < 8)
            assertEquals("marko", iterator.next());
        else if (i < 9)
            assertEquals("matthias", iterator.next());
        else
            assertEquals("stephen", iterator.next());
    }
    assertEquals(11, set.size());
}
 
Example #3
Source File: BulkSetTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHaveProperHashAndEquality() {
    final BulkSet<String> a = new BulkSet<>();
    final BulkSet<String> b = new BulkSet<>();
    a.add("stephen", 12);
    a.add("marko", 32);
    a.add("daniel", 74);
    b.add("stephen", 12);
    b.add("marko", 32);
    b.add("daniel", 74);
    assertEquals(a, b);
    assertTrue(a.equals(b));
    assertEquals(a.hashCode(), b.hashCode());
    assertTrue(a.hashCode() == b.hashCode());
    assertEquals(12, a.get("stephen"));
    assertEquals(12, b.get("stephen"));
    a.add("matthias", 99);
    assertFalse(a.equals(b));
    assertFalse(a.hashCode() == b.hashCode());
    assertNotEquals(a.hashCode(), b.hashCode());
}
 
Example #4
Source File: ObjectQueryTest.java    From gremlin-ogm with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("cast")
public void testQueryBySubTraversals() {
  when(traversal.hasLabel(anyString())).thenReturn(traversal);
  when(traversal.has(anyString(), (Object) any())).thenReturn(traversal);
  BulkSet<Vertex> bulkSet = BulkSetSupplier.<Vertex>instance().get();
  bulkSet.add(vertex);
  when(traversal.toBulkSet()).thenReturn(bulkSet);

  List<Person> actuals = query
      .by(HasKeys.of(marko, PrimaryKey.class))
      .list(Person.class);

  assertEquals(Arrays.asList(marko), actuals);
}
 
Example #5
Source File: CoreTraversalTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldFilterOnIterate() {
    final Traversal<Vertex,String> traversal = g.V().out().out().<String>values("name").aggregate("x").iterate();
    assertFalse(traversal.hasNext());
    assertEquals(2, traversal.asAdmin().getSideEffects().<BulkSet>get("x").size());
    assertTrue(traversal.asAdmin().getSideEffects().<BulkSet>get("x").contains("ripple"));
    assertTrue(traversal.asAdmin().getSideEffects().<BulkSet>get("x").contains("lop"));
    assertEquals(Traversal.Symbols.none, traversal.asAdmin().getBytecode().getStepInstructions().get(traversal.asAdmin().getBytecode().getStepInstructions().size()-1).getOperator());
}
 
Example #6
Source File: ComplexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.GRATEFUL)
public void classicRecommendation() {
    final Traversal<Vertex, String> traversal = getClassicRecommendation();
    printTraversalForm(traversal);
    checkResults(Arrays.asList("LET IT GROW", "UNCLE JOHNS BAND", "I KNOW YOU RIDER", "SHIP OF FOOLS", "GOOD LOVING"), traversal);
    final List<Map<String, Object>> list = new ArrayList<>(traversal.asAdmin().getSideEffects().<BulkSet<Map<String, Object>>>get("m"));
    assertEquals(5, list.size());
    assertFalse(traversal.hasNext());
    assertEquals("LET IT GROW", list.get(0).get("x"));
    assertEquals(276, list.get(0).get("y"));
    assertEquals(21L, list.get(0).get("z"));
    //
    assertEquals("UNCLE JOHNS BAND", list.get(1).get("x"));
    assertEquals(332, list.get(1).get("y"));
    assertEquals(20L, list.get(1).get("z"));
    //
    assertEquals("I KNOW YOU RIDER", list.get(2).get("x"));
    assertEquals(550, list.get(2).get("y"));
    assertEquals(20L, list.get(2).get("z"));
    //
    assertEquals("SHIP OF FOOLS", list.get(3).get("x"));
    assertEquals(225, list.get(3).get("y"));
    assertEquals(18L, list.get(3).get("z"));
    //
    assertEquals("GOOD LOVING", list.get(4).get("x"));
    assertEquals(428, list.get(4).get("y"));
    assertEquals(18L, list.get(4).get("z"));
    //
    checkSideEffects(traversal.asAdmin().getSideEffects(), "m", BulkSet.class, "stash", BulkSet.class);

}
 
Example #7
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void checkSideEffects() {
    assertEquals(0, this.haltedTraversers.size());
    assertTrue(this.haltedTraversers.isEmpty());
    final TraversalSideEffects sideEffects = this.traversal.get().getSideEffects();
    assertTrue(sideEffects instanceof MemoryTraversalSideEffects);
    assertEquals(1, sideEffects.keys().size());
    assertFalse(sideEffects.exists(TraversalVertexProgram.HALTED_TRAVERSERS));
    assertTrue(sideEffects.exists("x"));
    final BulkSet<String> bulkSet = sideEffects.get("x");
    assertEquals(4, bulkSet.size());
    assertEquals(4, bulkSet.get("java"));
}
 
Example #8
Source File: ProgramTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void g_V_outXcreatedX_aggregateXxX_byXlangX_groupCount_programXTestProgramX_asXaX_selectXa_xX() {
    final Traversal<Vertex, Map<String, Object>> traversal = get_g_V_outXcreatedX_aggregateXxX_byXlangX_groupCount_programXTestProgramX_asXaX_selectXa_xX();
    final List<Map<String, Object>> results = traversal.toList();
    assertFalse(traversal.hasNext());
    assertEquals(6, results.size());
    final BulkSet<String> bulkSet = new BulkSet<>();
    bulkSet.add("java", 4);
    for (int i = 0; i < 4; i++) {
        assertEquals(bulkSet, results.get(i).get("x"));
    }
    final Set<String> strings = new HashSet<>();
    strings.add((String) results.get(0).get("a"));
    strings.add((String) results.get(1).get("a"));
    strings.add((String) results.get(2).get("a"));
    strings.add((String) results.get(3).get("a"));
    strings.add((String) results.get(4).get("a"));
    strings.add((String) results.get(5).get("a"));
    assertEquals(6, strings.size());
    assertTrue(strings.contains("hello"));
    assertTrue(strings.contains("gremlin"));
    assertTrue(strings.contains("lop"));
    assertTrue(strings.contains("ripple"));
    assertTrue(strings.contains("marko-is-my-name"));
    assertTrue(strings.contains("the-v-o-double-g"));
}
 
Example #9
Source File: BulkSetSupplierTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSupplyNewBulkSetOnEachInvocation() {
    final BulkSet<Object> l1 = BulkSetSupplier.instance().get();
    final BulkSet<Object> l2 = BulkSetSupplier.instance().get();
    final BulkSet<Object> l3 = BulkSetSupplier.instance().get();

    assertNotSame(l1, l2);
    assertNotSame(l1, l3);
    assertNotSame(l2, l3);
}
 
Example #10
Source File: BulkSetTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHaveProperCountAndNotOutOfMemoryException() {
    final Set<Boolean> list = new BulkSet<>();
    final Random random = new Random();
    for (int i = 0; i < 10000000; i++) {
        list.add(random.nextBoolean());
    }
    assertEquals(10000000, list.size());
}
 
Example #11
Source File: GraphSONMapperEmbeddedTypeTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleBulkSet() throws Exception {
    // only supported on V3
    assumeThat(version, not(anyOf(startsWith("v1"), startsWith("v2"))));

    final BulkSet<String> bs = new BulkSet<>();
    bs.add("test1", 1);
    bs.add("test2", 2);
    bs.add("test3", 3);

    assertEquals(bs, serializeDeserialize(mapper, bs, BulkSet.class));
}
 
Example #12
Source File: TraversalHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static <S> void addToCollection(final Collection<S> collection, final S s, final long bulk) {
    if (collection instanceof BulkSet) {
        ((BulkSet<S>) collection).add(s, bulk);
    } else if (collection instanceof Set) {
        collection.add(s);
    } else {
        for (long i = 0; i < bulk; i++) {
            collection.add(s);
        }
    }
}
 
Example #13
Source File: AggregateGlobalStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void processAllStarts() {
    if (this.starts.hasNext()) {
        final BulkSet<Object> bulkSet = new BulkSet<>();
        while (this.starts.hasNext()) {
            final Traverser.Admin<S> traverser = this.starts.next();
            bulkSet.add(TraversalUtil.applyNullable(traverser, this.aggregateTraversal), traverser.bulk());
            traverser.setStepId(this.getNextStep().getId()); // when barrier is reloaded, the traversers should be at the next step
            this.barrier.add(traverser);
        }
        this.getTraversal().getSideEffects().add(this.sideEffectKey, bulkSet);
    }
}
 
Example #14
Source File: BulkSetSerializer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeValue(final BulkSet value, final Buffer buffer, final GraphBinaryWriter context) throws IOException {
    final Map<Object,Long> raw = value.asBulk();
    buffer.writeInt(raw.size());

    for (Object key : raw.keySet()) {
        context.write(key, buffer);
        buffer.writeLong(value.get(key));
    }
}
 
Example #15
Source File: BulkSetSerializer.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected BulkSet readValue(final Buffer buffer, final GraphBinaryReader context) throws IOException {
    final int length = buffer.readInt();

    final BulkSet result = new BulkSet();
    for (int i = 0; i < length; i++) {
        result.add(context.read(buffer), buffer.readLong());
    }

    return result;
}
 
Example #16
Source File: TraversalSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public BulkSet deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {

    final BulkSet<Object> bulkSet = new BulkSet<>();

    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
        final Object key = deserializationContext.readValue(jsonParser, Object.class);
        jsonParser.nextToken();
        final Long val = deserializationContext.readValue(jsonParser, Long.class);
        bulkSet.add(key, val);
    }

    return bulkSet;
}
 
Example #17
Source File: TraversalSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final BulkSet bulkSet, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider)
        throws IOException {
    jsonGenerator.writeStartArray();
    for (Map.Entry entry : (Set<Map.Entry>) bulkSet.asBulk().entrySet()) {
        jsonGenerator.writeObject(entry.getKey());
        jsonGenerator.writeObject(entry.getValue());
    }
    jsonGenerator.writeEndArray();
}
 
Example #18
Source File: TypeSerializerFailureTests.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "Value={0}")
public static Collection input() {
    final Bytecode.Binding b = new Bytecode.Binding(null, "b");

    final ReferenceVertex vertex = new ReferenceVertex("a vertex", null);

    final Bytecode bytecode = new Bytecode();
    bytecode.addStep(null);

    final BulkSet<Object> bulkSet = new BulkSet<>();
    bulkSet.add(vertex, 1L);

    final MutableMetrics metrics = new MutableMetrics("a metric", null);

    final Tree<Vertex> tree = new Tree<>();
    tree.put(vertex, null);

    // Provide instances that are malformed for serialization to fail
    return Arrays.asList(
            b,
            vertex,
            Collections.singletonMap("one", b),
            bulkSet,
            bytecode,
            Collections.singletonList(vertex),
            new ReferenceEdge("an edge", null, vertex, vertex),
            Lambda.supplier(null),
            metrics,
            new DefaultTraversalMetrics(1L, Collections.singletonList(metrics)),
            new DefaultRemoteTraverser<>(new Object(), 1L),
            tree,
            new ReferenceVertexProperty<>("a prop", null, "value"),
            new InvalidPath()
    );
}
 
Example #19
Source File: ObjectQueryTest.java    From gremlin-ogm with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("cast")
public void testQueryByAnyTraversal() {
  when(traversal.hasLabel(anyString())).thenReturn(traversal);
  when(traversal.has(anyString(), (Object) any())).thenReturn(traversal);
  BulkSet<Vertex> bulkSet = BulkSetSupplier.<Vertex>instance().get();
  bulkSet.add(vertex);
  when(traversal.toBulkSet()).thenReturn(bulkSet);

  Person actual = query
      .by(g -> g.V().hasLabel(marko.label()).has("name", marko.name()))
      .one(Person.class);

  assertEquals(marko, actual);
}
 
Example #20
Source File: AggregateLocalStep.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected void sideEffect(final Traverser.Admin<S> traverser) {
    final BulkSet<Object> bulkSet = new BulkSet<>();
    bulkSet.add(TraversalUtil.applyNullable(traverser, this.storeTraversal), traverser.bulk());
    this.getTraversal().getSideEffects().add(this.sideEffectKey, bulkSet);
}
 
Example #21
Source File: BulkSetSupplier.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public BulkSet<A> get() {
    return new BulkSet<>();
}
 
Example #22
Source File: BulkSetSupplierTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldSupplyBulkSetInstance() {
    assertEquals(0, BulkSetSupplier.instance().get().size());
    assertThat(BulkSetSupplier.instance().get(), instanceOf(BulkSet.class));
}
 
Example #23
Source File: TraversalSerializersV3d0.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public BulkSetJacksonDeserializer() {
    super(BulkSet.class);
}
 
Example #24
Source File: TraversalSerializersV3d0.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public BulkSetJacksonSerializer() {
    super(BulkSet.class);
}
 
Example #25
Source File: GraphSONTypeSerializerV3d0.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Class getClassFromObject(final Object o) {
    final Class c = o.getClass();
    if (classMap.containsKey(c))
        return classMap.get(c);

    final Class mapped;
    if (Map.class.isAssignableFrom(c)) {
        if (Tree.class.isAssignableFrom(c))
            mapped = Tree.class;
        else
            mapped = Map.class;
    } else if (List.class.isAssignableFrom(c))
        mapped = List.class;
    else if (Set.class.isAssignableFrom(c) && !BulkSet.class.isAssignableFrom(c))   // we have special handling of BulkSet
        mapped = Set.class;
    else if (Vertex.class.isAssignableFrom(c))
        mapped = Vertex.class;
    else if (Edge.class.isAssignableFrom(c))
        mapped = Edge.class;
    else if (Path.class.isAssignableFrom(c))
        mapped = Path.class;
    else if (VertexProperty.class.isAssignableFrom(c))
        mapped = VertexProperty.class;
    else if (Metrics.class.isAssignableFrom(c))
        mapped = Metrics.class;
    else if (TraversalMetrics.class.isAssignableFrom(c))
        mapped = TraversalMetrics.class;
    else if (Property.class.isAssignableFrom(c))
        mapped = Property.class;
    else if (ByteBuffer.class.isAssignableFrom(c))
        mapped = ByteBuffer.class;
    else if (InetAddress.class.isAssignableFrom(c))
        mapped = InetAddress.class;
    else if (Traverser.class.isAssignableFrom(c))
        mapped = Traverser.class;
    else if (Lambda.class.isAssignableFrom(c))
        mapped = Lambda.class;
    else if (VertexProperty.Cardinality.class.isAssignableFrom(c))
        mapped = VertexProperty.Cardinality.class;
    else if (Column.class.isAssignableFrom(c))
        mapped = Column.class;
    else if (Direction.class.isAssignableFrom(c))
        mapped = Direction.class;
    else if (Operator.class.isAssignableFrom(c))
        mapped = Operator.class;
    else if (Order.class.isAssignableFrom(c))
        mapped = Order.class;
    else if (Pop.class.isAssignableFrom(c))
        mapped = Pop.class;
    else if (SackFunctions.Barrier.class.isAssignableFrom(c))
        mapped = SackFunctions.Barrier.class;
    else if (TraversalOptionParent.Pick.class.isAssignableFrom(c))
        mapped = TraversalOptionParent.Pick.class;
    else if (Scope.class.isAssignableFrom(c))
        mapped = Scope.class;
    else if (T.class.isAssignableFrom(c))
        mapped = T.class;
    else
        mapped = c;

    classMap.put(c, mapped);
    return mapped;
}
 
Example #26
Source File: CoreTraversalTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
@LoadGraphWith(MODERN)
public void shouldHaveNextAndToCollectionWorkProperly() {
    final Traversal<Vertex, Vertex> traversal = g.V();
    assertTrue(traversal.next() instanceof Vertex);
    assertEquals(4, traversal.next(4).size());
    assertTrue(traversal.hasNext());
    assertTrue(traversal.tryNext().isPresent());
    assertFalse(traversal.hasNext());
    assertFalse(traversal.tryNext().isPresent());
    assertFalse(traversal.hasNext());

    Traversal<Integer, Integer> intTraversal = inject(7, 7, 2, 3, 6);
    assertTrue(intTraversal.hasNext());
    final List<Integer> list = intTraversal.toList();
    assertFalse(intTraversal.hasNext());
    assertEquals(5, list.size());
    assertEquals(7, list.get(0).intValue());
    assertEquals(7, list.get(1).intValue());
    assertEquals(2, list.get(2).intValue());
    assertEquals(3, list.get(3).intValue());
    assertEquals(6, list.get(4).intValue());
    assertFalse(intTraversal.hasNext());
    assertFalse(intTraversal.tryNext().isPresent());

    intTraversal = inject(7, 7, 2, 3, 6);
    assertTrue(intTraversal.hasNext());
    final Set<Integer> set = intTraversal.toSet();
    assertFalse(intTraversal.hasNext());
    assertEquals(4, set.size());
    assertTrue(set.contains(7));
    assertTrue(set.contains(2));
    assertTrue(set.contains(3));
    assertTrue(set.contains(6));
    assertFalse(intTraversal.hasNext());
    assertFalse(intTraversal.tryNext().isPresent());

    intTraversal = inject(7, 7, 2, 3, 6);
    assertTrue(intTraversal.hasNext());
    final BulkSet<Integer> bulkSet = intTraversal.toBulkSet();
    assertFalse(intTraversal.hasNext());
    assertEquals(4, bulkSet.uniqueSize());
    assertEquals(5, bulkSet.longSize());
    assertEquals(5, bulkSet.size());
    assertTrue(bulkSet.contains(7));
    assertTrue(bulkSet.contains(2));
    assertTrue(bulkSet.contains(3));
    assertTrue(bulkSet.contains(6));
    assertEquals(2, bulkSet.get(7));
    assertFalse(intTraversal.hasNext());
    assertFalse(intTraversal.tryNext().isPresent());
}
 
Example #27
Source File: Traversal.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Put all the results into a {@link BulkSet}.
 * This can reduce both time and space when aggregating results by ensuring a weighted set.
 *
 * @return the results in a bulk set
 */
public default BulkSet<E> toBulkSet() {
    return this.fill(new BulkSet<>());
}