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 Entry to an object containing properties. | 8 * MetadataCache is a map from Entry 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 * type - metadata type; | 69 * type - metadata type; |
70 * callback - the callback. | 70 * callback - the callback. |
71 * @private | 71 * @private |
72 */ | 72 */ |
73 this.observers_ = []; | 73 this.observers_ = []; |
74 this.observerId_ = 0; | 74 this.observerId_ = 0; |
75 | 75 |
76 this.batchCount_ = 0; | 76 this.batchCount_ = 0; |
77 this.totalCount_ = 0; | 77 this.totalCount_ = 0; |
78 | 78 |
79 this.currentCacheSize_ = 0; | |
80 | |
79 /** | 81 /** |
80 * Time of first get query of the current batch. Items updated later than this | 82 * Time of first get query of the current batch. Items updated later than this |
81 * will not be evicted. | 83 * will not be evicted. |
82 * @private | 84 * @private |
83 */ | 85 */ |
84 this.lastBatchStart_ = new Date(); | 86 this.lastBatchStart_ = new Date(); |
85 } | 87 } |
86 | 88 |
87 /** | 89 /** |
88 * Observer type: it will be notified if the changed Entry is exactly the same | 90 * Observer type: it will be notified if the changed Entry is exactly the same |
89 * as the observed Entry. | 91 * as the observed Entry. |
90 */ | 92 */ |
91 MetadataCache.EXACT = 0; | 93 MetadataCache.EXACT = 0; |
92 | 94 |
93 /** | 95 /** |
94 * Observer type: it will be notified if the changed Entry is an immediate child | 96 * Observer type: it will be notified if the changed Entry is an immediate child |
95 * of the observed Entry. | 97 * of the observed Entry. |
96 */ | 98 */ |
97 MetadataCache.CHILDREN = 1; | 99 MetadataCache.CHILDREN = 1; |
98 | 100 |
99 /** | 101 /** |
100 * Observer type: it will be notified if the changed Entry is a descendant of | 102 * Observer type: it will be notified if the changed Entry is a descendant of |
101 * of the observer Entry. | 103 * of the observer Entry. |
102 */ | 104 */ |
103 MetadataCache.DESCENDANTS = 2; | 105 MetadataCache.DESCENDANTS = 2; |
104 | 106 |
105 /** | 107 /** |
106 * Minimum number of items in cache to start eviction. | 108 * Margin of the cache size. This amount of caches may be kept in addition. |
107 */ | 109 */ |
108 MetadataCache.EVICTION_NUMBER = 1000; | 110 MetadataCache.EVICTION_THRESHOLD_MARGIN = 500; |
109 | 111 |
110 /** | 112 /** |
111 * @return {MetadataCache!} The cache with all providers. | 113 * @return {MetadataCache!} The cache with all providers. |
112 */ | 114 */ |
113 MetadataCache.createFull = function() { | 115 MetadataCache.createFull = function() { |
114 var cache = new MetadataCache(); | 116 var cache = new MetadataCache(); |
115 cache.providers_.push(new FilesystemProvider()); | 117 cache.providers_.push(new FilesystemProvider()); |
116 cache.providers_.push(new DriveProvider()); | 118 cache.providers_.push(new DriveProvider()); |
117 cache.providers_.push(new ContentProvider()); | 119 cache.providers_.push(new ContentProvider()); |
118 return cache; | 120 return cache; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 * @return {boolean} Whether all providers are ready. | 153 * @return {boolean} Whether all providers are ready. |
152 */ | 154 */ |
153 MetadataCache.prototype.isInitialized = function() { | 155 MetadataCache.prototype.isInitialized = function() { |
154 for (var index = 0; index < this.providers_.length; index++) { | 156 for (var index = 0; index < this.providers_.length; index++) { |
155 if (!this.providers_[index].isInitialized()) return false; | 157 if (!this.providers_[index].isInitialized()) return false; |
156 } | 158 } |
157 return true; | 159 return true; |
158 }; | 160 }; |
159 | 161 |
160 /** | 162 /** |
163 * Sets the size of cache. The actual cache size may be larger than the given | |
164 * value. | |
165 * @param {number} size The cache size to be set. | |
166 */ | |
167 MetadataCache.prototype.setCacheSize = function(size) { | |
168 this.currentCacheSize_ = size; | |
169 | |
170 if (this.totalCount_ > this.currentEvictionThreshold_()) | |
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.currentCacheSize_ * 2 + MetadataCache.EVICTION_THRESHOLD_MARGIN; | |
182 }; | |
183 | |
184 /** | |
161 * 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. |
162 * 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. |
163 * @param {Entry|Array.<Entry>} entries The list of entries. May be just a | 187 * @param {Entry|Array.<Entry>} entries The list of entries. May be just a |
164 * single item. | 188 * single item. |
165 * @param {string} type The metadata type. | 189 * @param {string} type The metadata type. |
166 * @param {function(Object)} callback The metadata is passed to callback. | 190 * @param {function(Object)} callback The metadata is passed to callback. |
167 */ | 191 */ |
168 MetadataCache.prototype.get = function(entries, type, callback) { | 192 MetadataCache.prototype.get = function(entries, type, callback) { |
169 if (!(entries instanceof Array)) { | 193 if (!(entries instanceof Array)) { |
170 this.getOne(entries, type, callback); | 194 this.getOne(entries, type, callback); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 if (this.batchCount_ === 1) | 474 if (this.batchCount_ === 1) |
451 this.lastBatchStart_ = new Date(); | 475 this.lastBatchStart_ = new Date(); |
452 }; | 476 }; |
453 | 477 |
454 /** | 478 /** |
455 * End batch updates. Notifies observers if all nested updates are finished. | 479 * End batch updates. Notifies observers if all nested updates are finished. |
456 */ | 480 */ |
457 MetadataCache.prototype.endBatchUpdates = function() { | 481 MetadataCache.prototype.endBatchUpdates = function() { |
458 this.batchCount_--; | 482 this.batchCount_--; |
459 if (this.batchCount_ !== 0) return; | 483 if (this.batchCount_ !== 0) return; |
460 if (this.totalCount_ > MetadataCache.EVICTION_NUMBER) | 484 if (this.totalCount_ > this.currentEvictionThreshold_()) |
461 this.evict_(); | 485 this.evict_(); |
462 for (var index = 0; index < this.observers_.length; index++) { | 486 for (var index = 0; index < this.observers_.length; index++) { |
463 var observer = this.observers_[index]; | 487 var observer = this.observers_[index]; |
464 var entries = []; | 488 var entries = []; |
465 var properties = {}; | 489 var properties = {}; |
466 for (var entryURL in observer.pending) { | 490 for (var entryURL in observer.pending) { |
467 if (observer.pending.hasOwnProperty(entryURL) && | 491 if (observer.pending.hasOwnProperty(entryURL) && |
468 entryURL in this.cache_) { | 492 entryURL in this.cache_) { |
469 var entry = observer.pending[entryURL]; | 493 var entry = observer.pending[entryURL]; |
470 entries.push(entry); | 494 entries.push(entry); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
505 | 529 |
506 /** | 530 /** |
507 * Removes the oldest items from the cache. | 531 * Removes the oldest items from the cache. |
508 * This method never removes the items from last batch. | 532 * This method never removes the items from last batch. |
509 * @private | 533 * @private |
510 */ | 534 */ |
511 MetadataCache.prototype.evict_ = function() { | 535 MetadataCache.prototype.evict_ = function() { |
512 var toRemove = []; | 536 var toRemove = []; |
513 | 537 |
514 // We leave only a half of items, so we will not call evict_ soon again. | 538 // We leave only a half of items, so we will not call evict_ soon again. |
515 var desiredCount = Math.round(MetadataCache.EVICTION_NUMBER / 2); | 539 var desiredCount = this.currentEvictionThreshold(); |
mtomasz
2014/01/10 00:57:59
Error in response to fileBrowserPrivate.searchDriv
yoshiki
2014/01/10 03:41:53
Oops, sorry. I didn't catch it. I'll fix it soon.
| |
516 var removeCount = this.totalCount_ - desiredCount; | 540 var removeCount = this.totalCount_ - desiredCount; |
517 for (var url in this.cache_) { | 541 for (var url in this.cache_) { |
518 if (this.cache_.hasOwnProperty(url) && | 542 if (this.cache_.hasOwnProperty(url) && |
519 this.cache_[url].time < this.lastBatchStart_) { | 543 this.cache_[url].time < this.lastBatchStart_) { |
520 toRemove.push(url); | 544 toRemove.push(url); |
521 } | 545 } |
522 } | 546 } |
523 | 547 |
524 toRemove.sort(function(a, b) { | 548 toRemove.sort(function(a, b) { |
525 var aTime = this.cache_[a].time; | 549 var aTime = this.cache_[a].time; |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1012 | 1036 |
1013 /** | 1037 /** |
1014 * Handles the 'log' message from the worker. | 1038 * Handles the 'log' message from the worker. |
1015 * @param {Array.<*>} arglist Log arguments. | 1039 * @param {Array.<*>} arglist Log arguments. |
1016 * @private | 1040 * @private |
1017 */ | 1041 */ |
1018 ContentProvider.prototype.onLog_ = function(arglist) { | 1042 ContentProvider.prototype.onLog_ = function(arglist) { |
1019 if (MetadataCache.log) // Avoid log spam by default. | 1043 if (MetadataCache.log) // Avoid log spam by default. |
1020 console.log.apply(console, ['metadata:'].concat(arglist)); | 1044 console.log.apply(console, ['metadata:'].concat(arglist)); |
1021 }; | 1045 }; |
OLD | NEW |