| 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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 /** | 295 /** |
| 296 * Fix the state of the [exception] to match the current state of the entry. | 296 * Fix the state of the [exception] to match the current state of the entry. |
| 297 */ | 297 */ |
| 298 void fixExceptionState() { | 298 void fixExceptionState() { |
| 299 if (!hasErrorState()) { | 299 if (!hasErrorState()) { |
| 300 _exception = null; | 300 _exception = null; |
| 301 } | 301 } |
| 302 } | 302 } |
| 303 | 303 |
| 304 /** | 304 /** |
| 305 * Look up the [ResultData] of [descriptor], or add a new one if it isn't |
| 306 * there. |
| 307 */ |
| 308 ResultData getResultData(ResultDescriptor descriptor) { |
| 309 return _resultMap.putIfAbsent(descriptor, () => new ResultData(descriptor)); |
| 310 } |
| 311 |
| 312 /** |
| 305 * Return the state of the result represented by the given [descriptor]. | 313 * Return the state of the result represented by the given [descriptor]. |
| 306 */ | 314 */ |
| 307 CacheState getState(ResultDescriptor descriptor) { | 315 CacheState getState(ResultDescriptor descriptor) { |
| 308 ResultData data = _resultMap[descriptor]; | 316 ResultData data = _resultMap[descriptor]; |
| 309 if (data == null) { | 317 if (data == null) { |
| 310 return CacheState.INVALID; | 318 return CacheState.INVALID; |
| 311 } | 319 } |
| 312 return data.state; | 320 return data.state; |
| 313 } | 321 } |
| 314 | 322 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 if (state == CacheState.VALID) { | 399 if (state == CacheState.VALID) { |
| 392 throw new ArgumentError('use setValue() to set the state to VALID'); | 400 throw new ArgumentError('use setValue() to set the state to VALID'); |
| 393 } | 401 } |
| 394 _validateStateChange(descriptor, state); | 402 _validateStateChange(descriptor, state); |
| 395 if (state == CacheState.INVALID) { | 403 if (state == CacheState.INVALID) { |
| 396 ResultData data = _resultMap[descriptor]; | 404 ResultData data = _resultMap[descriptor]; |
| 397 if (data != null) { | 405 if (data != null) { |
| 398 _invalidate(descriptor); | 406 _invalidate(descriptor); |
| 399 } | 407 } |
| 400 } else { | 408 } else { |
| 401 ResultData data = _getResultData(descriptor); | 409 ResultData data = getResultData(descriptor); |
| 402 data.state = state; | 410 data.state = state; |
| 403 if (state != CacheState.IN_PROCESS) { | 411 if (state != CacheState.IN_PROCESS) { |
| 404 // | 412 // |
| 405 // If the state is in-process, we can leave the current value in the | 413 // If the state is in-process, we can leave the current value in the |
| 406 // cache for any 'get' methods to access. | 414 // cache for any 'get' methods to access. |
| 407 // | 415 // |
| 408 data.value = descriptor.defaultValue; | 416 data.value = descriptor.defaultValue; |
| 409 } | 417 } |
| 410 } | 418 } |
| 411 } | 419 } |
| 412 | 420 |
| 413 /** | 421 /** |
| 414 * Set the value of the result represented by the given [descriptor] to the | 422 * Set the value of the result represented by the given [descriptor] to the |
| 415 * given [value]. | 423 * given [value]. |
| 416 */ | 424 */ |
| 417 /*<V>*/ void setValue(ResultDescriptor /*<V>*/ descriptor, dynamic /*V*/ | 425 /*<V>*/ void setValue(ResultDescriptor /*<V>*/ descriptor, dynamic /*V*/ |
| 418 value, List<TargetedResult> dependedOn) { | 426 value, List<TargetedResult> dependedOn) { |
| 419 // print(' setValue: $descriptor for $target dependedOn=$dependedOn'); | 427 // print(' setValue: $descriptor for $target dependedOn=$dependedOn'); |
| 420 _validateStateChange(descriptor, CacheState.VALID); | 428 _validateStateChange(descriptor, CacheState.VALID); |
| 421 TargetedResult thisResult = new TargetedResult(target, descriptor); | 429 TargetedResult thisResult = new TargetedResult(target, descriptor); |
| 422 if (_partition != null) { | 430 if (_partition != null) { |
| 423 _partition.resultStored(thisResult, value); | 431 _partition.resultStored(thisResult, value); |
| 424 } | 432 } |
| 425 ResultData data = _getResultData(descriptor); | 433 ResultData data = getResultData(descriptor); |
| 426 _setDependedOnResults(data, thisResult, dependedOn); | 434 _setDependedOnResults(data, thisResult, dependedOn); |
| 427 data.state = CacheState.VALID; | 435 data.state = CacheState.VALID; |
| 428 data.value = value == null ? descriptor.defaultValue : value; | 436 data.value = value == null ? descriptor.defaultValue : value; |
| 429 } | 437 } |
| 430 | 438 |
| 439 /** |
| 440 * Set the value of the result represented by the given [descriptor] to the |
| 441 * given [value], keep its dependency, invalidate all the dependent result. |
| 442 */ |
| 443 void setValueIncremental(ResultDescriptor descriptor, dynamic value) { |
| 444 ResultData data = getResultData(descriptor); |
| 445 List<TargetedResult> dependedOn = data.dependedOnResults; |
| 446 _invalidate(descriptor); |
| 447 setValue(descriptor, value, dependedOn); |
| 448 } |
| 449 |
| 431 @override | 450 @override |
| 432 String toString() { | 451 String toString() { |
| 433 StringBuffer buffer = new StringBuffer(); | 452 StringBuffer buffer = new StringBuffer(); |
| 434 _writeOn(buffer); | 453 _writeOn(buffer); |
| 435 return buffer.toString(); | 454 return buffer.toString(); |
| 436 } | 455 } |
| 437 | 456 |
| 438 /** | 457 /** |
| 439 * Return the value of the flag with the given [index]. | 458 * Return the value of the flag with the given [index]. |
| 440 */ | 459 */ |
| 441 bool _getFlag(int index) => BooleanArray.get(_flags, index); | 460 bool _getFlag(int index) => BooleanArray.get(_flags, index); |
| 442 | 461 |
| 443 /** | 462 /** |
| 444 * Look up the [ResultData] of [descriptor], or add a new one if it isn't | |
| 445 * there. | |
| 446 */ | |
| 447 ResultData _getResultData(ResultDescriptor descriptor) { | |
| 448 return _resultMap.putIfAbsent(descriptor, () => new ResultData(descriptor)); | |
| 449 } | |
| 450 | |
| 451 /** | |
| 452 * Invalidate the result represented by the given [descriptor] and propagate | 463 * Invalidate the result represented by the given [descriptor] and propagate |
| 453 * invalidation to other results that depend on it. | 464 * invalidation to other results that depend on it. |
| 454 */ | 465 */ |
| 455 void _invalidate(ResultDescriptor descriptor) { | 466 void _invalidate(ResultDescriptor descriptor) { |
| 456 ResultData thisData = _resultMap.remove(descriptor); | 467 ResultData thisData = _resultMap.remove(descriptor); |
| 457 // print('invalidate: $descriptor for $target'); | 468 // print('invalidate: $descriptor for $target'); |
| 458 if (thisData == null) { | 469 if (thisData == null) { |
| 459 return; | 470 return; |
| 460 } | 471 } |
| 461 // Stop depending on other results. | 472 // Stop depending on other results. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 data.dependentResults.add(thisResult); | 521 data.dependentResults.add(thisResult); |
| 511 } | 522 } |
| 512 }); | 523 }); |
| 513 } | 524 } |
| 514 | 525 |
| 515 /** | 526 /** |
| 516 * Set states of the given and dependent results to [CacheState.ERROR] and | 527 * Set states of the given and dependent results to [CacheState.ERROR] and |
| 517 * their values to the corresponding default values | 528 * their values to the corresponding default values |
| 518 */ | 529 */ |
| 519 void _setErrorState(ResultDescriptor descriptor, CaughtException exception) { | 530 void _setErrorState(ResultDescriptor descriptor, CaughtException exception) { |
| 520 ResultData thisData = _getResultData(descriptor); | 531 ResultData thisData = getResultData(descriptor); |
| 521 // Set the error state. | 532 // Set the error state. |
| 522 _exception = exception; | 533 _exception = exception; |
| 523 thisData.state = CacheState.ERROR; | 534 thisData.state = CacheState.ERROR; |
| 524 thisData.value = descriptor.defaultValue; | 535 thisData.value = descriptor.defaultValue; |
| 525 // Propagate the error state. | 536 // Propagate the error state. |
| 526 thisData.dependentResults.forEach((TargetedResult dependentResult) { | 537 thisData.dependentResults.forEach((TargetedResult dependentResult) { |
| 527 CacheEntry entry = _partition.get(dependentResult.target); | 538 CacheEntry entry = _partition.get(dependentResult.target); |
| 528 entry._setErrorState(dependentResult.result, exception); | 539 entry._setErrorState(dependentResult.result, exception); |
| 529 }); | 540 }); |
| 530 } | 541 } |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 _pathToSources.putIfAbsent(fullName, () => <Source>[]).add(target); | 865 _pathToSources.putIfAbsent(fullName, () => <Source>[]).add(target); |
| 855 } | 866 } |
| 856 } | 867 } |
| 857 } | 868 } |
| 858 | 869 |
| 859 ResultData _getDataFor(TargetedResult result, {bool orNull: false}) { | 870 ResultData _getDataFor(TargetedResult result, {bool orNull: false}) { |
| 860 CacheEntry entry = context.analysisCache.get(result.target); | 871 CacheEntry entry = context.analysisCache.get(result.target); |
| 861 if (orNull) { | 872 if (orNull) { |
| 862 return entry != null ? entry._resultMap[result.result] : null; | 873 return entry != null ? entry._resultMap[result.result] : null; |
| 863 } else { | 874 } else { |
| 864 return entry._getResultData(result.result); | 875 return entry.getResultData(result.result); |
| 865 } | 876 } |
| 866 } | 877 } |
| 867 | 878 |
| 868 /** | 879 /** |
| 869 * Return the [CacheFlushManager] for the given [descriptor], not `null`. | 880 * Return the [CacheFlushManager] for the given [descriptor], not `null`. |
| 870 */ | 881 */ |
| 871 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { | 882 CacheFlushManager _getFlushManager(ResultDescriptor descriptor) { |
| 872 ResultCachingPolicy policy = descriptor.cachingPolicy; | 883 ResultCachingPolicy policy = descriptor.cachingPolicy; |
| 873 if (identical(policy, DEFAULT_CACHING_POLICY)) { | 884 if (identical(policy, DEFAULT_CACHING_POLICY)) { |
| 874 return UnlimitedCacheFlushManager.INSTANCE; | 885 return UnlimitedCacheFlushManager.INSTANCE; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 void resultAccessed(TargetedResult result) {} | 1052 void resultAccessed(TargetedResult result) {} |
| 1042 | 1053 |
| 1043 @override | 1054 @override |
| 1044 List<TargetedResult> resultStored(TargetedResult newResult, newValue) { | 1055 List<TargetedResult> resultStored(TargetedResult newResult, newValue) { |
| 1045 return TargetedResult.EMPTY_LIST; | 1056 return TargetedResult.EMPTY_LIST; |
| 1046 } | 1057 } |
| 1047 | 1058 |
| 1048 @override | 1059 @override |
| 1049 void targetRemoved(AnalysisTarget target) {} | 1060 void targetRemoved(AnalysisTarget target) {} |
| 1050 } | 1061 } |
| OLD | NEW |