Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.src.context.cache; | 5 library analyzer.src.context.cache; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/engine.dart' | 9 import 'package:analyzer/src/generated/engine.dart' |
| 10 show AnalysisEngine, CacheState, InternalAnalysisContext, RetentionPriority; | 10 show AnalysisEngine, CacheState, InternalAnalysisContext, RetentionPriority; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 // } | 58 // } |
| 59 // return data; | 59 // return data; |
| 60 // } | 60 // } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * Return the entry associated with the given [target]. | 63 * Return the entry associated with the given [target]. |
| 64 */ | 64 */ |
| 65 CacheEntry get(AnalysisTarget target) { | 65 CacheEntry get(AnalysisTarget target) { |
| 66 int count = _partitions.length; | 66 int count = _partitions.length; |
| 67 for (int i = 0; i < count; i++) { | 67 for (int i = 0; i < count; i++) { |
| 68 if (_partitions[i].contains(target)) { | 68 CachePartition partition = _partitions[i]; |
| 69 return _partitions[i].get(target); | 69 if (partition.isResponsibleFor(target)) { |
| 70 return partition.get(target); | |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 // | 73 // |
| 73 // We should never get to this point because the last partition should | 74 // We should never get to this point because the last partition should |
| 74 // always be a universal partition, except in the case of the SDK context, | 75 // always be a universal partition, except in the case of the SDK context, |
| 75 // in which case the target should always be part of the SDK. | 76 // in which case the target should always be part of the SDK. |
| 76 // | 77 // |
| 77 return null; | 78 return null; |
| 78 } | 79 } |
| 79 | 80 |
| 80 /** | 81 /** |
| 81 * Return the context to which the given [target] was explicitly added. | 82 * Return the context to which the given [target] was explicitly added. |
| 82 */ | 83 */ |
| 83 InternalAnalysisContext getContextFor(AnalysisTarget target) { | 84 InternalAnalysisContext getContextFor(AnalysisTarget target) { |
| 84 int count = _partitions.length; | 85 int count = _partitions.length; |
| 85 for (int i = 0; i < count; i++) { | 86 for (int i = 0; i < count; i++) { |
| 86 if (_partitions[i].contains(target)) { | 87 CachePartition partition = _partitions[i]; |
| 87 return _partitions[i].context; | 88 if (partition.isResponsibleFor(target)) { |
| 89 return partition.context; | |
| 88 } | 90 } |
| 89 } | 91 } |
| 90 // | 92 // |
| 91 // We should never get to this point because the last partition should | 93 // We should never get to this point because the last partition should |
| 92 // always be a universal partition, except in the case of the SDK context, | 94 // always be a universal partition, except in the case of the SDK context, |
| 93 // in which case the target should always be part of the SDK. | 95 // in which case the target should always be part of the SDK. |
| 94 // | 96 // |
| 95 // TODO(brianwilkerson) Throw an exception here. | 97 // TODO(brianwilkerson) Throw an exception here. |
| 96 AnalysisEngine.instance.logger.logInformation( | 98 AnalysisEngine.instance.logger.logInformation( |
| 97 'Could not find context for $target', | 99 'Could not find context for $target', |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 112 return new MultipleMapIterator<AnalysisTarget, CacheEntry>(maps); | 114 return new MultipleMapIterator<AnalysisTarget, CacheEntry>(maps); |
| 113 } | 115 } |
| 114 | 116 |
| 115 /** | 117 /** |
| 116 * Associate the given [entry] with the given [target]. | 118 * Associate the given [entry] with the given [target]. |
| 117 */ | 119 */ |
| 118 void put(AnalysisTarget target, CacheEntry entry) { | 120 void put(AnalysisTarget target, CacheEntry entry) { |
| 119 entry.fixExceptionState(); | 121 entry.fixExceptionState(); |
| 120 int count = _partitions.length; | 122 int count = _partitions.length; |
| 121 for (int i = 0; i < count; i++) { | 123 for (int i = 0; i < count; i++) { |
| 122 if (_partitions[i].contains(target)) { | 124 CachePartition partition = _partitions[i]; |
| 125 if (partition.isResponsibleFor(target)) { | |
| 123 if (_TRACE_CHANGES) { | 126 if (_TRACE_CHANGES) { |
| 124 CacheEntry oldEntry = _partitions[i].get(target); | 127 CacheEntry oldEntry = partition.get(target); |
| 125 if (oldEntry == null) { | 128 if (oldEntry == null) { |
| 126 AnalysisEngine.instance.logger | 129 AnalysisEngine.instance.logger |
| 127 .logInformation('Added a cache entry for $target.'); | 130 .logInformation('Added a cache entry for $target.'); |
| 128 } else { | 131 } else { |
| 129 AnalysisEngine.instance.logger | 132 AnalysisEngine.instance.logger |
| 130 .logInformation('Modified the cache entry for $target.'); | 133 .logInformation('Modified the cache entry for $target.'); |
| 131 // 'Diff = ${entry.getDiff(oldEntry)}'); | 134 // 'Diff = ${entry.getDiff(oldEntry)}'); |
| 132 } | 135 } |
| 133 } | 136 } |
| 134 _partitions[i].put(target, entry); | 137 partition.put(target, entry); |
| 135 return; | 138 return; |
| 136 } | 139 } |
| 137 } | 140 } |
| 138 // TODO(brianwilkerson) Handle the case where no partition was found, | 141 // TODO(brianwilkerson) Handle the case where no partition was found, |
| 139 // possibly by throwing an exception. | 142 // possibly by throwing an exception. |
| 140 } | 143 } |
| 141 | 144 |
| 142 /** | 145 /** |
| 143 * Remove all information related to the given [target] from this cache. | 146 * Remove all information related to the given [target] from this cache. |
| 144 */ | 147 */ |
| 145 void remove(AnalysisTarget target) { | 148 void remove(AnalysisTarget target) { |
| 146 int count = _partitions.length; | 149 int count = _partitions.length; |
| 147 for (int i = 0; i < count; i++) { | 150 for (int i = 0; i < count; i++) { |
| 148 if (_partitions[i].contains(target)) { | 151 CachePartition partition = _partitions[i]; |
| 152 if (partition.isResponsibleFor(target)) { | |
| 149 if (_TRACE_CHANGES) { | 153 if (_TRACE_CHANGES) { |
| 150 AnalysisEngine.instance.logger | 154 AnalysisEngine.instance.logger |
| 151 .logInformation('Removed the cache entry for $target.'); | 155 .logInformation('Removed the cache entry for $target.'); |
| 152 } | 156 } |
| 153 _partitions[i].remove(target); | 157 partition.remove(target); |
| 154 return; | 158 return; |
| 155 } | 159 } |
| 156 } | 160 } |
| 157 } | 161 } |
| 158 | 162 |
| 159 /** | 163 /** |
| 160 * Return the number of targets that are mapped to cache entries. | 164 * Return the number of targets that are mapped to cache entries. |
| 161 */ | 165 */ |
| 162 int size() { | 166 int size() { |
| 163 int size = 0; | 167 int size = 0; |
| 164 int count = _partitions.length; | 168 int count = _partitions.length; |
| 165 for (int i = 0; i < count; i++) { | 169 for (int i = 0; i < count; i++) { |
| 166 size += _partitions[i].size(); | 170 size += _partitions[i].size(); |
| 167 } | 171 } |
| 168 return size; | 172 return size; |
| 169 } | 173 } |
| 170 | 174 |
| 171 ResultData _getDataFor(TargetedResult result) { | 175 ResultData _getDataFor(TargetedResult result) { |
| 172 AnalysisTarget target = result.target; | 176 CacheEntry entry = get(result.target); |
| 173 int count = _partitions.length; | 177 if (entry != null) { |
| 174 for (int i = 0; i < count; i++) { | 178 return entry._getResultData(result.result); |
| 175 if (_partitions[i].contains(target)) { | |
| 176 CacheEntry entry = _partitions[i].get(target); | |
| 177 return entry._getResultData(result.result); | |
| 178 } | |
| 179 } | 179 } |
| 180 return null; | 180 return null; |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 | 183 |
| 184 /** | 184 /** |
| 185 * The information cached by an analysis context about an individual target. | 185 * The information cached by an analysis context about an individual target. |
| 186 */ | 186 */ |
| 187 class CacheEntry { | 187 class CacheEntry { |
| 188 /** | 188 /** |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 668 /** | 668 /** |
| 669 * Return a table mapping the targets known to the context to the information | 669 * Return a table mapping the targets known to the context to the information |
| 670 * known about the target. | 670 * known about the target. |
| 671 * | 671 * |
| 672 * <b>Note:</b> This method is only visible for use by [AnalysisCache] and | 672 * <b>Note:</b> This method is only visible for use by [AnalysisCache] and |
| 673 * should not be used for any other purpose. | 673 * should not be used for any other purpose. |
| 674 */ | 674 */ |
| 675 Map<AnalysisTarget, CacheEntry> get map => _targetMap; | 675 Map<AnalysisTarget, CacheEntry> get map => _targetMap; |
| 676 | 676 |
| 677 /** | 677 /** |
| 678 * Return `true` if the given [target] is contained in this partition. | 678 * Return `true` if this partition is responsible for the given [target]. |
| 679 */ | 679 */ |
| 680 // TODO(brianwilkerson) Rename this to something more meaningful, such as | 680 bool isResponsibleFor(AnalysisTarget target); |
|
Brian Wilkerson
2015/05/07 20:03:07
Thanks!
| |
| 681 // isResponsibleFor. | |
| 682 bool contains(AnalysisTarget target); | |
| 683 | 681 |
| 684 /** | 682 /** |
| 685 * Return the entry associated with the given [target]. | 683 * Return the entry associated with the given [target]. |
| 686 */ | 684 */ |
| 687 CacheEntry get(AnalysisTarget target) => _targetMap[target]; | 685 CacheEntry get(AnalysisTarget target) => _targetMap[target]; |
| 688 | 686 |
| 689 /** | 687 /** |
| 690 * Return an iterator returning all of the map entries mapping targets to | 688 * Return an iterator returning all of the map entries mapping targets to |
| 691 * cache entries. | 689 * cache entries. |
| 692 */ | 690 */ |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 824 * A cache partition that contains all of the targets in the SDK. | 822 * A cache partition that contains all of the targets in the SDK. |
| 825 */ | 823 */ |
| 826 class SdkCachePartition extends CachePartition { | 824 class SdkCachePartition extends CachePartition { |
| 827 /** | 825 /** |
| 828 * Initialize a newly created cache partition, belonging to the given | 826 * Initialize a newly created cache partition, belonging to the given |
| 829 * [context]. | 827 * [context]. |
| 830 */ | 828 */ |
| 831 SdkCachePartition(InternalAnalysisContext context) : super(context); | 829 SdkCachePartition(InternalAnalysisContext context) : super(context); |
| 832 | 830 |
| 833 @override | 831 @override |
| 834 bool contains(AnalysisTarget target) { | 832 bool isResponsibleFor(AnalysisTarget target) { |
| 835 if (target is AnalysisContextTarget) { | 833 if (target is AnalysisContextTarget) { |
| 836 return true; | 834 return true; |
| 837 } | 835 } |
| 838 Source source = target.source; | 836 Source source = target.source; |
| 839 return source != null && source.isInSystemLibrary; | 837 return source != null && source.isInSystemLibrary; |
| 840 } | 838 } |
| 841 } | 839 } |
| 842 | 840 |
| 843 /** | 841 /** |
| 844 * A specification of a specific result computed for a specific target. | 842 * A specification of a specific result computed for a specific target. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 884 * A cache partition that contains all targets not contained in other partitions . | 882 * A cache partition that contains all targets not contained in other partitions . |
| 885 */ | 883 */ |
| 886 class UniversalCachePartition extends CachePartition { | 884 class UniversalCachePartition extends CachePartition { |
| 887 /** | 885 /** |
| 888 * Initialize a newly created cache partition, belonging to the given | 886 * Initialize a newly created cache partition, belonging to the given |
| 889 * [context]. | 887 * [context]. |
| 890 */ | 888 */ |
| 891 UniversalCachePartition(InternalAnalysisContext context) : super(context); | 889 UniversalCachePartition(InternalAnalysisContext context) : super(context); |
| 892 | 890 |
| 893 @override | 891 @override |
| 894 bool contains(AnalysisTarget target) => true; | 892 bool isResponsibleFor(AnalysisTarget target) => true; |
| 895 } | 893 } |
| OLD | NEW |