Skip to content

Instantly share code, notes, and snippets.

@phatduckk
Created December 10, 2009 18:43
Show Gist options
  • Save phatduckk/253553 to your computer and use it in GitHub Desktop.
Save phatduckk/253553 to your computer and use it in GitHub Desktop.
package com.digg.tmp;
import org.neo4j.api.core.*;
import org.neo4j.util.index.SingleValueIndex;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Hack example that reveals a "More than one relationship" issue
*
* User: Arin Sarkissian
* Date: Dec 10, 2009
* Time: 9:40:27 AM
*/
public class SVINotFound {
private static final String USERNAME_INDEX = "usernameIndex";
private static final int N_THREADS = 5;
private static final int NUM_LINES = 300000;
public static void main(String[] args) {
NeoService neo = new EmbeddedNeo("test-" + System.currentTimeMillis());
Transaction tx = neo.beginTx();
Node indexNode = null;
// use node #1 as the index node & create it if it doesn't exist
// is there a better way to do this?
try {
indexNode = neo.getNodeById(1);
} catch (NotFoundException nfe) {
indexNode = neo.createNode();
} finally {
tx.success();
tx.finish();
}
// now create the index. do i need a transaction about this?
// setup a pool
final SingleValueIndex svi = new SingleValueIndex(USERNAME_INDEX, indexNode, neo);
final ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS);
// now here's some fake data that we'll index in threads
final String[] fakeData = {"phatduckk", "arin sarkissian"};
for (int i = 0; i < NUM_LINES; i++) {
System.out.println("Processing line: " + i);
IndexRunner command = new IndexRunner(fakeData, neo, svi);
executorService.execute(command);
}
}
static class IndexRunner implements Runnable {
NeoService neo;
SingleValueIndex svi;
String[] fakeData;
IndexRunner(String[] fakeData, NeoService neo, SingleValueIndex svi) {
this.fakeData = fakeData;
this.neo = neo;
this.svi = svi;
}
public void run() {
Iterable<Node> foundInIndex = svi.getNodesFor(fakeData[0]);
if (foundInIndex.iterator().hasNext()) {
foundInIndex.iterator().next();
if (foundInIndex.iterator().hasNext()) {
// why are there multiple values in a single value index?
// this next line will get mad
svi.getSingleNodeFor(fakeData[0]);
System.exit(-1);
}
} else {
Transaction nodetx = neo.beginTx();
Node newNode = neo.createNode();
newNode.setProperty("username", fakeData[0]);
newNode.setProperty("fullName", fakeData[1]);
svi.index(newNode, fakeData[0]);
nodetx.success();
nodetx.finish();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment