Java Code Examples for org.apache.jena.system.Txn#executeWrite()

The following examples show how to use org.apache.jena.system.Txn#executeWrite() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRDFChangesCancel.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Test public void changeSuppressEmptyCommit_4() {
    Quad q = SSE.parseQuad("(_ :s :p 'object')");
    Triple t = SSE.parseTriple("(:t :p 'object')");

    Txn.executeRead(dsg,   ()->{});
    testCounters(counter.summary(), 0, 0);

    Txn.executeWrite(dsg,  ()->{dsg.add(q);});
    testCounters(counter.summary(), 1, 0);

    Txn.executeWrite(dsg,  ()->{dsg.getDefaultGraph().add(t);});
    testCounters(counter.summary(), 2, 0);

    Txn.executeWrite(dsg,  ()->{dsg.getDefaultGraph().getPrefixMapping().setNsPrefix("", "http://example/");});
    testCounters(counter.summary(), 2, 1);

    Txn.executeWrite(dsg,  ()->{});
    testCounters(counter.summary(), 2, 1);
}
 
Example 2
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_graph_1() {
    Txn.executeWrite(dsg, ()-> {
        Graph g = dsg.getDefaultGraph();
        g.add(triple1);
    });
    DatasetGraph dsg2 = replay();
    check(dsg2, Quad.create(Quad.defaultGraphIRI, triple1));
}
 
Example 3
Source File: TestDatasetGraphWithAbort.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public void abort_data_1() {
    DatasetGraph dsg = create().asDatasetGraph() ;
    Txn.executeWrite(dsg, ()->dsg.add(q1)) ;
    Assert.assertTrue(dsg.contains(q1)) ;
    Assert.assertFalse(dsg.contains(q2)) ;
    dsg.begin(ReadWrite.WRITE);
    dsg.add(q2) ;
    dsg.abort();
    dsg.end();
    Assert.assertTrue(dsg.contains(q1)) ;
    Assert.assertFalse(dsg.contains(q2)) ;
}
 
Example 4
Source File: DeltaEx06_LocalDatasetToFuseki.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public static void main2(String ...args) {
    // ---- Fuseki server with patch operation.
    int PORT = 2020 ;
    // In-memory dataset
    DatasetGraph dsgFuseki = DatasetGraphFactory.createTxnMem();

    String serviceName = "patch";
    FusekiServer server = fusekiServerWithPatch("/ds", PORT, dsgFuseki, serviceName);
    server.start();

    // ---- Destination for changes is the Fuseki patch opration.
    String url = "http://localhost:"+PORT+"/ds/"+serviceName;

    RDFChanges changeSender = DeltaClientLib.destination(url);
    // In the case of http/https URLs, this is done with
    //    RDFChanges changeSender = new RDFChangesHTTP(url);

    // ---- Local dataset.
    DatasetGraph dsgLocal = DatasetGraphFactory.createTxnMem();
    // Combined datasetgraph and changes.
    // Changes will be POSTed to the URL.
    DatasetGraph dsg = RDFPatchOps.changes(dsgLocal, changeSender);

    // ---- Do something. Read in data.ttl inside a transaction.
    // (data.ttl is in src/main/resources/)
    Txn.executeWrite(dsg,
        ()->RDFDataMgr.read(dsg, "data.ttl")
        );

    // ---- Query Fuseki
    RDFConnection conn = RDFConnectionFactory.connect("http://localhost:"+PORT+"/ds");
    try( QueryExecution qExec = conn.query("PREFIX ex: <http://example.org/> SELECT * { ?s ?p ?o }") ) {
        ResultSet rs = qExec.execSelect();
        ResultSetFormatter.out(rs, qExec.getQuery());
    }
}
 
Example 5
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_add_add_1() {
    Txn.executeWrite(dsg, ()-> {
        dsg.add(quad1);
        dsg.add(quad2);
    });
    DatasetGraph dsg2 = replay();
    check(dsg2, quad1, quad2);
}
 
Example 6
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_add_delete_3() {
    Txn.executeWrite(dsg, ()-> {
        dsg.delete(quad2);
        dsg.add(quad1);
    });
    DatasetGraph dsg2 = replay();
    check(dsg2, quad1);
}
 
Example 7
Source File: TestRDFChangesCancel.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void changeSuppressEmptyCommit_3() {
    Txn.executeWrite(dsg, ()->dsg.add(quad1));

    PatchSummary s1 = counter.summary();
    assertEquals(1, s1.getCountTxnBegin());
    assertEquals(1, s1.getCountTxnCommit());
    assertEquals(0, s1.getCountTxnAbort());
}
 
Example 8
Source File: TestAssemblerFileLog.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void assemData() {
    Dataset ds = (Dataset)AssemblerUtils.build(ADIR+"/assem-logged.ttl", VocabPatch.tLoggedDataset);
    Txn.executeWrite(ds, ()->RDFDataMgr.read(ds, ADIR+"/data.ttl"));

    String patchfile = "target/filelog/log.rdfp.0001";
    assertTrue("Patch file does not exist: "+patchfile, FileOps.exists(patchfile));
    RDFPatch patch = RDFPatchOps.read(patchfile);
    DatasetGraph dsg1 = DatasetGraphFactory.createTxnMem();
    RDFPatchOps.applyChange(dsg1, patch);
    // Same term, no bnode isomorphism.
    boolean b = IsoMatcher.isomorphic(ds.asDatasetGraph(), dsg1);
    assertTrue(b);
}
 
Example 9
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_graph_3() {
    Txn.executeWrite(dsg, ()-> {
        Graph g = dsg.getDefaultGraph();
        g.add(triple1);
        dsg.delete(Quad.create(Quad.defaultGraphIRI, triple1));
    });
    DatasetGraph dsg2 = replay();
    check(dsg2);
}
 
Example 10
Source File: DeltaEx02_DatasetCollectPatch.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
public static void main(String ...args) {
    // -- Base dataset
    DatasetGraph dsgBase = DatasetGraphFactory.createTxnMem();

    // -- Destination for changes.
    // Text form of output.
    OutputStream out = System.out;
    // Create an RDFChanges that writes to "out".
    RDFChanges changeLog = RDFPatchOps.textWriter(out);


    // ---- Collect up changes.
    //RDFPatchOps.collect();
    RDFChangesCollector rcc = new RDFChangesCollector();
    DatasetGraph dsg = RDFPatchOps.changes(dsgBase, rcc);
    Dataset ds = DatasetFactory.wrap(dsg);
    Txn.executeWrite(ds,
                     ()->RDFDataMgr.read(dsg, "data.ttl")
                     );
    // Again - different bnodes.
    // Note all changes are recorded - even if they have no effect
    // (e.g the prefix, the triple "ex:s ex:p ex:o").
    Txn.executeWrite(ds,
                     ()->RDFDataMgr.read(dsg, "data.ttl")
                     );

    // Collected (in-memory) patch.
    RDFPatch patch = rcc.getRDFPatch();
    // Write it.
    patch.apply(changeLog);
}
 
Example 11
Source File: AbstractTestDeltaClient.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test
public void update_1() {
    // Create on the Delta link then setup DeltaClient
    DeltaLink dLink = getLink();
    String DS_NAME = "123";

    Id dsRef = dLink.newDataSource(DS_NAME, "http://example/datasource_update_1");
    DeltaClient dClient = createDeltaClient();
    dClient.register(dsRef, LocalStorageType.MEM, SyncPolicy.NONE);
    DeltaConnection dConn = dClient.get(DS_NAME);
    assertNotNull(dConn);
    assertEquals(Version.INIT, dConn.getLocalVersion());
    assertEquals(Version.INIT, dConn.getRemoteVersionLatest());

    Quad quad = SSE.parseQuad("(_ :s :p :o)");
    DatasetGraph dsg = dConn.getDatasetGraph();
    long x0 = Iter.count(dsg.find());
    assertEquals(0, x0);
    Txn.executeWrite(dsg, ()->dsg.add(quad));

    long x1 = Txn.calculateRead(dsg, ()->Iter.count(dsg.find()));
    assertEquals(1, x1);

    DatasetGraph dsgx = dConn.getStorage();
    long x2 = Txn.calculateRead(dsgx, ()->Iter.count(dsgx.find()));
    assertEquals(1, x1);

}
 
Example 12
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_remove_graph_1() {
    Txn.executeWrite(dsg, ()->dsg.add(quad1));
    Node gn = quad1.getGraph();

    Txn.executeWrite(dsg, ()-> {
        dsg.removeGraph(gn);
    });

    DatasetGraph dsg2 = replay();
    check(dsg2);
}
 
Example 13
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_graph_2() {
    Txn.executeWrite(dsg, ()-> {
        Graph g = dsg.getDefaultGraph();
        g.add(triple1);
        g.delete(triple1);
        g.add(triple2);
    });
    DatasetGraph dsg2 = replay();
    check(dsg2, Quad.create(Quad.defaultGraphIRI, triple2));
}
 
Example 14
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@Test public void record_add_delete_4() {
    Txn.executeWrite(dsg, ()-> {
        dsg.delete(quad1);
        dsg.add(quad1);
    });
    DatasetGraph dsg2 = replay();
    check(dsg2, quad1);
}
 
Example 15
Source File: RdfDocumentGraphConsumer.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void outputModel(String documentSourceName, OntModel model)
    throws AnalysisEngineProcessException {
  try (RDFConnection connect = createConnection()) {
    Txn.executeWrite(connect, () -> connect.load(model));
  } catch (Exception e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 16
Source File: TestManagedDatasetBuilder2.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test public void buildManaged_persistentZone() {
    // Restart.
    Location loc = Location.create(ZONE_DIR);
    Zone zone = Zone.connect(loc);

    Quad q1 = SSE.parseQuad("(:g :s :p 1)");
    Quad q2 = SSE.parseQuad("(:g :s :p 2)");

    {
        DatasetGraph dsg = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            .storageType(LocalStorageType.TDB2)
            .build();

        DeltaConnection conn = (DeltaConnection)(dsg.getContext().get(symDeltaConnection));
        Txn.executeWrite(dsg, ()->dsg.add(q1));
        Txn.executeRead(  conn.getDatasetGraph(), ()->assertTrue(conn.getDatasetGraph().contains(q1)) );
    }

    // Same zone
    Zone.clearZoneCache();
    zone = Zone.connect(loc);
    // Storage should be recovered from the on-disk state.

    {
        DatasetGraph dsg1 = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            //.storageType(LocalStorageType.TDB2) // Storage required. Should detect [FIXME]
            .build();
        DatasetGraph dsg2 = ManagedDatasetBuilder.create()
            .deltaLink(deltaLink)
            .logName("ABD")
            .zone(zone)
            .syncPolicy(SyncPolicy.TXN_RW)
            //.storageType(LocalStorageType.TDB) // Wrong storage - does not matter; dsg1 setup choice applies
            .build();
        DeltaConnection conn1 = (DeltaConnection)(dsg1.getContext().get(symDeltaConnection));
        DeltaConnection conn2 = (DeltaConnection)(dsg2.getContext().get(symDeltaConnection));

        Txn.executeRead(conn1.getDatasetGraph(), ()->assertTrue(conn1.getDatasetGraph().contains(q1)) );
        Txn.executeWrite(conn2.getDatasetGraph(), ()->conn2.getDatasetGraph().add(q2));
        Txn.executeRead(conn1.getDatasetGraph(), ()->assertTrue(conn1.getDatasetGraph().contains(q2)) );
    }
}
 
Example 17
Source File: load.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public static void main(String...argv) {
    Logger LOG = LoggerFactory.getLogger("Load");

    if ( argv.length == 0 ) {
        System.err.println("Usage: load FILE...");
        System.exit(1);
    }

    String[] args = argv; //new String[] {"/home/afs/Datasets/BSBM/bsbm-5m.nt.gz"};

    String DIR = "ZoneX";
    String URL = "http://localhost:1066/";
    String DS  = "DS";

    FileOps.ensureDir(DIR);
    FileOps.clearDirectory(DIR);
    Zone zone = Zone.connect(Location.create(DIR));
    DeltaLink dLink = DeltaLinkHTTP.connect(URL);

    DeltaClient dClient = DeltaClient.create(zone, dLink);
    Id dsRef = dClient.createDataSource(DS, "http://example/"+DS, LocalStorageType.TDB, SyncPolicy.TXN_RW);

    long count = -99;
    Timer timer = new Timer();
    timer.startTimer();
    try ( DeltaConnection dConn = dClient.get(dsRef) ) {
        DatasetGraph dsg = dConn.getDatasetGraph();
        StreamRDF dest = StreamRDFLib.dataset(dsg);
        StreamRDFCounting cdest = StreamRDFLib.count(dest);
        Txn.executeWrite(dsg, ()->{
            for ( String fn : args ) {
                System.out.printf("File: %s\n", fn);
                RDFDataMgr.parse(cdest, fn);
            }
        });
        count = cdest.count();
    }
    long x = timer.endTimer();
    double seconds = x/1000.0;
    FmtLog.info(LOG, "Time  = %.2fs\n", seconds);
    FmtLog.info(LOG, "Count = %,d\n", count);
    FmtLog.info(LOG, "Rate  = %,.2f TPS\n", count/seconds);
}
 
Example 18
Source File: AbstractTestDeltaConnection.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test
public void change_empty_commit_1() {
    Quad q = SSE.parseQuad("(:g :s :p :o)") ;
    String NAME = "change_empty_commit_1";
    DeltaClient dClient = createRegister(NAME);
    try(DeltaConnection dConn = dClient.get(NAME)) {

        Id patchId0 = dConn.getRemoteIdLatest();
        assertNull(patchId0);
        Version ver0 = dConn.getRemoteVersionLatest();

        // The "no empty commits" dsg
        DatasetGraph dsg = dConn.getDatasetGraphNoEmpty();

        Txn.executeWrite(dsg, ()->{});
        Id patchId1 = dConn.getLatestPatchId();
        Version ver1 = dConn.getRemoteVersionLatest();
        // No change at start of log.
        assertEquals(patchId0, patchId1);
        assertEquals(ver0, ver1);

        Txn.executeWrite(dsg, ()->dsg.add(q));
        Id patchId2 = dConn.getLatestPatchId();
        Version ver2 = dConn.getRemoteVersionLatest();
        assertNotEquals(patchId0, patchId2);
        assertNotEquals(ver0, ver2);

        DatasetGraph dsgx = dConn.getDatasetGraph();
        Txn.executeWrite(dsgx, ()->{});
        Id patchId3 = dConn.getLatestPatchId();
        Version ver3 = dConn.getRemoteVersionLatest();
        assertNotEquals(patchId2, patchId3);
        assertNotEquals(ver2, ver3);

        // No change mid log.
        Txn.executeWrite(dsg, ()->Iter.count(dsg.find()));
        Id patchId4 = dConn.getLatestPatchId();
        Version ver4 = dConn.getRemoteVersionLatest();
        assertEquals(patchId3, patchId4);
        assertEquals(ver3, ver4);

    }
}
 
Example 19
Source File: DeltaEx04_DatasetToPatchLogServer.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public static void main2(String ...args) {
    //-- Setup a PatchLogServer
    // Ensure its work area exists and is empty.
    FileOps.exists(PLOG_DIR);
    FileOps.clearAll(PLOG_DIR);

    DeltaServer server = DeltaServer.server(PLOG_PORT, PLOG_DIR);
    try { server.start(); }
    catch (BindException ex) {
        System.err.println("Can't start the patch log server: "+ex.getMessage());
        System.exit(1);
    }

    // -- Setup connection to the patch log server.
    DeltaLink dLink = DeltaLinkHTTP.connect("http://localhost:"+PLOG_PORT+"/");

    //-- Setup a Zone (workspace for recording local state like version of the dataset.
    // Ensure its work area exists and is empty.
    FileOps.exists(Zone_DIR);
    FileOps.clearAll(Zone_DIR);
    Zone zone = Zone.connect(Zone_DIR);
    DeltaClient dClient = DeltaClient.create(zone, dLink);

    // Create a patch log at the server
    Id dsRef = dClient.newDataSource(DS_NAME, "http://example/"+DS_NAME);

    // Create/attach the reference to a locally create TDB database.
    dClient.register(dsRef, LocalStorageType.TDB, SyncPolicy.TXN_RW);

    // "register" is these two steps:
    // dClient.attach(dsRef, LocalStorageType.TDB);
    // dClient.connect(dsRef, TxnSyncPolicy.TXN_RW);

    // -- Use the dataset via a DeltaConnection.

    try( DeltaConnection dConn = dClient.get(dsRef) ) {
        Dataset ds = dConn.getDataset();
        Txn.executeWrite(ds,
            ()->RDFDataMgr.read(ds, "data.ttl")
            );
    }

    server.stop();

    // -- Dump the patch.
    // The patch will be in "DeltaServer/ABCD/Log/patch-0001".
    System.out.println();
    RDFPatch patch = RDFPatchOps.read("DeltaServer/ABCD/Log/patch-0001");
    RDFPatchOps.write(System.out, patch);
    //Files.copy(Paths.get("DeltaServer/ABCD/Log/patch-0001"), System.out);
    System.exit(0);
}
 
Example 20
Source File: TestManagedDatasetBuilder.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Test public void buildManaged_2_sameZone() {
    Zone zone = Zone.connect(Location.mem());
    DatasetGraph dsg1 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink)
        .logName("ABC")
        .zone(zone)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();
    // Rebuild from with same zone. Should get the same underlying storage database.
    // i.e. same Zone item.

    DeltaLink deltaLink2 = DeltaLinkLocal.connect(localServer);
    DatasetGraph dsg2 = ManagedDatasetBuilder.create()
        .deltaLink(deltaLink2)
        .logName("ABC")
        .zone(zone)
        .syncPolicy(SyncPolicy.TXN_RW)
        .storageType(LocalStorageType.MEM)
        .build();

    assertNotSame(dsg1, dsg2);
    DeltaConnection conn1 = (DeltaConnection)(dsg1.getContext().get(symDeltaConnection));
    DeltaConnection conn2 = (DeltaConnection)(dsg2.getContext().get(symDeltaConnection));
    // Same zone, same underlying storage client-side
    assertSame(conn1.getStorage(), conn2.getStorage());

    Quad q1 = SSE.parseQuad("(:g :s :p 1)");
    Quad q2 = SSE.parseQuad("(:g :s :p 2)");
    Txn.executeWrite(dsg1, ()->dsg1.add(q1));
    Txn.executeRead(dsg2, ()->assertTrue(dsg2.contains(q1)));

    try ( DeltaConnection c = conn1 ) {
        DatasetGraph dsg  = c.getDatasetGraph();
        Txn.executeRead(  c.getDatasetGraph(), ()->assertTrue(c.getDatasetGraph().contains(q1)) );
        Txn.executeWrite( c.getDatasetGraph(), ()->c.getDatasetGraph().add(q2) );
    }
    try ( DeltaConnection c = conn2 ) {
        Txn.executeRead(c.getDatasetGraph(), ()->assertTrue(c.getDatasetGraph().contains(q2)));
    }
}