Any feedback?  Is this safe?

On Monday, April 10, 2017 at 8:01:55 PM UTC-4, odbuser wrote:
>
> I would like to use Graph Elements (not just the graph) across threads.  I 
> understand what is necessary to use the Graph across threads but want to 
> know if an already retrieved Element (and iterator) can also be used across 
> threads (safely) in a similar manner (simply by calling graph.makeActive() 
> before using the graph in each thread).
>
> The following code works as expected.  It uses 2 threads.  Everything 
> works as expected as the reused graph is only used from one thread at a 
> time (not concurrent).  This type of behavior is necessary in order to pass 
> state from thread to thread.  Otherwise, I'd have to retrieve the vertices 
> in each thread (by passing ids I guess) which would be highly inefficient - 
> especially when the results are large and can't fit in memory.
>
> Note that the Element.getGraph() will return null on the new thread but 
> will return the graph properly if you call OrientBaseGraph.makeActive() 
> first.
>
>     @Test
>     public void testGraphAcrossThread() throws InterruptedException, 
> ExecutionException {
>         //Create the graph
>         OrientBaseGraph g = new OrientGraphFactory(
> "plocal:tmp/orientdb/thread").getTx();
>         
>         //Create a vertex
>         OrientVertex oV = g.addVertex(null).setProperties("name", "Luigi"
> );
>         
>         //Query for the vertex
>         final Iterator<Vertex> iterator = g.getVertices("name","Luigi").
> iterator();
>         
>         //Create a new thread and submit a runnable that uses the same 
> Graph
>         Future<?> future = Executors.newSingleThreadExecutor().submit(new 
> Runnable() {
>             @Override
>             public void run() {
>                 //Element's graph will be null on this thread
>                 Assert.assertNull(oV.getGraph());
>                 
>                 //Make the graph active on the new thread
>                 g.makeActive();
>                 
>                 //Element's graph is now non-null on this thread
>                 Assert.assertNotNull(oV.getGraph());
>                 
>                 //Everything works as expected
>                 Assert.assertTrue(iterator.hasNext());
>                 Assert.assertEquals("Luigi", oV.getProperty("name"));
>                 Assert.assertEquals("Luigi", (String)iterator.next().
> getProperty("name"));
>                 oV.setProperty("age", "20");
>             }
>         });
>         
>         //Call get on the future so that the thread's work completes 
> before using the graph on this thread
>         future.get();
>         
>         //Graph is not null like it was on a new thread
>         Assert.assertNotNull(oV.getGraph());
>         
>         //But the everything still works as expected
>         Assert.assertEquals("20", oV.getProperty("age"));
>         Iterator<Vertex> iterator2 = g.getVertices("name","Luigi").
> iterator();
>         Assert.assertEquals("Luigi", (String)iterator2.next().getProperty(
> "name"));
>
>         g.commit();
>         g.drop();
>     }
>
>
>

-- 

--- 
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.

Reply via email to