Extract KeyWriter and ValueWriter

This commit is contained in:
Paul Campbell 2018-06-14 22:31:41 +01:00
parent f00d900dcb
commit d392295401
3 changed files with 164 additions and 23 deletions

View file

@ -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.
*

View file

@ -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);
}
}

View file

@ -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);
}
}