Esempio n. 1
0
  @RequestMapping(value = "/single/{time}", method = RequestMethod.GET)
  @ResponseBody
  public JsonNode getInstant(
      @PathVariable long time,
      @RequestParam(required = false) String resolution,
      @RequestParam(required = false) String timezone) {

    TimeInstant timeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(time, resolution, timezone));

    long id;

    try (Transaction tx = database.beginTx()) {
      id = timeTree.getOrCreateInstant(timeInstant).getId();
      tx.success();
    }

    JsonNode result;

    try (Transaction tx = database.beginTx()) {
      result = new JsonNode(database.getNodeById(id));
      tx.success();
    }

    return result;
  }
Esempio n. 2
0
  private long createDb() {
    GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);

    long firstNodeId = -1;

    Transaction tx = graphDb.beginTx();
    try {
      Node firstNode = graphDb.createNode();
      firstNodeId = firstNode.getId();
      firstNode.setProperty("message", "Hello, ");
      firstNode.setProperty(
          "someJson", "{\n  \"is\" : \"vcard\",\n  \"name\" : \"Jim Barritt\"\n}");

      Node secondNode = graphDb.createNode();
      secondNode.setProperty("message", "World!");

      Relationship relationship = firstNode.createRelationshipTo(secondNode, KNOWS);
      relationship.setProperty("message", "brave Neo4j ");

      tx.success();

      return firstNodeId;
    } finally {
      tx.finish();
      graphDb.shutdown();
    }
  }
  /**
   * Removes all nodes with non-valid resource classes from the graph.
   *
   * @param docGraph
   * @param validResourceClasses
   */
  private void preProcessGraph(DocGraph docGraph, Set<String> validResourceClasses) {
    log.info(String.format("Preprocessing DocGraph[%d]", docGraph.getId()));
    Node n;
    int cnt = 0;
    try (Transaction tx = graphDB.beginTx()) {
      for (Long nodeId : docGraph.getNodes()) {
        n = graphDB.getNodeById(nodeId);
        // node's class is resource class and it's not in the valid set
        if (n.hasProperty(Constants.NODE_SUPER_CLASS_KEY)
            && n.getProperty(Constants.NODE_SUPER_CLASS_KEY)
                .equals(Constants.NODE_SUPER_CLASS_RESOURCE_VALUE)
            && !validResourceClasses.contains(getNodeClass(n))) {
          try (Transaction innerTx = graphDB.beginTx()) {

            log.info("Deleting " + n);
            for (Relationship e : n.getRelationships()) {
              e.delete();
            }
            n.delete();
            innerTx.success();
            cnt++;
          }
        }
      }
      tx.success();
    }

    log.info(
        String.format("Preprocessing removed %d nodes from DocGraph[%d]", cnt, docGraph.getId()));
  }
Esempio n. 4
0
 @Override
 public WeightedPath findSinglePath(Node start, Node end) {
   lastMetadata = new Metadata();
   AStarIterator iterator = new AStarIterator(start, end);
   while (iterator.hasNext()) {
     Node node = iterator.next();
     GraphDatabaseService graphDb = node.getGraphDatabase();
     if (node.equals(end)) {
       // Hit, return path
       double weight = iterator.visitData.get(node.getId()).wayLength;
       LinkedList<Relationship> rels = new LinkedList<Relationship>();
       Relationship rel =
           graphDb.getRelationshipById(iterator.visitData.get(node.getId()).cameFromRelationship);
       while (rel != null) {
         rels.addFirst(rel);
         node = rel.getOtherNode(node);
         long nextRelId = iterator.visitData.get(node.getId()).cameFromRelationship;
         rel = nextRelId == -1 ? null : graphDb.getRelationshipById(nextRelId);
       }
       Path path = toPath(start, rels);
       lastMetadata.paths++;
       return new WeightedPathImpl(weight, path);
     }
   }
   return null;
 }
  public void run(GraphDatabaseService graphDb) {
    timerOff(MAIN_TIMER);
    Transaction tx = graphDb.beginTx();
    try {
      this.container = createContainer(graphDb);
      tx.success();
    } finally {
      tx.finish();
    }
    timerOn(MAIN_TIMER);

    tx = graphDb.beginTx();
    try {
      int max = getNumberOfIterations();
      Object propertyValue = getPropertyValue();
      if (getPropertyValue().getClass().isArray()) {
        int arraySize = Array.getLength(getPropertyValue());
        max /= arraySize;
      }
      for (int i = 0; i < max; i++) {
        container.setProperty("my_key", propertyValue);
      }
      tx.success();
    } finally {
      tx.finish();
    }
    container = null;
  }
  @Test
  public void modulesShouldBeDelegatedToInCorrectOrder() throws InterruptedException {
    GraphDatabaseService database =
        builder()
            .setConfig(
                "com.graphaware.module.test1.1",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                "com.graphaware.module.test3.3",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                "com.graphaware.module.test2.2",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .newGraphDatabase();

    registerShutdownHook(database);

    try (Transaction tx = database.beginTx()) {
      database.createNode();
      tx.success();
    }

    assertEquals(3, TEST_RUNTIME_MODULES.size());
    assertEquals("test1", TEST_RUNTIME_MODULES.get(0).getId());
    assertEquals("test2", TEST_RUNTIME_MODULES.get(1).getId());
    assertEquals("test3", TEST_RUNTIME_MODULES.get(2).getId());
  }
Esempio n. 7
0
  public void copyStore(StoreCopyRequester requester, CancellationRequest cancellationRequest)
      throws IOException {
    // Clear up the current temp directory if there
    File storeDir = config.get(InternalAbstractGraphDatabase.Configuration.store_dir);
    File tempStore = new File(storeDir, TEMP_COPY_DIRECTORY_NAME);
    cleanDirectory(tempStore);

    // Request store files and transactions that will need recovery
    try (Response response =
        requester.copyStore(decorateWithProgressIndicator(new ToFileStoreWriter(tempStore)))) {
      // Update highest archived log id
      // Write transactions that happened during the copy to the currently active logical log
      writeTransactionsToActiveLogFile(tempStore, response);
    } finally {
      requester.done();
    }

    // This is a good place to check if the switch has been cancelled
    checkCancellation(cancellationRequest, tempStore);

    // Run recovery, so that the transactions we just wrote into the active log will be applied.
    GraphDatabaseService graphDatabaseService = newTempDatabase(tempStore);
    graphDatabaseService.shutdown();

    // This is a good place to check if the switch has been cancelled
    checkCancellation(cancellationRequest, tempStore);

    // All is well, move the streamed files to the real store directory
    for (File candidate : tempStore.listFiles(STORE_FILE_FILTER)) {
      FileUtils.moveFileToDirectory(candidate, storeDir);
    }
  }
Esempio n. 8
0
  @Test
  public void shouldBeAbleToImplementAnonymousGraphQuery() throws Exception {
    // given
    GraphDatabaseService db = Db.impermanentDb();
    Transaction tx = db.beginTx();
    Node firstNode = db.createNode();
    Node secondNode = db.createNode();
    firstNode.createRelationshipTo(secondNode, withName("CONNECTED_TO"));
    tx.success();
    tx.finish();

    GraphQuery query =
        new GraphQuery() {
          @Override
          public Iterable<Node> execute(Node startNode) {
            return startNode.traverse(
                Traverser.Order.DEPTH_FIRST,
                StopEvaluator.DEPTH_ONE,
                ReturnableEvaluator.ALL_BUT_START_NODE,
                withName("CONNECTED_TO"),
                Direction.OUTGOING);
          }
        };

    // when
    Iterable<Node> result = query.execute(firstNode);

    // then
    Iterator<Node> iterator = result.iterator();
    assertEquals(secondNode, iterator.next());
    assertFalse(iterator.hasNext());
  }
Esempio n. 9
0
  public OnlineBackup full(String targetDirectory, boolean verification) {
    if (directoryContainsDb(targetDirectory)) {
      throw new RuntimeException(targetDirectory + " already contains a database");
    }

    BackupClient client =
        new BackupClient(hostNameOrIp, port, StringLogger.DEV_NULL, Client.NO_STORE_ID_GETTER);
    try {
      Response<Void> response = client.fullBackup(new ToFileStoreWriter(targetDirectory));
      GraphDatabaseService targetDb =
          startTemporaryDb(targetDirectory, VerificationLevel.NONE /* run full check instead */);
      try {
        unpackResponse(response, targetDb, MasterUtil.txHandlerForFullCopy());
      } finally {
        targetDb.shutdown();
      }
      if (verification) {
        StoreAccess newStore = new StoreAccess(targetDirectory);
        try {
          ConsistencyCheck.run(newStore, false);
        } finally {
          newStore.close();
        }
      }
    } finally {
      client.shutdown();
    }
    return this;
  }
Esempio n. 10
0
  @Test
  public void shouldMigrate() throws IOException, ConsistencyCheckIncompleteException {
    // WHEN
    upgrader(new StoreMigrator(monitor, fs)).migrateIfNeeded(find20FormatStoreDirectory(storeDir));

    // THEN
    assertEquals(100, monitor.eventSize());
    assertTrue(monitor.isStarted());
    assertTrue(monitor.isFinished());

    GraphDatabaseService database =
        cleanup.add(new GraphDatabaseFactory().newEmbeddedDatabase(storeDir.getAbsolutePath()));

    try {
      DatabaseContentVerifier verifier = new DatabaseContentVerifier(database);
      verifier.verifyNodes(501);
      verifier.verifyRelationships(500);
      verifier.verifyNodeIdsReused();
      verifier.verifyRelationshipIdsReused();
      verifier.verifyLegacyIndex();
      verifier.verifyIndex();
    } finally {
      // CLEANUP
      database.shutdown();
    }

    NeoStore neoStore = cleanup.add(storeFactory.newNeoStore(false));
    verifyNeoStore(neoStore);
    neoStore.close();
    assertConsistentStore(storeDir);
  }
Esempio n. 11
0
 @Override
 /**
  * Cleanup any state for this DB. Called once per DB instance; there is one DB instance per client
  * thread.
  */
 public void cleanup() throws DBException {
   try {
     if (mode == Mode.EMBEDDED) {
       synchronized (LOCK) {
         instances--;
         if (instances == 0) {
           GDS.shutdown();
           System.out.println("neo4j connection closed with @" + GDS);
         }
       }
     } else {
       gds.shutdown();
       System.out.println("neo4j connection closed with @" + gds);
     }
   } catch (Exception e1) {
     System.err.println("Could not close MongoDB connection pool: " + e1.toString());
     e1.printStackTrace();
     return;
   }
 }
Esempio n. 12
0
  @Override
  public void persistModel() throws IOException, XMLStreamException {
    try (Transaction tx = graphDb.beginTx()) {
      final ProgressReporter reporter = new ProgressReporter(null, null);

      final StringWriter writer = new StringWriter();
      final XmlGraphMLWriter xmlGraphMLWriter = new XmlGraphMLWriter();
      final Config config = Config.config();
      xmlGraphMLWriter.write(new DatabaseSubGraph(graphDb), writer, reporter, config.withTypes());
      tx.success();

      final String fileName = generatorConfig.getModelPathNameWithoutExtension() + ".graphml";

      String graphmlContent = writer.toString();
      // this is required to be compatibile with OrientDB
      graphmlContent =
          graphmlContent.replaceAll(
              "<graph id=\"G\" edgedefault=\"directed\">",
              "<graph id=\"G\" edgedefault=\"directed\">\n<key id=\"labels\" for=\"node\" attr.name=\"labels\" attr.type=\"string\"/>");

      FileUtils.writeStringToFile(new File(fileName), graphmlContent.trim());
    } finally {
      graphDb.shutdown();

      // cleanup: delete the database directory
      if (new File(databasePath).exists()) {
        FileUtils.deleteDirectory(new File(databasePath));
      }
    }
  }
  @Test
  public void shouldBeAbleToAccessExceptionThrownInEventHook() {
    class MyFancyException extends Exception {}

    ExceptionThrowingEventHandler handler =
        new ExceptionThrowingEventHandler(new MyFancyException(), null, null);
    GraphDatabaseService db = dbRule.getGraphDatabaseService();
    db.registerTransactionEventHandler(handler);

    try {
      Transaction tx = db.beginTx();
      try {
        db.createNode().delete();
        tx.success();
        tx.close();
        fail("Should fail commit");
      } catch (TransactionFailureException e) {
        Throwable currentEx = e;
        do {
          currentEx = currentEx.getCause();
          if (currentEx instanceof MyFancyException) {
            return;
          }
        } while (currentEx.getCause() != null);
        fail(
            "Expected to find the exception thrown in the event hook as the cause of transaction failure.");
      }
    } finally {
      db.unregisterTransactionEventHandler(handler);
    }
  }
Esempio n. 14
0
  @RequestMapping(value = "/range/{startTime}/{endTime}", method = RequestMethod.GET)
  @ResponseBody
  public JsonNode[] getInstants(
      @PathVariable long startTime,
      @PathVariable long endTime,
      @RequestParam(required = false) String resolution,
      @RequestParam(required = false) String timezone) {

    TimeInstant startTimeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(startTime, resolution, timezone));
    TimeInstant endTimeInstant =
        TimeInstant.fromValueObject(new TimeInstantVO(endTime, resolution, timezone));

    List<Node> nodes;
    try (Transaction tx = database.beginTx()) {
      nodes = timeTree.getOrCreateInstants(startTimeInstant, endTimeInstant);
      tx.success();
    }

    JsonNode[] result;
    try (Transaction tx = database.beginTx()) {
      result = jsonNodes(nodes);
      tx.success();
    }

    return result;
  }
  @Test
  public void moduleShouldBeInitializedWhenRuntimeIsEnabledWithoutAnyTransactions()
      throws InterruptedException {
    GraphDatabaseService database =
        builder()
            .setConfig(
                TestModuleBootstrapper.MODULE_ENABLED,
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                TestModuleBootstrapper.MODULE_CONFIG,
                TestModuleBootstrapper.MODULE_CONFIG.getDefaultValue())
            .newGraphDatabase();

    registerShutdownHook(database);

    Thread.sleep(1000);

    assertEquals(1, TEST_RUNTIME_MODULES.size());
    assertTrue(TEST_RUNTIME_MODULES.get(0).isInitialized());
    assertEquals("configValue", TEST_RUNTIME_MODULES.get(0).getConfig().get("configKey"));

    database.shutdown();

    assertFalse(TEST_RUNTIME_MODULES.get(0).isInitialized());
  }
Esempio n. 16
0
  @Test
  public void testRemoveReferenceNode() throws Exception {
    GraphDatabaseService db = new ImpermanentGraphDatabase();
    final GraphDatabaseShellServer server = new GraphDatabaseShellServer(db, false);
    ShellClient client = new SameJvmClient(server);

    Documenter doc = new Documenter("sample session", client);
    doc.add("pwd", "", "where are we?");
    doc.add("set name \"Jon\"", "", "On the current node, set the key \"name\" to value \"Jon\"");
    doc.add("start n=node(0) return n", "Jon", "send a cypher query");
    doc.add(
        "mkrel -c -d i -t LIKES --np \"{'app':'foobar'}\"",
        "",
        "make an incoming relationship of type LIKES, create the end node with the node properties specified.");
    doc.add("ls", "1", "where are we?");
    doc.add("cd 1", "", "change to the newly created node");
    doc.add("ls -avr", "LIKES", "list relationships, including relationshship id");

    doc.add(
        "mkrel -c -d i -t KNOWS --np \"{'name':'Bob'}\"",
        "",
        "create one more KNOWS relationship and the end node");
    doc.add("pwd", "0", "print current history stack");
    doc.add("ls -avr", "KNOWS", "verbose list relationships");
    doc.run();
    // TODO: implement support for removing root node and previous nodes in the history stack of PWD
    // client.getServer().interpretLine( "rmnode -f 0", client.session(), client.getOutput() );
    //        client.getServer().interpretLine( "cd", client.session(), client.getOutput() );
    //        client.getServer().interpretLine( "pwd", client.session(), client.getOutput() );
    server.shutdown();
    db.shutdown();
  }
  @Test
  public void moduleShouldBeInitializedWhenAnotherModuleIsMisConfigured()
      throws InterruptedException {
    GraphDatabaseService database =
        builder()
            .setConfig("com.graphaware.module.wrong1.enabled", "com.not.existent.Bootstrapper")
            .setConfig("com.graphaware.module.wrong2.2", "com.not.existent.Bootstrapper")
            .setConfig(
                TestModuleBootstrapper.MODULE_ENABLED,
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                TestModuleBootstrapper.MODULE_CONFIG,
                TestModuleBootstrapper.MODULE_CONFIG.getDefaultValue())
            .newGraphDatabase();

    registerShutdownHook(database);

    try (Transaction tx = database.beginTx()) {
      database.createNode();
      tx.success();
    }

    assertEquals(1, TEST_RUNTIME_MODULES.size());
    assertTrue(TEST_RUNTIME_MODULES.get(0).isInitialized());
    assertEquals("configValue", TEST_RUNTIME_MODULES.get(0).getConfig().get("configKey"));

    database.shutdown();

    assertFalse(TEST_RUNTIME_MODULES.get(0).isInitialized());
  }
Esempio n. 18
0
 @Override
 public Map<String, Node> create(GraphDatabaseService graphdb) {
   Map<String, Node> result = new HashMap<String, Node>();
   Transaction tx = graphdb.beginTx();
   try {
     graphdb.index().getRelationshipAutoIndexer().setEnabled(autoIndexRelationships);
     for (NODE def : nodes) {
       result.put(
           def.name(),
           init(
               graphdb.createNode(),
               def.setNameProperty() ? def.name() : null,
               def.properties(),
               graphdb.index().getNodeAutoIndexer(),
               autoIndexNodes));
     }
     for (REL def : rels) {
       init(
           result
               .get(def.start())
               .createRelationshipTo(
                   result.get(def.end()), DynamicRelationshipType.withName(def.type())),
           def.setNameProperty() ? def.name() : null,
           def.properties(),
           graphdb.index().getRelationshipAutoIndexer(),
           autoIndexRelationships);
     }
     tx.success();
   } finally {
     tx.finish();
   }
   return result;
 }
  @Test
  public void modulesShouldBeDelegatedToInRandomOrderWhenOrderClashes()
      throws InterruptedException {
    GraphDatabaseService database =
        builder()
            .setConfig(
                "com.graphaware.module.test1.1",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                "com.graphaware.module.test3.1",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                "com.graphaware.module.test2.1",
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .newGraphDatabase();

    registerShutdownHook(database);

    try (Transaction tx = database.beginTx()) {
      database.createNode();
      tx.success();
    }

    assertEquals(3, TEST_RUNTIME_MODULES.size());
    Set<String> remaining = new HashSet<>(Arrays.asList("test1", "test2", "test3"));
    assertTrue(remaining.remove(TEST_RUNTIME_MODULES.get(0).getId()));
    assertTrue(remaining.remove(TEST_RUNTIME_MODULES.get(1).getId()));
    assertTrue(remaining.remove(TEST_RUNTIME_MODULES.get(2).getId()));
    assertTrue(remaining.isEmpty());
  }
 /** {@inheritDoc} */
 @Override
 protected Relationship create(GraphDatabaseService database) {
   return database
       .getNodeById(startNodeGraphId)
       .createRelationshipTo(
           database.getNodeById(endNodeGraphId), DynamicRelationshipType.withName(type));
 }
Esempio n. 21
0
  @Test
  public void testFindByIdFound() {
    // Arrange
    //
    String id = "ABCDEF";
    SubscriptionExpiry expiry = SubscriptionExpiry.NEVER;
    Subscription expected = Subscription.builder().id(id).expiry(expiry).build();

    when(mock_node.getProperty(eq(Subscription.PROP_ID))).thenReturn(expected.id());
    when(mock_node.getProperty(eq(Subscription.PROP_EXPIRY)))
        .thenReturn(expected.expiry().toString());

    when(mock_db.beginTx()).thenReturn(mock_tx);
    when(mock_db.findNode(eq(label), eq(Subscription.PROP_ID), eq(expected.id())))
        .thenReturn(mock_node);

    // Act
    //
    Subscription subs = s_dao.findById(id);

    // Assert
    //
    assertNotNull(subs);
    assertTrue(expected.equals(subs));
    verify(mock_db, atLeastOnce()).beginTx();
    verify(mock_db, times(1)).findNode(any(), any(), any());
    verify(mock_tx, atLeastOnce()).success();
    verify(mock_tx, times(1)).close();
  }
Esempio n. 22
0
  public static int getDocumentSizeForFeature(GraphDatabaseService db, Long id) {
    int documentSize;

    String cacheKey = "DOCUMENT_SIZE_FEATURE_" + id;

    if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
      Node startNode = db.getNodeById(id);

      Iterator<Node> classes =
          db.traversalDescription()
              .depthFirst()
              .relationships(withName("HAS_CLASS"), Direction.OUTGOING)
              .evaluator(Evaluators.fromDepth(1))
              .evaluator(Evaluators.toDepth(1))
              .traverse(startNode)
              .nodes()
              .iterator();

      documentSize = IteratorUtil.count(classes);

      vectorSpaceModelCache.put(cacheKey, documentSize);
    } else {
      documentSize = (Integer) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return documentSize;
  }
Esempio n. 23
0
  @Test
  public void clearGraphWithoutRuntimeShouldDeleteBasedOnRelInclusionPolicy() {
    try (Transaction tx = database.beginTx()) {
      String cypher =
          "CREATE "
              + "(purple:Purple {name:'Purple'})<-[:REL]-(red1:Red {name:'Red'})-[:REL]->(black1:Black {name:'Black'})-[:REL]->(green:Green {name:'Green'}),"
              + "(red2:Red {name:'Red'})-[:REL]->(black2:Black {name:'Black'}), (blue1:Blue)-[:REL2]->(blue2:Blue)";

      populateDatabase(cypher);
      tx.success();
    }

    InclusionPolicies inclusionPolicies =
        InclusionPolicies.all().with(new BlueNodeInclusionPolicy()).with(new Rel2InclusionPolicy());
    try (Transaction tx = database.beginTx()) {
      clearGraph(database, inclusionPolicies);
      tx.success();
    }

    try (Transaction tx = database.beginTx()) {
      assertEquals(6, count(at(database).getAllNodes()));
      assertEquals(4, count(at(database).getAllRelationships()));
      tx.success();
    }
  }
Esempio n. 24
0
  private static List<Double> getWeightVectorForClass(
      Map<String, List<LinkedHashMap<String, Object>>> documents,
      String key,
      List<Integer> featureIndexList,
      GraphDatabaseService db) {
    List<Double> weightVector;

    Transaction tx = db.beginTx();
    // Get class id
    Long classId =
        db.findNodesByLabelAndProperty(DynamicLabel.label("Class"), "name", key)
            .iterator()
            .next()
            .getId();

    // Get weight vector for class
    List<Long> longs =
        documents
            .get(key)
            .stream()
            .map(a -> ((Integer) a.get("feature")).longValue())
            .collect(Collectors.toList());

    weightVector =
        featureIndexList
            .stream()
            .map(i -> longs.contains(i.longValue()) ? tfidf(db, i.longValue(), classId) : 0.0)
            .collect(Collectors.toList());
    tx.success();
    tx.close();
    return weightVector;
  }
  @Test
  public void testUpdateNode() {
    double lon = -139.12;
    double lat = -45.24;
    Transaction tx = graphDb.beginTx();
    String query = "MATCH n-[r]-() DELETE n, r";
    graphDb.execute(query);
    query = "MATCH (n) WHERE HAS (n.geohash) DELETE n";
    graphDb.execute(query);
    root = PrefixTree.getOrCreateRoot(INDEXLABEL, graphDb);
    List<Node> candidate = gendata.insertNode(graphDb, 10, true);
    for (Node node : candidate) {
      PrefixTree.addNode(graphDb, root, node);
    }

    String geoHashPre = (String) candidate.get(0).getProperty(GEOHASH);
    String geoHashCur = GeoHash.getHash(lon, lat);
    List<Node> child = PrefixTree.getChild(geoHashPre, root, 7);
    assertEquals(child.size(), 1);
    PrefixTree.updateNode(graphDb, root, candidate.get(0), lon, lat);
    child = PrefixTree.getChild(geoHashPre, root, 7);
    assertEquals(child.size(), 0);
    child = PrefixTree.getChild(geoHashCur, root, 7);
    assertEquals(child.size(), 1);
    tx.close();
    graphDb.shutdown();
  }
Esempio n. 26
0
  public static Map<Long, Integer> getTermFrequencyMapForDocument(
      GraphDatabaseService db, Long classId) {
    Map<Long, Integer> termDocumentMatrix;

    String cacheKey = "TERM_DOCUMENT_FREQUENCY_" + classId;

    if (vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
      Node classNode = db.getNodeById(classId);

      termDocumentMatrix = new HashMap<>();

      IteratorUtil.asCollection(
              db.traversalDescription()
                  .depthFirst()
                  .relationships(withName("HAS_CLASS"), Direction.INCOMING)
                  .evaluator(Evaluators.fromDepth(1))
                  .evaluator(Evaluators.toDepth(1))
                  .traverse(classNode))
          .stream()
          .forEach(
              p ->
                  termDocumentMatrix.put(
                      p.endNode().getId(), (Integer) p.lastRelationship().getProperty("matches")));

      vectorSpaceModelCache.put(cacheKey, termDocumentMatrix);
    } else {
      termDocumentMatrix = (Map<Long, Integer>) vectorSpaceModelCache.getIfPresent(cacheKey);
    }

    return termDocumentMatrix;
  }
  @Test
  public void removeNodesWithARelation() {
    int originalNodeCount = countNodesOf(graphDb);
    int removedNodeCount = 0;

    Transaction tx = graphDb.beginTx();
    try {
      Set<Node> toRemove = new HashSet<Node>();
      for (Node node : graphDb.getAllNodes()) {
        for (Relationship relationship : node.getRelationships(RelationType.ON)) {
          toRemove.add(relationship.getStartNode());
          toRemove.add(relationship.getEndNode());
          relationship.delete();
        }
      }
      for (Node node : toRemove) {
        node.delete();
        removedNodeCount++;
      }
      tx.success();
    } finally {
      tx.finish();
    }

    int finalNodeCount = countNodesOf(graphDb);
    assertEquals(
        Integer.valueOf(originalNodeCount), Integer.valueOf(finalNodeCount + removedNodeCount));
  }
  @Test
  public void moduleShouldBeInitializedWhenRuntimeIsEnabled() throws InterruptedException {
    GraphDatabaseService database =
        builder()
            .setConfig(
                TestModuleBootstrapper.MODULE_ENABLED,
                TestModuleBootstrapper.MODULE_ENABLED.getDefaultValue())
            .setConfig(
                TestModuleBootstrapper.MODULE_CONFIG,
                TestModuleBootstrapper.MODULE_CONFIG.getDefaultValue())
            .newGraphDatabase();

    registerShutdownHook(database);

    try (Transaction tx = database.beginTx()) {
      database.createNode(); // tx just to kick off Runtime init
      tx.success();
    }

    assertEquals(1, TEST_RUNTIME_MODULES.size());
    assertTrue(TEST_RUNTIME_MODULES.get(0).isInitialized());
    assertEquals("configValue", TEST_RUNTIME_MODULES.get(0).getConfig().get("configKey"));

    database.shutdown();

    assertFalse(TEST_RUNTIME_MODULES.get(0).isInitialized());
  }
Esempio n. 29
0
  public void rn() throws IOException {
    List<RoadLink> links = loadToDatabase("/Users/duduba/gj.mif", "/Users/duduba/gj.mid", false);

    GraphDatabaseService graphDb = neo.getDb();
    Index<Node> nodeIndex = neo.getNodeIndex();

    for (RoadLink link : links) {
      Transaction tx = graphDb.beginTx();
      try {

        Node fromNode = nodeIndex.get("id", link.fromNode).getSingle();
        if (fromNode == null) {
          fromNode = graphDb.createNode();
          fromNode.setProperty("id", link.fromNode);
          nodeIndex.add(fromNode, "id", link.fromNode);
        }
        Node toNode = nodeIndex.get("id", link.toNode).getSingle();
        if (toNode == null) {
          toNode = graphDb.createNode();
          toNode.setProperty("id", link.toNode);
          nodeIndex.add(toNode, "id", link.toNode);
        }

        Relationship r = fromNode.createRelationshipTo(toNode, Neo.RelTypes.TO);
        r.setProperty("no", link.no);
        r.setProperty("name", link.name);

        tx.success();
      } finally {
        tx.finish();
      }
    }
    logger.debug("haha, it's ok!");
  }
  /**
   * Tests an issue where loading all relationship types and property indexes after the neostore
   * data source had been started internally. The db would be in a state where it would need
   * recovery for the neostore data source, as well as some other data source. This would fail since
   * eventually TxManager#getTransaction() would be called, which would fail since it hadn't as of
   * yet recovered fully. Whereas that failure would happen in a listener and merely be logged, one
   * effect of it would be that there would seem to be no relationship types in the database.
   */
  @Test
  public void recoverNeoAndIndexHavingAllRelationshipTypesAfterRecovery() throws Exception {
    // Given (create transactions and kill process, leaving it needing for recovery)
    deleteRecursively(new File(dir));
    assertEquals(
        0,
        getRuntime()
            .exec(
                new String[] {
                  "java",
                  "-Djava.awt.headless=true",
                  "-cp",
                  getProperty("java.class.path"),
                  getClass().getName()
                })
            .waitFor());

    // When
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(dir);

    // Then
    try (Transaction ignored = db.beginTx()) {
      assertEquals(
          MyRelTypes.TEST.name(),
          GlobalGraphOperations.at(db).getAllRelationshipTypes().iterator().next().name());
    } finally {
      db.shutdown();
    }
  }