xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryLookupUncached.java (revision 6d9d3df9426166f52ef9659c0471e331849351aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2020, Anatoly Akkerman <akkerman@gmail.com>, <anatoly.akkerman@twosigma.com>.
22  */
23 package org.opengrok.indexer.history;
24 
25 import org.opengrok.indexer.logger.LoggerFactory;
26 
27 import java.io.IOException;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Collection;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 
36 /**
37  * RepositoryLookup uncached implementation (original logic taken from HistoryGuru.getRepository).
38  */
39 public class RepositoryLookupUncached implements RepositoryLookup {
40     private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryLookupUncached.class);
41 
42     @Override
getRepository(final Path filePath, Set<String> repoParentDirs, Map<String, Repository> repositories, PathCanonicalizer canonicalizer)43     public Repository getRepository(final Path filePath, Set<String> repoParentDirs,
44         Map<String, Repository> repositories, PathCanonicalizer canonicalizer) {
45         Path path = filePath;
46 
47         while (path != null) {
48             String nextPath = path.toString();
49             for (String rootKey : repoParentDirs) {
50                 String rel;
51                 try {
52                     rel = canonicalizer.resolve(path, path.getFileSystem().getPath(rootKey));
53                 } catch (IOException e) {
54                     LOGGER.log(Level.WARNING,
55                         "Failed to get relative to canonical for " + nextPath,
56                         e);
57                     return null;
58                 }
59                 Repository repo;
60                 if (rel.equals(nextPath)) {
61                     repo = repositories.get(nextPath);
62                 } else {
63                     String inRootPath = Paths.get(rootKey, rel).toString();
64                     repo = repositories.get(inRootPath);
65                 }
66                 if (repo != null) {
67                     return repo;
68                 }
69             }
70 
71             path = path.getParent();
72         }
73 
74         return null;
75     }
76 
77     @Override
clear()78     public void clear() {
79         // Do nothing
80     }
81 
82     @Override
repositoriesRemoved(Collection<Repository> removedRepos)83     public void repositoriesRemoved(Collection<Repository> removedRepos) {
84         // Do nothings
85     }
86 }
87