OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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) { | |
mtomasz
2013/12/24 00:22:10
Contents is uncountable. Also, Cache shouldn't kno
yoshiki
2014/01/08 08:08:31
Thanks! Done.
| |
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 return this.currentContentNumber_ * 2 + MetadataCache.EVICTION_NUMBER / 2; | |
mtomasz
2013/12/24 00:22:10
Eviction policy got tricky here. Why this formula?
yoshiki
2014/01/08 08:08:31
Made the rule and the code simpler.
| |
182 }; | |
183 | |
184 /** | |
162 * Fetches the metadata, puts it in the cache, and passes to callback. | 185 * 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. | 186 * 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 | 187 * @param {string|Entry|Array.<string|Entry>} items The list of entries or |
165 * file urls. May be just a single item. | 188 * file urls. May be just a single item. |
166 * @param {string} type The metadata type. | 189 * @param {string} type The metadata type. |
167 * @param {function(Object)} callback The metadata is passed to callback. | 190 * @param {function(Object)} callback The metadata is passed to callback. |
168 */ | 191 */ |
169 MetadataCache.prototype.get = function(items, type, callback) { | 192 MetadataCache.prototype.get = function(items, type, callback) { |
170 if (!(items instanceof Array)) { | 193 if (!(items instanceof Array)) { |
171 this.getOne(items, type, callback); | 194 this.getOne(items, type, callback); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 if (this.batchCount_ == 1) | 472 if (this.batchCount_ == 1) |
450 this.lastBatchStart_ = new Date(); | 473 this.lastBatchStart_ = new Date(); |
451 }; | 474 }; |
452 | 475 |
453 /** | 476 /** |
454 * End batch updates. Notifies observers if all nested updates are finished. | 477 * End batch updates. Notifies observers if all nested updates are finished. |
455 */ | 478 */ |
456 MetadataCache.prototype.endBatchUpdates = function() { | 479 MetadataCache.prototype.endBatchUpdates = function() { |
457 this.batchCount_--; | 480 this.batchCount_--; |
458 if (this.batchCount_ != 0) return; | 481 if (this.batchCount_ != 0) return; |
459 if (this.totalCount_ > MetadataCache.EVICTION_NUMBER) | 482 if (this.totalCount_ > this.currentEvictionThreshold_()) |
460 this.evict_(); | 483 this.evict_(); |
461 for (var index = 0; index < this.observers_.length; index++) { | 484 for (var index = 0; index < this.observers_.length; index++) { |
462 var observer = this.observers_[index]; | 485 var observer = this.observers_[index]; |
463 var urls = []; | 486 var urls = []; |
464 var properties = []; | 487 var properties = []; |
465 for (var url in observer.pending) { | 488 for (var url in observer.pending) { |
466 if (observer.pending.hasOwnProperty(url) && url in this.cache_) { | 489 if (observer.pending.hasOwnProperty(url) && url in this.cache_) { |
467 urls.push(url); | 490 urls.push(url); |
468 properties.push(this.cache_[url].properties[observer.type] || null); | 491 properties.push(this.cache_[url].properties[observer.type] || null); |
469 } | 492 } |
(...skipping 27 matching lines...) Expand all Loading... | |
497 | 520 |
498 /** | 521 /** |
499 * Removes the oldest items from the cache. | 522 * Removes the oldest items from the cache. |
500 * This method never removes the items from last batch. | 523 * This method never removes the items from last batch. |
501 * @private | 524 * @private |
502 */ | 525 */ |
503 MetadataCache.prototype.evict_ = function() { | 526 MetadataCache.prototype.evict_ = function() { |
504 var toRemove = []; | 527 var toRemove = []; |
505 | 528 |
506 // We leave only a half of items, so we will not call evict_ soon again. | 529 // We leave only a half of items, so we will not call evict_ soon again. |
507 var desiredCount = Math.round(MetadataCache.EVICTION_NUMBER / 2); | 530 var desiredCount = Math.round(this.currentEvictionThreshold_() / 2); |
508 var removeCount = this.totalCount_ - desiredCount; | 531 var removeCount = this.totalCount_ - desiredCount; |
509 for (var url in this.cache_) { | 532 for (var url in this.cache_) { |
510 if (this.cache_.hasOwnProperty(url) && | 533 if (this.cache_.hasOwnProperty(url) && |
511 this.cache_[url].time < this.lastBatchStart_) { | 534 this.cache_[url].time < this.lastBatchStart_) { |
512 toRemove.push(url); | 535 toRemove.push(url); |
513 } | 536 } |
514 } | 537 } |
515 | 538 |
516 toRemove.sort(function(a, b) { | 539 toRemove.sort(function(a, b) { |
517 var aTime = this.cache_[a].time; | 540 var aTime = this.cache_[a].time; |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1042 | 1065 |
1043 /** | 1066 /** |
1044 * Handles the 'log' message from the worker. | 1067 * Handles the 'log' message from the worker. |
1045 * @param {Array.<*>} arglist Log arguments. | 1068 * @param {Array.<*>} arglist Log arguments. |
1046 * @private | 1069 * @private |
1047 */ | 1070 */ |
1048 ContentProvider.prototype.onLog_ = function(arglist) { | 1071 ContentProvider.prototype.onLog_ = function(arglist) { |
1049 if (MetadataCache.log) // Avoid log spam by default. | 1072 if (MetadataCache.log) // Avoid log spam by default. |
1050 console.log.apply(console, ['metadata:'].concat(arglist)); | 1073 console.log.apply(console, ['metadata:'].concat(arglist)); |
1051 }; | 1074 }; |
OLD | NEW |