Skip to content

Commit cfe5845

Browse files
committed
Merge pull request #780 from vladak/union_workaround
do not run expensive JDBC query if FILEMOVES table is empty or tracking ...
2 parents f60e7ba + ea54fd1 commit cfe5845

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

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

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

2020
/*
21-
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323

2424
package org.opensolaris.opengrok.history;
@@ -103,6 +103,9 @@ class JDBCHistoryCache implements HistoryCache {
103103
private static final PreparedQuery GET_LAST_MODIFIED_TIMES =
104104
new PreparedQuery(getQuery("getLastModifiedTimes"));
105105

106+
private static final PreparedQuery GET_FILEMOVES_COUNT =
107+
new PreparedQuery(getQuery("getFilemovesCount"));
108+
106109
/**
107110
* The number of times to retry an operation that failed in a way that
108111
* indicates that it may succeed if it's tried again.
@@ -516,6 +519,8 @@ private static String truncate(String str, int length) {
516519
*/
517520
private static final PreparedQuery GET_FILE_HISTORY =
518521
new PreparedQuery(getQuery("getFileHistory"));
522+
private static final PreparedQuery GET_FILE_HISTORY_FOLDED =
523+
new PreparedQuery(getQuery("getFileHistoryFolded"));
519524

520525
/**
521526
* Statement that gets the history for all files matching a pattern in the
@@ -550,6 +555,31 @@ public History get(File file, Repository repository, boolean withFiles)
550555
}
551556
}
552557

558+
/**
559+
* Get the number of rows in the FILEMOVES table. This is used as a
560+
* workaround/optimization since JavaDB cannot currently handle the
561+
* GET_FILE_HISTORY very well.
562+
* @return number of rows in the FILEMOVES table
563+
* @throws SQLException
564+
*/
565+
private int getFilemovesCount() throws SQLException {
566+
final ConnectionResource conn;
567+
conn = connectionManager.getConnectionResource();
568+
569+
try {
570+
final PreparedStatement cntPS = conn.getStatement(GET_FILEMOVES_COUNT);
571+
try (ResultSet rs = cntPS.executeQuery()) {
572+
if (rs.next()) {
573+
return rs.getInt(1);
574+
}
575+
}
576+
} finally {
577+
connectionManager.releaseConnection(conn);
578+
}
579+
580+
return -1;
581+
}
582+
553583
/**
554584
* Helper for {@link #get(File, Repository)}.
555585
*/
@@ -561,6 +591,8 @@ private History getHistory(
561591
final ArrayList<HistoryEntry> entries = new ArrayList<HistoryEntry>();
562592
final ConnectionResource conn =
563593
connectionManager.getConnectionResource();
594+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
595+
564596
try {
565597
final PreparedStatement ps;
566598
if (file.isDirectory()) {
@@ -569,7 +601,8 @@ private History getHistory(
569601
ps.setString(2, filePath);
570602
} else {
571603
// Fetch history for a single file only.
572-
ps = conn.getStatement(GET_FILE_HISTORY);
604+
ps = conn.getStatement(env.RenamedFilesEnabled() && (getFilemovesCount() > 0) ?
605+
GET_FILE_HISTORY : GET_FILE_HISTORY_FOLDED);
573606
ps.setString(2, getParentPath(filePath));
574607
ps.setString(3, getBaseName(filePath));
575608
}
@@ -609,7 +642,6 @@ private History getHistory(
609642
History history = new History();
610643
history.setHistoryEntries(entries);
611644

612-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
613645
if (env.isTagsEnabled() && repository.hasFileBasedTags()) {
614646
repository.assignTagsInHistory(history);
615647
}

src/org/opensolaris/opengrok/history/JDBCHistoryCache_queries.properties

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ getFileHistory=\
124124
R.PATH = ? AND D.PATH = ? AND F.NAME = ? \
125125
ORDER BY CS.ID DESC
126126

127+
getFileHistoryFolded=\
128+
SELECT CS.REVISION, A.NAME, CS.TIME, CS.MESSAGE, CS.ID \
129+
FROM \
130+
OPENGROK.CHANGESETS CS \
131+
JOIN OPENGROK.FILECHANGES FC ON CS.ID = FC.CHANGESET \
132+
JOIN OPENGROK.FILES F ON FC.FILE = F.ID \
133+
JOIN OPENGROK.DIRECTORIES D ON D.ID = F.DIRECTORY \
134+
JOIN OPENGROK.AUTHORS A ON A.ID = CS.AUTHOR \
135+
JOIN OPENGROK.REPOSITORIES R ON D.REPOSITORY = R.ID \
136+
WHERE \
137+
R.PATH = ? AND D.PATH = ? AND F.NAME = ? \
138+
ORDER BY CS.ID DESC
139+
140+
getFilemovesCount=\
141+
SELECT COUNT(*) FROM OPENGROK.FILEMOVES
142+
127143
getDirHistory=\
128144
SELECT CS.REVISION, A.NAME, CS.TIME, CS.MESSAGE, CS.ID \
129145
FROM \

0 commit comments

Comments
 (0)