Extract impl package
This commit is contained in:
parent
ee585e2fb8
commit
50a304a941
13 changed files with 227 additions and 95 deletions
|
@ -21,6 +21,8 @@
|
|||
|
||||
package net.kemitix.gitdb;
|
||||
|
||||
import net.kemitix.gitdb.impl.LocalGitDB;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
@ -59,8 +61,6 @@ public interface GitDB {
|
|||
*/
|
||||
static GitDB openLocal(final Path dbDir, final String userName, final String userEmailAddress) {
|
||||
return LocalGitDB.open(dbDir, userName, userEmailAddress);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,54 +21,15 @@
|
|||
|
||||
package net.kemitix.gitdb;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* API for interacting with a branch in a GirDB.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class GitDBBranch {
|
||||
|
||||
private static final String KEY_PREFIX = "key:";
|
||||
private final Ref branchRef;
|
||||
private final GitDBRepo gitDBRepo;
|
||||
private final String userName;
|
||||
private final String userEmailAddress;
|
||||
|
||||
private static GitDBBranch select(
|
||||
final Ref branchRef,
|
||||
final GitDBRepo gitDBRepo,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
return new GitDBBranch(branchRef, gitDBRepo, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the creation of new GitDBBranch instances.
|
||||
*
|
||||
* @param repository the Git Repository
|
||||
* @param userName the user name to record against changes
|
||||
* @param userEmailAddress the user's email address to record against changes
|
||||
* @return a Function for creating a GitDBBranch when supplied with a Ref for a branch
|
||||
*/
|
||||
static Function<Ref, GitDBBranch> init(
|
||||
final Repository repository,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
return ref -> select(ref, new GitDBRepo(repository), userName, userEmailAddress);
|
||||
}
|
||||
public interface GitDBBranch {
|
||||
|
||||
/**
|
||||
* Lookup a value for the key.
|
||||
|
@ -77,9 +38,7 @@ public class GitDBBranch {
|
|||
* @return an Optional containing the value, if it exists, or empty if not
|
||||
* @throws IOException if there was an error reading the value
|
||||
*/
|
||||
public Optional<String> get(final String key) throws IOException {
|
||||
return gitDBRepo.readValue(branchRef, KEY_PREFIX + key);
|
||||
}
|
||||
Optional<String> get(String key) throws IOException;
|
||||
|
||||
/**
|
||||
* Put a value into the store for the key.
|
||||
|
@ -89,16 +48,7 @@ public class GitDBBranch {
|
|||
* @return an updated branch containing the new key/value
|
||||
* @throws IOException if there was an error writing the value
|
||||
*/
|
||||
public GitDBBranch put(final String key, final String value) throws IOException {
|
||||
final ObjectId newTree = gitDBRepo.writeValue(branchRef, KEY_PREFIX + key, value);
|
||||
final Ref newBranch =
|
||||
gitDBRepo.writeCommit(branchRef, newTree, commitMessageForAdd(key, value), userName, userEmailAddress);
|
||||
return select(newBranch, gitDBRepo, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
private String commitMessageForAdd(final String key, final String value) {
|
||||
return String.format("Add key [%s] = [%s]", key, value);
|
||||
}
|
||||
GitDBBranch put(String key, String value) throws IOException;
|
||||
|
||||
/**
|
||||
* Removes a key and its value from the store.
|
||||
|
@ -106,8 +56,5 @@ public class GitDBBranch {
|
|||
* @param key the key to remove
|
||||
* @return an updated branch without the key, or the original if the key was not found
|
||||
*/
|
||||
public GitDBBranch remove(final String key) {
|
||||
return this;
|
||||
}
|
||||
|
||||
GitDBBranch remove(String key);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import org.eclipse.jgit.lib.*;
|
||||
|
96
src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java
Normal file
96
src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
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 lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.kemitix.gitdb.GitDBBranch;
|
||||
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.function.Function;
|
||||
|
||||
/**
|
||||
* API for interacting with a branch in a GirDB.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
class GitDBBranchImpl implements GitDBBranch {
|
||||
|
||||
private static final String KEY_PREFIX = "key:";
|
||||
private final Ref branchRef;
|
||||
private final GitDBRepo gitDBRepo;
|
||||
private final String userName;
|
||||
private final String userEmailAddress;
|
||||
|
||||
private static GitDBBranch select(
|
||||
final Ref branchRef,
|
||||
final GitDBRepo gitDBRepo,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the creation of new GitDBBranch instances.
|
||||
*
|
||||
* @param repository the Git Repository
|
||||
* @param userName the user name to record against changes
|
||||
* @param userEmailAddress the user's email address to record against changes
|
||||
* @return a Function for creating a GitDBBranch when supplied with a Ref for a branch
|
||||
*/
|
||||
static Function<Ref, GitDBBranch> init(
|
||||
final Repository repository,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
return ref -> select(ref, new GitDBRepo(repository), userName, userEmailAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> get(final String key) throws IOException {
|
||||
return gitDBRepo.readValue(branchRef, KEY_PREFIX + key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GitDBBranch put(final String key, final String value) throws IOException {
|
||||
final ObjectId newTree = gitDBRepo.writeValue(branchRef, KEY_PREFIX + key, value);
|
||||
final Ref newBranch =
|
||||
gitDBRepo.writeCommit(branchRef, newTree, commitMessageForAdd(key, value), userName, userEmailAddress);
|
||||
return select(newBranch, gitDBRepo, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GitDBBranch remove(final String key) {
|
||||
return this;
|
||||
}
|
||||
|
||||
private String commitMessageForAdd(final String key, final String value) {
|
||||
return String.format("Add key [%s] = [%s]", key, value);
|
||||
}
|
||||
|
||||
}
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import lombok.val;
|
||||
import org.eclipse.jgit.lib.*;
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Repository;
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import org.eclipse.jgit.lib.*;
|
||||
|
61
src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java
Normal file
61
src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
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 net.kemitix.gitdb.GitDB;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* API for connecting to a Local Git repo as a database.
|
||||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
public interface LocalGitDB extends GitDB {
|
||||
|
||||
/**
|
||||
* Create a new GitDB instance, while initialising a new git repo.
|
||||
*
|
||||
* @param dbDir the path to instantiate the git repo in
|
||||
* @param userName the user name
|
||||
* @param userEmailAddress the user email address
|
||||
* @return a GitDB instance for the created local gitdb
|
||||
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
|
||||
*/
|
||||
static GitDB init(final Path dbDir, final String userName, final String userEmailAddress) throws IOException {
|
||||
return LocalGitDBImpl.init(dbDir, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new GitDB instance using the Git repo.
|
||||
*
|
||||
* @param dbDir the path to instantiate the git repo in
|
||||
* @param userName the user name
|
||||
* @param userEmailAddress the user email address
|
||||
* @return a GitDB instance for the created local gitdb
|
||||
*/
|
||||
static GitDB open(final Path dbDir, final String userName, final String userEmailAddress) {
|
||||
return LocalGitDBImpl.open(dbDir, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
}
|
|
@ -19,8 +19,11 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import net.kemitix.gitdb.GitDB;
|
||||
import net.kemitix.gitdb.GitDBBranch;
|
||||
import net.kemitix.gitdb.InvalidRepositoryException;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.lib.*;
|
||||
|
||||
|
@ -34,7 +37,7 @@ import java.util.function.Function;
|
|||
*
|
||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||
*/
|
||||
final class LocalGitDB implements GitDB {
|
||||
final class LocalGitDBImpl implements GitDB, LocalGitDB {
|
||||
|
||||
private static final String NOT_A_BARE_REPO = "Not a bare repo";
|
||||
private static final String ERROR_OPENING_REPOSITORY = "Error opening repository";
|
||||
|
@ -45,7 +48,7 @@ final class LocalGitDB implements GitDB {
|
|||
|
||||
private final Function<Ref, GitDBBranch> branchInit;
|
||||
|
||||
private LocalGitDB(
|
||||
private LocalGitDBImpl(
|
||||
final Repository repository,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
|
@ -53,31 +56,7 @@ final class LocalGitDB implements GitDB {
|
|||
this.repository = repository;
|
||||
this.userName = userName;
|
||||
this.userEmailAddress = userEmailAddress;
|
||||
branchInit = GitDBBranch.init(this.repository, this.userName, this.userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new GitDB instance using the Git repo.
|
||||
*
|
||||
* @param dbDir the path to instantiate the git repo in
|
||||
* @param userName the user name
|
||||
* @param userEmailAddress the user email address
|
||||
* @return a GitDB instance for the created local gitdb
|
||||
*/
|
||||
static GitDB open(
|
||||
final Path dbDir,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
try {
|
||||
return Optional.of(Git.open(dbDir.toFile()))
|
||||
.map(Git::getRepository)
|
||||
.filter(Repository::isBare)
|
||||
.map(repository -> new LocalGitDB(repository, userName, userEmailAddress))
|
||||
.orElseThrow(() -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir));
|
||||
} catch (IOException e) {
|
||||
throw new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e);
|
||||
}
|
||||
branchInit = GitDBBranchImpl.init(this.repository, this.userName, this.userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -98,6 +77,30 @@ final class LocalGitDB implements GitDB {
|
|||
return open(dbDir, userName, userEmailAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new GitDB instance using the Git repo.
|
||||
*
|
||||
* @param dbDir the path to instantiate the git repo in
|
||||
* @param userName the user name
|
||||
* @param userEmailAddress the user email address
|
||||
* @return a GitDB instance for the created local gitdb
|
||||
*/
|
||||
static GitDB open(
|
||||
final Path dbDir,
|
||||
final String userName,
|
||||
final String userEmailAddress
|
||||
) {
|
||||
try {
|
||||
return Optional.of(Git.open(dbDir.toFile()))
|
||||
.map(Git::getRepository)
|
||||
.filter(Repository::isBare)
|
||||
.map(repository -> new LocalGitDBImpl(repository, userName, userEmailAddress))
|
||||
.orElseThrow(() -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir));
|
||||
} catch (IOException e) {
|
||||
throw new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<GitDBBranch> branch(final String name) throws IOException {
|
||||
return Optional.ofNullable(repository.findRef(name)).map(branchInit);
|
|
@ -19,7 +19,7 @@
|
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb;
|
||||
package net.kemitix.gitdb.impl;
|
||||
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
26
src/main/java/net/kemitix/gitdb/impl/package-info.java
Normal file
26
src/main/java/net/kemitix/gitdb/impl/package-info.java
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* GitDB - using git as a key/value database.
|
||||
*/
|
||||
|
||||
package net.kemitix.gitdb.impl;
|
|
@ -8,7 +8,6 @@ import org.eclipse.jgit.api.Git;
|
|||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Constants;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
|
Loading…
Reference in a new issue