Search in sources :

Example 1 with DatasetGraphTxn

use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.

the class T_TxnDeadlockTest method test.

//@Test
public void test() {
    final StoreConnection storeConnection = StoreConnection.make(Location.mem());
    //ExecutorService executor = Executors.newCachedThreadPool()  ;     // Not seen blocking. 
    // 4 blocks maybe 1 in 4 times
    // 8 blocks (quad core) 2 in 3 times.
    ExecutorService executor = Executors.newFixedThreadPool(8);
    final AtomicInteger nbQuadruplesAdded = new AtomicInteger();
    final CountDownLatch doneSignal = new CountDownLatch(CONCURRENT_RANDOM_OPERATIONS);
    for (int i = 0; i < CONCURRENT_RANDOM_OPERATIONS; i++) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if (numberGenerator.nextInt(2) == 0) {
                        DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.WRITE);
                        txnGraph.add(new Quad(NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt())));
                        txnGraph.commit();
                        txnGraph.end();
                        nbQuadruplesAdded.incrementAndGet();
                    } else {
                        DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
                        txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
                        //Iterator<Quad> iter = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
                        //Iter.count(iter) ; // Consume
                        txnGraph.end();
                    }
                } finally {
                    doneSignal.countDown();
                }
            }
        });
    }
    // shutdown is orderly so sync'ing up before the shutdown is nice but not needed.
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        executor.shutdown();
    }
    DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
    Iterator<Quad> result = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
    long count = 0;
    while (result.hasNext()) {
        result.next();
        count++;
    }
    txnGraph.end();
    StoreConnection.release(storeConnection.getLocation());
    //System.out.println() ;
    System.out.println("FINISHED");
//        // This is unsafe - the quad adds may generate duplicates (ity's unlikly 4 random number reoccur but it's possible). 
//        Assert.assertEquals(count, nbQuadruplesAdded.get());
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) Quad(org.apache.jena.sparql.core.Quad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with DatasetGraphTxn

use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.

the class AbstractStoreConnections method store_6.

@Test
public void store_6() {
    // Transaction - release - reattach
    // This tests that the dataset is sync'ed when going into transactional mode.
    StoreConnection sConn = getStoreConnection();
    Location loc = sConn.getLocation();
    DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
    dsgTxn.add(q1);
    assertTrue(dsgTxn.contains(q1));
    dsgTxn.commit();
    dsgTxn.end();
    sConn.forceRecoverFromJournal();
    assertTrue(sConn.getBaseDataset().contains(q1));
    StoreConnection.release(loc);
    sConn = StoreConnection.make(loc);
    DatasetGraph dsg2 = sConn.getBaseDataset();
    assertTrue(dsg2.contains(q1));
    DatasetGraphTxn dsgTxn2 = sConn.begin(ReadWrite.READ);
    assertTrue(dsgTxn2.contains(q1));
    dsgTxn2.end();
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Location(org.apache.jena.tdb.base.file.Location) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 3 with DatasetGraphTxn

use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.

the class AbstractStoreConnections method store_7.

@Test
public void store_7() {
    // No transaction, plain update, then transaction.
    // This tests that the dataset is sync'ed when going into transactional mode.
    boolean nonTxnData = true;
    StoreConnection sConn = getStoreConnection();
    Location loc = sConn.getLocation();
    DatasetGraph dsg = sConn.getBaseDataset();
    if (nonTxnData) {
        dsg.add(q);
        TDB.sync(dsg);
        assertTrue(dsg.contains(q));
    }
    DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
    if (nonTxnData)
        assertTrue(dsgTxn.contains(q));
    dsgTxn.add(q1);
    assertTrue(dsgTxn.contains(q1));
    if (nonTxnData)
        assertTrue(dsgTxn.contains(q));
    dsgTxn.commit();
    dsgTxn.end();
    // Should have flushed to disk.
    if (nonTxnData) {
        sConn.forceRecoverFromJournal();
        assertTrue(dsg.contains(q));
    }
    assertTrue(dsg.contains(q1));
    // release via the transactional machinery
    StoreConnection.release(loc);
    sConn = null;
    StoreConnection sConn2 = StoreConnection.make(loc);
    DatasetGraph dsg2 = sConn2.getBaseDataset();
    if (nonTxnData)
        assertTrue(dsg2.contains(q));
    assertTrue(dsg2.contains(q1));
    DatasetGraphTxn dsgTxn2 = sConn2.begin(ReadWrite.READ);
    if (nonTxnData)
        assertTrue(dsgTxn2.contains(q));
    assertTrue(dsgTxn2.contains(q1));
    dsgTxn2.end();
    // Check API methods work.
    Dataset ds = TDBFactory.createDataset(loc);
    ds.begin(ReadWrite.READ);
    Model m = (q.isDefaultGraph() ? ds.getDefaultModel() : ds.getNamedModel("g"));
    assertEquals(nonTxnData ? 2 : 1, m.size());
    ds.end();
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) Dataset(org.apache.jena.query.Dataset) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Model(org.apache.jena.rdf.model.Model) Location(org.apache.jena.tdb.base.file.Location) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 4 with DatasetGraphTxn

use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.

the class AbstractStoreConnections method store_3.

@Test(expected = TDBTransactionException.class)
public void store_3() {
    // Expel.
    StoreConnection sConn = getStoreConnection();
    DatasetGraphTxn dsgR1 = sConn.begin(ReadWrite.WRITE);
    StoreConnection.release(sConn.getLocation());
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 5 with DatasetGraphTxn

use of org.apache.jena.tdb.transaction.DatasetGraphTxn in project jena by apache.

the class AbstractStoreConnections method store_1.

@Test
public void store_1() {
    // Expel.
    StoreConnection sConn = getStoreConnection();
    DatasetGraphTxn dsgR1 = sConn.begin(ReadWrite.READ);
    DatasetGraphTxn dsgW1 = sConn.begin(ReadWrite.WRITE);
    dsgW1.add(q1);
    dsgW1.commit();
    dsgW1.end();
    dsgR1.end();
    assertTrue(sConn.isValid());
    StoreConnection.release(sConn.getLocation());
    assertFalse(sConn.isValid());
    sConn = null;
    StoreConnection sConn2 = getStoreConnection();
    assertTrue(sConn2.isValid());
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Aggregations

DatasetGraphTxn (org.apache.jena.tdb.transaction.DatasetGraphTxn)33 StoreConnection (org.apache.jena.tdb.StoreConnection)32 BaseTest (org.apache.jena.atlas.junit.BaseTest)27 Test (org.junit.Test)27 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)15 ConfigTest (org.apache.jena.tdb.ConfigTest)8 Dataset (org.apache.jena.query.Dataset)2 Quad (org.apache.jena.sparql.core.Quad)2 Location (org.apache.jena.tdb.base.file.Location)2 DatasetGraphTDB (org.apache.jena.tdb.store.DatasetGraphTDB)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Model (org.apache.jena.rdf.model.Model)1 BeforeClass (org.junit.BeforeClass)1