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 import 'dart:collection'; | |
| 6 | |
| 5 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 6 import 'package:analyzer/src/dart/analysis/byte_store.dart'; | 8 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 7 | 9 |
| 8 /** | 10 /** |
| 9 * [ByteStore] that stores values as [File]s. | 11 * [ByteStore] that stores values as [File]s. |
| 10 * | |
| 11 * TODO(scheglov) Add some eviction policies. | |
| 12 */ | 12 */ |
| 13 class FileByteStore implements ByteStore { | 13 class FileByteStore implements ByteStore { |
| 14 final Folder folder; | 14 final Folder _folder; |
| 15 final String _tempName; | |
| 16 final int _maxSizeBytes; | |
| 15 | 17 |
| 16 FileByteStore(this.folder); | 18 final LinkedHashMap<File, int> _files = new LinkedHashMap<File, int>(); |
| 19 int _currentSizeBytes = 0; | |
| 20 | |
| 21 FileByteStore(this._folder, this._tempName, this._maxSizeBytes) { | |
| 22 for (Resource file in _folder.getChildren()) { | |
| 23 if (file is File) { | |
| 24 int length = file.lengthSync; | |
| 25 _files[file] = length; | |
| 26 _currentSizeBytes += length; | |
| 27 } | |
| 28 } | |
| 29 } | |
| 17 | 30 |
| 18 @override | 31 @override |
| 19 List<int> get(String key) { | 32 List<int> get(String key) { |
| 20 try { | 33 try { |
| 21 File file = folder.getChildAssumingFile(key); | 34 File file = _folder.getChildAssumingFile(key); |
| 22 return file.readAsBytesSync(); | 35 List<int> bytes = file.readAsBytesSync(); |
| 36 // Update the file position. | |
| 37 _files.remove(file); | |
| 38 _files[file] = bytes.length; | |
| 39 return bytes; | |
| 23 } catch (_) { | 40 } catch (_) { |
| 24 return null; | 41 return null; |
| 25 } | 42 } |
| 26 } | 43 } |
| 27 | 44 |
| 28 @override | 45 @override |
| 29 void put(String key, List<int> bytes) { | 46 void put(String key, List<int> bytes) { |
| 30 try { | 47 try { |
| 31 File file = folder.getChildAssumingFile(key); | 48 File tempFile = _folder.getChildAssumingFile(_tempName); |
| 32 file.writeAsBytesSync(bytes); | 49 tempFile.writeAsBytesSync(bytes); |
| 50 File file = _folder.getChildAssumingFile(key); | |
| 51 tempFile.renameSync(file.path); | |
| 52 // Update the size. | |
| 53 _currentSizeBytes -= _files[key] ?? 0; | |
| 54 _files[file] = bytes.length; | |
| 55 _currentSizeBytes += bytes.length; | |
| 56 _evict(); | |
| 33 } catch (_) {} | 57 } catch (_) {} |
| 34 } | 58 } |
| 59 | |
| 60 void _evict() { | |
| 61 while (_currentSizeBytes > _maxSizeBytes) { | |
| 62 if (_files.isEmpty) { | |
| 63 break; | |
| 64 } | |
| 65 File file = _files.keys.first; | |
| 66 // Update the size. | |
| 67 int size = _files.remove(file); | |
| 68 _currentSizeBytes -= size; | |
| 69 // Delete the file. | |
| 70 try { | |
| 71 file.delete(); | |
|
Brian Wilkerson
2016/11/10 22:12:04
Do we want to decrease the size if we can't actual
| |
| 72 } on FileSystemException {} | |
| 73 } | |
| 74 } | |
| 35 } | 75 } |
| OLD | NEW |