Commit b2584620 authored by yoshiki@chromium.org's avatar yoshiki@chromium.org

[Files.app] Change sort timing during metadata fetch

This patch is a reland of r243816, fixing the tiny typo.

Previously, sort was not started until all the metadata is fetched. When fetching took a time, the user saw unsorted list for a while. With this patch, sort is happened just after every updates. User can see sorted list every time.

BUG=327479
TEST=manually tested

Review URL: https://codereview.chromium.org/132683004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@244205 0039d316-1c4b-4281-b951-d872f2087c98
parent bdfd8427
...@@ -460,7 +460,6 @@ function DirectoryContents(context, isSearch, directoryEntry, ...@@ -460,7 +460,6 @@ function DirectoryContents(context, isSearch, directoryEntry,
this.scanner_ = null; this.scanner_ = null;
this.prefetchMetadataQueue_ = new AsyncUtil.Queue(); this.prefetchMetadataQueue_ = new AsyncUtil.Queue();
this.scanCancelled_ = false; this.scanCancelled_ = false;
this.fileList_.prepareSort = this.prepareSort_.bind(this);
} }
/** /**
...@@ -483,8 +482,11 @@ DirectoryContents.prototype.clone = function() { ...@@ -483,8 +482,11 @@ DirectoryContents.prototype.clone = function() {
* @param {Array|cr.ui.ArrayDataModel} fileList The new file list. * @param {Array|cr.ui.ArrayDataModel} fileList The new file list.
*/ */
DirectoryContents.prototype.setFileList = function(fileList) { DirectoryContents.prototype.setFileList = function(fileList) {
this.fileList_ = fileList; if (fileList instanceof cr.ui.ArrayDataModel)
this.fileList_.prepareSort = this.prepareSort_.bind(this); this.fileList_ = fileList;
else
this.fileList_ = new cr.ui.ArrayDataModel(fileList);
this.context_.metadataCache.setCacheSize(this.fileList_.length);
}; };
/** /**
...@@ -493,11 +495,12 @@ DirectoryContents.prototype.setFileList = function(fileList) { ...@@ -493,11 +495,12 @@ DirectoryContents.prototype.setFileList = function(fileList) {
*/ */
DirectoryContents.prototype.replaceContextFileList = function() { DirectoryContents.prototype.replaceContextFileList = function() {
if (this.context_.fileList !== this.fileList_) { if (this.context_.fileList !== this.fileList_) {
var spliceArgs = [].slice.call(this.fileList_); var spliceArgs = this.fileList_.slice();
var fileList = this.context_.fileList; var fileList = this.context_.fileList;
spliceArgs.unshift(0, fileList.length); spliceArgs.unshift(0, fileList.length);
fileList.splice.apply(fileList, spliceArgs); fileList.splice.apply(fileList, spliceArgs);
this.fileList_ = fileList; this.fileList_ = fileList;
this.context_.metadataCache.setCacheSize(this.fileList_.length);
} }
}; };
...@@ -605,6 +608,8 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) { ...@@ -605,6 +608,8 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) {
this.fileList_.push.apply(this.fileList_, entriesFiltered); this.fileList_.push.apply(this.fileList_, entriesFiltered);
cr.dispatchSimpleEvent(this, 'scan-updated'); cr.dispatchSimpleEvent(this, 'scan-updated');
this.context_.metadataCache.setCacheSize(this.fileList_.length);
// Because the prefetchMetadata can be slow, throttling by splitting entries // Because the prefetchMetadata can be slow, throttling by splitting entries
// into smaller chunks to reduce UI latency. // into smaller chunks to reduce UI latency.
// TODO(hidehiko,mtomasz): This should be handled in MetadataCache. // TODO(hidehiko,mtomasz): This should be handled in MetadataCache.
...@@ -619,6 +624,16 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) { ...@@ -619,6 +624,16 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) {
return; return;
} }
// TODO(yoshiki): Here we should fire the update event of changed
// items. Currently we have a method this.fileList_.updateIndex() to
// fire an event, but this method takes only 1 argument and invokes sort
// one by one. It is obviously time wasting. Instead, we call sort
// directory.
// In future, we should implement a good method like updateIndexes and
// use it here.
var status = this.fileList_.sortStatus;
this.fileList_.sort(status.field, status.direction);
cr.dispatchSimpleEvent(this, 'scan-updated'); cr.dispatchSimpleEvent(this, 'scan-updated');
callback(); callback();
}.bind(this)); }.bind(this));
...@@ -626,19 +641,6 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) { ...@@ -626,19 +641,6 @@ DirectoryContents.prototype.onNewEntries_ = function(entries) {
} }
}; };
/**
* Cache necessary data before a sort happens.
*
* This is called by the table code before a sort happens, so that we can
* go fetch data for the sort field that we may not have yet.
* @param {string} field Sort field.
* @param {function(Object)} callback Called when done.
* @private
*/
DirectoryContents.prototype.prepareSort_ = function(field, callback) {
this.prefetchMetadata(this.fileList_.slice(), callback);
};
/** /**
* @param {Array.<Entry>} entries Files. * @param {Array.<Entry>} entries Files.
* @param {function(Object)} callback Callback on done. * @param {function(Object)} callback Callback on done.
......
...@@ -2597,7 +2597,6 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52; ...@@ -2597,7 +2597,6 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
if (this.commandHandler) if (this.commandHandler)
this.commandHandler.updateAvailability(); this.commandHandler.updateAvailability();
this.hideSpinnerLater_(); this.hideSpinnerLater_();
this.refreshCurrentDirectoryMetadata_();
if (this.scanUpdatedTimer_) { if (this.scanUpdatedTimer_) {
clearTimeout(this.scanUpdatedTimer_); clearTimeout(this.scanUpdatedTimer_);
...@@ -2689,7 +2688,6 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52; ...@@ -2689,7 +2688,6 @@ var BOTTOM_MARGIN_FOR_PREVIEW_PANEL_PX = 52;
* @private * @private
*/ */
FileManager.prototype.onRescanCompleted_ = function() { FileManager.prototype.onRescanCompleted_ = function() {
this.refreshCurrentDirectoryMetadata_();
this.selectionHandler_.onFileSelectionChanged(); this.selectionHandler_.onFileSelectionChanged();
}; };
......
...@@ -76,6 +76,8 @@ function MetadataCache() { ...@@ -76,6 +76,8 @@ function MetadataCache() {
this.batchCount_ = 0; this.batchCount_ = 0;
this.totalCount_ = 0; this.totalCount_ = 0;
this.currentCacheSize_ = 0;
/** /**
* Time of first get query of the current batch. Items updated later than this * Time of first get query of the current batch. Items updated later than this
* will not be evicted. * will not be evicted.
...@@ -103,9 +105,9 @@ MetadataCache.CHILDREN = 1; ...@@ -103,9 +105,9 @@ MetadataCache.CHILDREN = 1;
MetadataCache.DESCENDANTS = 2; MetadataCache.DESCENDANTS = 2;
/** /**
* Minimum number of items in cache to start eviction. * Margin of the cache size. This amount of caches may be kept in addition.
*/ */
MetadataCache.EVICTION_NUMBER = 1000; MetadataCache.EVICTION_THRESHOLD_MARGIN = 500;
/** /**
* @param {VolumeManagerWrapper} volumeManager Volume manager instance. * @param {VolumeManagerWrapper} volumeManager Volume manager instance.
...@@ -158,6 +160,28 @@ MetadataCache.prototype.isInitialized = function() { ...@@ -158,6 +160,28 @@ MetadataCache.prototype.isInitialized = function() {
return true; return true;
}; };
/**
* Sets the size of cache. The actual cache size may be larger than the given
* value.
* @param {number} size The cache size to be set.
*/
MetadataCache.prototype.setCacheSize = function(size) {
this.currentCacheSize_ = size;
if (this.totalCount_ > this.currentEvictionThreshold_())
this.evict_();
};
/**
* Returns the current threshold to evict caches. When the number of caches
* exceeds this, the cache should be evicted.
* @return {number} Threshold to evict caches.
* @private
*/
MetadataCache.prototype.currentEvictionThreshold_ = function() {
return this.currentCacheSize_ * 2 + MetadataCache.EVICTION_THRESHOLD_MARGIN;
};
/** /**
* Fetches the metadata, puts it in the cache, and passes to callback. * Fetches the metadata, puts it in the cache, and passes to callback.
* If required metadata is already in the cache, does not fetch it again. * If required metadata is already in the cache, does not fetch it again.
...@@ -458,7 +482,7 @@ MetadataCache.prototype.startBatchUpdates = function() { ...@@ -458,7 +482,7 @@ MetadataCache.prototype.startBatchUpdates = function() {
MetadataCache.prototype.endBatchUpdates = function() { MetadataCache.prototype.endBatchUpdates = function() {
this.batchCount_--; this.batchCount_--;
if (this.batchCount_ !== 0) return; if (this.batchCount_ !== 0) return;
if (this.totalCount_ > MetadataCache.EVICTION_NUMBER) if (this.totalCount_ > this.currentEvictionThreshold_())
this.evict_(); this.evict_();
for (var index = 0; index < this.observers_.length; index++) { for (var index = 0; index < this.observers_.length; index++) {
var observer = this.observers_[index]; var observer = this.observers_[index];
...@@ -513,7 +537,7 @@ MetadataCache.prototype.evict_ = function() { ...@@ -513,7 +537,7 @@ MetadataCache.prototype.evict_ = function() {
var toRemove = []; var toRemove = [];
// We leave only a half of items, so we will not call evict_ soon again. // We leave only a half of items, so we will not call evict_ soon again.
var desiredCount = Math.round(MetadataCache.EVICTION_NUMBER / 2); var desiredCount = this.currentEvictionThreshold_();
var removeCount = this.totalCount_ - desiredCount; var removeCount = this.totalCount_ - desiredCount;
for (var url in this.cache_) { for (var url in this.cache_) {
if (this.cache_.hasOwnProperty(url) && if (this.cache_.hasOwnProperty(url) &&
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment