From d3922954019ef6b561faab2ae153c1f40ec4e220 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 14 Jun 2018 22:31:41 +0100 Subject: [PATCH] Extract KeyWriter and ValueWriter --- .../java/net/kemitix/gitdb/GitDBRepo.java | 39 ++++---- .../java/net/kemitix/gitdb/KeyWriter.java | 89 +++++++++++++++++++ .../java/net/kemitix/gitdb/ValueWriter.java | 59 ++++++++++++ 3 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 src/main/java/net/kemitix/gitdb/KeyWriter.java create mode 100644 src/main/java/net/kemitix/gitdb/ValueWriter.java diff --git a/src/main/java/net/kemitix/gitdb/GitDBRepo.java b/src/main/java/net/kemitix/gitdb/GitDBRepo.java index 8b0e6ed..5896dfa 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/GitDBRepo.java @@ -21,7 +21,6 @@ package net.kemitix.gitdb; -import lombok.RequiredArgsConstructor; import lombok.val; import org.eclipse.jgit.lib.*; @@ -36,10 +35,22 @@ import java.util.*; * * @author Paul Campbell (pcampbell@kemitix.net) */ -@RequiredArgsConstructor class GitDBRepo { private final Repository repository; + private final ValueWriter valueWriter; + private final KeyWriter keyWriter; + + /** + * Creates a new instance of this class. + * + * @param repository the Git Repository + */ + GitDBRepo(final Repository repository) { + this.repository = repository; + valueWriter = new ValueWriter(repository); + keyWriter = new KeyWriter(repository); + } /** * Insert a blob into the store, returning its unique id. @@ -49,7 +60,7 @@ class GitDBRepo { * @throws IOException the blob could not be stored */ ObjectId insertBlob(final byte[] blob) throws IOException { - return repository.getObjectDatabase().newInserter().insert(Constants.OBJ_BLOB, blob); + return valueWriter.write(blob); } /** @@ -64,8 +75,7 @@ class GitDBRepo { final String key, final ObjectId valueId ) throws IOException { - final TreeFormatter treeFormatter = new TreeFormatter(); - return writeTree(key, valueId, treeFormatter); + return keyWriter.writeFirst(key, valueId); } /** @@ -82,7 +92,7 @@ class GitDBRepo { final String key, final ObjectId valueId ) throws IOException { - return writeTree(key, valueId, treeFormatterForBranch(branchRef)); + return keyWriter.write(key, valueId, branchRef); } /** @@ -149,23 +159,6 @@ class GitDBRepo { return Optional.empty(); } - private ObjectId writeTree( - final String key, - final ObjectId valueId, - final TreeFormatter treeFormatter - ) throws IOException { - treeFormatter.append(key, FileMode.REGULAR_FILE, valueId); - return repository.getObjectDatabase().newInserter().insert(treeFormatter); - } - - private TreeFormatter treeFormatterForBranch(final Ref branchRef) throws IOException { - final TreeFormatter treeFormatter = new TreeFormatter(); - final GitTreeReader gitTreeReader = new GitTreeReader(repository); - gitTreeReader.stream(branchRef) - .forEach(item -> treeFormatter.append(item.getName(), item.getRevBlob())); - return treeFormatter; - } - /** * Add the key/value to the repo, returning the tree containing the update. * diff --git a/src/main/java/net/kemitix/gitdb/KeyWriter.java b/src/main/java/net/kemitix/gitdb/KeyWriter.java new file mode 100644 index 0000000..e768188 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/KeyWriter.java @@ -0,0 +1,89 @@ +/* + 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; + +import org.eclipse.jgit.lib.*; + +import java.io.IOException; + +/** + * Writes Keys into the Git Repository. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +class KeyWriter { + + private final Repository repository; + private final ObjectInserter objectInserter; + + /** + * Create new instance of this class. + * + * @param repository the repository to write keys to + */ + KeyWriter(final Repository repository) { + this.repository = repository; + objectInserter = repository.getObjectDatabase().newInserter(); + } + + /** + * Write the first key into a new tree. + * + * @param key the key + * @param valueId the id of the value + * @return the id of the new tree + * @throws IOException if there is an error writing the key + */ + ObjectId writeFirst(final String key, final ObjectId valueId) throws IOException { + return writeTree(key, valueId, new TreeFormatter()); + } + + /** + * Write the key into a tree. + * + * @param key the key + * @param valueId the id of the value + * @param branchRef the branch whose tree should be updated + * @return the id of the updated tree + * @throws IOException if there is an error writing the key + */ + ObjectId write(final String key, final ObjectId valueId, final Ref branchRef) throws IOException { + return writeTree(key, valueId, getTreeFormatter(branchRef)); + } + + private TreeFormatter getTreeFormatter(final Ref branchRef) throws IOException { + final TreeFormatter treeFormatter = new TreeFormatter(); + final GitTreeReader gitTreeReader = new GitTreeReader(repository); + gitTreeReader.stream(branchRef) + .forEach(item -> treeFormatter.append(item.getName(), item.getRevBlob())); + return treeFormatter; + } + + private ObjectId writeTree( + final String key, + final ObjectId valueId, + final TreeFormatter treeFormatter + ) throws IOException { + treeFormatter.append(key, FileMode.REGULAR_FILE, valueId); + return objectInserter.insert(treeFormatter); + } +} diff --git a/src/main/java/net/kemitix/gitdb/ValueWriter.java b/src/main/java/net/kemitix/gitdb/ValueWriter.java new file mode 100644 index 0000000..034dd29 --- /dev/null +++ b/src/main/java/net/kemitix/gitdb/ValueWriter.java @@ -0,0 +1,59 @@ +/* + 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; + +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectInserter; +import org.eclipse.jgit.lib.Repository; + +import java.io.IOException; + +/** + * Writes Values into the Git Repository. + * + * @author Paul Campbell (pcampbell@kemitix.net) + */ +class ValueWriter { + + private final ObjectInserter objectInserter; + + /** + * Create new instance of this class. + * + * @param repository the repository to write values to + */ + ValueWriter(final Repository repository) { + objectInserter = repository.getObjectDatabase().newInserter(); + } + + /** + * Write a value into the repository. + * + * @param blob the value blob + * @return the id of the value object + * @throws IOException if there is an error writing the value + */ + ObjectId write(final byte[] blob) throws IOException { + return objectInserter.insert(Constants.OBJ_BLOB, blob); + } +}