public void testIt() throws Exception {
    String key = "mykey";
    String value = "myvalue";
    Collection<Node> nodes = new ArrayList<Node>();
    for (int i = 0; i < 20000; i++) {
      Node node = graphDb().createNode();
      indexService.index(node, key, value);
      nodes.add(node);
      if (i == 2000) {
        Iterable<Node> itr = indexService.getNodes(key, value);
        assertCollection(asCollection(itr), nodes.toArray(new Node[0]));
      }

      if (i % 10000 == 0) {
        restartTx();
      }
    }
    restartTx();

    Node[] nodeArray = nodes.toArray(new Node[0]);
    long total = 0;
    long totalTotal = 0;
    int counter = 0;
    for (int i = 0; i < 10; i++) {
      // So that it'll get the nodes in the synchronous way
      ((LuceneIndexService) indexService).setLazySearchResultThreshold(nodes.size() + 10);
      long time = System.currentTimeMillis();
      Iterable<Node> itr = indexService.getNodes(key, value);
      long syncTime = System.currentTimeMillis() - time;
      assertCollection(asCollection(itr), nodeArray);
      long syncTotalTime = System.currentTimeMillis() - time;

      // So that it'll get the nodes in the lazy way
      ((LuceneIndexService) indexService).setLazySearchResultThreshold(nodes.size() - 10);
      time = System.currentTimeMillis();
      itr = indexService.getNodes(key, value);
      long lazyTime = System.currentTimeMillis() - time;
      assertCollection(asCollection(itr), nodeArray);
      long lazyTotalTime = System.currentTimeMillis() - time;
      //            System.out.println( "lazy:" + lazyTime + " (" + lazyTotalTime +
      //                "), sync:" + syncTime + " (" + syncTotalTime + ")" );

      if (i > 0) {
        total += syncTime;
        totalTotal += syncTotalTime;
        counter++;
      }

      // At the very least
      assertTrue(lazyTime < syncTime / 3);
    }

    //        System.out.println( "avg:" + ( total / counter ) + ", " +
    //            ( totalTotal / counter ) );

    for (Node node : nodes) {
      node.delete();
    }
  }
  @Test
  public void backup() throws IOException {
    System.out.println("starting tests");
    EmbeddedGraphDatabase graphDb = Util.startGraphDbInstance(STORE_LOCATION_DIR);
    IndexService indexService = new LuceneIndexService(graphDb);

    configureSourceDb(graphDb);

    System.out.println("backing up original db without any changes");
    tryBackup(graphDb, BACKUP_LOCATION_DIR, 1);

    Transaction tx = graphDb.beginTx();
    try {
      indexService.index(addNode(graphDb), "number", 2);
      tx.success();
    } finally {
      tx.finish();
    }
    System.out.println("one node added");
    tryBackup(graphDb, BACKUP_LOCATION_DIR, 2);

    tx = graphDb.beginTx();
    try {
      indexService.index(addNode(graphDb), "number", 3);
      tx.success();
    } finally {
      tx.finish();
    }
    System.out.println("one node added");

    tx = graphDb.beginTx();
    try {
      indexService.index(addNode(graphDb), "number", 4);
      System.out.println("one node added, not commited");
      tryBackup(graphDb, BACKUP_LOCATION_DIR, 3);
      tx.success();
    } finally {
      tx.finish();
    }
    System.out.println("previous add commited");
    tryBackup(graphDb, BACKUP_LOCATION_DIR, 4);

    Util.stopGraphDb(graphDb, indexService);
  }
Example #3
0
  public Node indexToNode(String index_name, final String index_value) {

    Node user = null;
    try {
      user = indexService.getSingleNode(index_name, index_value);
    } catch (Exception e) {
      System.out.println("exception in indexToNode function");
    }

    return user;
  }
  protected void tryBackup(EmbeddedGraphDatabase graphDb, String location, int relCount)
      throws IOException {
    setupBackup(graphDb, location);

    EmbeddedGraphDatabase bDb = Util.startGraphDbInstance(location);
    IndexService bIndexService = new LuceneIndexService(bDb);
    Transaction bTx = bDb.beginTx();
    try {
      List<Relationship> rels = new ArrayList<Relationship>();
      for (Relationship rel : bDb.getReferenceNode().getRelationships()) {
        rels.add(rel);
      }
      assertEquals(relCount, rels.size());
      Node node = bIndexService.getSingleNode("number", relCount);
      assertEquals(true, node != null);
      assertEquals(node.getId(), (long) (Long) node.getProperty("theId", -1L));
      bTx.success();
    } finally {
      bTx.finish();
    }
    Util.stopGraphDb(bDb, bIndexService);
  }
  @Before
  public void setup() {
    Util.deleteDir(new File(VAR));

    System.out.println("setting up database and backup-copy including Lucene");

    EmbeddedGraphDatabase graphDb = Util.startGraphDbInstance(STORE_LOCATION_DIR);

    IndexService indexService = new LuceneIndexService(graphDb);

    configureSourceDb(graphDb);

    Transaction tx = graphDb.beginTx();
    try {
      indexService.index(addNode(graphDb), "number", 1);
      tx.success();
    } finally {
      tx.finish();
    }
    Util.stopGraphDb(graphDb, indexService);

    Util.copyDir(STORE_LOCATION_DIR, BACKUP_LOCATION_DIR);
  }
Example #6
0
  public Node createAndIndexNode(String index_name, final String index_value) {

    System.out.println("index_value=" + index_value);
    if (db == null) {
      System.out.println("empty db");
    }
    Node node = db.createNode();
    try {

      indexService.index(node, index_name, index_value);
    } catch (Exception e) {
      System.out.println("exception in createAndIndexNode function");
    }

    return node;
  }
 @Override
 public void close() {
   lucene.shutdown();
   graphDb.shutdown();
 }
Example #8
0
 public void shutdown(String s) {
   System.out.println("shutdown ->" + s);
   indexService.shutdown();
   db.shutdown();
 }