Skip to content

Commit

Permalink
remove historycache entries automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Jul 21, 2017
1 parent 3f6755e commit f0cd42e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 46 deletions.
24 changes: 24 additions & 0 deletions src/org/opensolaris/opengrok/history/FileHistoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,30 @@ public void clear(Repository repository) {
}
}

@Override
public void clearFile(String path) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
File historyFile;
try {
historyFile = getCachedFile(new File(env.getSourceRootPath() + path));
} catch (HistoryException ex) {
LOGGER.log(Level.WARNING, "cannot get history file for file " + path, ex);
return;
}
File parent = historyFile.getParentFile();

if (!historyFile.delete() && historyFile.exists()) {
LOGGER.log(Level.WARNING,
"Failed to remove obsolete history cache-file: {0}",
historyFile.getAbsolutePath());
}

if (parent.delete()) {
LOGGER.log(Level.FINE, "Removed empty history cache dir:{0}",
parent.getAbsolutePath());
}
}

@Override
public String getInfo() {
return getClass().getSimpleName();
Expand Down
6 changes: 6 additions & 0 deletions src/org/opensolaris/opengrok/history/HistoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ Map<String, Date> getLastModifiedTimes(
*/
void clear(Repository repository) throws HistoryException;

/**
* Clear entry for single file from history cache.
* @param file path to the file
*/
void clearFile(String file);

/**
* Get a string with information about the history cache.
*
Expand Down
20 changes: 16 additions & 4 deletions src/org/opensolaris/opengrok/history/HistoryGuru.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ public void createCache(Collection<String> repositories) {
createCacheReal(getReposFromString(repositories));
}

private HistoryCache getHistoryCache() {
HistoryCache cache = historyCache;
if (cache == null) {
cache = new FileHistoryCache();
}

return cache;
}

/**
* Remove history data for a list of repositories
* @param repositories list of repository paths
Expand All @@ -628,10 +637,8 @@ public void createCache(Collection<String> repositories) {
*/
public List<Repository> clearCache(Collection<String> repositories) throws HistoryException {
List<Repository> repos = getReposFromString(repositories);
HistoryCache cache = historyCache;
if (cache == null) {
cache = new FileHistoryCache();
}
HistoryCache cache = getHistoryCache();

for (Repository r : repos) {
try {
cache.clear(r);
Expand All @@ -647,6 +654,11 @@ public List<Repository> clearCache(Collection<String> repositories) throws Histo
return repos;
}

public void clearCacheFile(String path) {
HistoryCache cache = getHistoryCache();
cache.clearFile(path);
}

/**
* Remove history data for a list of repositories and invalidate the list
* of repositories accordingly.
Expand Down
72 changes: 57 additions & 15 deletions src/org/opensolaris/opengrok/index/IndexDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -592,9 +594,13 @@ private void removeXrefFile(String path) {
}
}

private void removeHistoryFile(String path) {
HistoryGuru.getInstance().clearCacheFile(path);
}

/**
* Remove a stale file (uidIter.term().text()) from the index database (and
* the xref file)
* Remove a stale file (uidIter.term().text()) from the index database,
* history cache and xref.
*
* @throws java.io.IOException if an error occurs
*/
Expand All @@ -610,6 +616,7 @@ private void removeFile() throws IOException {
writer.commit();

removeXrefFile(path);
removeHistoryFile(path);

setDirty();
for (IndexChangedListener listener : listeners) {
Expand Down Expand Up @@ -951,28 +958,32 @@ public void removeIndexChangedListener(IndexChangedListener listener) {
}

/**
* List all files in all of the index databases
* Get all files in all of the index databases.
*
* @throws IOException if an error occurs
* @return set of files
*/
public static void listAllFiles() throws IOException {
listAllFiles(null);
public static Set<String> getAllFiles() throws IOException {
return getAllFiles(null);
}

/**
* List all files in some of the index databases
* List all files in some of the index databases.
*
* @param subFiles Subdirectories for the various projects to list the files
* for (or null or an empty list to dump all projects)
* @throws IOException if an error occurs
* @return set of files in the index databases specified by the subFiles parameter
*/
public static void listAllFiles(List<String> subFiles) throws IOException {
public static Set<String> getAllFiles(List<String> subFiles) throws IOException {
Set<String> files = new HashSet<>();
RuntimeEnvironment env = RuntimeEnvironment.getInstance();

if (env.hasProjects()) {
if (subFiles == null || subFiles.isEmpty()) {
for (Project project : env.getProjectList()) {
IndexDatabase db = new IndexDatabase(project);
db.listFiles();
files.addAll(db.getFiles());
}
} else {
for (String path : subFiles) {
Expand All @@ -981,25 +992,29 @@ public static void listAllFiles(List<String> subFiles) throws IOException {
LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
} else {
IndexDatabase db = new IndexDatabase(project);
db.listFiles();
files.addAll(db.getFiles());
}
}
}
} else {
IndexDatabase db = new IndexDatabase();
db.listFiles();
files = db.getFiles();
}

return files;
}

/**
* List all of the files in this index database
* Get all files in this index database.
*
* @throws IOException If an IO error occurs while reading from the database
* @return set of files in this index database
*/
public void listFiles() throws IOException {
public Set<String> getFiles() throws IOException {
IndexReader ireader = null;
TermsEnum iter;
Terms terms = null;
Set<String> files = new HashSet<>();

try {
ireader = DirectoryReader.open(indexDirectory); // open existing index
Expand All @@ -1010,12 +1025,37 @@ public void listFiles() throws IOException {
}
iter = terms.iterator(); // init uid iterator
while (iter != null && iter.term() != null) {
LOGGER.fine(Util.uid2url(iter.term().utf8ToString()));
BytesRef next=iter.next();
if (next==null) {iter=null;}
files.add(Util.uid2url(iter.term().utf8ToString()));
BytesRef next = iter.next();
if (next == null) {
iter = null;
}
}
} finally {
if (ireader != null) {
try {
ireader.close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "An error occured while closing index reader", e);
}
}
}

return files;
}

/**
* Get number of documents in this index database.
* @return number of documents
*/
public int getNumFiles() throws IOException {
IndexReader ireader = null;
int numDocs = 0;

try {
ireader = DirectoryReader.open(indexDirectory); // open existing index
numDocs = ireader.numDocs();
} finally {
if (ireader != null) {
try {
ireader.close();
Expand All @@ -1024,6 +1064,8 @@ public void listFiles() throws IOException {
}
}
}

return numDocs;
}

static void listFrequentTokens() throws IOException {
Expand Down
5 changes: 4 additions & 1 deletion src/org/opensolaris/opengrok/index/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,11 @@ public void prepareIndexer(RuntimeEnvironment env,
LOGGER.info("Done...");
}
}

if (listFiles) {
IndexDatabase.listAllFiles(subFiles);
for (String file : IndexDatabase.getAllFiles(subFiles)) {
LOGGER.fine(file);
}
}

if (createDict) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public void testStoreAndGet() throws Exception {
* rename+change file.
*
* The scenario goes as follows:
* - create repo
* - create Mercurial repository
* - perform full reindex
* - add changesets which renamed and modify a file
* - perform incremental reindex
Expand Down
Loading

0 comments on commit f0cd42e

Please sign in to comment.