Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: chrome/browser/resources/file_manager/foreground/js/metadata/metadata_cache.js

Issue 113293007: [Files.app] Change sort timing during metadata fetch (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * MetadataCache is a map from url to an object containing properties. 8 * MetadataCache is a map from url to an object containing properties.
9 * Properties are divided by types, and all properties of one type are accessed 9 * Properties are divided by types, and all properties of one type are accessed
10 * at once. 10 * at once.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 * callback - the callback. 70 * callback - the callback.
71 * TODO(dgozman): pass entries to observer if present. 71 * TODO(dgozman): pass entries to observer if present.
72 * @private 72 * @private
73 */ 73 */
74 this.observers_ = []; 74 this.observers_ = [];
75 this.observerId_ = 0; 75 this.observerId_ = 0;
76 76
77 this.batchCount_ = 0; 77 this.batchCount_ = 0;
78 this.totalCount_ = 0; 78 this.totalCount_ = 0;
79 79
80 this.currentContentNumber_ = 0;
81
80 /** 82 /**
81 * Time of first get query of the current batch. Items updated later than this 83 * Time of first get query of the current batch. Items updated later than this
82 * will not be evicted. 84 * will not be evicted.
83 * @private 85 * @private
84 */ 86 */
85 this.lastBatchStart_ = new Date(); 87 this.lastBatchStart_ = new Date();
86 } 88 }
87 89
88 /** 90 /**
89 * Observer type: it will be notified if the url changed is exactly the same 91 * Observer type: it will be notified if the url changed is exactly the same
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 * @return {boolean} Whether all providers are ready. 154 * @return {boolean} Whether all providers are ready.
153 */ 155 */
154 MetadataCache.prototype.isInitialized = function() { 156 MetadataCache.prototype.isInitialized = function() {
155 for (var index = 0; index < this.providers_.length; index++) { 157 for (var index = 0; index < this.providers_.length; index++) {
156 if (!this.providers_[index].isInitialized()) return false; 158 if (!this.providers_[index].isInitialized()) return false;
157 } 159 }
158 return true; 160 return true;
159 }; 161 };
160 162
161 /** 163 /**
164 * Sets the number of contents currently shown.
165 * @param {number} number The number of the contents currently shown.
166 **/
167 MetadataCache.prototype.setCurrentContentNumber = function(number) {
168 this.currentContentNumber_ = number;
169
170 if (this.totalCount_ > this.currentEvictionNumber_())
171 this.evict_();
172 };
173
174 /**
175 * Returns the current threshold to evict caches. When the number of caches
176 * exceeds this, the cache should be evicted.
177 * @return {number} Threshold to evict caches.
178 * @private
179 */
180 MetadataCache.prototype.currentEvictionThreshold_ = function() {
181 var desiredNumber =
182 this.currentContentNumber_ * 2 + MetadataCache.EVICTION_NUMBER / 2;
183 return desiredNumber;
hirono 2013/12/17 11:35:15 nit: How about just returning the expression.
yoshiki 2013/12/17 12:11:40 Done.
184 };
185
186 /**
162 * Fetches the metadata, puts it in the cache, and passes to callback. 187 * Fetches the metadata, puts it in the cache, and passes to callback.
163 * If required metadata is already in the cache, does not fetch it again. 188 * If required metadata is already in the cache, does not fetch it again.
164 * @param {string|Entry|Array.<string|Entry>} items The list of entries or 189 * @param {string|Entry|Array.<string|Entry>} items The list of entries or
165 * file urls. May be just a single item. 190 * file urls. May be just a single item.
166 * @param {string} type The metadata type. 191 * @param {string} type The metadata type.
167 * @param {function(Object)} callback The metadata is passed to callback. 192 * @param {function(Object)} callback The metadata is passed to callback.
168 */ 193 */
169 MetadataCache.prototype.get = function(items, type, callback) { 194 MetadataCache.prototype.get = function(items, type, callback) {
170 if (!(items instanceof Array)) { 195 if (!(items instanceof Array)) {
171 this.getOne(items, type, callback); 196 this.getOne(items, type, callback);
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 if (this.batchCount_ == 1) 474 if (this.batchCount_ == 1)
450 this.lastBatchStart_ = new Date(); 475 this.lastBatchStart_ = new Date();
451 }; 476 };
452 477
453 /** 478 /**
454 * End batch updates. Notifies observers if all nested updates are finished. 479 * End batch updates. Notifies observers if all nested updates are finished.
455 */ 480 */
456 MetadataCache.prototype.endBatchUpdates = function() { 481 MetadataCache.prototype.endBatchUpdates = function() {
457 this.batchCount_--; 482 this.batchCount_--;
458 if (this.batchCount_ != 0) return; 483 if (this.batchCount_ != 0) return;
459 if (this.totalCount_ > MetadataCache.EVICTION_NUMBER) 484 if (this.totalCount_ > this.currentEvictionThreshold_())
460 this.evict_(); 485 this.evict_();
461 for (var index = 0; index < this.observers_.length; index++) { 486 for (var index = 0; index < this.observers_.length; index++) {
462 var observer = this.observers_[index]; 487 var observer = this.observers_[index];
463 var urls = []; 488 var urls = [];
464 var properties = []; 489 var properties = [];
465 for (var url in observer.pending) { 490 for (var url in observer.pending) {
466 if (observer.pending.hasOwnProperty(url) && url in this.cache_) { 491 if (observer.pending.hasOwnProperty(url) && url in this.cache_) {
467 urls.push(url); 492 urls.push(url);
468 properties.push(this.cache_[url].properties[observer.type] || null); 493 properties.push(this.cache_[url].properties[observer.type] || null);
469 } 494 }
(...skipping 27 matching lines...) Expand all
497 522
498 /** 523 /**
499 * Removes the oldest items from the cache. 524 * Removes the oldest items from the cache.
500 * This method never removes the items from last batch. 525 * This method never removes the items from last batch.
501 * @private 526 * @private
502 */ 527 */
503 MetadataCache.prototype.evict_ = function() { 528 MetadataCache.prototype.evict_ = function() {
504 var toRemove = []; 529 var toRemove = [];
505 530
506 // We leave only a half of items, so we will not call evict_ soon again. 531 // We leave only a half of items, so we will not call evict_ soon again.
507 var desiredCount = Math.round(MetadataCache.EVICTION_NUMBER / 2); 532 var desiredCount = Math.round(this.currentEvictionThreshold_() / 2);
508 var removeCount = this.totalCount_ - desiredCount; 533 var removeCount = this.totalCount_ - desiredCount;
509 for (var url in this.cache_) { 534 for (var url in this.cache_) {
510 if (this.cache_.hasOwnProperty(url) && 535 if (this.cache_.hasOwnProperty(url) &&
511 this.cache_[url].time < this.lastBatchStart_) { 536 this.cache_[url].time < this.lastBatchStart_) {
512 toRemove.push(url); 537 toRemove.push(url);
513 } 538 }
514 } 539 }
515 540
516 toRemove.sort(function(a, b) { 541 toRemove.sort(function(a, b) {
517 var aTime = this.cache_[a].time; 542 var aTime = this.cache_[a].time;
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1042 1067
1043 /** 1068 /**
1044 * Handles the 'log' message from the worker. 1069 * Handles the 'log' message from the worker.
1045 * @param {Array.<*>} arglist Log arguments. 1070 * @param {Array.<*>} arglist Log arguments.
1046 * @private 1071 * @private
1047 */ 1072 */
1048 ContentProvider.prototype.onLog_ = function(arglist) { 1073 ContentProvider.prototype.onLog_ = function(arglist) {
1049 if (MetadataCache.log) // Avoid log spam by default. 1074 if (MetadataCache.log) // Avoid log spam by default.
1050 console.log.apply(console, ['metadata:'].concat(arglist)); 1075 console.log.apply(console, ['metadata:'].concat(arglist));
1051 }; 1076 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698