Search in sources :

Example 11 with GeogigTransaction

use of org.locationtech.geogig.api.GeogigTransaction in project GeoGig by boundlessgeo.

the class OSMDownload method runInternal.

@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(filterFile != null ^ bbox != null || update, "You must specify a filter file or a bounding box");
    checkParameter((filterFile != null || bbox != null) ^ update, "Filters cannot be used when updating");
    GeoGIG geogig = cli.getGeogig();
    checkState(geogig.getRepository().index().isClean() && geogig.getRepository().workingTree().isClean(), "Working tree and index are not clean");
    checkParameter(!rebase || update, "--rebase switch can only be used when updating");
    checkParameter(filterFile == null || filterFile.exists(), "The specified filter file does not exist");
    checkParameter(bbox == null || bbox.size() == 4, "The specified bounding box is not correct");
    osmAPIUrl = resolveAPIURL();
    Optional<OSMReport> report;
    GeogigTransaction tx = geogig.command(TransactionBegin.class).call();
    try {
        AbstractGeoGigOp<Optional<OSMReport>> cmd;
        if (update) {
            cmd = tx.command(OSMUpdateOp.class).setAPIUrl(osmAPIUrl).setRebase(rebase).setMessage(message).setProgressListener(cli.getProgressListener());
        } else {
            cmd = tx.command(OSMDownloadOp.class).setBbox(bbox).setFilterFile(filterFile).setKeepFiles(keepFiles).setMessage(message).setMappingFile(mappingFile).setOsmAPIUrl(osmAPIUrl).setSaveFile(saveFile).setProgressListener(cli.getProgressListener());
        }
        report = cmd.call();
        tx.commit();
    } catch (RuntimeException e) {
        tx.abort();
        if (e instanceof NothingToCommitException) {
            throw new CommandFailedException(e.getMessage(), e);
        }
        throw e;
    }
    if (report.isPresent()) {
        OSMReport rep = report.get();
        String msg;
        if (rep.getUnpprocessedCount() > 0) {
            msg = String.format("\nSome elements returned by the specified filter could not be processed.\n" + "Processed entities: %,d.\nWrong or uncomplete elements: %,d.\nNodes: %,d.\nWays: %,d.\n", rep.getCount(), rep.getUnpprocessedCount(), rep.getNodeCount(), rep.getWayCount());
        } else {
            msg = String.format("\nProcessed entities: %,d.\n Nodes: %,d.\n Ways: %,d\n", rep.getCount(), rep.getNodeCount(), rep.getWayCount());
        }
        cli.getConsole().println(msg);
    }
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) Optional(com.google.common.base.Optional) NothingToCommitException(org.locationtech.geogig.api.porcelain.NothingToCommitException) OSMReport(org.locationtech.geogig.osm.internal.OSMReport) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) OSMUpdateOp(org.locationtech.geogig.osm.internal.OSMUpdateOp) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) OSMDownloadOp(org.locationtech.geogig.osm.internal.OSMDownloadOp) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 12 with GeogigTransaction

use of org.locationtech.geogig.api.GeogigTransaction in project GeoGig by boundlessgeo.

the class GeogigTransactionTest method testBranchCreateCollision.

@Test
public void testBranchCreateCollision() throws Exception {
    // make a commit
    insertAndAdd(points1);
    RevCommit mainCommit = geogig.command(CommitOp.class).setMessage("Commit1").call();
    // start the first transaction
    GeogigTransaction transaction1 = geogig.command(TransactionBegin.class).call();
    // make a new branch
    transaction1.command(BranchCreateOp.class).setAutoCheckout(true).setName("branch1").call();
    // perform a commit in the transaction
    insertAndAdd(transaction1, points2);
    RevCommit transaction1Commit = transaction1.command(CommitOp.class).setMessage("Commit2").call();
    // Verify that the base repository is unchanged
    Iterator<RevCommit> logs = geogig.command(LogOp.class).call();
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    // Verify that the transaction has the commit
    logs = transaction1.command(LogOp.class).call();
    assertEquals(logs.next(), transaction1Commit);
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    // start the second transaction
    GeogigTransaction transaction2 = geogig.command(TransactionBegin.class).call();
    // make a new branch
    transaction2.command(BranchCreateOp.class).setAutoCheckout(true).setName("branch1").call();
    // perform a commit in the transaction
    insertAndAdd(transaction2, points3);
    RevCommit transaction2Commit = transaction2.command(CommitOp.class).setMessage("Commit3").call();
    // Verify that the base repository is unchanged
    logs = geogig.command(LogOp.class).call();
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    // Verify that the transaction has the commit
    logs = transaction2.command(LogOp.class).call();
    assertEquals(logs.next(), transaction2Commit);
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    // Commit the first transaction
    geogig.command(TransactionEnd.class).setTransaction(transaction1).setRebase(true).call();
    // Verify that the base repository has the changes from the transaction
    logs = geogig.command(LogOp.class).call();
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    geogig.command(CheckoutOp.class).setSource("branch1").call();
    logs = geogig.command(LogOp.class).call();
    assertEquals(logs.next(), transaction1Commit);
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
    // Now try to commit the second transaction
    geogig.command(TransactionEnd.class).setTransaction(transaction2).setRebase(true).call();
    // Verify that the base repository has the changes from the transaction
    logs = geogig.command(LogOp.class).call();
    RevCommit lastCommit = logs.next();
    assertFalse(lastCommit.equals(transaction2Commit));
    assertEquals(lastCommit.getMessage(), transaction2Commit.getMessage());
    assertEquals(lastCommit.getAuthor(), transaction2Commit.getAuthor());
    assertEquals(lastCommit.getCommitter().getName(), transaction2Commit.getCommitter().getName());
    assertFalse(lastCommit.getCommitter().getTimestamp() == transaction2Commit.getCommitter().getTimestamp());
    assertEquals(logs.next(), transaction1Commit);
    assertEquals(logs.next(), mainCommit);
    assertFalse(logs.hasNext());
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) BranchCreateOp(org.locationtech.geogig.api.porcelain.BranchCreateOp) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) LogOp(org.locationtech.geogig.api.porcelain.LogOp) RevCommit(org.locationtech.geogig.api.RevCommit) TransactionEnd(org.locationtech.geogig.api.plumbing.TransactionEnd) Test(org.junit.Test)

Example 13 with GeogigTransaction

use of org.locationtech.geogig.api.GeogigTransaction in project GeoGig by boundlessgeo.

the class GeogigTransactionTest method testSyncTransaction.

@Test
public void testSyncTransaction() throws Exception {
    LinkedList<RevCommit> expectedMain = new LinkedList<RevCommit>();
    LinkedList<RevCommit> expectedTransaction = new LinkedList<RevCommit>();
    // make a commit
    insertAndAdd(points1);
    RevCommit firstCommit = geogig.command(CommitOp.class).call();
    expectedMain.addFirst(firstCommit);
    expectedTransaction.addFirst(firstCommit);
    // start a transaction
    GeogigTransaction t = geogig.command(TransactionBegin.class).call();
    // perform a commit in the transaction
    insertAndAdd(t, points2);
    RevCommit transactionCommit = t.command(CommitOp.class).call();
    expectedTransaction.addFirst(transactionCommit);
    // perform a commit on the repo
    insertAndAdd(points3);
    RevCommit repoCommit = geogig.command(CommitOp.class).call();
    expectedMain.addFirst(repoCommit);
    // Verify that the base repository is unchanged
    Iterator<RevCommit> logs = geogig.command(LogOp.class).call();
    List<RevCommit> logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expectedMain, logged);
    // Verify that the transaction has the commit
    logs = t.command(LogOp.class).call();
    logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expectedTransaction, logged);
    // Commit the transaction
    geogig.command(TransactionEnd.class).setTransaction(t).call();
    // Verify that a merge commit was created
    logs = geogig.command(LogOp.class).call();
    RevCommit lastCommit = logs.next();
    assertFalse(lastCommit.equals(repoCommit));
    assertTrue(lastCommit.getMessage().contains("Merge commit"));
    assertEquals(lastCommit.getParentIds().get(0), transactionCommit.getId());
    assertEquals(lastCommit.getParentIds().get(1), repoCommit.getId());
    assertEquals(logs.next(), repoCommit);
    assertEquals(logs.next(), transactionCommit);
    assertEquals(logs.next(), firstCommit);
    assertFalse(logs.hasNext());
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) LogOp(org.locationtech.geogig.api.porcelain.LogOp) ArrayList(java.util.ArrayList) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) LinkedList(java.util.LinkedList) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 14 with GeogigTransaction

use of org.locationtech.geogig.api.GeogigTransaction in project GeoGig by boundlessgeo.

the class GeogigTransactionTest method testCancelTransaction.

@Test
public void testCancelTransaction() throws Exception {
    LinkedList<RevCommit> expectedMain = new LinkedList<RevCommit>();
    // make a commit
    insertAndAdd(points1);
    RevCommit commit = geogig.command(CommitOp.class).call();
    expectedMain.addFirst(commit);
    // start a transaction
    GeogigTransaction t = geogig.command(TransactionBegin.class).call();
    // perform a commit in the transaction
    insertAndAdd(t, points2);
    commit = t.command(CommitOp.class).call();
    // Verify that the base repository is unchanged
    Iterator<RevCommit> logs = geogig.command(LogOp.class).call();
    List<RevCommit> logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expectedMain, logged);
    // Cancel the transaction
    geogig.command(TransactionEnd.class).setCancel(true).setTransaction(t).call();
    // Verify that the base repository is unchanged
    logs = geogig.command(LogOp.class).call();
    logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expectedMain, logged);
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) LogOp(org.locationtech.geogig.api.porcelain.LogOp) ArrayList(java.util.ArrayList) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) LinkedList(java.util.LinkedList) RevCommit(org.locationtech.geogig.api.RevCommit) TransactionEnd(org.locationtech.geogig.api.plumbing.TransactionEnd) Test(org.junit.Test)

Example 15 with GeogigTransaction

use of org.locationtech.geogig.api.GeogigTransaction in project GeoGig by boundlessgeo.

the class GeogigTransactionTest method testConflictIsolation.

@Test
public void testConflictIsolation() throws Exception {
    insertAndAdd(points2);
    geogig.command(CommitOp.class).setMessage("foo").call();
    geogig.command(BranchCreateOp.class).setAutoCheckout(true).setName("branch1").call();
    insertAndAdd(points1);
    RevCommit mainCommit = geogig.command(CommitOp.class).setMessage("Commit1").call();
    geogig.command(CheckoutOp.class).setSource("master").call();
    insertAndAdd(points1_modified);
    RevCommit modifiedCommit = geogig.command(CommitOp.class).setMessage("Commit2").call();
    GeogigTransaction tx = geogig.command(TransactionBegin.class).call();
    try {
        tx.command(MergeOp.class).addCommit(Suppliers.ofInstance(mainCommit.getId())).call();
        fail("Expected a merge conflict!");
    } catch (org.locationtech.geogig.api.porcelain.MergeConflictsException e) {
    // expected.
    }
    List<Conflict> txConflicts = tx.command(ConflictsReadOp.class).call();
    List<Conflict> baseConflicts = geogig.command(ConflictsReadOp.class).call();
    assertTrue("There should be no conflicts outside the transaction", baseConflicts.size() == 0);
    assertTrue("There should be conflicts in the transaction", txConflicts.size() != 0);
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) ConflictsReadOp(org.locationtech.geogig.api.plumbing.merge.ConflictsReadOp) BranchCreateOp(org.locationtech.geogig.api.porcelain.BranchCreateOp) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Aggregations

GeogigTransaction (org.locationtech.geogig.api.GeogigTransaction)19 TransactionBegin (org.locationtech.geogig.api.plumbing.TransactionBegin)14 Test (org.junit.Test)10 RevCommit (org.locationtech.geogig.api.RevCommit)10 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)7 LogOp (org.locationtech.geogig.api.porcelain.LogOp)6 TransactionEnd (org.locationtech.geogig.api.plumbing.TransactionEnd)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Optional (com.google.common.base.Optional)3 Context (org.locationtech.geogig.api.Context)3 GeoGIG (org.locationtech.geogig.api.GeoGIG)3 Ref (org.locationtech.geogig.api.Ref)3 CommandResponse (org.locationtech.geogig.web.api.CommandResponse)3 CommandSpecException (org.locationtech.geogig.web.api.CommandSpecException)3 ResponseWriter (org.locationtech.geogig.web.api.ResponseWriter)3 ObjectId (org.locationtech.geogig.api.ObjectId)2 UpdateRef (org.locationtech.geogig.api.plumbing.UpdateRef)2 MergeScenarioReport (org.locationtech.geogig.api.plumbing.merge.MergeScenarioReport)2 BranchCreateOp (org.locationtech.geogig.api.porcelain.BranchCreateOp)2