Example #1
0
    @SuppressWarnings("unchecked")
    private void expand() {
      Iterable<Relationship> expand = expander.expand(this, BranchState.NO_STATE);
      for (Relationship rel : expand) {
        lastMetadata.rels++;
        Node node = rel.getOtherNode(lastNode);
        Visit visit = visitData.get(node.getId());
        if (visit != null && visit.visited) {
          continue;
        }

        Visit lastVisit = visitData.get(lastNode.getId());
        double tentativeGScore =
            lastVisit.wayLength + lengthEvaluator.getCost(rel, Direction.OUTGOING);
        double estimate = estimateEvaluator.getCost(node, end);

        if (visit == null || !visit.next || tentativeGScore < visit.wayLength) {
          if (visit == null) {
            visit = new Visit(rel.getId(), tentativeGScore, estimate);
            visitData.put(node.getId(), visit);
          } else {
            visit.update(rel.getId(), tentativeGScore, estimate);
          }
          addNext(node, estimate + tentativeGScore, visit);
        }
      }
    }
Example #2
0
 static Edge addEdge(Graph graph, Relationship relationship) {
   Edge edge = graph.getEdge(relationship.getId());
   if (null == edge) {
     Vertex outVertex = addNode(graph, relationship.getStartNode());
     Vertex inVertex = addNode(graph, relationship.getEndNode());
     String label = relationship.getType().name();
     // TODO #152 add CurieUtil to resolve IRI to Curie
     edge = graph.addEdge(relationship.getId(), outVertex, inVertex, label);
     copyProperties(relationship, edge);
   }
   return edge;
 }
Example #3
0
 private int sizeOfRelationship(Relationship relationship) {
   try (Transaction ignore = db.beginTx()) {
     return getRelationshipCache()
         .get(relationship.getId())
         .sizeOfObjectInBytesIncludingOverhead();
   }
 }
  @Test
  public void shouldBeAbleToGetPropertiesOnRelationship() throws Exception {

    long relationshipId;
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("foo", "bar");
    properties.put("neo", "Thomas A. Anderson");
    properties.put("number", 15L);
    Transaction tx = database.getGraph().beginTx();
    try {
      Node startNode = database.getGraph().createNode();
      Node endNode = database.getGraph().createNode();
      Relationship relationship =
          startNode.createRelationshipTo(endNode, DynamicRelationshipType.withName("knows"));
      for (Map.Entry<String, Object> entry : properties.entrySet()) {
        relationship.setProperty(entry.getKey(), entry.getValue());
      }
      relationshipId = relationship.getId();
      tx.success();
    } finally {
      tx.finish();
    }

    Map<String, Object> readProperties =
        serialize(actions.getAllRelationshipProperties(relationshipId));
    assertEquals(properties, readProperties);
  }
Example #5
0
  private boolean isConnected(NodeOrRelationship current, TypedId newId) throws ShellException {
    if (current.isNode()) {
      Node currentNode = current.asNode();
      for (Relationship rel : currentNode.getRelationships()) {
        if (newId.isNode()) {
          if (rel.getOtherNode(currentNode).getId() == newId.getId()) {
            return true;
          }
        } else {
          if (rel.getId() == newId.getId()) {
            return true;
          }
        }
      }
    } else {
      if (newId.isRelationship()) {
        return false;
      }

      Relationship relationship = current.asRelationship();
      if (relationship.getStartNode().getId() == newId.getId()
          || relationship.getEndNode().getId() == newId.getId()) {
        return true;
      }
    }
    return false;
  }
 private Object toJsonCompatible(Object value) {
   if (value instanceof Node) {
     final Node node = (Node) value;
     final Map<String, Object> result = SubGraph.toMap(node);
     result.put("_id", node.getId());
     return result;
   }
   if (value instanceof Relationship) {
     final Relationship relationship = (Relationship) value;
     final Map<String, Object> result = SubGraph.toMap(relationship);
     result.put("_id", relationship.getId());
     result.put("_start", relationship.getStartNode().getId());
     result.put("_end", relationship.getEndNode().getId());
     result.put("_type", relationship.getType().name());
     return result;
   }
   if (value instanceof Iterable) {
     final List<Object> result = new ArrayList<Object>();
     for (Object inner : (Iterable) value) {
       result.add(toJsonCompatible(inner));
     }
     return result;
   }
   return value;
 }
Example #7
0
 // TODO unit test that
 static boolean removeEdge(Graph graph, Relationship relationship) {
   Edge edge = graph.getEdge(relationship.getId());
   if (null != edge) {
     graph.removeEdge(edge);
     return true;
   } else {
     return false;
   }
 }
 @Override
 public BinaryEdge getSingleBinaryEdge(Vertex vertex, Direction dir) {
   Relationship rel = vertex.getNode().getSingleRelationship(getRelationshipType(), dir);
   if (rel == null) {
     return null;
   } else {
     return new BinaryEdgeImpl(db, rel.getId());
   }
 }
  @Override
  public int hashCode() {

    if (this.dbRelationship == null) {

      return (super.hashCode());
    }

    return Long.valueOf(dbRelationship.getId()).hashCode();
  }
  public int compareTo(Relationship r) {
    int ourId = (int) this.getId(), theirId = (int) r.getId();

    if (ourId < theirId) {
      return -1;
    } else if (ourId > theirId) {
      return 1;
    } else {
      return 0;
    }
  }
Example #11
0
 void addRel(Relationship rel) {
   long[] newRels = null;
   if (relsToHere == null) {
     newRels = new long[1];
   } else {
     newRels = new long[relsToHere.length + 1];
     System.arraycopy(relsToHere, 0, newRels, 0, relsToHere.length);
   }
   newRels[newRels.length - 1] = rel.getId();
   relsToHere = newRels;
 }
 public Object resolveParameter(Object value, String parameterName, int index) {
   final Class<?> type = value.getClass();
   if (template.isNodeEntity(type)) {
     final Node state = template.getPersistentState(value);
     if (state != null) return state.getId();
   }
   if (template.isRelationshipEntity(type)) {
     final Relationship state = template.getPersistentState(value);
     if (state != null) return state.getId();
   }
   return value;
 }
Example #13
0
 public long createRelationship(String type) throws DatabaseBlockedException {
   Transaction tx = database.graph.beginTx();
   try {
     Node startNode = database.graph.createNode();
     Node endNode = database.graph.createNode();
     Relationship relationship =
         startNode.createRelationshipTo(endNode, DynamicRelationshipType.withName(type));
     tx.success();
     return relationship.getId();
   } finally {
     tx.finish();
   }
 }
  @Test
  public void correctlySaysRelIsDeleted() throws Exception {
    // Given
    state.relationshipDoDelete(1l, 1, 1l, 2l);

    Relationship rel = mock(Relationship.class);
    when(rel.getId()).thenReturn(1l);
    when(ops.relationshipGetAllProperties(1l))
        .thenReturn(Collections.<DefinedProperty>emptyIterator());

    // When & Then
    assertThat(snapshot().isDeleted(rel), equalTo(true));
  }
  @Test
  public void shouldProvideTheCorrectRelationshipData() {
    GraphDatabaseService db = dbRule.getGraphDatabaseService();

    // create a rel type so the next type id is non zero
    try (Transaction tx = db.beginTx()) {
      db.createNode()
          .createRelationshipTo(db.createNode(), DynamicRelationshipType.withName("TYPE"));
    }

    RelationshipType livesIn = DynamicRelationshipType.withName("LIVES_IN");
    long relId;

    try (Transaction tx = db.beginTx()) {
      Node person = db.createNode(DynamicLabel.label("Person"));

      Node city = db.createNode(DynamicLabel.label("City"));

      Relationship rel = person.createRelationshipTo(city, livesIn);
      rel.setProperty("since", 2009);
      relId = rel.getId();
      tx.success();
    }

    final Set<String> changedRelationships = new HashSet<>();

    db.registerTransactionEventHandler(
        new TransactionEventHandler.Adapter<Void>() {
          @Override
          public Void beforeCommit(TransactionData data) throws Exception {
            for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
              changedRelationships.add(entry.entity().getType().name());
            }

            return null;
          }
        });

    try (Transaction tx = db.beginTx()) {
      Relationship rel = db.getRelationshipById(relId);
      rel.setProperty("since", 2010);
      tx.success();
    }

    assertEquals(1, changedRelationships.size());
    assertTrue(
        livesIn + " not in " + changedRelationships.toString(),
        changedRelationships.contains(livesIn.name()));
  }
  @Test
  public void testCreateRelationshipToNodeOutsideofBatch() throws Exception {
    final Node node1 = restAPI.createNode(map());
    final Transaction tx = restAPI.beginTx();

    Node node2 = restAPI.createNode(map());
    final Relationship relationship =
        node1.createRelationshipTo(node2, DynamicRelationshipType.withName("foo"));

    tx.success();
    tx.finish();
    assertEquals("foo", relationship.getType().name());
    assertEquals(
        "foo", getGraphDatabase().getRelationshipById(relationship.getId()).getType().name());
  }
Example #17
0
  public Collection<Long> getIndexedRelationships(String indexName, String key, Object value)
      throws DatabaseBlockedException {

    Index<Relationship> index = database.getRelationshipIndex(indexName);
    Transaction tx = database.graph.beginTx();
    try {
      Collection<Long> result = new ArrayList<Long>();
      for (Relationship relationship : index.get(key, value)) {
        result.add(relationship.getId());
      }
      tx.success();
      return result;
    } finally {
      tx.finish();
    }
  }
Example #18
0
 @Override
 public String relationshipRepresentation(Path path, Node from, Relationship relationship) {
   String prefix = "--", suffix = "--";
   if (from.equals(relationship.getEndNode())) {
     prefix = "<--";
   } else {
     suffix = "-->";
   }
   return prefix
       + "["
       + relationship.getType().name()
       + ","
       + relationship.getId()
       + "]"
       + suffix;
 }
Example #19
0
  /**
   * @param server the {@link GraphDatabaseShellServer} to run at.
   * @param session the {@link Session} used by the client.
   * @param relationship the {@link Relationship} to get a display name for.
   * @param verbose whether or not to include the relationship id as well.
   * @return a display string for the {@code relationship}.
   */
  public static String getDisplayName(
      GraphDatabaseShellServer server,
      Session session,
      Relationship relationship,
      boolean verbose,
      boolean checkForMe)
      throws ShellException {
    if (checkForMe && isCurrent(session, NodeOrRelationship.wrap(relationship))) {
      return getDisplayNameForCurrent(server, session);
    }

    StringBuilder result = new StringBuilder("[");
    result.append(":" + relationship.getType().name());
    result.append(verbose ? "," + relationship.getId() : "");
    result.append("]");
    return result.toString();
  }
 @Test
 public void testAddCacheCleared() {
   Node nodeA = getGraphDb().createNode();
   nodeA.setProperty("1", 1);
   Node nodeB = getGraphDb().createNode();
   Relationship rel = nodeA.createRelationshipTo(nodeB, MyRelTypes.TEST);
   rel.setProperty("1", 1);
   getTransaction().success();
   getTransaction().finish();
   newTransaction();
   NodeManager nodeManager =
       ((EmbeddedGraphDatabase) getGraphDb()).getConfig().getGraphDbModule().getNodeManager();
   nodeManager.clearCache();
   nodeA.createRelationshipTo(nodeB, MyRelTypes.TEST);
   int count = 0;
   for (Relationship relToB : nodeA.getRelationships(MyRelTypes.TEST)) {
     count++;
   }
   assertEquals(2, count);
   nodeA.setProperty("2", 2);
   assertEquals(1, nodeA.getProperty("1"));
   rel.setProperty("2", 2);
   assertEquals(1, rel.getProperty("1"));
   nodeManager.clearCache();
   // trigger empty load
   getGraphDb().getNodeById(nodeA.getId());
   getGraphDb().getRelationshipById(rel.getId());
   // apply COW maps
   getTransaction().success();
   getTransaction().finish();
   newTransaction();
   count = 0;
   for (Relationship relToB : nodeA.getRelationships(MyRelTypes.TEST)) {
     count++;
   }
   assertEquals(2, count);
   assertEquals(1, nodeA.getProperty("1"));
   assertEquals(1, rel.getProperty("1"));
   assertEquals(2, nodeA.getProperty("2"));
   assertEquals(2, rel.getProperty("2"));
 }
  @Test
  public void arrayIndexOutOfBoundsInRelTypeArrayWhenCreatingRelationshipsConcurrently()
      throws Exception {
    // GIVEN
    // -- a node manager capable of serving light-weight RelationshipProxy capable of answering
    // getId()
    NodeManager nodeManager = mock(NodeManager.class);
    when(nodeManager.newRelationshipProxyById(anyLong())).thenAnswer(relationshipProxyWithId());

    // -- a node that says it cannot load any more relationships
    NodeImpl node = mock(NodeImpl.class);
    when(node.getMoreRelationships(nodeManager)).thenReturn(LoadStatus.NOTHING);

    // -- a type iterator that at this point contains one relationship (0)
    ControlledRelIdIterator typeIterator = new ControlledRelIdIterator(0L);
    RelationshipIterator iterator =
        new RelationshipIterator(
            new RelIdIterator[] {typeIterator}, node, OUTGOING, nodeManager, false, false);
    // -- go forth one step in the iterator
    iterator.next();

    // WHEN
    // -- one relationship has been returned, and we're in the middle of the next call to next()
    //    typeIterator will get one more relationship in it. To mimic this we control the outcome of
    //    RelIdIterator#hasNext() so that we get to the correct branch in the RelationshipIterator
    // code
    typeIterator.queueHasNextAnswers(false, false, true);
    long otherRelationship = 1, thirdRelationship = 2;
    typeIterator.add(otherRelationship, thirdRelationship);

    // -- go one more step, getting us into the state where the type index in RelationshipIterator
    //    was incremented by mistake. Although this particular call to next() succeeds
    iterator.next();

    // -- call next() again, where the first thing happening is to get the RelIdIterator with the
    //    now invalid type index, causing ArrayIndexOutOfBoundsException
    Relationship returnedThirdRelationship = iterator.next();

    // THEN
    assertEquals(thirdRelationship, returnedThirdRelationship.getId());
  }
Example #22
0
    private Object toJsonCompatible(Object value) {
      if (value instanceof Node) {
        final Node node = (Node) value;
        final Map<String, Object> result = SubGraph.toMap((PropertyContainer) node);
        result.put("_id", node.getId());

        final List<String> labelNames = SubGraph.getLabelNames(node);
        if (!labelNames.isEmpty()) result.put("_labels", labelNames);
        return result;
      }
      if (value instanceof Relationship) {
        final Relationship relationship = (Relationship) value;
        final Map<String, Object> result = SubGraph.toMap((PropertyContainer) relationship);
        result.put("_id", relationship.getId());
        result.put("_start", relationship.getStartNode().getId());
        result.put("_end", relationship.getEndNode().getId());
        result.put("_type", relationship.getType().name());
        return result;
      }
      if (value instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) value;
        final Map<String, Object> result = new LinkedHashMap<>(map.size());
        for (Map.Entry<String, Object> entry : map.entrySet()) {
          result.put(entry.getKey(), toJsonCompatible(entry.getValue()));
        }
        return result;
      }
      if (value instanceof Iterable) {
        final List<Object> result = new ArrayList<>();
        for (Object inner : (Iterable) value) {
          result.add(toJsonCompatible(inner));
        }
        return result;
      }
      return value;
    }
 protected String getRelationshipUri(Relationship node) {
   return getDataUri() + "relationship/" + node.getId();
 }
  /** Compare the specified source property of the two provided relationships. */
  @Override
  public int compare(Relationship rel1, Relationship rel2) {

    Object v1 = null;
    Object v2 = null;
    if (rel1.hasProperty("source") == false || rel2.hasProperty("source") == false) {

      if (VERBOSE) {
        System.out.println("Ack!!! A relationship has no source!");
        System.out.println(
            "rel1 source = "
                + rel1.getProperty("source")
                + "; rel2 source = "
                + rel2.getProperty("source"));
      }

      return 0;
    }

    IndexHits<Node> h1 = metadataNodeIndex.get("source", rel1.getProperty("source"));
    IndexHits<Node> h2 = metadataNodeIndex.get("source", rel2.getProperty("source"));
    if (h1.size() == 0 || h2.size() == 0) {

      if (VERBOSE) {
        System.out.println("Dorf!!! A relationship has no metaDataNode!");
        System.out.println(
            "rel1 MDN = "
                + metadataNodeIndex.get("source", rel1.getProperty("source"))
                + "; rel2 MDN = "
                + metadataNodeIndex.get("source", rel1.getProperty("source")));
      }

      return 0;
    }
    Node m1 = h1.next();
    Node m2 = h2.next();

    h1.close();
    h2.close();
    if (m1.hasProperty(property.propertyName)) {
      v1 = m1.getProperty(property.propertyName);
    }
    if (m2.hasProperty(property.propertyName)) {
      v2 = m2.getProperty(property.propertyName);
    }

    if (v1 == null && v2 == null) {
      return 0;
    } else if (v2 == NOTRANKED) {
      return -1;
    } else if (v1 == NOTRANKED) {
      return 1;
    }

    Integer rank1 = NOTRANKED;
    Integer rank2 = NOTRANKED;

    if (property.type == String.class) {
      if (priorityMapString.containsKey(v1)) {
        rank1 = priorityMapString.get(v1);
      }
      if (priorityMapString.containsKey(v2)) {
        rank2 = priorityMapString.get(v2);
      }
    } else if (property.type == Long.class || property.type == Integer.class) {
      if (priorityMapString.containsKey(v1)) {
        rank1 = priorityMapLong.get(v1);
      }
      if (priorityMapString.containsKey(v2)) {
        rank2 = priorityMapLong.get(v2);
      }
    } else if (property.type == Double.class) {
      if (priorityMapString.containsKey(v1)) {
        rank1 = priorityMapDouble.get(v1);
      }
      if (priorityMapString.containsKey(v2)) {
        rank2 = priorityMapDouble.get(v2);
      }
    } else {
      throw new java.lang.UnsupportedOperationException(
          "the source property datatype " + String.valueOf(property.type) + " is unrecognized");
    }

    Integer retval = null;
    if (rank1 == NOTRANKED && rank2 == NOTRANKED) {
      retval = 0;

      if (VERBOSE) {
        System.out.println("Glarg!!! Both relationships are unranked");
      }

    } else if (rank2 == NOTRANKED) {
      retval = -1;
      nRankableRelsCompared++;

      if (VERBOSE) {
        System.out.println(
            "rel 2 (relid "
                + rel2.getId()
                + "; source "
                + rel2.getProperty("source")
                + ") property "
                + v2
                + " is not in priority list; preferring rel 1 (relid "
                + rel1.getId()
                + "; source "
                + rel1.getProperty("source")
                + ") property "
                + v1
                + ")");
      }
    } else if (rank1 == NOTRANKED) {
      retval = 1;
      nRankableRelsCompared++;

      if (VERBOSE) {
        System.out.println(
            "rel 1 (relid "
                + rel1.getId()
                + "; name "
                + rel1.getProperty("source")
                + ") property "
                + v1
                + " is not in priority list; preferring rel 2 (relid "
                + rel2.getId()
                + "; source "
                + rel2.getProperty("source")
                + ") property "
                + v2
                + ")");
      }
    } else {
      retval = rank1.compareTo(rank2);
      nRankableRelsCompared += 2;

      if (VERBOSE) {
        System.out.println(
            "rel 1 (relid "
                + rel1.getId()
                + "; name "
                + rel1.getProperty("source")
                + ") property "
                + v1
                + " || rel 2 (relid "
                + rel2.getId()
                + "; source "
                + rel2.getProperty("source")
                + ") property "
                + v2
                + ")");
        System.out.println("\t" + retval);
      }
    }

    // priority is indicated by proximity to the beginning of the list, so we sort in REVERSE!
    // return retval * -1;
    return retval;
  }
  private List<Long> findBestNonOverlapping(Collection<Relationship> rels) {

    if (rels.size() < 1) {
      if (VERBOSE) {
        System.out.println("(no non-singleton rels to select from)");
      }
      return new ArrayList<Long>();
    }

    TLongBitArraySet[] mrcaSetsForRels = new TLongBitArraySet[rels.size()];
    double[] weights = new double[rels.size()];
    Long[] relIds = new Long[rels.size()];

    // used to check if no overlap exists among any rels, if not don't bother with the mwis
    // really we could be smarter about this and find the rels that have no overlap and exclude them
    int taxSum = 0;

    HashSet<Long> uniqueTips = new HashSet<Long>();

    Iterator<Relationship> relsIter = rels.iterator();
    for (int i = 0; relsIter.hasNext(); i++) {
      Relationship rel = relsIter.next();

      TLongBitArraySet currDesc = mrcaTipsAndInternal(rel);

      // this represents a pathological case, so die horribly
      if (currDesc == null) {
        throw new IllegalStateException("Found a rel with no descendants: " + rel);
      }

      relIds[i] = rel.getId();
      mrcaSetsForRels[i] = currDesc;
      weights[i] = getScoreNodeCount(rel);

      //			long [] currTips = mrcaTips(rel);
      TLongBitArraySet currTips = mrcaTips(rel.getStartNode());
      taxSum += currTips.size();
      //			for (int j = 0; j < currTips.size(); j++) {
      //				uniqueTips.add(currTips.get(j));
      //			}
      for (long t : currTips) {
        uniqueTips.add(t);
      }

      if (VERBOSE) {
        System.out.println(
            rel.getId()
                + ": nodeMrca("
                + rel.getStartNode().getId()
                + ") = "
                + nodeMrcaTipsAndInternal.get(rel.getStartNode().getId())
                + ". score = "
                + weights[i]);
      }
    }

    System.out.println(
        "taxSum = "
            + taxSum
            + "; uniqueTips size = "
            + uniqueTips.size()
            + "; incoming rels = "
            + relIds.length);

    if (taxSum
        == uniqueTips
            .size()) { // if there is no conflict, skip the set comparisons and just add everything
                       // (saves major time)
      System.out.println("no conflict! saving all incoming rels.");
      return new ArrayList<Long>(Arrays.asList(relIds));

    } else if (relIds.length
        <= BruteWeightedIS
            .MAX_TRACTABLE_N) { // otherwise if the set is small enough find the exact MWIS solution
      return new BruteWeightedIS(relIds, weights, mrcaSetsForRels).best();

    } else { // otherwise we need to use the approximation method
      return new GreedyApproximateWeightedIS(relIds, weights, mrcaSetsForRels).best();
    }
  }
 @Test
 @Transactional
 public void testGetRelationship() throws Exception {
   Relationship lookedUpRelationship = neo4jTemplate.getRelationship(relationship1.getId());
   assertThat(lookedUpRelationship, is(relationship1));
 }
 protected String getRelationshipUri(Relationship relationship) {
   return getDataUri() + PATH_RELATIONSHIPS + "/" + relationship.getId();
 }
Example #28
0
 public void put(Relationship node) {
   cache.put(node.getId(), node);
 }
  public long getInternalId() {

    return dbRelationship.getId();
  }
Example #30
0
 protected void assertRelationshipDoesntExist(Relationship relationship) {
   assertRelationshipDoesntExist(relationship.getId());
 }