Skip to content

Commit

Permalink
Extracted a block repository class out
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignas committed Dec 17, 2023
1 parent 267b52a commit 4e659c0
Show file tree
Hide file tree
Showing 7 changed files with 525 additions and 474 deletions.
2 changes: 1 addition & 1 deletion spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<FindBugsFilter>
<Match>
<Class name="~.*"/>
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2,VA_FORMAT_STRING_USES_NEWLINE"/>
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2,VA_FORMAT_STRING_USES_NEWLINE,CT_CONSTRUCTOR_THROW"/>
</Match>
<Match>
<Class name="~.*\.Immutable.*"/>
Expand Down
78 changes: 39 additions & 39 deletions src/main/java/lt/pow/nukagit/dfs/DfsRepositoryResolver.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lt.pow.nukagit.dfs;

import io.minio.MinioClient;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import lt.pow.nukagit.db.dao.NukagitDfsDao;
import lt.pow.nukagit.minio.NukagitBlockRepository;
import org.eclipse.jgit.internal.storage.dfs.DfsReaderOptions;
import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Expand All @@ -19,43 +19,43 @@
@Singleton
public class DfsRepositoryResolver {

private static final Logger LOGGER = LoggerFactory.getLogger(DfsRepositoryResolver.class);
private final ConcurrentHashMap<String, Repository> repositoryCache;
private final NukagitDfsDao dfsDao;
private final MinioClient minio;

@Inject
public DfsRepositoryResolver(NukagitDfsDao dfsDao, MinioClient minio) {
this.dfsDao = dfsDao;
this.minio = minio;
repositoryCache = new ConcurrentHashMap<>();
}

@WithSpan
public synchronized Repository resolveDfsRepository(String username, String[] args)
throws IOException {
LOGGER.debug("resolveDfsRepository: username={}, args={}", username, args);
String repositoryName = args[1];

if (!repositoryName.startsWith("/memory/")) {
var id = dfsDao.getRepositoryIdByName(repositoryName);
if (id == null) {
throw new IOException(String.format("Repository with the name %s does not exist!", repositoryName));
}
return new NukagitDfsRepository.Builder(dfsDao, minio)
.setRepositoryDescription(new NukagitDfsRepositoryDescription(id, repositoryName))
// .withPath(new Path("testRepositories", name))
// .withBlockSize(64)
// .withReplication((short) 2)
.setReaderOptions(new DfsReaderOptions())
.build();
private static final Logger LOGGER = LoggerFactory.getLogger(DfsRepositoryResolver.class);
private final ConcurrentHashMap<String, Repository> repositoryCache;
private final NukagitDfsDao dfsDao;
private final NukagitBlockRepository blockRepository;

@Inject
public DfsRepositoryResolver(NukagitDfsDao dfsDao, NukagitBlockRepository blockRepository) {
this.dfsDao = dfsDao;
this.blockRepository = blockRepository;
repositoryCache = new ConcurrentHashMap<>();
}

@WithSpan
public synchronized Repository resolveDfsRepository(String username, String[] args)
throws IOException {
LOGGER.debug("resolveDfsRepository: username={}, args={}", username, args);
String repositoryName = args[1];

if (!repositoryName.startsWith("/memory/")) {
var id = dfsDao.getRepositoryIdByName(repositoryName);
if (id == null) {
throw new IOException(String.format("Repository with the name %s does not exist!", repositoryName));
}
return new NukagitDfsRepository.Builder(dfsDao, blockRepository)
.setRepositoryDescription(new NukagitDfsRepositoryDescription(id, repositoryName))
// .withPath(new Path("testRepositories", name))
// .withBlockSize(64)
// .withReplication((short) 2)
.setReaderOptions(new DfsReaderOptions())
.build();
}
// Keep in memory repositories for simple testing
return repositoryCache.computeIfAbsent(
repositoryName, (key) -> new InMemoryRepository(new DfsRepositoryDescription(key)));
}

public synchronized List<String> listRepositories() {
return List.copyOf(repositoryCache.keySet());
}
// Keep in memory repositories for simple testing
return repositoryCache.computeIfAbsent(
repositoryName, (key) -> new InMemoryRepository(new DfsRepositoryDescription(key)));
}

public synchronized List<String> listRepositories() {
return List.copyOf(repositoryCache.keySet());
}
}
5 changes: 4 additions & 1 deletion src/main/java/lt/pow/nukagit/dfs/GitDfsPackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public GitDfsPackCommand(
@WithSpan
public void run() {
String username = (String) getServerSession().getIoSession().getAttribute("username");
String command = getCommand();
MDC.put("username", username);
NukagitDfsRepository.USERNAME.set(username);

String command = getCommand();
try {
var args = extractQuotedStrings(command);

Expand Down Expand Up @@ -65,6 +67,7 @@ public void run() {
MDC.remove("command");
MDC.remove("git.repository");
MDC.remove("git.command");
NukagitDfsRepository.USERNAME.remove();
}
}

Expand Down
Loading

0 comments on commit 4e659c0

Please sign in to comment.