Chromium Code Reviews| 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.currentCacheSize_ = 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 |
| 90 * as the url passed. | 92 * as the url passed. |
| 91 */ | 93 */ |
| 92 MetadataCache.EXACT = 0; | 94 MetadataCache.EXACT = 0; |
| 93 | 95 |
| 94 /** | 96 /** |
| 95 * Observer type: it will be notified if the url changed is an immediate child | 97 * Observer type: it will be notified if the url changed is an immediate child |
| 96 * of the url passed. | 98 * of the url passed. |
| 97 */ | 99 */ |
| 98 MetadataCache.CHILDREN = 1; | 100 MetadataCache.CHILDREN = 1; |
| 99 | 101 |
| 100 /** | 102 /** |
| 101 * Observer type: it will be notified if the url changed is any descendant | 103 * Observer type: it will be notified if the url changed is any descendant |
| 102 * of the url passed. | 104 * of the url passed. |
| 103 */ | 105 */ |
| 104 MetadataCache.DESCENDANTS = 2; | 106 MetadataCache.DESCENDANTS = 2; |
| 105 | 107 |
| 106 /** | 108 /** |
| 107 * Minimum number of items in cache to start eviction. | 109 * Margin of the cache size. This amount of caches may be kept in addition. |
| 108 */ | 110 */ |
| 109 MetadataCache.EVICTION_NUMBER = 1000; | 111 MetadataCache.EVICTION_THRESHOLD_MARGIN = 500; |
| 110 | 112 |
| 111 /** | 113 /** |
| 112 * @return {MetadataCache!} The cache with all providers. | 114 * @return {MetadataCache!} The cache with all providers. |
| 113 */ | 115 */ |
| 114 MetadataCache.createFull = function() { | 116 MetadataCache.createFull = function() { |
| 115 var cache = new MetadataCache(); | 117 var cache = new MetadataCache(); |
| 116 cache.providers_.push(new FilesystemProvider()); | 118 cache.providers_.push(new FilesystemProvider()); |
| 117 cache.providers_.push(new DriveProvider()); | 119 cache.providers_.push(new DriveProvider()); |
| 118 cache.providers_.push(new ContentProvider()); | 120 cache.providers_.push(new ContentProvider()); |
| 119 return cache; | 121 return cache; |
| (...skipping 32 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 size of cache. The acutual cache size may be larger than the given | |
|
mtomasz
2014/01/08 08:11:02
nit: actual
yoshiki
2014/01/08 09:04:45
Done.
| |
| 165 * value. | |
| 166 * @param {number} size The cache size to be set. | |
| 167 */ | |
| 168 MetadataCache.prototype.setCacheSize = function(size) { | |
| 169 this.currentCacheSize_ = size; | |
| 170 | |
| 171 if (this.totalCount_ > this.currentEvictionThreshold_()) | |
| 172 this.evict_(); | |
| 173 }; | |
| 174 | |
| 175 /** | |
| 176 * Returns the current threshold to evict caches. When the number of caches | |
| 177 * exceeds this, the cache should be evicted. | |
| 178 * @return {number} Threshold to evict caches. | |
| 179 * @private | |
| 180 */ | |
| 181 MetadataCache.prototype.currentEvictionThreshold_ = function() { | |
| 182 return this.currentCacheSize_ * 2 + MetadataCache.EVICTION_THRESHOLD_MARGIN; | |
| 183 }; | |
| 184 | |
| 185 /** | |
| 162 * Fetches the metadata, puts it in the cache, and passes to callback. | 186 * 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. | 187 * 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 | 188 * @param {string|Entry|Array.<string|Entry>} items The list of entries or |
| 165 * file urls. May be just a single item. | 189 * file urls. May be just a single item. |
| 166 * @param {string} type The metadata type. | 190 * @param {string} type The metadata type. |
| 167 * @param {function(Object)} callback The metadata is passed to callback. | 191 * @param {function(Object)} callback The metadata is passed to callback. |
| 168 */ | 192 */ |
| 169 MetadataCache.prototype.get = function(items, type, callback) { | 193 MetadataCache.prototype.get = function(items, type, callback) { |
| 170 if (!(items instanceof Array)) { | 194 if (!(items instanceof Array)) { |
| 171 this.getOne(items, type, callback); | 195 this.getOne(items, type, callback); |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 449 if (this.batchCount_ == 1) | 473 if (this.batchCount_ == 1) |
| 450 this.lastBatchStart_ = new Date(); | 474 this.lastBatchStart_ = new Date(); |
| 451 }; | 475 }; |
| 452 | 476 |
| 453 /** | 477 /** |
| 454 * End batch updates. Notifies observers if all nested updates are finished. | 478 * End batch updates. Notifies observers if all nested updates are finished. |
| 455 */ | 479 */ |
| 456 MetadataCache.prototype.endBatchUpdates = function() { | 480 MetadataCache.prototype.endBatchUpdates = function() { |
| 457 this.batchCount_--; | 481 this.batchCount_--; |
| 458 if (this.batchCount_ != 0) return; | 482 if (this.batchCount_ != 0) return; |
| 459 if (this.totalCount_ > MetadataCache.EVICTION_NUMBER) | 483 if (this.totalCount_ > this.currentEvictionThreshold_()) |
| 460 this.evict_(); | 484 this.evict_(); |
| 461 for (var index = 0; index < this.observers_.length; index++) { | 485 for (var index = 0; index < this.observers_.length; index++) { |
| 462 var observer = this.observers_[index]; | 486 var observer = this.observers_[index]; |
| 463 var urls = []; | 487 var urls = []; |
| 464 var properties = []; | 488 var properties = []; |
| 465 for (var url in observer.pending) { | 489 for (var url in observer.pending) { |
| 466 if (observer.pending.hasOwnProperty(url) && url in this.cache_) { | 490 if (observer.pending.hasOwnProperty(url) && url in this.cache_) { |
| 467 urls.push(url); | 491 urls.push(url); |
| 468 properties.push(this.cache_[url].properties[observer.type] || null); | 492 properties.push(this.cache_[url].properties[observer.type] || null); |
| 469 } | 493 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 497 | 521 |
| 498 /** | 522 /** |
| 499 * Removes the oldest items from the cache. | 523 * Removes the oldest items from the cache. |
| 500 * This method never removes the items from last batch. | 524 * This method never removes the items from last batch. |
| 501 * @private | 525 * @private |
| 502 */ | 526 */ |
| 503 MetadataCache.prototype.evict_ = function() { | 527 MetadataCache.prototype.evict_ = function() { |
| 504 var toRemove = []; | 528 var toRemove = []; |
| 505 | 529 |
| 506 // We leave only a half of items, so we will not call evict_ soon again. | 530 // We leave only a half of items, so we will not call evict_ soon again. |
| 507 var desiredCount = Math.round(MetadataCache.EVICTION_NUMBER / 2); | 531 var desiredCount = this.currentEvictionThreshold(); |
| 508 var removeCount = this.totalCount_ - desiredCount; | 532 var removeCount = this.totalCount_ - desiredCount; |
| 509 for (var url in this.cache_) { | 533 for (var url in this.cache_) { |
| 510 if (this.cache_.hasOwnProperty(url) && | 534 if (this.cache_.hasOwnProperty(url) && |
| 511 this.cache_[url].time < this.lastBatchStart_) { | 535 this.cache_[url].time < this.lastBatchStart_) { |
| 512 toRemove.push(url); | 536 toRemove.push(url); |
| 513 } | 537 } |
| 514 } | 538 } |
| 515 | 539 |
| 516 toRemove.sort(function(a, b) { | 540 toRemove.sort(function(a, b) { |
| 517 var aTime = this.cache_[a].time; | 541 var aTime = this.cache_[a].time; |
| (...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 | 1066 |
| 1043 /** | 1067 /** |
| 1044 * Handles the 'log' message from the worker. | 1068 * Handles the 'log' message from the worker. |
| 1045 * @param {Array.<*>} arglist Log arguments. | 1069 * @param {Array.<*>} arglist Log arguments. |
| 1046 * @private | 1070 * @private |
| 1047 */ | 1071 */ |
| 1048 ContentProvider.prototype.onLog_ = function(arglist) { | 1072 ContentProvider.prototype.onLog_ = function(arglist) { |
| 1049 if (MetadataCache.log) // Avoid log spam by default. | 1073 if (MetadataCache.log) // Avoid log spam by default. |
| 1050 console.log.apply(console, ['metadata:'].concat(arglist)); | 1074 console.log.apply(console, ['metadata:'].concat(arglist)); |
| 1051 }; | 1075 }; |
| OLD | NEW |