xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryLookup.java (revision a964b78c95d85df4d53a32479f7d0aa2f6799da4)
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 java.io.IOException;
26 import java.nio.file.Path;
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Set;
30 
31 /**
32  * Interface for finding enclosing Repository for a given Path, used by HistoryGuru.
33  * <p>
34  * Two implementations exists
35  * - uncached, (legacy behavior) extracted from HistoryGuru into a stand-alone impl
36  * - cached, new implementation, which reduces number of expensive canonicalization calls
37  * <p>
38  * We preserve both cached and uncached implementations in order to verify correctness of the cached impl.
39  */
40 public interface RepositoryLookup {
41 
cached()42     static RepositoryLookupCached cached() {
43         return new RepositoryLookupCached();
44     }
45 
uncached()46     static RepositoryLookupUncached uncached() {
47         return new RepositoryLookupUncached();
48     }
49 
50     /**
51      * This interface allows intercepting PathUtils.getRelativeToCanonical in order to measure the impact of caching.
52      *
53      * In practice, PathUtils::getRelativeToCanonical is the implementation of this interface
54      */
55     @FunctionalInterface
56     interface PathCanonicalizer {
resolve(Path path, Path relativeTo)57         String resolve(Path path, Path relativeTo) throws IOException;
58     }
59 
60     /**
61      * Find enclosing repository for a given path.
62      *
63      * @param path path to find enclosing repository for
64      * @param repoParentDirs Set of repository parent dirs (parents of repository roots)
65      * @param repositories Map of repository root to Repository
66      * @param canonicalizer PathCanonicalizer reference
67      * @return enclosing Repository or null if not found
68      */
getRepository(Path path, Set<String> repoParentDirs, Map<String, Repository> repositories, PathCanonicalizer canonicalizer)69     Repository getRepository(Path path, Set<String> repoParentDirs, Map<String, Repository> repositories,
70         PathCanonicalizer canonicalizer);
71 
72     /**
73      * Lifecycle method to clear internal cache (if any) when repositories are invalidated.
74      */
clear()75     void clear();
76 
77     /**
78      * Lifecycle method to invalidate any cache entries that point to given repositories that are being removed.
79      *
80      * @param removedRepos collection of repositories
81      */
repositoriesRemoved(Collection<Repository> removedRepos)82     void repositoriesRemoved(Collection<Repository> removedRepos);
83 }
84