[jsword-svn] r2040 - in trunk: common-swing/src/main/java/org/crosswire/common/swing jsword/src/main/java/org/crosswire/common/progress jsword/src/main/java/org/crosswire/common/util jsword/src/main/java/org/crosswire/jsword/book/install jsword/src/main/java/org/crosswire/jsword/book/install/sword jsword/src/main/java/org/crosswire/jsword/index/lucene jsword-limbo/src/main/java/org/crosswire/jsword/book/install/sword
dmsmith at crosswire.org
dmsmith at crosswire.org
Sat Dec 4 15:17:15 MST 2010
Author: dmsmith
Date: 2010-12-04 15:17:15 -0700 (Sat, 04 Dec 2010)
New Revision: 2040
Modified:
trunk/common-swing/src/main/java/org/crosswire/common/swing/ActionFactory.java
trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/install/sword/FtpSwordInstaller.java
trunk/jsword/src/main/java/org/crosswire/common/progress/Job.java
trunk/jsword/src/main/java/org/crosswire/common/progress/Progress.java
trunk/jsword/src/main/java/org/crosswire/common/util/NetUtil.java
trunk/jsword/src/main/java/org/crosswire/common/util/WebResource.java
trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java
trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java
trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/HttpSwordInstaller.java
trunk/jsword/src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java
Log:
JS-133: The last change was entirely non-performant by at least 10x on android.
In progress and web resource, changed back to int for sizes rather than long. This gives a file size limit of 2G which is vastly larger than what our current modules are.
Changed Job to only notify listeners when the percentage changes.
Modified: trunk/common-swing/src/main/java/org/crosswire/common/swing/ActionFactory.java
===================================================================
--- trunk/common-swing/src/main/java/org/crosswire/common/swing/ActionFactory.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/common-swing/src/main/java/org/crosswire/common/swing/ActionFactory.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -381,7 +381,7 @@
private CWAction buildAction(String key, String name, String tooltip, String smallIconPath, String largeIconPath, String acceleratorSpec, String enabled) {
if (key == null || key.length() == 0) {
log.warn("Acronymn is missing for CWAction");
- }
+ }
CWAction cwAction = (CWAction) actions.get(key);
@@ -407,7 +407,7 @@
cwAction.addSmallIcon(smallIconPath);
try {
- cwAction.addAccelerator(acceleratorSpec);
+ cwAction.addAccelerator(acceleratorSpec);
} catch (NumberFormatException nfe) {
log.warn("Could not parse integer for accelerator of action " + key, nfe);
}
Modified: trunk/jsword/src/main/java/org/crosswire/common/progress/Job.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/common/progress/Job.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/common/progress/Job.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -69,9 +69,9 @@
}
/* (non-Javadoc)
- * @see org.crosswire.common.progress.Progress#beginJob(java.lang.String, long)
+ * @see org.crosswire.common.progress.Progress#beginJob(java.lang.String, int)
*/
- public void beginJob(String sectionName, long totalWork) {
+ public void beginJob(String sectionName, int totalWork) {
if (this.finished) {
return;
}
@@ -96,7 +96,6 @@
}
synchronized (this) {
- finished = false;
currentSectionName = sectionName;
predictionMapURI = predictURI;
jobMode = ProgressMode.PREDICTIVE;
@@ -116,7 +115,7 @@
}
// And the predictions for next time
- nextPredictionMap = new HashMap();
+ nextPredictionMap = new HashMap<String, Integer>();
}
// Report that the Job has started.
@@ -140,14 +139,14 @@
/* (non-Javadoc)
* @see org.crosswire.common.progress.Progress#getTotalWork()
*/
- public synchronized long getTotalWork() {
+ public synchronized int getTotalWork() {
return totalUnits;
}
/* (non-Javadoc)
- * @see org.crosswire.common.progress.Progress#setTotalWork(long)
+ * @see org.crosswire.common.progress.Progress#setTotalWork(int)
*/
- public synchronized void setTotalWork(long totalWork) {
+ public synchronized void setTotalWork(int totalWork) {
this.totalUnits = totalWork;
}
@@ -159,23 +158,23 @@
}
/* (non-Javadoc)
- * @see org.crosswire.common.progress.Progress#setWork(long)
+ * @see org.crosswire.common.progress.Progress#setWork(int)
*/
- public synchronized void setWork(long work) {
+ public synchronized void setWork(int work) {
setWorkDone(work);
}
/* (non-Javadoc)
* @see org.crosswire.common.progress.Progress#getWorkDone()
*/
- public synchronized long getWorkDone() {
+ public synchronized int getWorkDone() {
return workUnits;
}
/* (non-Javadoc)
- * @see org.crosswire.common.progress.Progress#setWork(long)
+ * @see org.crosswire.common.progress.Progress#setWork(int)
*/
- public void setWorkDone(long work) {
+ public void setWorkDone(int work) {
if (finished) {
return;
}
@@ -186,17 +185,36 @@
}
workUnits = work;
+
+ int oldPercent = percent;
percent = (int) (100 * workUnits / totalUnits);
+ if (oldPercent == percent) {
+ return;
+ }
}
JobManager.fireWorkProgressed(this);
}
/* (non-Javadoc)
- * @see org.crosswire.common.progress.Progress#incrementWorkDone(long)
+ * @see org.crosswire.common.progress.Progress#incrementWorkDone(int)
*/
- public void incrementWorkDone(long step) {
- setWorkDone(getWorkDone() + step);
+ public synchronized void incrementWorkDone(int step) {
+ if (finished) {
+ return;
+ }
+
+ synchronized (this) {
+ workUnits += step;
+
+ int oldPercent = percent;
+ percent = (int) (100 * workUnits / totalUnits);
+ if (oldPercent == percent) {
+ return;
+ }
+ }
+
+ JobManager.fireWorkProgressed(this);
}
/* (non-Javadoc)
@@ -214,34 +232,41 @@
return;
}
+ boolean doUpdate = false;
synchronized (this) {
// If we are in some kind of predictive mode, then measure progress toward the expected end.
if (jobMode == ProgressMode.PREDICTIVE || jobMode == ProgressMode.UNKNOWN) {
- updateProgress(System.currentTimeMillis());
+ doUpdate = updateProgress(System.currentTimeMillis());
// We are done with the current section and are starting another
// So record the length of the last section
if (nextPredictionMap != null) {
- nextPredictionMap.put(currentSectionName, Long.valueOf(workUnits));
+ nextPredictionMap.put(currentSectionName, Integer.valueOf(workUnits));
}
}
currentSectionName = sectionName;
}
- // Tell listeners that the label changed.
- JobManager.fireWorkProgressed(this);
+ // Don't automatically tell listeners that the label changed.
+ // Only do so if it is time to do an update.
+ if (doUpdate) {
+ JobManager.fireWorkProgressed(this);
+ }
}
/* (non-Javadoc)
* @see org.crosswire.common.progress.Progress#done()
*/
public void done() {
+ // TRANSLATOR: This shows up in a progress bar when progress is finished.
+ String sectionName = UserMsg.gettext("Done");
+
synchronized (this) {
finished = true;
- // TRANSLATOR: This shows up in a progress bar when progress is finished.
- currentSectionName = UserMsg.gettext("Done");
+ currentSectionName = sectionName;
+
// Turn off the timer
if (fakingTimer != null) {
fakingTimer.cancel();
@@ -252,10 +277,11 @@
percent = 100;
if (nextPredictionMap != null) {
- nextPredictionMap.put(currentSectionName, Long.valueOf(System.currentTimeMillis() - startTime));
+ nextPredictionMap.put(currentSectionName, Integer.valueOf((int)(System.currentTimeMillis() - startTime)));
}
}
+ // Report that the job is done.
JobManager.fireWorkProgressed(this);
synchronized (this) {
@@ -288,14 +314,14 @@
/* (non-Javadoc)
* @see org.crosswire.common.progress.Progress#isCancelable()
*/
- public synchronized boolean isCancelable() {
+ public boolean isCancelable() {
return cancelable;
}
/* (non-Javadoc)
* @see org.crosswire.common.progress.Progress#setCancelable(boolean)
*/
- public synchronized void setCancelable(boolean newInterruptable) {
+ public void setCancelable(boolean newInterruptable) {
if (workerThread == null || finished) {
return;
}
@@ -343,34 +369,21 @@
// We ought only to tell listeners about jobs that are in our
// list of jobs so we need to fire before delete.
- long count = temp.size();
+ int count = temp.size();
for (int i = 0; i < count; i++) {
((WorkListener) temp.get(i)).workStateChanged(ev);
}
}
/**
- * Predict a percentage complete
+ * Get estimated the percent progress
+ *
+ * @return true if there is an update to progress.
*/
- private synchronized long getAgeFromMap(Map props, String message) {
- if (props == null) {
- return 0;
- }
+ protected synchronized boolean updateProgress(long now) {
+ int oldPercent = percent;
+ workUnits = (int) (now - startTime);
- Long time = (Long) props.get(message);
- if (time != null) {
- return time.longValue();
- }
-
- return 0;
- }
-
- /**
- * Get estimated the percent progress, extrapolating between sections
- */
- protected synchronized void updateProgress(long now) {
- workUnits = now - startTime;
-
// Are we taking more time than expected?
// Then we are at 100%
if (workUnits > totalUnits) {
@@ -379,15 +392,16 @@
} else {
percent = (int) (100 * workUnits / totalUnits);
}
+ return oldPercent != percent;
}
/**
* Load the predictive timings if any
*/
- private synchronized long loadPredictions() {
- long maxAge = UNKNOWN;
+ private synchronized int loadPredictions() {
+ int maxAge = UNKNOWN;
try {
- currentPredictionMap = new HashMap();
+ currentPredictionMap = new HashMap<String, Integer>();
Properties temp = NetUtil.loadProperties(predictionMapURI);
// Determine the predicted time from the current prediction map
@@ -397,11 +411,11 @@
String timestr = temp.getProperty(title);
try {
- Long time = Long.valueOf(timestr);
+ Integer time = Integer.valueOf(timestr);
currentPredictionMap.put(title, time);
// if this time is later than the latest
- long age = time.longValue();
+ int age = time.intValue();
if (maxAge < age) {
maxAge = age;
}
@@ -426,8 +440,8 @@
Iterator iter = nextPredictionMap.keySet().iterator();
while (iter.hasNext()) {
String sectionName = (String) iter.next();
- long age = getAgeFromMap(nextPredictionMap, sectionName);
- predictions.setProperty(sectionName, Long.toString(age));
+ Integer age = nextPredictionMap.get(sectionName);
+ predictions.setProperty(sectionName, age.toString());
}
// And save. It's not a disaster if this goes wrong
@@ -461,7 +475,7 @@
/**
* Total amount of work to do.
*/
- private long totalUnits;
+ private int totalUnits;
/**
* Does this job allow interruptions?
@@ -476,7 +490,7 @@
/**
* The amount of work done against the total.
*/
- private long workUnits;
+ private int workUnits;
/**
* The officially reported progress
@@ -511,7 +525,7 @@
/**
* The timings as measured this time
*/
- private Map nextPredictionMap;
+ private Map <String, Integer> nextPredictionMap;
/**
* When did this job start? Measured in milliseconds since beginning of epoch.
@@ -537,8 +551,9 @@
*/
@Override
public void run() {
- updateProgress(System.currentTimeMillis());
- JobManager.fireWorkProgressed(Job.this);
+ if (updateProgress(System.currentTimeMillis())) {
+ JobManager.fireWorkProgressed(Job.this);
+ }
}
/**
Modified: trunk/jsword/src/main/java/org/crosswire/common/progress/Progress.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/common/progress/Progress.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/common/progress/Progress.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -34,7 +34,7 @@
/**
* Indicate that the total amount of work is unknown.
*/
- long UNKNOWN = -1;
+ int UNKNOWN = -1;
/**
* Start the task measured from 0 to 100. It is the caller's responsibility to compute percentages.
@@ -53,7 +53,7 @@
* @param totalWork
* the total amount that is to be worked.
*/
- void beginJob(String sectionName, long totalWork);
+ void beginJob(String sectionName, int totalWork);
/**
* Start the task using timings from a prior run as a guess for the current
@@ -82,7 +82,7 @@
/**
* @return the total amount of work to be done, or UNKNOWN if it not known
*/
- long getTotalWork();
+ int getTotalWork();
/**
* Set the total amount of work to be done. This can be called any time. It
@@ -93,7 +93,7 @@
* the total amount of work to be done in units that make sense
* to the caller.
*/
- void setTotalWork(long totalWork);
+ void setTotalWork(int totalWork);
/**
* Return the computed percentage as an integer, typically from 0 to 100.
@@ -108,12 +108,12 @@
* @param progress
* a part of the whole.
*/
- void setWork(long progress);
+ void setWork(int progress);
/**
* @return the amount of work done so far as reported by the caller
*/
- long getWorkDone();
+ int getWorkDone();
/**
* Indicate progress toward the whole. It is up to the caller to give a
@@ -123,7 +123,7 @@
* @param progress
* a part of the whole.
*/
- void setWorkDone(long progress);
+ void setWorkDone(int progress);
/**
* Indicate progress toward the whole. It is up to the caller to give a
@@ -132,7 +132,7 @@
* @param step
* the amount of work done since the last call.
*/
- void incrementWorkDone(long step);
+ void incrementWorkDone(int step);
/**
* The section name is used in reporting progress.
Modified: trunk/jsword/src/main/java/org/crosswire/common/util/NetUtil.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/common/util/NetUtil.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/common/util/NetUtil.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -620,19 +620,19 @@
* the resource whose size is wanted
* @return the size of that resource
*/
- public static long getSize(URI uri) {
+ public static int getSize(URI uri) {
return getSize(uri, null, null);
}
- public static long getSize(URI uri, String proxyHost) {
+ public static int getSize(URI uri, String proxyHost) {
return getSize(uri, proxyHost, null);
}
- public static long getSize(URI uri, String proxyHost, Integer proxyPort) {
+ public static int getSize(URI uri, String proxyHost, Integer proxyPort) {
try {
if (uri.getScheme().equals(PROTOCOL_HTTP)) {
WebResource resource = new WebResource(uri, proxyHost, proxyPort);
- long size = resource.getSize();
+ int size = resource.getSize();
resource.shutdown();
return size;
}
Modified: trunk/jsword/src/main/java/org/crosswire/common/util/WebResource.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/common/util/WebResource.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/common/util/WebResource.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -195,7 +195,7 @@
*
* @return the size of the file
*/
- public long getSize() {
+ public int getSize() {
HttpRequestBase method = new HttpHead(uri);
HttpResponse response = null;
try {
@@ -203,7 +203,7 @@
response = client.execute(method);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
- return getHeaderAsLong(response, "Content-Length");
+ return getHeaderAsInt(response, "Content-Length");
}
String reason = response.getStatusLine().getReasonPhrase();
// TRANSLATOR: Common error condition: {0} is a placeholder for the
@@ -259,28 +259,30 @@
public void copy(URI dest, Progress meter) throws LucidException {
InputStream in = null;
OutputStream out = null;
-
+long startDownload = System.currentTimeMillis();
HttpRequestBase method = new HttpGet(uri);
HttpResponse response = null;
HttpEntity entity = null;
try {
// Execute the method.
response = client.execute(method);
-
+int afterExecute = (int) (System.currentTimeMillis() - startDownload);
// Initialize the meter, if present
if (meter != null) {
// Find out how big it is
- long size = getHeaderAsLong(response, "Content-Length");
+ int size = getHeaderAsInt(response, "Content-Length");
// Sometimes the Content-Length is not given and we have to grab it via HEAD method
if (size == 0) {
size = getSize();
}
meter.setTotalWork(size);
}
+ long afterSize = (System.currentTimeMillis() - startDownload - afterExecute);
entity = response.getEntity();
if (entity != null) {
in = entity.getContent();
+ long afterEntity = (System.currentTimeMillis() - startDownload - afterSize);
// Download the index file
out = NetUtil.getOutputStream(dest);
@@ -294,6 +296,12 @@
out.write(buf, 0, count);
count = in.read(buf);
}
+ long afterWrite = (System.currentTimeMillis() - startDownload - afterEntity);
+ System.out.println("execute = " + afterExecute + "ms\n" +
+ "size = " + afterSize + "ms\n" +
+ "entity = " + afterEntity + "ms\n" +
+ "write = " + afterWrite + "ms\n"
+ );
} else {
String reason = response.getStatusLine().getReasonPhrase();
// TRANSLATOR: Common error condition: {0} is a placeholder for
@@ -330,13 +338,13 @@
*
* @param response The response from the request
* @param field the header field to check
- * @return the long value for the field
+ * @return the int value for the field
*/
- private long getHeaderAsLong(HttpResponse response, String field) {
+ private int getHeaderAsInt(HttpResponse response, String field) {
Header header = response.getFirstHeader(field);
String value = header.getValue();
try {
- return Long.parseLong(value);
+ return Integer.parseInt(value);
} catch (NumberFormatException ex) {
return 0;
}
Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/Installer.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -88,7 +88,7 @@
* The book meta-data to check on.
* @return whether there is a newer version to install
*/
- long getSize(Book book);
+ int getSize(Book book);
/**
* Return true if the book is not installed or there is a newer version to
Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/AbstractSwordInstaller.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -239,10 +239,10 @@
*/
/* @Override */
public void run() {
- Progress job = JobManager.createJob("Install", this);
-
// TRANSLATOR: Progress label indicating the installation of a book. {0} is a placeholder for the name of the book.
String jobName = UserMsg.gettext("Installing book: {0}", sbmd.getName());
+ Progress job = JobManager.createJob(jobName, this);
+
// Don't bother setting a size, we'll do it later.
job.beginJob(jobName);
@@ -304,9 +304,10 @@
* @see org.crosswire.jsword.book.install.Installer#reloadIndex()
*/
public void reloadBookList() throws InstallException {
- Progress job = JobManager.createJob("BookList", Thread.currentThread());
// TRANSLATOR: Progress label for downloading one or more files.
- job.beginJob(UserMsg.gettext("Downloading files"));
+ String jobName = UserMsg.gettext("Downloading files");
+ Progress job = JobManager.createJob(jobName, Thread.currentThread());
+ job.beginJob(jobName);
try {
URI scratchfile = getCachedIndexFile();
@@ -328,9 +329,10 @@
* .jsword.book.BookMetaData, java.net.URI)
*/
public void downloadSearchIndex(Book book, URI localDest) throws InstallException {
- Progress job = JobManager.createJob("SearchIndex", Thread.currentThread());
// TRANSLATOR: Progress label for downloading one or more files.
- job.beginJob(UserMsg.gettext("Downloading files"));
+ String jobName = UserMsg.gettext("Downloading files");
+ Progress job = JobManager.createJob(jobName, Thread.currentThread());
+ job.beginJob(jobName);
try {
download(job, packageDirectory + '/' + SEARCH_DIR, book.getInitials() + ZIP_SUFFIX, localDest);
Modified: trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/HttpSwordInstaller.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/HttpSwordInstaller.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/book/install/sword/HttpSwordInstaller.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -59,7 +59,7 @@
/* (non-Javadoc)
* @see org.crosswire.jsword.book.install.Installer#getSize(org.crosswire.jsword.book.Book)
*/
- public long getSize(Book book) {
+ public int getSize(Book book) {
return NetUtil.getSize(toRemoteURI(book), proxyHost, proxyPort);
}
Modified: trunk/jsword/src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword/src/main/java/org/crosswire/jsword/index/lucene/LuceneIndex.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -151,8 +151,9 @@
DataPolice.setBook(book.getBookMetaData());
// TRANSLATOR: Progress label indicating the start of indexing. {0} is a placeholder for the book's short name.
- Progress job = JobManager.createJob("CreateIndex", Thread.currentThread());
- job.beginJob(UserMsg.gettext("Creating index. Processing {0}", book.getInitials()));
+ String jobName = UserMsg.gettext("Creating index. Processing {0}", book.getInitials());
+ Progress job = JobManager.createJob(jobName, Thread.currentThread());
+ job.beginJob(jobName);
IndexStatus finalStatus = IndexStatus.UNDONE;
Modified: trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/install/sword/FtpSwordInstaller.java
===================================================================
--- trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/install/sword/FtpSwordInstaller.java 2010-12-04 13:53:31 UTC (rev 2039)
+++ trunk/jsword-limbo/src/main/java/org/crosswire/jsword/book/install/sword/FtpSwordInstaller.java 2010-12-04 22:17:15 UTC (rev 2040)
@@ -61,7 +61,7 @@
* org.crosswire.jsword.book.install.Installer#getSize(org.crosswire.jsword
* .book.Book)
*/
- public long getSize(Book book) {
+ public int getSize(Book book) {
// not implemented
return 0;
}
More information about the jsword-svn
mailing list