use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.
the class OGraphImporterMTAPITest method main.
public static void main(String[] args) throws IOException, InterruptedException {
OGlobalConfiguration.ENVIRONMENT_LOCK_MANAGER_CONCURRENCY_LEVEL.setValue(64);
// String dbUrl = "memory:amazonReviews";
final String dbUrl = "plocal:/temp/databases/amazonReviews";
final File f = new File("/temp/databases/amazonReviews");
if (f.exists())
OFileUtils.deleteRecursively(f);
final OrientGraph roGraph = new OrientGraph(dbUrl, "admin", "admin");
final OrientGraphNoTx graph = new OrientGraphNoTx(dbUrl, "admin", "admin");
OrientVertexType user = graph.createVertexType("User", 64);
user.createProperty("uid", OType.STRING);
user.createIndex("User.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
OrientVertexType product = graph.createVertexType("Product", 64);
product.createProperty("uid", OType.STRING);
product.createIndex("Product.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
graph.createEdgeType("Reviewed");
final File file = new File("/Users/luca/Downloads/ratings_Books.csv");
final BufferedReader br = new BufferedReader(new FileReader(file));
final AtomicLong retry = new AtomicLong();
Orient.instance().scheduleTask(new TimerTask() {
@Override
public void run() {
roGraph.makeActive();
final long vertexCount = roGraph.countVertices();
final long edgeCount = roGraph.countEdges();
System.out.println(String.format("%d vertices=%d %d/sec edges=%d %d/sec retry=%d", row, vertexCount, ((vertexCount - lastVertexCount) * 1000 / 2000), edgeCount, ((edgeCount - lastEdgeCount) * 1000 / 2000), retry.get()));
lastVertexCount = vertexCount;
lastEdgeCount = edgeCount;
}
}, 2000, 2000);
final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10000);
final Thread[] threads = new Thread[parallel];
for (int i = 0; i < parallel; ++i) {
threads[i] = new Thread() {
@Override
public void run() {
final OrientGraph localGraph = new OrientGraph(dbUrl, "admin", "admin", false);
final OIndex<?> userIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("User.uid");
final OIndex<?> productIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("Product.uid");
localGraph.begin();
for (int i = 0; ; ++i) {
final String line;
try {
line = queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
final String[] parts = line.split(",");
if (parts.length != 4) {
// SKIP IT
System.out.print("Skipped invalid line " + i + ": " + line);
continue;
}
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("score", new Float(parts[2]).intValue());
properties.put("date", Long.parseLong(parts[3]));
for (int localRetry = 0; localRetry < 100; ++localRetry) {
try {
final Object k1 = userIndex.get(parts[0]);
OrientVertex v1;
if (k1 == null) {
v1 = localGraph.addVertex("class:User", "uid", parts[0]);
} else
v1 = localGraph.getVertex(k1);
final Object k2 = productIndex.get(parts[1]);
OrientVertex v2;
if (k2 == null) {
v2 = localGraph.addVertex("class:Product", "uid", parts[1]);
} else
v2 = localGraph.getVertex(k2);
final OrientEdge edge = localGraph.addEdge(null, v1, v2, "Reviewed");
edge.setProperties(properties);
if (i % 2 == 0) {
localGraph.commit();
localGraph.begin();
}
break;
} catch (ONeedRetryException e) {
// RETRY
retry.incrementAndGet();
} catch (ORecordDuplicatedException e) {
// RETRY
retry.incrementAndGet();
}
}
}
}
};
}
for (int i = 0; i < parallel; ++i) threads[i].start();
try {
for (String line; (line = br.readLine()) != null; ) {
row++;
queue.put(line);
}
} finally {
br.close();
}
for (int i = 0; i < parallel; ++i) threads[i].join();
graph.shutdown();
roGraph.shutdown();
}
use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.
the class IndexConcurrencyTest method addSubTree.
public static void addSubTree(String parentName, char startLetter) {
ODatabaseDocumentTx db = new ODatabaseDocumentTx(url).open("admin", "admin");
for (int i = 0; ; ++i) {
try {
ODocument parent;
final List<ODocument> result = db.command(new OCommandSQL("select from Person where name = '" + parentName + "'")).execute();
parent = result.get(0);
if (parent == null) {
db.close();
return;
}
String newName = parentName;
newName += Character.toString(startLetter);
StringBuilder newIdentifier = new StringBuilder(newName);
newIdentifier.setCharAt(0, 'B');
db.begin();
PersonTree tree = new PersonTree();
tree.SetRoot(parent);
ODocument child = tree.AddChild(parent, newName, newIdentifier.toString());
buildTree(tree, child.getIdentity(), newName, subnodes, depth - 1, startLetter);
db.commit();
break;
} catch (ONeedRetryException e) {
System.out.println("Concurrency change, retry " + i);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// printPersons("After addSubTree", db);
db.close();
}
Aggregations