An example of a class that performs 3 operations:
- Create 100000 verticies of class 'Person' (extend V) with two properties
'id', 'name';
- Creates the 'Link' class (extend E)
- Connects via edge n vertices;
*CLASS MAIN*
public class threadSintetizzata {
static final String REMOTE = "remote:localhost/";
static final String NOMEDB = "popolate";
static final String CURRENTPATH = REMOTE + NOMEDB;
static final int NUMERODIVERTICI = 100000;
public static void main(String[] args) throws InterruptedException {
//CREATE 100000 VERTEX WITH 4 THREAD
ThreadParallel thread = new ThreadParallel(CURRENTPATH,
NUMERODIVERTICI);
System.out.println("start thread vertex--");
if (thread.StartConcurrentAccess()) {
System.out.println("End thread vertex ---");
}
//CREATE EDGE
OrientGraphNoTx g = new OrientGraphFactory(CURRENTPATH).getNoTx();
OClass edge;
edge = g.createEdgeType("MyEdge", "E");
System.out.println("---Edge created");
//CREATE LINK FROM VERTEX WITH EDGE
Map<String, Vertex> vertices = new HashMap<String, Vertex>();
for (Vertex v : g.getVertices())
vertices.put(v.getProperty("name").toString(), v);
//example to link n vertex
for(int i=0; i<20; i++){
g.addEdge("class:MyEdge", vertices.get("name"+i), vertices.get(
"name"+(i+1)), "link");
g.commit();
}
g.commit();
System.out.println("---link created");
g.shutdown();
System.out.println("end!!!");
}
}
*CLASS THREAD*
public class ThreadParallel {
private static OrientGraphFactory factory;
private static OrientGraphNoTx g;
private String url = "";
private static final String user = "admin";
private static final String pass = "admin";
private static final int THREADNUMBER = 4;
private static int numeroSubThread = 0; // total vertex =
(NUMEROSUBTHREAD*moltilicatoere) * 4 = 100000
private static int moltiplicatore = 1000;
private static int reportThread = 0; //per divisione in colonne dei
print
public ThreadParallel(String path, int numeroVertici) {
this.url = path;
numeroSubThread = (numeroVertici / THREADNUMBER)/
moltiplicatore ;
setup();
}
public void setup() {
factory = new OrientGraphFactory(url, user, pass).setupPool(1,4
);
}
public boolean StartConcurrentAccess() throws InterruptedException {
boolean terminato = false;
g = factory.getNoTx();
//crea la classe Persona
OClass clVertice = g.createVertexType("Person", "V");
clVertice.createProperty("id", OType.INTEGER);
clVertice.createProperty("name", OType.STRING);
//lancia i thread
ExecutorService executor = Executors.newFixedThreadPool(
THREADNUMBER);
for (int i = 0; i < THREADNUMBER; i++) {
Runnable worker = new MyThread(factory, i+1);
executor.execute(worker);
}
executor.shutdown();
//server per aspettare finche tutti i thread siano terminati
while (!executor.isTerminated()) {
}
g.shutdown();
terminato = true;
return terminato;
}
private static class MyThread implements Runnable {
OrientGraphNoTx noTxGraph;
public MyThread(OrientGraphFactory factory, int numberThred) {
noTxGraph = factory.getNoTx();
}
@Override
public void run() {
noTxGraph.getFeatures();
// every thread create 25000 vertex
for(int x=0; x < numeroSubThread; x++) {
System.out.print("##### "+x + " ");
reportThread++;
if ((reportThread % 4) == 0) {
System.out.print("\n");
}
int count = 0;
String name = "name";
for(int i=0;i < moltiplicatore; i++){
synchronized(MilioniVerticiInParalleloGetUniqueID.
getuniqueID()) {
ThreadParallelGetUniqueId myid =
ThreadParallelGetUniqueId.getuniqueID();
count = myid.getId();
noTxGraph.command(new OCommandSQL("insert into
Person(id, name) values("+count+",'name"+count+"' )")).execute();
myid.setId();
}
}
}
noTxGraph.shutdown();
}
}
}
*CLASS TO GET UNIQUE ID WITH TRHEAD*
public class ThreadParallelGetUniqueId {
private static ThreadParallelGetUniqueId uniqueID = new
ThreadParallelGetUniqueId();
//private static final java.util.concurrent.locks.Lock lock = new
java.util.concurrent.locks.ReentrantLock();
private int count = 0;
private ThreadParallelGetUniqueId() { }
public static ThreadParallelGetUniqueId getuniqueID() {
return uniqueID;
}
//public static Lock getLock() {
// return lock;
//}
public void setId(){
this.count++;
}
public int getId(){
return this.count;
}
}
This is just one example, the same thing can obviously be done in many ways
(also probably better than this). I hope it can help you.
--
---
You received this message because you are subscribed to the Google Groups
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.