When starting a named transaction then GitDbTransaction is returned

This commit is contained in:
Paul Campbell 2018-06-15 14:32:09 +01:00
parent 07bdac4b29
commit 57e39d71ad
6 changed files with 178 additions and 1 deletions

View file

@ -70,4 +70,26 @@ public interface GitDBBranch {
* @throws IOException error reading version
*/
Optional<Version> getFormatVersion() throws IOException;
/**
* Begins a new anonymous transaction.
*
* @return a new Transaction
*/
GitDBTransaction transaction() throws IOException;
/**
* Begins a new named transaction.
*
* @param name the transaction name
* @return a new Transaction
*/
GitDBTransaction transaction(String name) throws IOException;
/**
* Gets the name of the branch.
*
* @return the branch name
*/
String name();
}

View file

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2018 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.gitdb;
/**
* Represents a transaction (a git branch).
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface GitDBTransaction extends GitDBBranch {
/**
* The name of the transaction.
*
* @return the transaction name
*/
String name();
}

View file

@ -25,12 +25,14 @@ import com.github.zafarkhaja.semver.Version;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import java.io.*;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
/**
@ -46,6 +48,7 @@ class GitDBBranchImpl implements GitDBBranch {
private final GitDBRepo gitDBRepo;
private final String userName;
private final String userEmailAddress;
private final String name;
private static GitDBBranch select(
final Ref branchRef,
@ -53,7 +56,7 @@ class GitDBBranchImpl implements GitDBBranch {
final String userName,
final String userEmailAddress
) {
return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress);
return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress, branchRef.getName());
}
/**
@ -106,6 +109,23 @@ class GitDBBranchImpl implements GitDBBranch {
.map(Version::valueOf);
}
@Override
public GitDBTransaction transaction(String name) throws IOException {
final Ref ref = gitDBRepo.createBranch(branchRef, UUID.randomUUID().toString());
final GitDBBranch branch = new GitDBBranchImpl(ref, gitDBRepo, userName, userEmailAddress, name);
return new UnnamedTransaction(this, branch);
}
@Override
public GitDBTransaction transaction() throws IOException {
return transaction(UUID.randomUUID().toString());
}
@Override
public String name() {
return name;
}
private String commitMessageForAdd(final String key, final String value) {
return String.format("Add key [%s] = [%s]", key, value);
}

View file

@ -227,4 +227,8 @@ class GitDBRepo {
Optional<ObjectId> removeKey(final Ref branchRef, final String key) throws IOException {
return keyRemover.remove(branchRef, key);
}
Ref createBranch(final Ref branchRef, final String name) throws IOException {
return repository.getRefDatabase().newUpdate(name, false).getRef();
}
}

View file

@ -0,0 +1,79 @@
/*
The MIT License (MIT)
Copyright (c) 2018 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.gitdb.impl;
import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
import java.util.Optional;
/**
* An anonymous transaction.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
class UnnamedTransaction implements GitDBTransaction {
private final GitDBBranch base;
private final GitDBBranch branch;
UnnamedTransaction(final GitDBBranch base, final GitDBBranch branch) {
this.base = base;
this.branch = branch;
}
@Override
public Optional<String> get(String key) {
return Optional.empty();
}
@Override
public GitDBBranch put(String key, String value) {
return null;
}
@Override
public GitDBBranch remove(String key) {
return null;
}
@Override
public Optional<Version> getFormatVersion() {
return Optional.empty();
}
@Override
public GitDBTransaction transaction() {
return null;
}
@Override
public GitDBTransaction transaction(String name) {
return null;
}
@Override
public String name() {
return branch.name();
}
}

View file

@ -3,6 +3,7 @@ package net.kemitix.gitdb.test;
import com.github.zafarkhaja.semver.Version;
import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.GitDBTransaction;
import net.kemitix.gitdb.InvalidRepositoryException;
import org.assertj.core.api.WithAssertions;
import org.eclipse.jgit.api.Git;
@ -310,6 +311,20 @@ class GitDBTest implements WithAssertions {
}
// When starting a named transaction then GitDbTransaction is returned
@Test
void startTransaction_thenReturnGitDBTransaction() throws IOException {
//given
final String key = stringSupplier.get();
final String value = stringSupplier.get();
final GitDBBranch gitDBBranch = gitDBBranchWithKeyValue(key, value);
final String name = stringSupplier.get();
//when
final GitDBTransaction transaction = gitDBBranch.transaction(name);
//then
assertThat(transaction).isNotNull();
assertThat(transaction.name()).isEqualTo(name);
}
// When starting an anonymous transaction then a GitDbTransaction is returned
// Given a GitDbTransaction handle (i.e. a new branch)