Skip to content

Commit d3dbeac

Browse files
author
Vladimir Kotal
authored
remove historycache entries automatically (#1675)
fixes #767
1 parent f78e21f commit d3dbeac

File tree

9 files changed

+230
-51
lines changed

9 files changed

+230
-51
lines changed

src/org/opensolaris/opengrok/history/FileHistoryCache.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,30 @@ public void clear(Repository repository) {
715715
}
716716
}
717717

718+
@Override
719+
public void clearFile(String path) {
720+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
721+
File historyFile;
722+
try {
723+
historyFile = getCachedFile(new File(env.getSourceRootPath() + path));
724+
} catch (HistoryException ex) {
725+
LOGGER.log(Level.WARNING, "cannot get history file for file " + path, ex);
726+
return;
727+
}
728+
File parent = historyFile.getParentFile();
729+
730+
if (!historyFile.delete() && historyFile.exists()) {
731+
LOGGER.log(Level.WARNING,
732+
"Failed to remove obsolete history cache-file: {0}",
733+
historyFile.getAbsolutePath());
734+
}
735+
736+
if (parent.delete()) {
737+
LOGGER.log(Level.FINE, "Removed empty history cache dir:{0}",
738+
parent.getAbsolutePath());
739+
}
740+
}
741+
718742
@Override
719743
public String getInfo() {
720744
return getClass().getSimpleName();

src/org/opensolaris/opengrok/history/HistoryCache.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

@@ -125,6 +125,12 @@ Map<String, Date> getLastModifiedTimes(
125125
*/
126126
void clear(Repository repository) throws HistoryException;
127127

128+
/**
129+
* Clear entry for single file from history cache.
130+
* @param file path to the file relative to the source root
131+
*/
132+
void clearFile(String file);
133+
128134
/**
129135
* Get a string with information about the history cache.
130136
*

src/org/opensolaris/opengrok/history/HistoryGuru.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private HistoryGuru() {
8181
HistoryCache cache = null;
8282
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
8383
scanningDepth = env.getScanningDepth();
84+
8485
if (env.useHistoryCache()) {
8586
cache = new FileHistoryCache();
8687

@@ -627,14 +628,17 @@ public void createCache(Collection<String> repositories) {
627628
* @throws HistoryException
628629
*/
629630
public List<Repository> clearCache(Collection<String> repositories) throws HistoryException {
630-
List<Repository> repos = getReposFromString(repositories);
631+
List<Repository> repos = new ArrayList<>();
631632
HistoryCache cache = historyCache;
632-
if (cache == null) {
633-
cache = new FileHistoryCache();
633+
634+
if (!useCache()) {
635+
return repos;
634636
}
635-
for (Repository r : repos) {
637+
638+
for (Repository r : getReposFromString(repositories)) {
636639
try {
637640
cache.clear(r);
641+
repos.add(r);
638642
LOGGER.log(Level.INFO,
639643
"History cache for {0} cleared.", r.getDirectoryName());
640644
} catch (HistoryException e) {
@@ -647,13 +651,25 @@ public List<Repository> clearCache(Collection<String> repositories) throws Histo
647651
return repos;
648652
}
649653

654+
public void clearCacheFile(String path) {
655+
if (!useCache()) {
656+
return;
657+
}
658+
659+
historyCache.clearFile(path);
660+
}
661+
650662
/**
651663
* Remove history data for a list of repositories and invalidate the list
652664
* of repositories accordingly.
653665
* @param repositories list of repository paths
654666
* @throws HistoryException
655667
*/
656668
public void removeCache(Collection<String> repositories) throws HistoryException {
669+
if (!useCache()) {
670+
return;
671+
}
672+
657673
List<Repository> repos = clearCache(repositories);
658674
invalidateRepositories(repos);
659675
}

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import java.util.ArrayList;
3636
import java.util.Arrays;
3737
import java.util.Comparator;
38+
import java.util.HashSet;
3839
import java.util.List;
40+
import java.util.Set;
3941
import java.util.concurrent.ExecutorService;
4042
import java.util.logging.Level;
4143
import java.util.logging.Logger;
@@ -592,9 +594,13 @@ private void removeXrefFile(String path) {
592594
}
593595
}
594596

597+
private void removeHistoryFile(String path) {
598+
HistoryGuru.getInstance().clearCacheFile(path);
599+
}
600+
595601
/**
596-
* Remove a stale file (uidIter.term().text()) from the index database (and
597-
* the xref file)
602+
* Remove a stale file (uidIter.term().text()) from the index database,
603+
* history cache and xref.
598604
*
599605
* @throws java.io.IOException if an error occurs
600606
*/
@@ -610,6 +616,7 @@ private void removeFile() throws IOException {
610616
writer.commit();
611617

612618
removeXrefFile(path);
619+
removeHistoryFile(path);
613620

614621
setDirty();
615622
for (IndexChangedListener listener : listeners) {
@@ -951,28 +958,32 @@ public void removeIndexChangedListener(IndexChangedListener listener) {
951958
}
952959

953960
/**
954-
* List all files in all of the index databases
961+
* Get all files in all of the index databases.
955962
*
956963
* @throws IOException if an error occurs
964+
* @return set of files
957965
*/
958-
public static void listAllFiles() throws IOException {
959-
listAllFiles(null);
966+
public static Set<String> getAllFiles() throws IOException {
967+
return getAllFiles(null);
960968
}
961969

962970
/**
963-
* List all files in some of the index databases
971+
* List all files in some of the index databases.
964972
*
965973
* @param subFiles Subdirectories for the various projects to list the files
966974
* for (or null or an empty list to dump all projects)
967975
* @throws IOException if an error occurs
976+
* @return set of files in the index databases specified by the subFiles parameter
968977
*/
969-
public static void listAllFiles(List<String> subFiles) throws IOException {
978+
public static Set<String> getAllFiles(List<String> subFiles) throws IOException {
979+
Set<String> files = new HashSet<>();
970980
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
981+
971982
if (env.hasProjects()) {
972983
if (subFiles == null || subFiles.isEmpty()) {
973984
for (Project project : env.getProjectList()) {
974985
IndexDatabase db = new IndexDatabase(project);
975-
db.listFiles();
986+
files.addAll(db.getFiles());
976987
}
977988
} else {
978989
for (String path : subFiles) {
@@ -981,25 +992,29 @@ public static void listAllFiles(List<String> subFiles) throws IOException {
981992
LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
982993
} else {
983994
IndexDatabase db = new IndexDatabase(project);
984-
db.listFiles();
995+
files.addAll(db.getFiles());
985996
}
986997
}
987998
}
988999
} else {
9891000
IndexDatabase db = new IndexDatabase();
990-
db.listFiles();
1001+
files = db.getFiles();
9911002
}
1003+
1004+
return files;
9921005
}
9931006

9941007
/**
995-
* List all of the files in this index database
1008+
* Get all files in this index database.
9961009
*
9971010
* @throws IOException If an IO error occurs while reading from the database
1011+
* @return set of files in this index database
9981012
*/
999-
public void listFiles() throws IOException {
1013+
public Set<String> getFiles() throws IOException {
10001014
IndexReader ireader = null;
10011015
TermsEnum iter;
10021016
Terms terms = null;
1017+
Set<String> files = new HashSet<>();
10031018

10041019
try {
10051020
ireader = DirectoryReader.open(indexDirectory); // open existing index
@@ -1010,12 +1025,37 @@ public void listFiles() throws IOException {
10101025
}
10111026
iter = terms.iterator(); // init uid iterator
10121027
while (iter != null && iter.term() != null) {
1013-
LOGGER.fine(Util.uid2url(iter.term().utf8ToString()));
1014-
BytesRef next=iter.next();
1015-
if (next==null) {iter=null;}
1028+
files.add(Util.uid2url(iter.term().utf8ToString()));
1029+
BytesRef next = iter.next();
1030+
if (next == null) {
1031+
iter = null;
1032+
}
10161033
}
10171034
} finally {
1035+
if (ireader != null) {
1036+
try {
1037+
ireader.close();
1038+
} catch (IOException e) {
1039+
LOGGER.log(Level.WARNING, "An error occured while closing index reader", e);
1040+
}
1041+
}
1042+
}
10181043

1044+
return files;
1045+
}
1046+
1047+
/**
1048+
* Get number of documents in this index database.
1049+
* @return number of documents
1050+
*/
1051+
public int getNumFiles() throws IOException {
1052+
IndexReader ireader = null;
1053+
int numDocs = 0;
1054+
1055+
try {
1056+
ireader = DirectoryReader.open(indexDirectory); // open existing index
1057+
numDocs = ireader.numDocs();
1058+
} finally {
10191059
if (ireader != null) {
10201060
try {
10211061
ireader.close();
@@ -1024,6 +1064,8 @@ public void listFiles() throws IOException {
10241064
}
10251065
}
10261066
}
1067+
1068+
return numDocs;
10271069
}
10281070

10291071
static void listFrequentTokens() throws IOException {

src/org/opensolaris/opengrok/index/Indexer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,11 @@ public void prepareIndexer(RuntimeEnvironment env,
799799
LOGGER.info("Done...");
800800
}
801801
}
802+
802803
if (listFiles) {
803-
IndexDatabase.listAllFiles(subFiles);
804+
for (String file : IndexDatabase.getAllFiles(subFiles)) {
805+
LOGGER.fine(file);
806+
}
804807
}
805808

806809
if (createDict) {

test/org/opensolaris/opengrok/history/FileHistoryCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public void testStoreAndGet() throws Exception {
353353
* rename+change file.
354354
*
355355
* The scenario goes as follows:
356-
* - create repo
356+
* - create Mercurial repository
357357
* - perform full reindex
358358
* - add changesets which renamed and modify a file
359359
* - perform incremental reindex

0 commit comments

Comments
 (0)