Search in sources :

Example 1 with GeogigTransaction

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

the class GeogigTransactionTest method testCommitUpdatesRemoteRefs.

@Test
public void testCommitUpdatesRemoteRefs() throws Exception {
    // make a commit
    insertAndAdd(points1);
    RevCommit headCommit = geogig.command(CommitOp.class).call();
    geogig.command(RemoteAddOp.class).setName("upstream").setURL("http://test.com/geogig/upstream").call();
    final String remoteRef = "refs/remotes/upstream/master";
    final String unchangedRemoteRef = "refs/remotes/upstream/testbranch";
    Ref remoteHead = geogig.command(UpdateRef.class).setName(remoteRef).setNewValue(headCommit.getId()).call().get();
    assertEquals(headCommit.getId(), remoteHead.getObjectId());
    geogig.command(UpdateRef.class).setName(unchangedRemoteRef).setNewValue(headCommit.getId()).call().get();
    // start a transaction
    GeogigTransaction tx = geogig.command(TransactionBegin.class).call();
    // make a commit
    insertAndAdd(tx, points2);
    RevCommit newcommit = tx.command(CommitOp.class).call();
    // upadte remote
    Ref txRemoteHead = tx.command(UpdateRef.class).setName(remoteRef).setNewValue(newcommit.getId()).call().get();
    assertEquals(newcommit.getId(), txRemoteHead.getObjectId());
    // commit transaction
    tx.commit();
    txRemoteHead = geogig.command(RefParse.class).setName(remoteRef).call().get();
    assertEquals(newcommit.getId(), txRemoteHead.getObjectId());
    txRemoteHead = geogig.command(RefParse.class).setName(unchangedRemoteRef).call().get();
    assertEquals(headCommit.getId(), txRemoteHead.getObjectId());
}
Also used : Ref(org.locationtech.geogig.api.Ref) UpdateRef(org.locationtech.geogig.api.plumbing.UpdateRef) RemoteAddOp(org.locationtech.geogig.api.porcelain.RemoteAddOp) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 2 with GeogigTransaction

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

the class GeogigTransactionTest method testTransaction.

@Test
public void testTransaction() throws Exception {
    LinkedList<RevCommit> expectedMain = new LinkedList<RevCommit>();
    LinkedList<RevCommit> expectedTransaction = new LinkedList<RevCommit>();
    // make a commit
    insertAndAdd(points1);
    RevCommit commit = geogig.command(CommitOp.class).call();
    expectedMain.addFirst(commit);
    expectedTransaction.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();
    expectedTransaction.addFirst(commit);
    // 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).setRebase(true).call();
    // Verify that the base repository has the changes from the transaction
    logs = geogig.command(LogOp.class).call();
    logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expectedTransaction, 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 3 with GeogigTransaction

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

the class GeogigTransactionTest method testEndWithinTransaction.

@Test
public void testEndWithinTransaction() throws Exception {
    // make a commit
    insertAndAdd(points1);
    geogig.command(CommitOp.class).call();
    // start a transaction
    GeogigTransaction t = geogig.command(TransactionBegin.class).call();
    // perform a commit in the transaction
    insertAndAdd(t, points2);
    t.command(CommitOp.class).call();
    // End the transaction
    exception.expect(IllegalStateException.class);
    t.command(TransactionEnd.class).setTransaction(t).call();
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) Test(org.junit.Test)

Example 4 with GeogigTransaction

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

the class TransactionBegin method _call.

/**
     * Creates a new transaction and returns it.
     * 
     * @return the {@link GeogigTransaction} that was created by the operation
     */
@Override
protected GeogigTransaction _call() {
    Preconditions.checkState(!(context instanceof GeogigTransaction), "Cannot start a new transaction within a transaction!");
    GeogigTransaction t = new GeogigTransaction(context, UUID.randomUUID());
    // Lock the repository
    try {
        refDatabase().lock();
    } catch (TimeoutException e) {
        Throwables.propagate(e);
    }
    try {
        // Copy original refs
        t.create();
    } finally {
        // Unlock the repository
        refDatabase().unlock();
    }
    // Return the transaction
    return t;
}
Also used : GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with GeogigTransaction

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

the class GeoGigDataStore method createSchema.

/**
     * Creates a new feature type tree on the {@link #getOrFigureOutBranch() current branch}.
     * <p>
     * Implementation detail: the operation is the homologous to starting a transaction, checking
     * out the current/configured branch, creating the type tree inside the transaction, issueing a
     * geogig commit, and committing the transaction for the created tree to be merged onto the
     * configured branch.
     */
@Override
public void createSchema(SimpleFeatureType featureType) throws IOException {
    if (!allowTransactions) {
        throw new IllegalStateException("Configured head " + refspec + " is not a branch; transactions are not supported.");
    }
    GeogigTransaction tx = getCommandLocator(null).command(TransactionBegin.class).call();
    boolean abort = false;
    try {
        String treePath = featureType.getName().getLocalPart();
        // check out the datastore branch on the transaction space
        final String branch = getOrFigureOutBranch();
        tx.command(CheckoutOp.class).setForce(true).setSource(branch).call();
        // now we can use the transaction working tree with the correct branch checked out
        WorkingTree workingTree = tx.workingTree();
        workingTree.createTypeTree(treePath, featureType);
        tx.command(AddOp.class).addPattern(treePath).call();
        tx.command(CommitOp.class).setMessage("Created feature type tree " + treePath).call();
        tx.commit();
    } catch (IllegalArgumentException alreadyExists) {
        abort = true;
        throw new IOException(alreadyExists.getMessage(), alreadyExists);
    } catch (Exception e) {
        abort = true;
        throw Throwables.propagate(e);
    } finally {
        if (abort) {
            tx.abort();
        }
    }
}
Also used : WorkingTree(org.locationtech.geogig.repository.WorkingTree) GeogigTransaction(org.locationtech.geogig.api.GeogigTransaction) TransactionBegin(org.locationtech.geogig.api.plumbing.TransactionBegin) CheckoutOp(org.locationtech.geogig.api.porcelain.CheckoutOp) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

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