From 50a304a941eb43593aebd901bc11a55323520b5a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 15 Jun 2018 08:29:07 +0100 Subject: [PATCH] Extract impl package --- src/main/java/net/kemitix/gitdb/GitDB.java | 4 +- .../java/net/kemitix/gitdb/GitDBBranch.java | 63 +----------- .../gitdb/{ => impl}/CommitWriter.java | 2 +- .../kemitix/gitdb/impl/GitDBBranchImpl.java | 96 +++++++++++++++++++ .../kemitix/gitdb/{ => impl}/GitDBRepo.java | 2 +- .../gitdb/{ => impl}/GitTreeReader.java | 2 +- .../gitdb/{ => impl}/InitGitDBRepo.java | 2 +- .../kemitix/gitdb/{ => impl}/KeyWriter.java | 2 +- .../net/kemitix/gitdb/impl/LocalGitDB.java | 61 ++++++++++++ .../LocalGitDBImpl.java} | 59 ++++++------ .../kemitix/gitdb/{ => impl}/ValueWriter.java | 2 +- .../net/kemitix/gitdb/impl/package-info.java | 26 +++++ .../net/kemitix/gitdb/test/GitDBTest.java | 1 - 13 files changed, 227 insertions(+), 95 deletions(-) rename src/main/java/net/kemitix/gitdb/{ => impl}/CommitWriter.java (99%) create mode 100644 src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java rename src/main/java/net/kemitix/gitdb/{ => impl}/GitDBRepo.java (99%) rename src/main/java/net/kemitix/gitdb/{ => impl}/GitTreeReader.java (99%) rename src/main/java/net/kemitix/gitdb/{ => impl}/InitGitDBRepo.java (99%) rename src/main/java/net/kemitix/gitdb/{ => impl}/KeyWriter.java (99%) create mode 100644 src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java rename src/main/java/net/kemitix/gitdb/{LocalGitDB.java => impl/LocalGitDBImpl.java} (89%) rename src/main/java/net/kemitix/gitdb/{ => impl}/ValueWriter.java (98%) create mode 100644 src/main/java/net/kemitix/gitdb/impl/package-info.java diff --git a/src/main/java/net/kemitix/gitdb/GitDB.java b/src/main/java/net/kemitix/gitdb/GitDB.java index 32028ea..4bad3f5 100644 --- a/src/main/java/net/kemitix/gitdb/GitDB.java +++ b/src/main/java/net/kemitix/gitdb/GitDB.java @@ -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); - - } /** diff --git a/src/main/java/net/kemitix/gitdb/GitDBBranch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java index 823bd6e..b8eda62 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBBranch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -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 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 get(final String key) throws IOException { - return gitDBRepo.readValue(branchRef, KEY_PREFIX + key); - } + Optional 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); } diff --git a/src/main/java/net/kemitix/gitdb/CommitWriter.java b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java similarity index 99% rename from src/main/java/net/kemitix/gitdb/CommitWriter.java rename to src/main/java/net/kemitix/gitdb/impl/CommitWriter.java index cbf28e8..290ab6b 100644 --- a/src/main/java/net/kemitix/gitdb/CommitWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/CommitWriter.java @@ -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.*; diff --git a/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java new file mode 100644 index 0000000..3b59ca8 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBBranchImpl.java @@ -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 init( + final Repository repository, + final String userName, + final String userEmailAddress + ) { + return ref -> select(ref, new GitDBRepo(repository), userName, userEmailAddress); + } + + @Override + public Optional 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); + } + +} diff --git a/src/main/java/net/kemitix/gitdb/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java similarity index 99% rename from src/main/java/net/kemitix/gitdb/GitDBRepo.java rename to src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java index d9d4c1f..33aaeab 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitDBRepo.java @@ -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.*; diff --git a/src/main/java/net/kemitix/gitdb/GitTreeReader.java b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java similarity index 99% rename from src/main/java/net/kemitix/gitdb/GitTreeReader.java rename to src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java index b299bb1..05e4911 100644 --- a/src/main/java/net/kemitix/gitdb/GitTreeReader.java +++ b/src/main/java/net/kemitix/gitdb/impl/GitTreeReader.java @@ -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; diff --git a/src/main/java/net/kemitix/gitdb/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java similarity index 99% rename from src/main/java/net/kemitix/gitdb/InitGitDBRepo.java rename to src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index a84f751..149a630 100644 --- a/src/main/java/net/kemitix/gitdb/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -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; diff --git a/src/main/java/net/kemitix/gitdb/KeyWriter.java b/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java similarity index 99% rename from src/main/java/net/kemitix/gitdb/KeyWriter.java rename to src/main/java/net/kemitix/gitdb/impl/KeyWriter.java index e768188..1dc00c1 100644 --- a/src/main/java/net/kemitix/gitdb/KeyWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/KeyWriter.java @@ -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.*; diff --git a/src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java b/src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java new file mode 100644 index 0000000..b6bbb09 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/impl/LocalGitDB.java @@ -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); + } + +} diff --git a/src/main/java/net/kemitix/gitdb/LocalGitDB.java b/src/main/java/net/kemitix/gitdb/impl/LocalGitDBImpl.java similarity index 89% rename from src/main/java/net/kemitix/gitdb/LocalGitDB.java rename to src/main/java/net/kemitix/gitdb/impl/LocalGitDBImpl.java index 001add7..7d9f4d7 100644 --- a/src/main/java/net/kemitix/gitdb/LocalGitDB.java +++ b/src/main/java/net/kemitix/gitdb/impl/LocalGitDBImpl.java @@ -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 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 branch(final String name) throws IOException { return Optional.ofNullable(repository.findRef(name)).map(branchInit); diff --git a/src/main/java/net/kemitix/gitdb/ValueWriter.java b/src/main/java/net/kemitix/gitdb/impl/ValueWriter.java similarity index 98% rename from src/main/java/net/kemitix/gitdb/ValueWriter.java rename to src/main/java/net/kemitix/gitdb/impl/ValueWriter.java index 034dd29..f066551 100644 --- a/src/main/java/net/kemitix/gitdb/ValueWriter.java +++ b/src/main/java/net/kemitix/gitdb/impl/ValueWriter.java @@ -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; diff --git a/src/main/java/net/kemitix/gitdb/impl/package-info.java b/src/main/java/net/kemitix/gitdb/impl/package-info.java new file mode 100644 index 0000000..e842481 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/impl/package-info.java @@ -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; diff --git a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java index 2175365..99886fe 100644 --- a/src/test/java/net/kemitix/gitdb/test/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/test/GitDBTest.java @@ -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;